searchProduct.vue 12 KB

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