searchProduct.vue 9.5 KB

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