searchProduct.vue 12 KB

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