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