searchProduct.vue 12 KB

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