searchProduct.vue 12 KB

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