searchProduct.vue 9.5 KB

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