addCom.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <view class="container">
  3. <u-navbar leftIconColor="#fff" titleStyle="color:#fff;fontSize:36rpx;" :autoBack="true" bgColor="#2C6FF3"
  4. :placeholder="true" title="增加商品"></u-navbar>
  5. <!-- <view class="search" @click.native="searchClick">
  6. <u-search animation placeholder="商品搜索" disabled v-model="keyword" :showAction="false" search="search">
  7. </u-search>
  8. </view> -->
  9. <view class="search" @click="searchComm">
  10. <view class="search-input">
  11. <u-search placeholder="商品搜索" actionText="取消" :actionStyle="{color:'#2C6FF3'}" :showAction="!leftShow"
  12. :clearabled="false" v-model="keyword" @search="search" @custom="cancle"></u-search>
  13. <view @click="scan" :class="[leftShow?'scan-icon scan-left-show':'scan-icon scan-left-hidden']">
  14. <u-icon name="scan" size="22" color="#909399"></u-icon>
  15. </view>
  16. </view>
  17. <view class="search-history flex flex-wrap flex-start" v-if="!leftShow">
  18. <view class="history-item" v-for="(item,index) in historyList" :key="index" @click="searchFast(item)">
  19. {{item}}
  20. </view>
  21. </view>
  22. </view>
  23. <view class="xy-card tab-list" style="padding:0 0 24rpx;">
  24. <view class="tab-left">
  25. <u-tabs :list="tabList" :scrollable="false" :current="current" @click="tabClick" lineColor="#2C6FF3">
  26. </u-tabs>
  27. </view>
  28. <view class="use-list" @click="$tab.navigateTo(`/pages/commodity/commoditylist?id=${id}`)">
  29. 常用商品清单
  30. </view>
  31. </view>
  32. <view class="content">
  33. <view class="swiperitem-content" v-if="current==0">
  34. <view class="classify-wrap">
  35. <Classify storeName="perStor" :selectShow="true" :tabList="perTabList" :status="perStatus"
  36. :commList="perCommList" @switchMenu="perSwitchMenu" @lowerBottom="perLowerBottom"
  37. :isModal="true" :height="fullHeight" @comClick="comClick" :leftShow="leftShow" />
  38. </view>
  39. <view class="btn safe-bottom">
  40. <xbutton size="large" @click="addCom">添加到设备</xbutton>
  41. </view>
  42. </view>
  43. <view class="swiperitem-content" v-if="current==1">
  44. <view class="classify-wrap">
  45. <Classify storeName="pubStor" :selectShow="true" :tabList="pubTabList" :status="pubStatus"
  46. :commList="pubCommList" @switchMenu="pubSwitchMenu" @lowerBottom="pubLowerBottom"
  47. :height="fullHeight" :leftShow="leftShow" />
  48. </view>
  49. <view class="btn">
  50. <xbutton size="large" @click="addCom">添加到设备</xbutton>
  51. </view>
  52. </view>
  53. <!-- 选项卡内容轮播滑动显示,current为当前第几个swiper子项 -->
  54. <!-- <swiper @change="change" :current="current" class="swiper-content" :style="{height:height}">
  55. <swiper-item class="swiperitem-content">
  56. <view class="search">
  57. <u-search animation placeholder="商品搜索" v-model="keyword" :showAction="false" search="search">
  58. </u-search>
  59. </view>
  60. <view class="classify-wrap">
  61. <Classify storeName="perStor1" :height="fullHeight" :selectShow="true"
  62. v-model="commodityList" />
  63. </view>
  64. <view class="btn">
  65. <xbutton size="large" @click="addCom">已选中(21)样商品</xbutton>
  66. </view>
  67. </swiper-item>
  68. <swiper-item class="swiperitem-content">
  69. <view class="search">
  70. <u-search animation placeholder="商品搜索" v-model="keyword" :showAction="false" search="search">
  71. </u-search>
  72. </view>
  73. <view class="classify-wrap">
  74. <Classify storeName="perStor2" :height="fullHeight" :selectShow="true"
  75. v-model="commodityList" />
  76. </view>
  77. <view class="btn">
  78. <xbutton size="large" @click="addCom">已选中(21)样商品</xbutton>
  79. </view>
  80. </swiper-item>
  81. </swiper> -->
  82. </view>
  83. </view>
  84. </template>
  85. <script>
  86. import Classify from "@/components/classify/index.vue"
  87. import {
  88. goodsCategory as perGoodsCategory,
  89. } from "@/api/commodity/mercGoods.js"
  90. import {
  91. goodsCategory as pubGoodsCategory,
  92. pageByGoods,
  93. pageByGoodsMerc,
  94. bindDeviceByMercGoods
  95. } from "@/api/commodity/goods.js"
  96. export default {
  97. components: {
  98. Classify
  99. },
  100. data() {
  101. return {
  102. tabList: [{
  103. name: '私有商品库'
  104. },
  105. {
  106. name: '官方商品库'
  107. }
  108. ],
  109. current: 0,
  110. fullHeight: "0",
  111. // 私库
  112. perTabList: [], //商品类目
  113. perCommList: [], //商品列表
  114. perPage: 1, //商品分页
  115. perSize: 10,
  116. perStatus: 'loadmore', //加载更多
  117. // 公库
  118. pubTabList: [], //商品类目
  119. pubCommList: [], //商品列表
  120. pubPage: 1, //商品分页
  121. pubSize: 10,
  122. pubStatus: 'loadmore', //加载更多
  123. id: null, //设备id
  124. categoryCode: null,
  125. leftShow: true,
  126. historyList: [],
  127. keyword: ''
  128. }
  129. },
  130. onLoad(o) {
  131. if (o.id) {
  132. this.id = o.id;
  133. }
  134. let _this = this;
  135. const query = uni.createSelectorQuery().in(this);
  136. query.select(".classify-wrap").boundingClientRect((data) => {
  137. _this.top = data.top;
  138. uni.getSystemInfo({
  139. success(res) {
  140. // 针对iPhone X等机型底部安全距离做适配
  141. const model = res.model;
  142. const modelInclude = [
  143. "iPhone X",
  144. 'iPhone XR',
  145. "iPhone XS",
  146. "iPhone XS MAX",
  147. "iPhone 12/13 mini",
  148. "iPhone 12/13 (Pro)",
  149. "iPhone 12/13 Pro Max",
  150. "iPhone 14 Pro Max"
  151. ];
  152. let safeDistance = modelInclude.includes(model)
  153. //动态设置商品区域高度
  154. if (safeDistance) {
  155. _this.fullHeight = res.windowHeight - _this.top - 84 + 'px';
  156. } else {
  157. _this.fullHeight = res.windowHeight - _this.top - 54 + 'px';
  158. }
  159. },
  160. });
  161. }).exec();
  162. if (uni.getStorageSync('goods')) {
  163. this.historyList = JSON.parse(uni.getStorageSync('goods'))
  164. }
  165. this.getPerCategory()
  166. this.getPubCategory()
  167. },
  168. async onShow() {
  169. this.perReset()
  170. this.pubReset()
  171. await this.getPerCommList()
  172. await this.getPubCommList()
  173. },
  174. methods: {
  175. searchComm() {
  176. this.leftShow = false
  177. },
  178. search(val) {
  179. this.saveKeyWord('goods', this.keyword)
  180. if (this.current == 0) { //私库
  181. this.perReset();
  182. this.getPerCommList()
  183. } else { //公库
  184. this.pubReset();
  185. this.getPubCommList()
  186. }
  187. },
  188. cancle(val) {
  189. this.keyword = ''
  190. this.leftShow = true
  191. this.search()
  192. },
  193. saveKeyWord(type, val) {
  194. if (val) {
  195. let arr = []
  196. if (uni.getStorageSync(type)) {
  197. let arr = JSON.parse(uni.getStorageSync(type))
  198. if (arr.indexOf(val) != -1) {
  199. console.log('arr.indexOf(val)', arr.indexOf(val))
  200. arr.splice(arr.indexOf(val), 1)
  201. }
  202. arr.unshift(val)
  203. if (arr.length > 6) {
  204. arr.pop()
  205. }
  206. this.historyList = JSON.parse(JSON.stringify(arr))
  207. uni.setStorageSync(type, JSON.stringify(arr))
  208. } else {
  209. arr.unshift(val)
  210. this.historyList = JSON.parse(JSON.stringify(arr))
  211. uni.setStorageSync(type, JSON.stringify(arr))
  212. }
  213. } else {
  214. return
  215. }
  216. },
  217. searchFast(e) {
  218. this.keyword = e
  219. this.search()
  220. },
  221. //扫码
  222. scan() {
  223. uni.scanCode({
  224. success: (res) => {
  225. this.keyword = res.result;
  226. this.search()
  227. }
  228. });
  229. },
  230. /************************ 私库方法 ********************************/
  231. // 搜索商品
  232. searchClick() {
  233. let storeName = this.current == 0 ? 'perStor' : 'pubStor'
  234. this.$tab.navigateTo('/pages/commodity/allGoodsSearch?type=' + this.current + '&storeName=' + storeName +
  235. '&id=' +
  236. this.id)
  237. },
  238. //获取类目列表
  239. getPerCategory() {
  240. return new Promise((resolve, reject) => {
  241. perGoodsCategory().then(res => {
  242. if (res.data && res.data.length > 0) {
  243. this.perTabList = res.data.map(i => {
  244. if (i.categoryCode == null) {
  245. i.categoryName = '未定义'
  246. return i
  247. } else {
  248. return i
  249. }
  250. });
  251. if (this.perTabList && this.perTabList.length > 0) {
  252. this.categoryCode = this.perTabList[0].categoryCode
  253. }
  254. }
  255. resolve(res)
  256. }).catch(err => {
  257. reject(err)
  258. })
  259. })
  260. },
  261. //商品类目切换
  262. perSwitchMenu(item) {
  263. this.categoryCode = item.categoryCode
  264. this.perReset()
  265. this.getPerCommList()
  266. },
  267. //根据类目获取商品列表
  268. getPerCommList() {
  269. let params = {}
  270. if (this.leftShow) { //搜索
  271. params = {
  272. categoryCode: this.categoryCode,
  273. page: {
  274. current: this.perPage,
  275. size: this.perSize
  276. },
  277. deviceId: this.id
  278. }
  279. } else { //非搜索
  280. params = {
  281. page: {
  282. current: this.perPage,
  283. size: this.perSize
  284. },
  285. deviceId: this.id,
  286. keyword: this.keyword
  287. }
  288. }
  289. return new Promise((resolve, reject) => {
  290. pageByGoodsMerc(params).then(res => {
  291. let data = res.data.records;
  292. if (data && data.length > 0) {
  293. data = data.map(i => {
  294. i.name = i.goodsName;
  295. i.barcode = i.goodsBarcode;
  296. i.cover = i.goodsCover;
  297. i.price = i.price != null ? Number(i.price) / 100 : null;
  298. i.categoryName = i.categoryCodeName;
  299. i.noSelect = i.isBind;
  300. return i
  301. })
  302. }
  303. if (data.length < 10) {
  304. this.perStatus = "nomore"
  305. } else {
  306. this.perStatus = "loadmore"
  307. }
  308. this.perCommList = this.perCommList.concat(data)
  309. resolve(res)
  310. }).catch(err => {
  311. reject(err)
  312. })
  313. })
  314. },
  315. //触底加载更多
  316. perLowerBottom() {
  317. if (this.perStatus == 'nomore') return
  318. this.perPage++
  319. this.getPerCommList()
  320. },
  321. //重置
  322. perReset() {
  323. this.perStatus == 'loadmore'
  324. this.perPage = 1;
  325. this.perSize = 10;
  326. this.perCommList = [];
  327. },
  328. //新建模块商品先需要设置价格
  329. comClick(e) {
  330. uni.showModal({
  331. title: '提示',
  332. content: '当前商品为新商品需先设置商品价格,是否前往设置?',
  333. success: res => {
  334. if (res.confirm) {
  335. this.$tab.navigateTo('/pages/commodity/comEdit?id=' + e.id)
  336. } else if (res.cancel) {
  337. console.log('用户点击取消');
  338. }
  339. }
  340. });
  341. },
  342. /********************** 公库方法 *************************/
  343. //获取类目列表
  344. getPubCategory() {
  345. return new Promise((resolve, reject) => {
  346. pubGoodsCategory({
  347. deviceId:this.id
  348. }).then(res => {
  349. if (res.data && res.data.length > 0) {
  350. this.pubTabList = res.data.map(i => {
  351. if (i.categoryCode == null) {
  352. i.categoryName = '未定义'
  353. return i
  354. } else {
  355. return i
  356. }
  357. });
  358. if (this.pubTabList && this.pubTabList.length > 0) {
  359. this.categoryCode = this.pubTabList[0].categoryCode
  360. }
  361. }
  362. resolve(res)
  363. }).catch(err => {
  364. reject(err)
  365. })
  366. })
  367. },
  368. //商品类目切换
  369. pubSwitchMenu(item) {
  370. this.categoryCode = item.categoryCode
  371. this.pubReset()
  372. this.getPubCommList()
  373. },
  374. //根据类目获取商品列表
  375. getPubCommList() {
  376. let params = {}
  377. if (this.leftShow) { //搜索
  378. params = {
  379. categoryCodeLevel1: this.categoryCode,
  380. page: {
  381. current: this.pubPage,
  382. size: this.pubSize
  383. },
  384. deviceId: this.id
  385. }
  386. } else { //非搜索
  387. params = {
  388. page: {
  389. current: this.pubPage,
  390. size: this.pubSize
  391. },
  392. deviceId: this.id,
  393. keyword: this.keyword
  394. }
  395. }
  396. return new Promise((resolve, reject) => {
  397. pageByGoods(params).then(res => {
  398. let data = res.data.records;
  399. let newData = data.map(i => {
  400. i.categoryCode = i.categoryCodeLevel1;
  401. i.noSelect = i.isBind;
  402. return i
  403. })
  404. if (data.length < 10) {
  405. this.pubStatus = "nomore"
  406. } else {
  407. this.pubStatus = "loadmore"
  408. }
  409. this.pubCommList = this.pubCommList.concat(newData)
  410. resolve(res)
  411. }).catch(err => {
  412. reject(err)
  413. })
  414. })
  415. },
  416. //触底加载更多
  417. pubLowerBottom() {
  418. if (this.pubStatus == 'nomore') return
  419. this.pubPage++
  420. this.getPubCommList()
  421. },
  422. //重置
  423. pubReset() {
  424. this.pubStatus == 'loadmore'
  425. this.pubPage = 1;
  426. this.pubSize = 10;
  427. this.pubCommList = [];
  428. },
  429. tabClick(e) {
  430. this.current = e.index
  431. this.search()
  432. },
  433. change(e) {
  434. this.current = e.detail.current
  435. },
  436. /**
  437. * 添加商品
  438. * 私库添加到设备,不需要设置参数
  439. */
  440. addCom() {
  441. let storeName = this.current == 0 ? 'perStor' : 'pubStor';
  442. if (uni.getStorageSync(storeName) && JSON.parse(uni.getStorageSync(
  443. storeName)).length > 0) {
  444. if (this.current == 0) { //私库
  445. let commList = JSON.parse(uni.getStorageSync(storeName))
  446. let goodsIdList = commList.map(i => {
  447. return i.goodsId
  448. })
  449. bindDeviceByMercGoods({
  450. deviceIds: [this.id],
  451. goodsIdList: goodsIdList
  452. }).then(res => {
  453. if (res.code == 200) {
  454. this.$modal.msg('添加成功~')
  455. uni.setStorageSync(storeName, '') //清空购物车
  456. setTimeout(() => {
  457. // this.$tab.redirectTo(`/pages/equipment/comManage?id=${this.id}`)
  458. uni.navigateBack({
  459. delta: 1
  460. })
  461. }, 1000)
  462. }
  463. })
  464. }
  465. if (this.current == 1) { //公库
  466. let url = '';
  467. let type = this.current == 0 ? 1 : 2;
  468. url = `/pages/equipment/addComList?type=${type}&storeName=${storeName}&id=${this.id}`
  469. this.$tab.navigateTo(url)
  470. }
  471. } else {
  472. uni.$u.toast('请先选择商品!')
  473. return
  474. }
  475. },
  476. //私库添加至设备
  477. perToDev() {
  478. uni.showModal({
  479. title: '提示',
  480. content: '是否确认添加商品',
  481. success: res => {
  482. if (res.confirm) {
  483. } else if (res.cancel) {
  484. console.log('用户点击取消');
  485. }
  486. }
  487. });
  488. },
  489. }
  490. }
  491. </script>
  492. <style lang="scss" scoped>
  493. .container {
  494. height: calc(100vh);
  495. /* #ifdef H5 */
  496. height: calc(100vh - var(--window-top));
  497. /* #endif */
  498. display: flex;
  499. flex-direction: column;
  500. .search {
  501. padding: 24rpx 24rpx;
  502. background-color: #fff;
  503. .search-input {
  504. position: relative;
  505. .scan-icon {
  506. position: absolute;
  507. top: 50%;
  508. transform: translateY(-50%);
  509. z-index: 2;
  510. &.scan-left-show {
  511. right: 36rpx;
  512. }
  513. &.scan-left-hidden {
  514. right: 100rpx;
  515. }
  516. }
  517. }
  518. .search-history {
  519. .history-item {
  520. margin-right: 24rpx;
  521. padding: 0 12rpx;
  522. background-color: #f2f2f2;
  523. color: #333;
  524. font-size: 24rpx;
  525. line-height: 40rpx;
  526. border-radius: 40rpx;
  527. margin-top: 24rpx;
  528. }
  529. }
  530. }
  531. .tab-list {
  532. display: flex;
  533. flex-flow: row nowrap;
  534. justify-content: space-around;
  535. .tab-left {
  536. width: 60%;
  537. }
  538. .use-list {
  539. font-size: 30rpx;
  540. color: #606266;
  541. line-height: 88rpx;
  542. padding-right: 24rpx;
  543. }
  544. }
  545. .content {}
  546. }
  547. .swiperitem-content {
  548. // position: relative;
  549. }
  550. .btn {
  551. width: 100%;
  552. position: fixed;
  553. left: 0;
  554. bottom: 24rpx;
  555. padding: 0 24rpx;
  556. }
  557. </style>