searchProduct.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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&&categoryList.length>0">
  17. <view class="item-clz"
  18. v-for="item in categoryList"
  19. :key="item.categorySn"
  20. :class="item.categorySn==categorySn?'active':''"
  21. @click="handleTab(item)">
  22. {{item.categoryName}}
  23. </view>
  24. </view>
  25. <view class="right-box" :style="{width:clzId&&categoryList.length?'75%':'100%'}">
  26. <!-- body -->
  27. <scroll-view
  28. class="scrollPage-body"
  29. :class="clzId&&categoryList.length?'right-item':''"
  30. :style="{height: (screenHeight - statusBarHeight - safeAreaBottom - 40) + 'px'}"
  31. scroll-y="true"
  32. type="custom"
  33. @scrolltolower="reachBottom">
  34. <!-- 搜索,吸顶 -->
  35. <sticky-header>
  36. <view class="search-head">
  37. <uni-search-bar v-model="queryWord" :focus="focus" radius="100" placeholder="请输入商品名称/产品编码/原厂编码" textColor="#666" clearButton="auto" cancelButton="none" @confirm="searchList" @clear="clearList" />
  38. </view>
  39. </sticky-header>
  40. <!-- 产品列表 -->
  41. <chooseProductItem
  42. v-for="(item, index) in list"
  43. :key="item.id"
  44. :index="index"
  45. :list="item"
  46. :isView="true"
  47. @toDetail="viewDetail"
  48. >
  49. </chooseProductItem>
  50. <!-- loading -->
  51. <view class="loading-bar" v-if="total>0 && (loading||loadEnd)">{{loadText}}</view>
  52. <view class="empty-bar" v-if="total==0&&!loading">
  53. <image mode="aspectFit" :src="empty.imgUrl"></image>
  54. <view>{{empty.tip}}</view>
  55. </view>
  56. <!-- 搜索记录 -->
  57. <view class="search-history" v-if="historyList.length&&queryWord==''&&clzId==''">
  58. <view class="search-history-title">
  59. <text>搜索记录</text>
  60. <view class="search-history-right">
  61. <view class="search-history-expend" v-if="searchHistoryList.length>10" @click="expend=!expend">
  62. {{expend?'收起':'展开'}}
  63. <uni-icons :type="expend?'arrowup':'arrowdown'" size="14"></uni-icons>
  64. </view>
  65. <uni-icons type="trash" size="18" @click="clearHistory" color="#eb0000"></uni-icons>
  66. </view>
  67. </view>
  68. <view class="search-history-body">
  69. <view class="item" v-for="item in historyList" :key="item.id" @click="toSearch(item)">
  70. <text class="ellipsis-one" max-lines="1" overflow="ellipsis">{{item.text}}</text>
  71. </view>
  72. </view>
  73. </view>
  74. </scroll-view>
  75. </view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import {
  81. mapState,
  82. mapMutations,
  83. } from 'vuex'
  84. import chooseProductItem from '@/pagesB/components/chooseProductItemSkline.vue'
  85. import { getShopCategoryExt, getShopProductList } from '@/api/shop.js'
  86. export default {
  87. components:{
  88. chooseProductItem
  89. },
  90. data() {
  91. return {
  92. title: '',
  93. loadText: '加载中...',
  94. empty: {
  95. tip: '暂无商品',
  96. imgUrl: '/static/nodata.png'
  97. },
  98. queryWord:'',
  99. screenWidth: 325,
  100. screenHeight: 0,
  101. statusBarHeight: 44,
  102. safeAreaBottom: 0,
  103. pageSize: 10,
  104. pageNo:1,
  105. total:0,
  106. list: [],
  107. loading:false,
  108. loadEnd: false,
  109. searchHistoryList: [],
  110. focus: false,
  111. expend: false,
  112. clzId: null, // 一级分类
  113. categorySn: null ,// 当前分类sn
  114. categoryList: [], // 二级分类列表
  115. };
  116. },
  117. computed: {
  118. ...mapState(['hasLogin']),
  119. historyList() {
  120. return this.searchHistoryList.filter(item=>item.text!='').reverse().slice(0,this.expend?20:10)
  121. },
  122. // 登录用户信息
  123. userInfo(){
  124. return this.$store.state.vuex_userInfo
  125. },
  126. },
  127. onLoad(opts) {
  128. // 计算区域高度
  129. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
  130. this.screenWidth = uni.getSystemInfoSync().windowWidth
  131. this.screenHeight = uni.getSystemInfoSync().windowHeight
  132. this.safeAreaBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
  133. // 一级分类id
  134. this.clzId = opts.clzId
  135. // 搜索商品
  136. if(opts.clzId==''){
  137. this.title = '商城商品搜索'
  138. this.focus = true
  139. }else{
  140. // 分类下产品
  141. this.title = opts.clzName
  142. // 查二级目录
  143. this.getCategory()
  144. }
  145. // 搜索历史
  146. this.searchHistoryList = uni.getStorageSync('searchHistoryList') || []
  147. },
  148. methods: {
  149. // 返回
  150. goBack(){
  151. uni.navigateBack()
  152. },
  153. // 清空历史记录
  154. clearHistory(){
  155. const _this = this
  156. uni.showModal({
  157. title: '提示',
  158. content: '确定清空搜索历史?',
  159. confirmText: '确定',
  160. success(res) {
  161. if(res.confirm){
  162. _this.searchHistoryList = []
  163. uni.setStorageSync('searchHistoryList', _this.searchHistoryList)
  164. }
  165. }
  166. })
  167. },
  168. // 清空搜索
  169. clearList(){
  170. // 有二级分类
  171. if(this.clzId&&this.categoryList.length){
  172. this.queryWord = ''
  173. this.searchList()
  174. }else{
  175. this.queryWord = ''
  176. this.list = []
  177. this.total = 0
  178. this.pageNo = 1
  179. this.loadEnd = false
  180. this.loadText = '没有找到相关产品'
  181. }
  182. },
  183. // 选择搜索记录
  184. toSearch(item){
  185. this.queryWord = item.text
  186. this.searchList()
  187. },
  188. // 获取二级目录
  189. getCategory(){
  190. getShopCategoryExt({
  191. categorySn: this.clzId
  192. }).then(res => {
  193. if(res.status == 200){
  194. // 二级目录
  195. this.categoryList = res.data && res.data.shopCategoryList ? res.data.shopCategoryList : []
  196. // 如果有二级目录
  197. if(this.categoryList.length > 0){
  198. // this.categoryList.unshift({
  199. // categorySn: this.clzId,
  200. // categoryName: '全部'
  201. // })
  202. // 默认显示第一个
  203. this.categorySn = this.categoryList[0].categorySn
  204. // 目录下商品
  205. this.searchList()
  206. }else{
  207. // 只有产品
  208. const list = res.data && res.data.shopProductList ? res.data.shopProductList : []
  209. const ret = []
  210. // 更新已选状态
  211. list.forEach(k => {
  212. ret.push({
  213. id: k.id,
  214. price: k.price,
  215. productCode: k.productCode,
  216. productSn: k.productSn,
  217. productImage: k.productMsg,
  218. productName: k.productName,
  219. productOrigCode: k.productOrigCode,
  220. categorySn: k.categorySn,
  221. hotFlag: k.hotFlag,
  222. productSn: k.productSn,
  223. priceType: k.priceType,
  224. shopProductSn: k.shopProductSn,
  225. status: k.status,
  226. statusDictValue: k.statusDictValue,
  227. dealerScopeFlag: k.dealerScopeFlag
  228. })
  229. })
  230. // 追加数据
  231. this.list.push(ret)
  232. }
  233. }else{
  234. uni.showToast({
  235. title: res.message,
  236. icon: 'none'
  237. })
  238. }
  239. })
  240. },
  241. // 搜索列表
  242. searchList(event) {
  243. // 存储搜索记录
  244. const has = this.searchHistoryList.some(k => k.text == this.queryWord)
  245. if(!has && this.queryWord!=''){
  246. this.searchHistoryList.push({
  247. id: new Date().getTime(),
  248. text: this.queryWord
  249. })
  250. uni.setStorageSync('searchHistoryList', this.searchHistoryList)
  251. }
  252. this.loadEnd = false
  253. this.loadText = '加载中...'
  254. this.list = []
  255. this.pageNo = 1
  256. this.getList()
  257. },
  258. // 单击二级分类
  259. handleTab(item){
  260. this.categorySn = item.categorySn
  261. this.queryWord = ''
  262. this.searchList()
  263. },
  264. // 滚动到底部
  265. reachBottom() {
  266. if(!this.loading && !this.loadEnd){
  267. this.pageNo++
  268. this.getList()
  269. }
  270. },
  271. // 列表查询
  272. getList(){
  273. this.loading = true
  274. const dealerSn = this.hasLogin && this.userInfo && this.userInfo.sysUserFlag == '1' ? this.$store.state.vuex_storeShelf.dealerSn : ''
  275. getShopProductList({
  276. pageNo: this.pageNo,
  277. pageSize: this.pageSize,
  278. queryWord: this.queryWord,
  279. categorySn: this.categorySn,
  280. dealerSn: dealerSn
  281. }).then(res => {
  282. this.loading = false
  283. if(res.status == 200){
  284. this.total = Number(res.data.count)
  285. if(this.total){
  286. const list = res.data.list || []
  287. const ret = []
  288. // 更新已选状态
  289. list.forEach(k => {
  290. ret.push({
  291. id: k.id,
  292. price: k.price,
  293. productCode: k.productCode,
  294. productSn: k.productSn,
  295. productImage: k.productMsg,
  296. productName: k.productName,
  297. productOrigCode: k.productOrigCode,
  298. categorySn: k.categorySn,
  299. hotFlag: k.hotFlag,
  300. productSn: k.productSn,
  301. priceType: k.priceType,
  302. shopProductSn: k.shopProductSn,
  303. status: k.status,
  304. statusDictValue: k.statusDictValue,
  305. dealerScopeFlag: k.dealerScopeFlag
  306. })
  307. })
  308. // 追加数据
  309. this.list.push(ret)
  310. // 判断是否最后一页
  311. const maxPages = Math.ceil(this.total / this.pageSize)
  312. if(this.pageNo >= maxPages){
  313. this.loadEnd = true
  314. this.loadText = '没有更多了'
  315. }
  316. }else{
  317. this.loadEnd = false
  318. this.loadText = '没有找到相关产品'
  319. }
  320. }else{
  321. this.loadEnd = false
  322. this.loadText = res.message
  323. }
  324. }).catch(err => {
  325. this.loading = false
  326. })
  327. },
  328. // 产品详情查看
  329. viewDetail(item){
  330. uni.navigateTo({
  331. url:'/pagesB/shopiing/productDetail?sn='+item.shopProductSn
  332. })
  333. }
  334. }
  335. }
  336. </script>
  337. <style lang="less">
  338. .pages{
  339. height: 100vh;
  340. display: flex;
  341. flex-direction: column;
  342. .scrollPage-header{
  343. z-index: 1;
  344. .header-title{
  345. display: flex;
  346. align-items: center;
  347. justify-content: space-between;
  348. padding: 0 10px;
  349. height: 44px;
  350. background-color: #FFFFFF;
  351. box-shadow: 0px 1px 0px 0px #E6E6E6;
  352. .header-title{
  353. font-size: 16px;
  354. color: #333333;
  355. max-width: 70%;
  356. }
  357. .header-left{
  358. display: flex;
  359. align-items: center;
  360. text{
  361. font-size: 14px;
  362. color: #333333;
  363. margin-left: 5px;
  364. }
  365. }
  366. .header-right{
  367. width: 50px;
  368. }
  369. }
  370. }
  371. .search-result{
  372. flex-grow: 1;
  373. display: flex;
  374. justify-content: space-between;
  375. .tab-left{
  376. width: 25%;
  377. flex-grow: 1;
  378. background: #FFFFFF;
  379. .item-clz{
  380. font-size: 14px;
  381. padding: 8px 10px;
  382. line-height: 30px;
  383. }
  384. .active{
  385. border-left: 3px solid #00aaff;
  386. background: #f8f8f8;
  387. }
  388. }
  389. .right-box{
  390. width: 75%;
  391. .scrollPage-body{
  392. width:100%;
  393. .uni-searchbar{
  394. background-color: #FFFFFF;
  395. }
  396. /deep/ .choose-product-item{
  397. background-color: #FFFFFF;
  398. margin: 6px 10px;
  399. border-radius: 10px;
  400. }
  401. .loading-bar{
  402. text-align: center;
  403. padding: 10px 0;
  404. color: #999999;
  405. }
  406. .empty-bar{
  407. display: flex;
  408. flex-direction: column;
  409. align-items: center;
  410. justify-content: center;
  411. padding: 20px 0;
  412. image{
  413. width: 100px;
  414. height: 100px;
  415. }
  416. view{
  417. font-size: 12px;
  418. color: #999999;
  419. }
  420. }
  421. .search-history{
  422. padding: 0 1.5rem;
  423. .search-history-title{
  424. display: flex;
  425. align-items: center;
  426. justify-content: space-between;
  427. padding: 6px 0;
  428. font-size: 14px;
  429. color: #333333;
  430. border-bottom: 1px solid #E6E6E6;
  431. }
  432. .search-history-right{
  433. display: flex;
  434. align-items: center;
  435. }
  436. .search-history-expend{
  437. display: flex;
  438. align-items: center;
  439. font-size: 12px;
  440. margin-right: 15px;
  441. }
  442. .search-history-body{
  443. display: flex;
  444. flex-wrap: wrap;
  445. align-items: center;
  446. justify-content: space-between;
  447. padding: 10px 10px;
  448. .item{
  449. padding: 6px 0;
  450. background-color: #f8f8f8;
  451. border-radius: 20px;
  452. font-size: 12px;
  453. color: #333333;
  454. width: 46%;
  455. }
  456. }
  457. }
  458. }
  459. .right-item{
  460. /deep/ .choose-product-item{
  461. margin: 5px 8px;
  462. .choose-product-item-img{
  463. height: 130rpx;
  464. }
  465. .choose-product-item-info .choose-product-item-left-info-name{
  466. font-weight: normal;
  467. }
  468. }
  469. .search-head{
  470. background-color: #FFFFFF;
  471. /deep/ .uni-searchbar{
  472. padding: 5px 6px;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. </style>