searchProduct.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="pages">
  3. <!-- head -->
  4. <view class="scrollPage-header">
  5. <view :style="{height:statusBarHeight+'px'}"></view>
  6. <view class="header-title">
  7. <view class="header-left" @click="goBack">
  8. <image style="width: 16px; height: 16px;margin-right:3px;" mode="aspectFit" src="../static/arrow-left.png"></image>
  9. <text>返回</text>
  10. </view>
  11. <view class="header-title"><text class="ellipsis-one" overflow="ellipsis">{{title}}</text></view>
  12. <view class="header-right"></view>
  13. </view>
  14. </view>
  15. <view class="search-result">
  16. <view class="tab-left" v-if="clzId">
  17. <view class="item-clz active">滤清器</view>
  18. <view class="item-clz">机油</view>
  19. <view class="item-clz">轮胎</view>
  20. <view class="item-clz">火花塞</view>
  21. <view class="item-clz">刹车片</view>
  22. </view>
  23. <view class="right-box" :style="{width:clzId?'75%':'100%'}">
  24. <!-- body -->
  25. <scroll-view
  26. class="scrollPage-body"
  27. :style="{height: (screenHeight - statusBarHeight - safeAreaBottom - 40) + 'px'}"
  28. scroll-y="true"
  29. type="custom"
  30. @scrolltolower="reachBottom">
  31. <!-- 搜索,吸顶 -->
  32. <sticky-header>
  33. <view class="search-head">
  34. <uni-search-bar v-model="queryWord" :focus="focus" radius="100" placeholder="请输入商品名称/产品编码/原厂编码" bgColor="#fff" textColor="#666" clearButton="auto" cancelButton="none" @confirm="searchList" @clear="searchList" />
  35. </view>
  36. </sticky-header>
  37. <!-- 产品列表 -->
  38. <chooseProductItem
  39. v-for="(item, index) in list"
  40. :key="item.id"
  41. :index="index"
  42. :list="item"
  43. >
  44. </chooseProductItem>
  45. <!-- loading -->
  46. <view class="loading-bar" v-if="list.length && (loading||loadEnd)">{{loadText}}</view>
  47. <view class="empty-bar" v-if="total==0&&!loading">
  48. <image mode="aspectFit" :src="empty.imgUrl"></image>
  49. <view v-if="queryWord!=''&&clzId!=''">{{empty.tip}}</view>
  50. </view>
  51. <!-- 搜索记录 -->
  52. <view class="search-history" v-if="historyList.length&&queryWord==''&&clzId==''">
  53. <view class="search-history-title">
  54. <text>搜索记录</text>
  55. <view class="search-history-right">
  56. <view class="search-history-expend" v-if="searchHistoryList.length>10" @click="expend=!expend">
  57. {{expend?'收起':'展开'}}
  58. <uni-icons :type="expend?'arrowup':'arrowdown'" size="14"></uni-icons>
  59. </view>
  60. <uni-icons type="trash" size="18" @click="clearHistory" color="#eb0000"></uni-icons>
  61. </view>
  62. </view>
  63. <view class="search-history-body">
  64. <view class="item" v-for="item in historyList" :key="item.id" @click="toSearch(item)">
  65. <text class="ellipsis-one" max-lines="1" overflow="ellipsis">{{item.text}}</text>
  66. </view>
  67. </view>
  68. </view>
  69. </scroll-view>
  70. </view>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. import chooseProductItem from '@/pagesB/components/chooseProductItemSkline.vue'
  76. import { queryPromoProductByPage } from '@/api/video.js'
  77. export default {
  78. components:{
  79. chooseProductItem
  80. },
  81. data() {
  82. return {
  83. clzId: null,
  84. title: '',
  85. loadText: '加载中...',
  86. empty: {
  87. tip: '',
  88. imgUrl: '/static/nodata.png'
  89. },
  90. queryWord:'',
  91. screenWidth: 325,
  92. screenHeight: 0,
  93. statusBarHeight: 44,
  94. safeAreaBottom: 0,
  95. pageSize: 10,
  96. pageNo:1,
  97. total:0,
  98. list: [],
  99. loading:false,
  100. loadEnd: false,
  101. searchHistoryList: [],
  102. focus: false,
  103. expend: false
  104. };
  105. },
  106. computed: {
  107. historyList() {
  108. return this.searchHistoryList.filter(item=>item.text!='').reverse().slice(0,this.expend?20:10)
  109. }
  110. },
  111. onLoad(opts) {
  112. // 计算区域高度
  113. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
  114. this.screenWidth = uni.getSystemInfoSync().windowWidth
  115. this.screenHeight = uni.getSystemInfoSync().windowHeight
  116. this.safeAreaBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
  117. // 分类id
  118. this.clzId = opts.clzId
  119. // 搜索商品
  120. if(opts.clzId==''){
  121. this.title = '商城商品搜索'
  122. this.focus = true
  123. }else{
  124. // 分类下产品
  125. this.title = opts.clzName
  126. this.searchList()
  127. }
  128. // 搜索历史
  129. this.searchHistoryList = uni.getStorageSync('searchHistoryList') || []
  130. },
  131. methods: {
  132. goBack(){
  133. uni.navigateBack()
  134. },
  135. // 清空历史记录
  136. clearHistory(){
  137. const _this = this
  138. uni.showModal({
  139. title: '提示',
  140. content: '确定清空搜索历史?',
  141. confirmText: '确定',
  142. success(res) {
  143. if(res.confirm){
  144. _this.searchHistoryList = []
  145. uni.setStorageSync('searchHistoryList', _this.searchHistoryList)
  146. }
  147. }
  148. })
  149. },
  150. // 选择搜索记录
  151. toSearch(item){
  152. this.queryWord = item.text
  153. this.searchList()
  154. },
  155. // 搜索列表
  156. searchList(event) {
  157. // 存储搜索记录
  158. const has = this.searchHistoryList.some(k => k.text == this.queryWord)
  159. if(!has && this.queryWord!=''){
  160. this.searchHistoryList.push({
  161. id: new Date().getTime(),
  162. text: this.queryWord
  163. })
  164. uni.setStorageSync('searchHistoryList', this.searchHistoryList)
  165. }
  166. this.loadEnd = false
  167. this.loadText = '加载中...'
  168. this.list = []
  169. this.pageNo = 1
  170. this.getList()
  171. },
  172. // 滚动到底部
  173. reachBottom() {
  174. if(!this.loading && !this.loadEnd){
  175. this.pageNo++
  176. this.getList()
  177. }
  178. },
  179. // 列表查询
  180. getList(){
  181. this.loading = true
  182. queryPromoProductByPage({
  183. pageNo: this.pageNo,
  184. pageSize: this.pageSize,
  185. queryWord: this.queryWord,
  186. promoActiveSn: '693459699332595712'
  187. }).then(res => {
  188. this.loading = false
  189. if(res.status == 200){
  190. this.total = res.data.count
  191. if(this.total){
  192. const list = res.data.list || []
  193. const ret = []
  194. // 更新已选状态
  195. list.forEach(k => {
  196. ret.push({
  197. id: k.id,
  198. cost: k.cost,
  199. productCode: k.productCode,
  200. productSn: k.productSn,
  201. productImage: k.productImage,
  202. productName: k.productName,
  203. productOrigCode: k.productOrigCode,
  204. promoActiveSn: k.promoActiveSn,
  205. promoRuleSn: k.promoRuleSn,
  206. promoRuleValue: k.promoRuleValue,
  207. promoRuleType: k.promoRuleType,
  208. })
  209. })
  210. // 追加数据
  211. this.list.push(ret)
  212. // 判断是否最后一页
  213. const maxPages = Math.ceil(this.total / this.pageSize)
  214. if(this.pageNo >= maxPages){
  215. this.loadEnd = true
  216. this.loadText = '没有更多了'
  217. }
  218. }else{
  219. this.loadEnd = false
  220. this.loadText = '没有找到相关产品'
  221. }
  222. }else{
  223. this.loadEnd = false
  224. this.loadText = res.message
  225. }
  226. }).catch(err => {
  227. this.loading = false
  228. })
  229. },
  230. }
  231. }
  232. </script>
  233. <style lang="less">
  234. .pages{
  235. height: 100vh;
  236. display: flex;
  237. flex-direction: column;
  238. .scrollPage-header{
  239. z-index: 1;
  240. .header-title{
  241. display: flex;
  242. align-items: center;
  243. justify-content: space-between;
  244. padding: 0 10px;
  245. height: 44px;
  246. background-color: #FFFFFF;
  247. box-shadow: 0px 1px 0px 0px #E6E6E6;
  248. .header-title{
  249. font-size: 16px;
  250. color: #333333;
  251. max-width: 70%;
  252. }
  253. .header-left{
  254. display: flex;
  255. align-items: center;
  256. text{
  257. font-size: 14px;
  258. color: #333333;
  259. margin-left: 5px;
  260. }
  261. }
  262. .header-right{
  263. width: 50px;
  264. }
  265. }
  266. }
  267. .search-result{
  268. flex-grow: 1;
  269. display: flex;
  270. justify-content: space-between;
  271. .tab-left{
  272. width: 25%;
  273. flex-grow: 1;
  274. background: #FFFFFF;
  275. .item-clz{
  276. font-size: 14px;
  277. padding: 8px 10px;
  278. line-height: 30px;
  279. }
  280. .active{
  281. border-left: 3px solid #00aaff;
  282. background: #f8f8f8;
  283. }
  284. }
  285. .right-box{
  286. width: 75%;
  287. .scrollPage-body{
  288. width:100%;
  289. .search-head input{
  290. background-color: #FFFFFF;
  291. }
  292. .loading-bar{
  293. text-align: center;
  294. padding: 10px 0;
  295. color: #999999;
  296. }
  297. .empty-bar{
  298. display: flex;
  299. flex-direction: column;
  300. align-items: center;
  301. justify-content: center;
  302. padding: 20px 0;
  303. image{
  304. width: 100px;
  305. height: 100px;
  306. }
  307. view{
  308. font-size: 12px;
  309. color: #999999;
  310. }
  311. }
  312. .search-history{
  313. padding: 0 1.5rem;
  314. .search-history-title{
  315. display: flex;
  316. align-items: center;
  317. justify-content: space-between;
  318. padding: 6px 0;
  319. font-size: 14px;
  320. color: #333333;
  321. border-bottom: 1px solid #E6E6E6;
  322. }
  323. .search-history-right{
  324. display: flex;
  325. align-items: center;
  326. }
  327. .search-history-expend{
  328. display: flex;
  329. align-items: center;
  330. font-size: 12px;
  331. margin-right: 15px;
  332. }
  333. .search-history-body{
  334. display: flex;
  335. flex-wrap: wrap;
  336. align-items: center;
  337. justify-content: space-between;
  338. padding: 10px 10px;
  339. .item{
  340. padding: 6px 0;
  341. background-color: #f8f8f8;
  342. border-radius: 20px;
  343. font-size: 12px;
  344. color: #333333;
  345. width: 46%;
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. </style>