publicSearch.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <template>
  2. <view class="container">
  3. <u-navbar leftIconColor="#fff" titleStyle="color:#fff;fontSize:36rpx;" :autoBack="true" bgColor="#2C6FF3"
  4. :placeholder="true" :title="title"></u-navbar>
  5. <view class="content">
  6. <view class="search">
  7. <u-search animation placeholder="商品搜索" :clearabled="false" v-model="keyword" :showAction="false"
  8. @search="search"></u-search>
  9. <view class="scan-icon" @click="scan">
  10. <u-icon name="scan" size="22" color="#909399"></u-icon>
  11. </view>
  12. <view class="search-history flex flex-wrap flex-start">
  13. <view class="history-item" v-for="(item,index) in historyList" :key="index"
  14. @click="searchFast(item)">
  15. {{item}}
  16. </view>
  17. </view>
  18. </view>
  19. <view class="list" v-if="newCommList&&newCommList.length>0">
  20. <view class="thumb-box" v-for="(item, index) in newCommList" :key="index"
  21. @click.stop="commItemSelect(item)">
  22. <view>
  23. <image class="select-img"
  24. src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/commodity/no_selected.png"
  25. mode="widthFix" v-show="item.noSelect"></image>
  26. <image class="select-img"
  27. src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/commodity/selected.png"
  28. mode="widthFix" v-show="!item.noSelect&&item.checked"></image>
  29. <image class="select-img"
  30. src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/commodity/select.png"
  31. mode="widthFix" v-show="!item.noSelect&&!item.checked"></image>
  32. </view>
  33. <view class="check-content">
  34. <view class="comm-img">
  35. <u--image width="130rpx" height="130rpx" :src="item.cover" mode="widthFix"
  36. :lazy-lord="true"></u--image>
  37. </view>
  38. <view class="comm-main">
  39. <view>
  40. {{item.name}}
  41. </view>
  42. <view>
  43. 条形码:{{item.barcode}}
  44. </view>
  45. <view>
  46. 商品类型:{{item.categoryName}}
  47. </view>
  48. <view v-if="type==1">
  49. 价格:<text>¥{{item.price}}</text>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <u-loadmore :status="status" v-if="newCommList.length>=1" />
  55. </view>
  56. <view class="empty" v-if="newCommList.length==0">
  57. <u-empty mode="car" text="没有商品!"></u-empty>
  58. </view>
  59. </view>
  60. <view class="btn safe-bottom">
  61. <xbutton size="large" @click="submit">{{btnName}}({{selectList.length}})</xbutton>
  62. </view>
  63. </view>
  64. </template>
  65. <script>
  66. import {
  67. ownerGoodsList as perOwnerGoodsList
  68. } from "@/api/commodity/mercGoods.js"
  69. import {
  70. ownerGoodsList as pubOwnerGoodsList,
  71. pageByGoods,
  72. pageByGoodsMerc
  73. } from "@/api/commodity/goods.js"
  74. export default {
  75. data() {
  76. return {
  77. keyword: '',
  78. commList: [], //商品列表
  79. page: 1, //商品分页
  80. size: 10,
  81. status: 'loadmore', //加载更多
  82. type: null,
  83. storeName: null,
  84. title: '私有商品库',
  85. btnName: '添加到商品私库',
  86. selectList: [],
  87. id: null,
  88. historyList: []
  89. }
  90. },
  91. computed: {
  92. newCommList() {
  93. let newCommList = [];
  94. if (this.selectList && this.selectList.length > 0) {
  95. let selectList = this.selectList
  96. const idMapping = selectList.reduce((acc, el, i) => {
  97. acc[el.id] = i;
  98. return acc;
  99. }, {});
  100. this.commList.forEach(i => {
  101. if (idMapping[i.id] != undefined) { //重复值
  102. i.checked = true
  103. } else {
  104. i.checked = false
  105. }
  106. newCommList.push(i)
  107. })
  108. } else {
  109. newCommList = this.commList.map(i => {
  110. i.checked = false;
  111. return i
  112. })
  113. }
  114. console.log('newCommList', newCommList)
  115. return newCommList
  116. },
  117. },
  118. onLoad(o) {
  119. if (uni.getStorageSync('goods')) {
  120. this.historyList = JSON.parse(uni.getStorageSync('goods'))
  121. }
  122. if (o.type) {
  123. this.id = o.type != 0 ? o.id : null;
  124. this.type = o.type; //0公库到私库、1私库到设备、2公库到设备
  125. this.title = o.type == 1 ? '私有商品库' : '官方商品库';
  126. this.btnName = o.storeName == 'pubToPerStor' ? '添加到商品私库' : '添加到设备';
  127. this.storeName = o.storeName;
  128. if (uni.getStorageSync(this.storeName) && JSON.parse(uni.getStorageSync(
  129. this.storeName)).length > 0) {
  130. let commStor = JSON.parse(uni.getStorageSync(this.storeName))
  131. this.selectList = commStor;
  132. }
  133. this.getCommList();
  134. }
  135. },
  136. methods: {
  137. search(val) {
  138. this.saveKeyWord('goods', this.keyword)
  139. this.reset();
  140. this.getCommList()
  141. },
  142. saveKeyWord(type, val) {
  143. if (val) {
  144. let arr = []
  145. if (uni.getStorageSync(type)) {
  146. let arr = JSON.parse(uni.getStorageSync(type))
  147. if (arr.indexOf(val) != -1) {
  148. console.log('arr.indexOf(val)', arr.indexOf(val))
  149. arr.splice(arr.indexOf(val), 1)
  150. }
  151. arr.unshift(val)
  152. if (arr.length > 6) {
  153. arr.pop()
  154. }
  155. this.historyList = JSON.parse(JSON.stringify(arr))
  156. uni.setStorageSync(type, JSON.stringify(arr))
  157. } else {
  158. arr.unshift(val)
  159. this.historyList = JSON.parse(JSON.stringify(arr))
  160. uni.setStorageSync(type, JSON.stringify(arr))
  161. }
  162. } else {
  163. return
  164. }
  165. },
  166. searchFast(e) {
  167. this.keyword = e
  168. this.search()
  169. },
  170. //扫码
  171. scan() {
  172. uni.scanCode({
  173. success: (res) => {
  174. this.keyword = res.result;
  175. this.getCommList()
  176. }
  177. });
  178. },
  179. //根据类目获取商品列表
  180. getCommList(id) {
  181. if (this.type == 0) { //公库到私库
  182. pubOwnerGoodsList({
  183. page: {
  184. current: this.page,
  185. size: this.size
  186. },
  187. keyword: this.keyword
  188. }).then(res => {
  189. let data = res.data.records;
  190. let newData = data.map(i => {
  191. i.noSelect = i.mercGoodsId != null ? true : false;
  192. i.categoryCode = i.categoryCodeLevel1;
  193. return i
  194. })
  195. if (newData.length < 10) {
  196. this.status = "nomore"
  197. } else {
  198. this.status = "loadmore"
  199. }
  200. this.commList = this.commList.concat(newData)
  201. })
  202. }
  203. if (this.type == 1) { //私库到设备
  204. pageByGoodsMerc({
  205. page: {
  206. current: this.page,
  207. size: this.size
  208. },
  209. keyword: this.keyword,
  210. deviceId: this.id
  211. }).then(res => {
  212. let data = res.data.records;
  213. if (data && data.length > 0) {
  214. data = data.map(i => {
  215. i.noSelect = i.isBind;
  216. i.name = i.goodsName;
  217. i.barcode = i.goodsBarcode;
  218. i.cover = i.goodsCover;
  219. i.price = Number(i.price) / 100;
  220. i.categoryName = i.capacity == null ? '未分类' : i.capacity;
  221. return i
  222. })
  223. }
  224. if (data.length < 10) {
  225. this.status = "nomore"
  226. } else {
  227. this.status = "loadmore"
  228. }
  229. this.commList = this.commList.concat(data)
  230. })
  231. }
  232. if (this.type == 2) { //公库设备
  233. pageByGoods({
  234. page: {
  235. current: this.page,
  236. size: this.size
  237. },
  238. keyword: this.keyword,
  239. deviceId: this.id
  240. }).then(res => {
  241. let data = res.data.records;
  242. let newData = data.map(i => {
  243. i.noSelect = i.isBind;
  244. i.categoryCode = i.categoryCodeLevel1;
  245. return i
  246. })
  247. if (newData.length < 10) {
  248. this.status = "nomore"
  249. } else {
  250. this.status = "loadmore"
  251. }
  252. this.commList = this.commList.concat(newData)
  253. })
  254. }
  255. },
  256. //触底加载更多
  257. onReachBottom() {
  258. if (this.status == 'nomore') return
  259. this.page++
  260. this.getCommList()
  261. },
  262. reset() {
  263. this.status == 'loadmore'
  264. this.page = 1;
  265. this.size = 10;
  266. this.commList = [];
  267. },
  268. // 商品选中状态改变
  269. commItemSelect(e) {
  270. if (e.noSelect) {
  271. this.$modal.msg('当前商品已存在!')
  272. return
  273. }
  274. e.checked = !e.checked
  275. //选中商品存储到内存中,以便取用
  276. let commStor = []
  277. if (e.checked) { //选中添加到存储中
  278. if (uni.getStorageSync(this.storeName)) {
  279. commStor = JSON.parse(uni.getStorageSync(this.storeName));
  280. }
  281. commStor.push(e);
  282. } else { //取消选中删除,并且从存储中删除
  283. commStor = JSON.parse(uni.getStorageSync(this.storeName));
  284. for (let i = 0; i < commStor.length; i++) {
  285. let item = commStor[i]
  286. if (item.id == e.id) {
  287. commStor.splice(i, 1);
  288. break
  289. }
  290. }
  291. }
  292. //收集选中商品,更新存储
  293. this.selectList = commStor;
  294. commStor.length > 0 ? uni.setStorageSync(this.storeName, JSON.stringify(commStor)) : uni
  295. .setStorageSync(
  296. this.storeName, '')
  297. if (commStor.length > 0) {
  298. uni.setStorageSync(this.storeName, JSON.stringify(commStor))
  299. }
  300. },
  301. submit() {
  302. if (uni.getStorageSync(this.storeName) && JSON.parse(uni.getStorageSync(this.storeName)).length > 0) {
  303. this.$tab.navigateTo(
  304. `/pages/equipment/addComList?storeName=${this.storeName}&type=${this.type}&id=${this.id}`)
  305. } else {
  306. uni.$u.toast('请先选择商品!')
  307. }
  308. }
  309. }
  310. }
  311. </script>
  312. <style lang="scss" scoped>
  313. .container {
  314. .content {
  315. padding-bottom: 88rpx;
  316. .search {
  317. padding: 24rpx 24rpx 12rpx;
  318. background-color: #fff;
  319. position: relative;
  320. .scan-icon {
  321. position: absolute;
  322. right: 36rpx;
  323. top: 38rpx;
  324. z-index: 2;
  325. }
  326. .search-history {
  327. .history-item {
  328. margin-right: 24rpx;
  329. padding: 0 12rpx;
  330. background-color: #f2f2f2;
  331. color: #333;
  332. font-size: 24rpx;
  333. line-height: 40rpx;
  334. border-radius: 40rpx;
  335. margin-top: 24rpx;
  336. }
  337. }
  338. }
  339. .list {
  340. width: 100%;
  341. padding: 12rpx 24rpx;
  342. .thumb-box {
  343. margin-bottom: 12rpx;
  344. border-bottom: 1rpx solid #f4f4f4;
  345. display: flex;
  346. flex-flow: row nowrap;
  347. padding: 12rpx 12rpx;
  348. align-items: center;
  349. background-color: #fff;
  350. border-radius: 12rpx;
  351. }
  352. .select-img {
  353. width: 40rpx;
  354. height: 40rpx;
  355. }
  356. .check-content {
  357. display: flex;
  358. flex-direction: row;
  359. align-items: center;
  360. padding-left: 12rpx;
  361. .comm-img {
  362. width: 130rpx;
  363. height: 130rpx;
  364. display: flex;
  365. flex-direction: row;
  366. align-items: center;
  367. justify-content: space-around;
  368. image {
  369. width: 100%;
  370. }
  371. }
  372. .comm-main {
  373. box-sizing: border-box;
  374. padding-left: 18rpx;
  375. color: #999;
  376. >view {
  377. padding: 4rpx 0;
  378. }
  379. >view:nth-child(1) {
  380. font-size: 28rpx;
  381. font-weight: bold;
  382. color: #333;
  383. }
  384. >view:nth-child(2) {
  385. font-size: 26rpx;
  386. }
  387. >view:nth-child(3) {
  388. font-size: 26rpx;
  389. }
  390. >view:nth-child(4) {
  391. font-size: 26rpx;
  392. text {
  393. font-weight: bold;
  394. color: red;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. }
  401. .btn {
  402. width: 100%;
  403. position: fixed;
  404. left: 0;
  405. bottom: 24rpx;
  406. padding: 0 24rpx;
  407. }
  408. .empty {
  409. position: absolute;
  410. left: 50%;
  411. top: 50%;
  412. transform: translate(-50%, -50%);
  413. }
  414. }
  415. </style>