searchProduct.vue 8.6 KB

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