searchProduct.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. 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. status: 1
  216. }).then(res => {
  217. if(res.status == 200){
  218. this.total = Number(res.data.count)
  219. if(this.total){
  220. const list = res.data.list || []
  221. const ret = list.map(k => {
  222. return {
  223. id: k.id,
  224. price: k.price,
  225. productCode: k.productCode,
  226. productSn: k.productSn,
  227. productImage: k.productMsg,
  228. productName: k.productName,
  229. productOrigCode: k.productOrigCode,
  230. categorySn: k.categorySn,
  231. hotFlag: k.hotFlag,
  232. productSn: k.productSn,
  233. priceType: k.priceType,
  234. shopProductSn: k.shopProductSn,
  235. status: k.status,
  236. statusDictValue: k.statusDictValue,
  237. dealerScopeFlag: k.dealerScopeFlag
  238. }
  239. })
  240. // 追加数据
  241. this.list.push(ret)
  242. // 判断是否最后一页
  243. const maxPages = Math.ceil(this.total / this.pageSize)
  244. if(this.pageNo >= maxPages){
  245. this.loadEnd = true
  246. this.loadText = '没有更多了'
  247. }
  248. }else{
  249. this.loadEnd = false
  250. this.loadText = '没有找到相关产品'
  251. }
  252. }else{
  253. this.loadEnd = false
  254. this.loadText = res.message
  255. }
  256. this.loading = false
  257. }).catch(err => {
  258. this.loading = false
  259. })
  260. },
  261. // 产品详情查看
  262. viewDetail(item){
  263. uni.navigateTo({
  264. url:'/pagesB/shopiing/productDetail?sn='+item.shopProductSn
  265. })
  266. }
  267. }
  268. }
  269. </script>
  270. <style lang="less">
  271. .pages{
  272. height: 100vh;
  273. display: flex;
  274. flex-direction: column;
  275. .scrollPage-header{
  276. z-index: 1;
  277. .header-title{
  278. display: flex;
  279. align-items: center;
  280. justify-content: space-between;
  281. padding: 0 10px;
  282. height: 44px;
  283. background-color: #FFFFFF;
  284. box-shadow: 0px 1px 0px 0px #E6E6E6;
  285. .header-title{
  286. font-size: 16px;
  287. color: #333333;
  288. max-width: 70%;
  289. }
  290. .header-left{
  291. display: flex;
  292. align-items: center;
  293. text{
  294. font-size: 14px;
  295. color: #333333;
  296. margin-left: 5px;
  297. }
  298. }
  299. .header-right{
  300. width: 50px;
  301. }
  302. }
  303. }
  304. .scrollPage-body{
  305. width:100%;
  306. .uni-searchbar{
  307. background-color: #FFFFFF;
  308. }
  309. /deep/ .choose-product-item{
  310. background-color: #FFFFFF;
  311. margin: 6px 10px;
  312. border-radius: 10px;
  313. }
  314. .loading-bar{
  315. text-align: center;
  316. padding: 10px 0;
  317. color: #999999;
  318. }
  319. .empty-bar{
  320. display: flex;
  321. flex-direction: column;
  322. align-items: center;
  323. justify-content: center;
  324. padding: 20px 0;
  325. image{
  326. width: 100px;
  327. height: 100px;
  328. }
  329. view{
  330. font-size: 12px;
  331. color: #999999;
  332. }
  333. }
  334. .search-history{
  335. padding: 0 1.5rem;
  336. .search-history-title{
  337. display: flex;
  338. align-items: center;
  339. justify-content: space-between;
  340. padding: 6px 0;
  341. font-size: 14px;
  342. color: #333333;
  343. border-bottom: 1px solid #E6E6E6;
  344. }
  345. .search-history-right{
  346. display: flex;
  347. align-items: center;
  348. }
  349. .search-history-expend{
  350. display: flex;
  351. align-items: center;
  352. font-size: 12px;
  353. margin-right: 15px;
  354. }
  355. .search-history-body{
  356. display: flex;
  357. flex-wrap: wrap;
  358. align-items: center;
  359. justify-content: space-between;
  360. padding: 10px 10px;
  361. .item{
  362. padding: 6px 0;
  363. background-color: #f8f8f8;
  364. border-radius: 20px;
  365. font-size: 12px;
  366. color: #333333;
  367. width: 46%;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. </style>