allGoodsSearch.vue 12 KB

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