searchProduct.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. })
  219. })
  220. // 追加数据
  221. this.list.push(ret)
  222. }
  223. }
  224. })
  225. },
  226. // 搜索列表
  227. searchList(event) {
  228. // 存储搜索记录
  229. const has = this.searchHistoryList.some(k => k.text == this.queryWord)
  230. if(!has && this.queryWord!=''){
  231. this.searchHistoryList.push({
  232. id: new Date().getTime(),
  233. text: this.queryWord
  234. })
  235. uni.setStorageSync('searchHistoryList', this.searchHistoryList)
  236. }
  237. this.loadEnd = false
  238. this.loadText = '加载中...'
  239. this.list = []
  240. this.pageNo = 1
  241. this.getList()
  242. },
  243. // 单击二级分类
  244. handleTab(item){
  245. this.categorySn = item.categorySn
  246. this.queryWord = ''
  247. this.searchList()
  248. },
  249. // 滚动到底部
  250. reachBottom() {
  251. if(!this.loading && !this.loadEnd){
  252. this.pageNo++
  253. this.getList()
  254. }
  255. },
  256. // 列表查询
  257. getList(){
  258. this.loading = true
  259. getShopProductList({
  260. pageNo: this.pageNo,
  261. pageSize: this.pageSize,
  262. queryWord: this.queryWord,
  263. categorySn: this.categorySn
  264. }).then(res => {
  265. this.loading = false
  266. if(res.status == 200){
  267. this.total = Number(res.data.count)
  268. if(this.total){
  269. const list = res.data.list || []
  270. const ret = []
  271. // 更新已选状态
  272. list.forEach(k => {
  273. ret.push({
  274. id: k.id,
  275. price: k.price,
  276. productCode: k.productCode,
  277. productSn: k.productSn,
  278. productImage: k.productMsg,
  279. productName: k.productName,
  280. productOrigCode: k.productOrigCode,
  281. categorySn: k.categorySn,
  282. hotFlag: k.hotFlag,
  283. productSn: k.productSn,
  284. priceType: k.priceType,
  285. shopProductSn: k.shopProductSn,
  286. status: k.status,
  287. statusDictValue: k.statusDictValue
  288. })
  289. })
  290. // 追加数据
  291. this.list.push(ret)
  292. // 判断是否最后一页
  293. const maxPages = Math.ceil(this.total / this.pageSize)
  294. if(this.pageNo >= maxPages){
  295. this.loadEnd = true
  296. this.loadText = '没有更多了'
  297. }
  298. }else{
  299. this.loadEnd = false
  300. this.loadText = '没有找到相关产品'
  301. }
  302. }else{
  303. this.loadEnd = false
  304. this.loadText = res.message
  305. }
  306. }).catch(err => {
  307. this.loading = false
  308. })
  309. },
  310. // 产品详情查看
  311. viewDetail(item){
  312. uni.navigateTo({
  313. url:'/pagesB/shopiing/productDetail?sn='+item.shopProductSn
  314. })
  315. }
  316. }
  317. }
  318. </script>
  319. <style lang="less">
  320. .pages{
  321. height: 100vh;
  322. display: flex;
  323. flex-direction: column;
  324. .scrollPage-header{
  325. z-index: 1;
  326. .header-title{
  327. display: flex;
  328. align-items: center;
  329. justify-content: space-between;
  330. padding: 0 10px;
  331. height: 44px;
  332. background-color: #FFFFFF;
  333. box-shadow: 0px 1px 0px 0px #E6E6E6;
  334. .header-title{
  335. font-size: 16px;
  336. color: #333333;
  337. max-width: 70%;
  338. }
  339. .header-left{
  340. display: flex;
  341. align-items: center;
  342. text{
  343. font-size: 14px;
  344. color: #333333;
  345. margin-left: 5px;
  346. }
  347. }
  348. .header-right{
  349. width: 50px;
  350. }
  351. }
  352. }
  353. .search-result{
  354. flex-grow: 1;
  355. display: flex;
  356. justify-content: space-between;
  357. .tab-left{
  358. width: 25%;
  359. flex-grow: 1;
  360. background: #FFFFFF;
  361. .item-clz{
  362. font-size: 14px;
  363. padding: 8px 10px;
  364. line-height: 30px;
  365. }
  366. .active{
  367. border-left: 3px solid #00aaff;
  368. background: #f8f8f8;
  369. }
  370. }
  371. .right-box{
  372. width: 75%;
  373. .scrollPage-body{
  374. width:100%;
  375. .uni-searchbar{
  376. background-color: #FFFFFF;
  377. }
  378. /deep/ .choose-product-item{
  379. background-color: #FFFFFF;
  380. margin: 6px 10px;
  381. border-radius: 10px;
  382. }
  383. .loading-bar{
  384. text-align: center;
  385. padding: 10px 0;
  386. color: #999999;
  387. }
  388. .empty-bar{
  389. display: flex;
  390. flex-direction: column;
  391. align-items: center;
  392. justify-content: center;
  393. padding: 20px 0;
  394. image{
  395. width: 100px;
  396. height: 100px;
  397. }
  398. view{
  399. font-size: 12px;
  400. color: #999999;
  401. }
  402. }
  403. .search-history{
  404. padding: 0 1.5rem;
  405. .search-history-title{
  406. display: flex;
  407. align-items: center;
  408. justify-content: space-between;
  409. padding: 6px 0;
  410. font-size: 14px;
  411. color: #333333;
  412. border-bottom: 1px solid #E6E6E6;
  413. }
  414. .search-history-right{
  415. display: flex;
  416. align-items: center;
  417. }
  418. .search-history-expend{
  419. display: flex;
  420. align-items: center;
  421. font-size: 12px;
  422. margin-right: 15px;
  423. }
  424. .search-history-body{
  425. display: flex;
  426. flex-wrap: wrap;
  427. align-items: center;
  428. justify-content: space-between;
  429. padding: 10px 10px;
  430. .item{
  431. padding: 6px 0;
  432. background-color: #f8f8f8;
  433. border-radius: 20px;
  434. font-size: 12px;
  435. color: #333333;
  436. width: 46%;
  437. }
  438. }
  439. }
  440. }
  441. .right-item{
  442. /deep/ .choose-product-item{
  443. margin: 5px 8px;
  444. .choose-product-item-img{
  445. height: 130rpx;
  446. }
  447. .choose-product-item-info .choose-product-item-left-info-name{
  448. font-weight: normal;
  449. }
  450. }
  451. .search-head{
  452. background-color: #FFFFFF;
  453. /deep/ .uni-searchbar{
  454. padding: 5px 6px;
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. </style>