searchProduct.vue 9.5 KB

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