searchProduct.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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="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. uni.showLoading({
  208. title: '加载中...',
  209. mask: true
  210. })
  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. status: 1
  220. }).then(res => {
  221. if(res.status == 200){
  222. this.total = Number(res.data.count)
  223. if(this.total){
  224. const list = res.data.list || []
  225. const ret = list.map(k => {
  226. return {
  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. uni.hideLoading()
  261. this.loading = false
  262. }).catch(err => {
  263. this.loading = false
  264. })
  265. },
  266. // 产品详情查看
  267. viewDetail(item){
  268. uni.navigateTo({
  269. url:'/pagesB/shopiing/productDetail?sn='+item.shopProductSn
  270. })
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="less">
  276. .pages{
  277. height: 100vh;
  278. display: flex;
  279. flex-direction: column;
  280. .scrollPage-header{
  281. z-index: 1;
  282. .header-title{
  283. display: flex;
  284. align-items: center;
  285. justify-content: space-between;
  286. padding: 0 10px;
  287. height: 44px;
  288. background-color: #FFFFFF;
  289. box-shadow: 0px 1px 0px 0px #E6E6E6;
  290. .header-title{
  291. font-size: 16px;
  292. color: #333333;
  293. max-width: 70%;
  294. }
  295. .header-left{
  296. display: flex;
  297. align-items: center;
  298. text{
  299. font-size: 14px;
  300. color: #333333;
  301. margin-left: 5px;
  302. }
  303. }
  304. .header-right{
  305. width: 50px;
  306. }
  307. }
  308. }
  309. .scrollPage-body{
  310. width:100%;
  311. .uni-searchbar{
  312. background-color: #FFFFFF;
  313. }
  314. /deep/ .choose-product-item{
  315. background-color: #FFFFFF;
  316. margin: 6px 10px;
  317. border-radius: 10px;
  318. }
  319. .loading-bar{
  320. text-align: center;
  321. padding: 10px 0;
  322. color: #999999;
  323. }
  324. .empty-bar{
  325. display: flex;
  326. flex-direction: column;
  327. align-items: center;
  328. justify-content: center;
  329. padding: 20px 0;
  330. image{
  331. width: 100px;
  332. height: 100px;
  333. }
  334. view{
  335. font-size: 12px;
  336. color: #999999;
  337. }
  338. }
  339. .search-history{
  340. padding: 0 1.5rem;
  341. .search-history-title{
  342. display: flex;
  343. align-items: center;
  344. justify-content: space-between;
  345. padding: 6px 0;
  346. font-size: 14px;
  347. color: #333333;
  348. border-bottom: 1px solid #E6E6E6;
  349. }
  350. .search-history-right{
  351. display: flex;
  352. align-items: center;
  353. }
  354. .search-history-expend{
  355. display: flex;
  356. align-items: center;
  357. font-size: 12px;
  358. margin-right: 15px;
  359. }
  360. .search-history-body{
  361. display: flex;
  362. flex-wrap: wrap;
  363. align-items: center;
  364. justify-content: space-between;
  365. padding: 10px 10px;
  366. .item{
  367. padding: 6px 0;
  368. background-color: #f8f8f8;
  369. border-radius: 20px;
  370. font-size: 12px;
  371. color: #333333;
  372. width: 46%;
  373. }
  374. }
  375. }
  376. }
  377. }
  378. </style>