searchProduct.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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" 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. <image @click="clearHistory" style="width: 30px; height: 30px;" mode="aspectFit" src="../static/del.png"></image>
  56. </view>
  57. <view class="search-history-body">
  58. <view class="item" v-for="item in historyList" :key="item.id" @click="toSearch(item)">
  59. <text class="ellipsis-one" max-lines="1" overflow="ellipsis">{{item.text}}</text>
  60. </view>
  61. </view>
  62. </view>
  63. </scroll-view>
  64. </view>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import chooseProductItem from '@/pagesB/components/chooseProductItemSkline.vue'
  70. import { queryPromoProductByPage } from '@/api/video.js'
  71. export default {
  72. components:{
  73. chooseProductItem
  74. },
  75. data() {
  76. return {
  77. clzId: null,
  78. title: '',
  79. loadText: '加载中...',
  80. empty: {
  81. tip: '',
  82. imgUrl: '/static/nodata.png'
  83. },
  84. queryWord:'',
  85. screenWidth: 325,
  86. screenHeight: 0,
  87. statusBarHeight: 44,
  88. safeAreaBottom: 0,
  89. pageSize: 10,
  90. pageNo:1,
  91. total:0,
  92. list: [],
  93. loading:false,
  94. loadEnd: false,
  95. searchHistoryList: []
  96. };
  97. },
  98. computed: {
  99. historyList() {
  100. return this.searchHistoryList.filter(item=>item.text!='')
  101. }
  102. },
  103. onLoad(opts) {
  104. this.clzId = opts.clzId
  105. // 搜索历史
  106. this.searchHistoryList = uni.getStorageSync('searchHistoryList') || []
  107. if(opts.clzId==''){
  108. this.title = '商城商品搜索'
  109. this.queryWord = opts.queryWord
  110. }else{
  111. this.title = opts.clzName
  112. }
  113. this.searchList()
  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. },
  120. methods: {
  121. goBack(){
  122. uni.navigateBack()
  123. },
  124. // 清空历史记录
  125. clearHistory(){
  126. const _this = this
  127. uni.showModal({
  128. title: '提示',
  129. content: '确定清空搜索历史?',
  130. confirmText: '确定',
  131. success(res) {
  132. if(res.confirm){
  133. _this.searchHistoryList = []
  134. uni.setStorageSync('searchHistoryList', _this.searchHistoryList)
  135. }
  136. }
  137. })
  138. },
  139. // 选择搜索记录
  140. toSearch(item){
  141. this.queryWord = item.text
  142. this.searchList()
  143. },
  144. // 搜索列表
  145. searchList(event) {
  146. // 存储搜索记录
  147. const has = this.searchHistoryList.some(k => k.text == this.queryWord)
  148. if(!has && this.queryWord!=''){
  149. this.searchHistoryList.push({
  150. id: new Date().getTime(),
  151. text: this.queryWord
  152. })
  153. uni.setStorageSync('searchHistoryList', this.searchHistoryList)
  154. }
  155. this.loadEnd = false
  156. this.loadText = '加载中...'
  157. this.list = []
  158. this.pageNo = 1
  159. this.getList()
  160. },
  161. // 滚动到底部
  162. reachBottom() {
  163. if(!this.loading && !this.loadEnd){
  164. this.pageNo++
  165. this.getList()
  166. }
  167. },
  168. // 列表查询
  169. getList(){
  170. this.loading = true
  171. queryPromoProductByPage({
  172. pageNo: this.pageNo,
  173. pageSize: this.pageSize,
  174. queryWord: this.queryWord,
  175. promoActiveSn: '693459699332595712'
  176. }).then(res => {
  177. this.loading = false
  178. if(res.status == 200){
  179. this.total = res.data.count
  180. if(this.total){
  181. const list = res.data.list || []
  182. const ret = []
  183. // 更新已选状态
  184. list.forEach(k => {
  185. ret.push({
  186. id: k.id,
  187. cost: k.cost,
  188. productCode: k.productCode,
  189. productSn: k.productSn,
  190. productImage: k.productImage,
  191. productName: k.productName,
  192. productOrigCode: k.productOrigCode,
  193. promoActiveSn: k.promoActiveSn,
  194. promoRuleSn: k.promoRuleSn,
  195. promoRuleValue: k.promoRuleValue,
  196. promoRuleType: k.promoRuleType,
  197. })
  198. })
  199. // 追加数据
  200. this.list.push(ret)
  201. // 判断是否最后一页
  202. const maxPages = Math.ceil(this.total / this.pageSize)
  203. if(this.pageNo >= maxPages){
  204. this.loadEnd = true
  205. this.loadText = '没有更多了'
  206. }
  207. }else{
  208. this.loadEnd = false
  209. this.loadText = '没有找到相关产品'
  210. }
  211. }else{
  212. this.loadEnd = false
  213. this.loadText = res.message
  214. }
  215. }).catch(err => {
  216. this.loading = false
  217. })
  218. },
  219. }
  220. }
  221. </script>
  222. <style lang="less">
  223. .pages{
  224. height: 100vh;
  225. display: flex;
  226. flex-direction: column;
  227. .scrollPage-header{
  228. z-index: 1;
  229. .header-title{
  230. display: flex;
  231. align-items: center;
  232. justify-content: space-between;
  233. padding: 0 10px;
  234. height: 44px;
  235. background-color: #FFFFFF;
  236. box-shadow: 0px 1px 0px 0px #E6E6E6;
  237. .header-title{
  238. font-size: 16px;
  239. color: #333333;
  240. max-width: 70%;
  241. }
  242. .header-left{
  243. display: flex;
  244. align-items: center;
  245. text{
  246. font-size: 14px;
  247. color: #333333;
  248. margin-left: 5px;
  249. }
  250. }
  251. .header-right{
  252. width: 50px;
  253. }
  254. }
  255. }
  256. .search-result{
  257. flex-grow: 1;
  258. display: flex;
  259. justify-content: space-between;
  260. .tab-left{
  261. width: 25%;
  262. flex-grow: 1;
  263. background: #FFFFFF;
  264. .item-clz{
  265. font-size: 14px;
  266. padding: 8px 10px;
  267. line-height: 30px;
  268. }
  269. .active{
  270. border-left: 3px solid #00aaff;
  271. background: #f8f8f8;
  272. }
  273. }
  274. .right-box{
  275. width: 75%;
  276. .scrollPage-body{
  277. width:100%;
  278. .search-head input{
  279. background-color: #FFFFFF;
  280. }
  281. .loading-bar{
  282. text-align: center;
  283. padding: 10px 0;
  284. color: #999999;
  285. }
  286. .empty-bar{
  287. display: flex;
  288. flex-direction: column;
  289. align-items: center;
  290. justify-content: center;
  291. padding: 20px 0;
  292. image{
  293. width: 100px;
  294. height: 100px;
  295. }
  296. view{
  297. font-size: 12px;
  298. color: #999999;
  299. }
  300. }
  301. .search-history{
  302. padding: 0 1.5rem;
  303. .search-history-title{
  304. display: flex;
  305. align-items: center;
  306. justify-content: space-between;
  307. padding: 6px 0;
  308. font-size: 14px;
  309. color: #333333;
  310. border-bottom: 1px solid #E6E6E6;
  311. }
  312. .search-history-body{
  313. display: flex;
  314. flex-wrap: wrap;
  315. align-items: center;
  316. justify-content: space-between;
  317. padding: 10px 10px;
  318. .item{
  319. padding: 6px 0;
  320. background-color: #f8f8f8;
  321. border-radius: 20px;
  322. font-size: 12px;
  323. color: #333333;
  324. width: 46%;
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. </style>