searchProduct.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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="right-box">
  17. <!-- body -->
  18. <scroll-view
  19. class="scrollPage-body"
  20. :style="{height: (screenHeight - statusBarHeight - safeAreaBottom - 40) + 'px'}"
  21. scroll-y="true"
  22. type="custom"
  23. :bounces="false"
  24. @scrolltolower="reachBottom">
  25. <!-- 搜索,吸顶 -->
  26. <sticky-header>
  27. <view class="search-head">
  28. <uni-search-bar v-model="queryWord" :focus="focus" radius="100" placeholder="请输入商品名称/产品编码/原厂编码" textColor="#666" clearButton="auto" cancelButton="none" @input="searchChange" @confirm="searchList" @clear="clearList" />
  29. </view>
  30. </sticky-header>
  31. <!-- 产品列表 -->
  32. <chooseProductItem
  33. v-for="(item, index) in list"
  34. :key="item.id"
  35. :index="index"
  36. :list="item"
  37. :isView="true"
  38. :isLogin="hasLogin"
  39. @toDetail="viewDetail"
  40. >
  41. </chooseProductItem>
  42. <!-- loading -->
  43. <view class="loading-bar" v-if="total>0 && (loading||loadEnd)">{{loadText}}</view>
  44. <!-- <view class="empty-bar" v-if="total==0&&!loading">
  45. <image mode="aspectFit" :src="empty.imgUrl"></image>
  46. <view>{{empty.tip}}</view>
  47. </view> -->
  48. <!-- 搜索记录 -->
  49. <view class="search-history" v-if="historyList.length&&list.length==0&&queryWord==''&&clzId==''">
  50. <view class="search-history-title">
  51. <text>搜索记录</text>
  52. <view class="search-history-right">
  53. <view class="search-history-expend" v-if="searchHistoryList.length>10" @click="expend=!expend">
  54. {{expend?'收起':'展开'}}
  55. <uni-icons :type="expend?'arrowup':'arrowdown'" size="14"></uni-icons>
  56. </view>
  57. <uni-icons type="trash" size="18" @click="clearHistory" color="#eb0000"></uni-icons>
  58. </view>
  59. </view>
  60. <view class="search-history-body">
  61. <view class="item" v-for="item in historyList" :key="item.id" @click="toSearch(item)">
  62. <text class="ellipsis-one" max-lines="1" overflow="ellipsis">{{item.text}}</text>
  63. </view>
  64. </view>
  65. </view>
  66. </scroll-view>
  67. </view>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import {
  73. mapState,
  74. mapMutations,
  75. } from 'vuex'
  76. import chooseProductItem from '@/pagesB/components/chooseProductItemSkline.vue'
  77. import { getShopProductList } from '@/api/shop.js'
  78. export default {
  79. components:{
  80. chooseProductItem
  81. },
  82. data() {
  83. return {
  84. title: '',
  85. loadText: '加载中...',
  86. empty: {
  87. tip: '暂无商品',
  88. imgUrl: '/static/nodata.png'
  89. },
  90. queryWord:'',
  91. screenWidth: 325,
  92. screenHeight: 0,
  93. statusBarHeight: 44,
  94. safeAreaBottom: 0,
  95. pageSize: 10,
  96. pageNo:1,
  97. total:0,
  98. list: [],
  99. loading:false,
  100. loadEnd: false,
  101. searchHistoryList: [],
  102. focus: false,
  103. expend: false,
  104. clzId: null, // 一级分类
  105. categorySn: null ,// 当前分类sn
  106. };
  107. },
  108. computed: {
  109. ...mapState(['hasLogin']),
  110. historyList() {
  111. return this.searchHistoryList.filter(item=>!!item.text).reverse().slice(0,this.expend?20:10)
  112. },
  113. // 登录用户信息
  114. userInfo(){
  115. return this.$store.state.vuex_userInfo
  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. }
  131. // 搜索历史
  132. this.searchHistoryList = uni.getStorageSync('searchHistoryList') || []
  133. },
  134. onUnload() {
  135. // 历史记录最多存30条
  136. if(this.searchHistoryList.length>30){
  137. this.searchHistoryList = this.searchHistoryList.slice(this.searchHistoryList.length-30,this.searchHistoryList.length)
  138. uni.setStorageSync('searchHistoryList', this.searchHistoryList)
  139. }
  140. },
  141. methods: {
  142. // 返回
  143. goBack(){
  144. uni.navigateBack()
  145. },
  146. // 清空历史记录
  147. clearHistory(){
  148. const _this = this
  149. uni.showModal({
  150. title: '提示',
  151. content: '确定清空搜索历史?',
  152. confirmText: '确定',
  153. success(res) {
  154. if(res.confirm){
  155. _this.searchHistoryList = []
  156. uni.setStorageSync('searchHistoryList', _this.searchHistoryList)
  157. }
  158. }
  159. })
  160. },
  161. // 清空搜索
  162. clearList(){
  163. this.queryWord = ''
  164. this.list = []
  165. this.total = 0
  166. this.pageNo = 1
  167. this.loadEnd = false
  168. this.loadText = '没有找到相关产品'
  169. },
  170. // 失去焦点
  171. searchChange(){
  172. if(this.queryWord == ''){
  173. this.clearList()
  174. }
  175. },
  176. // 选择搜索记录
  177. toSearch(item){
  178. this.queryWord = item.text
  179. this.searchList()
  180. },
  181. // 搜索列表
  182. searchList(event) {
  183. // 存储搜索记录
  184. const has = this.searchHistoryList.some(k => k.text == this.queryWord)
  185. if(!has && this.queryWord!=''){
  186. this.searchHistoryList.push({
  187. id: new Date().getTime(),
  188. text: this.queryWord
  189. })
  190. uni.setStorageSync('searchHistoryList', this.searchHistoryList)
  191. }
  192. if(this.queryWord != ''){
  193. this.loadEnd = false
  194. this.loadText = '加载中...'
  195. this.list = []
  196. this.pageNo = 1
  197. this.getList()
  198. }
  199. },
  200. // 滚动到底部
  201. reachBottom() {
  202. console.log(11)
  203. if(!this.loading && !this.loadEnd){
  204. this.pageNo++
  205. this.getList()
  206. }
  207. },
  208. // 列表查询
  209. getList(){
  210. this.loading = true
  211. const storeShelf = this.$store.state.vuex_storeShelf
  212. const dealerSn = this.hasLogin && this.userInfo && this.userInfo.sysUserFlag == '1'&&storeShelf ? storeShelf.dealerSn : ''
  213. getShopProductList({
  214. pageNo: this.pageNo,
  215. pageSize: this.pageSize,
  216. productCode: this.queryWord,
  217. categorySn: this.categorySn,
  218. dealerSn: dealerSn,
  219. categoryStatus : 1,
  220. status: 1
  221. }).then(res => {
  222. this.loading = false
  223. if(res.status == 200){
  224. this.total = Number(res.data.count)
  225. if(this.total){
  226. const list = res.data.list || []
  227. const ret = []
  228. // 更新已选状态
  229. list.forEach(k => {
  230. ret.push({
  231. id: k.id,
  232. price: k.price,
  233. productCode: k.productCode,
  234. productSn: k.productSn,
  235. productImage: k.productMsg,
  236. productName: k.productName,
  237. productOrigCode: k.productOrigCode,
  238. categorySn: k.categorySn,
  239. hotFlag: k.hotFlag,
  240. productSn: k.productSn,
  241. priceType: k.priceType,
  242. shopProductSn: k.shopProductSn,
  243. status: k.status,
  244. statusDictValue: k.statusDictValue,
  245. dealerScopeFlag: k.dealerScopeFlag
  246. })
  247. })
  248. // 追加数据
  249. this.list.push(ret)
  250. // 判断是否最后一页
  251. const maxPages = Math.ceil(this.total / this.pageSize)
  252. if(this.pageNo >= maxPages){
  253. this.loadEnd = true
  254. this.loadText = '没有更多了'
  255. }
  256. }else{
  257. this.loadEnd = false
  258. this.loadText = '没有找到相关产品'
  259. }
  260. }else{
  261. this.loadEnd = false
  262. this.loadText = res.message
  263. }
  264. }).catch(err => {
  265. this.loading = false
  266. })
  267. },
  268. // 产品详情查看
  269. viewDetail(item){
  270. uni.navigateTo({
  271. url:'/pagesB/shopiing/productDetail?sn='+item.shopProductSn
  272. })
  273. }
  274. }
  275. }
  276. </script>
  277. <style lang="less">
  278. .pages{
  279. height: 100vh;
  280. display: flex;
  281. flex-direction: column;
  282. .scrollPage-header{
  283. z-index: 1;
  284. .header-title{
  285. display: flex;
  286. align-items: center;
  287. justify-content: space-between;
  288. padding: 0 10px;
  289. height: 44px;
  290. background-color: #FFFFFF;
  291. box-shadow: 0px 1px 0px 0px #E6E6E6;
  292. .header-title{
  293. font-size: 16px;
  294. color: #333333;
  295. max-width: 70%;
  296. }
  297. .header-left{
  298. display: flex;
  299. align-items: center;
  300. text{
  301. font-size: 14px;
  302. color: #333333;
  303. margin-left: 5px;
  304. }
  305. }
  306. .header-right{
  307. width: 50px;
  308. }
  309. }
  310. }
  311. .search-result{
  312. flex-grow: 1;
  313. display: flex;
  314. justify-content: space-between;
  315. .right-box{
  316. width: 100%;
  317. .scrollPage-body{
  318. width:100%;
  319. .uni-searchbar{
  320. background-color: #FFFFFF;
  321. }
  322. /deep/ .choose-product-item{
  323. background-color: #FFFFFF;
  324. margin: 6px 10px;
  325. border-radius: 10px;
  326. }
  327. .loading-bar{
  328. text-align: center;
  329. padding: 10px 0;
  330. color: #999999;
  331. }
  332. .empty-bar{
  333. display: flex;
  334. flex-direction: column;
  335. align-items: center;
  336. justify-content: center;
  337. padding: 20px 0;
  338. image{
  339. width: 100px;
  340. height: 100px;
  341. }
  342. view{
  343. font-size: 12px;
  344. color: #999999;
  345. }
  346. }
  347. .search-history{
  348. padding: 0 1.5rem;
  349. .search-history-title{
  350. display: flex;
  351. align-items: center;
  352. justify-content: space-between;
  353. padding: 6px 0;
  354. font-size: 14px;
  355. color: #333333;
  356. border-bottom: 1px solid #E6E6E6;
  357. }
  358. .search-history-right{
  359. display: flex;
  360. align-items: center;
  361. }
  362. .search-history-expend{
  363. display: flex;
  364. align-items: center;
  365. font-size: 12px;
  366. margin-right: 15px;
  367. }
  368. .search-history-body{
  369. display: flex;
  370. flex-wrap: wrap;
  371. align-items: center;
  372. justify-content: space-between;
  373. padding: 10px 10px;
  374. .item{
  375. padding: 6px 0;
  376. background-color: #f8f8f8;
  377. border-radius: 20px;
  378. font-size: 12px;
  379. color: #333333;
  380. width: 46%;
  381. }
  382. }
  383. }
  384. }
  385. .right-item{
  386. /deep/ .choose-product-item{
  387. margin: 5px 8px;
  388. .choose-product-item-img{
  389. height: 130rpx;
  390. }
  391. .choose-product-item-info .choose-product-item-left-info-name{
  392. font-weight: normal;
  393. }
  394. }
  395. .search-head{
  396. background-color: #FFFFFF;
  397. /deep/ .uni-searchbar{
  398. padding: 5px 6px;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. </style>