ghsProduct.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. import { getGoodsDataApi,getProductDataApi,getPxClassDataApi } from "@/api/product/index";
  2. import { mapGetters, mapActions } from "vuex";
  3. import { copyObject } from "@/utils/util";
  4. export default {
  5. data() {
  6. return {
  7. pageType: "bill", //选择商品页面类别 用于区分不同功能的数据
  8. suitInfo: [], //成品-商品信息
  9. suitClassIndex: 0, //成品-当前商品类别下标
  10. productInfo: [], //商品信息
  11. pxClassInfo: [], //培训班开单列表
  12. classIndex: 0, //当前商品类别下标
  13. py: "", //拼音搜索条件
  14. type: "", //库存预警时,固定为waring,其他情况不传或者传空
  15. autoLoad: true, //自动获取商品信息
  16. isLimit: true //选择数量不能大于库存限制
  17. };
  18. },
  19. onLoad(options) {
  20. const { pageType } = options;
  21. if (pageType) {
  22. this.pageType = pageType;
  23. }
  24. if (this.autoLoad) {
  25. this.initGoodsData();//成品
  26. this.initData();//组合
  27. this.initPxClassData();//课程
  28. }
  29. },
  30. onUnload() {},
  31. computed: {
  32. ...mapGetters(["getSelectInfo"]),
  33. filterProductInfo() {
  34. let list = copyObject(this.productInfo);
  35. if (this.py) {
  36. let commonList = list.find(ele => {
  37. //在常用中过滤
  38. return ele.classId == -2;
  39. });
  40. if (commonList) {
  41. let commonChildList = [];
  42. if (!this.$util.isEmpty(commonList.child)) {
  43. commonList.child.forEach(ele => {
  44. let hasName = ele.itemName && ele.itemName.indexOf(this.py) >= 0;
  45. let elePy = (ele.py && ele.py.toLocaleUpperCase()) || "";
  46. let hasPy = elePy.indexOf(this.py.toLocaleUpperCase()) >= 0;
  47. if (hasName || hasPy) {
  48. commonChildList.push(ele);
  49. }
  50. });
  51. }
  52. commonList.child = commonChildList;
  53. list = [commonList];
  54. }
  55. }
  56. return list;
  57. },
  58. /**
  59. * 搜索、初始化数据
  60. * */
  61. suitFilterProductInfo() {
  62. let list = copyObject(this.suitInfo);
  63. if (this.py) {
  64. let commonList = list.find(ele => {
  65. //在常用中过滤
  66. return ele.classId == -2;
  67. });
  68. if (commonList) {
  69. let commonChildList = [];
  70. if (!this.$util.isEmpty(commonList.child)) {
  71. commonList.child.forEach(ele => {
  72. let hasName = ele.itemName && ele.itemName.indexOf(this.py) >= 0;
  73. let elePy = (ele.py && ele.py.toLocaleUpperCase()) || "";
  74. let hasPy = elePy.indexOf(this.py.toLocaleUpperCase()) >= 0;
  75. if (hasName || hasPy) {
  76. commonChildList.push(ele);
  77. }
  78. });
  79. }
  80. commonList.child = commonChildList;
  81. list = [commonList];
  82. }
  83. }
  84. return list;
  85. },
  86. /**
  87. * 当前页面选中的商品列表
  88. */
  89. selectList() {
  90. return this.getSelectInfo[this.pageType] || [];
  91. },
  92. scrollClassId() {
  93. const curClass = this.productInfo[this.classIndex];
  94. if (curClass) {
  95. return `class_${curClass.classId}`;
  96. }
  97. return "";
  98. },
  99. //选择商品总价
  100. allPrice() {
  101. let price = 0;
  102. if (this.selectList) {
  103. for (const item of this.selectList) {
  104. const itemInfo = this.getSelectItemById(item.id, item.classId);
  105. if (
  106. itemInfo &&
  107. !this.$util.isEmpty(itemInfo.bigPrice) &&
  108. !this.$util.isEmpty(itemInfo.smallPrice)
  109. ) {
  110. price += item.bigCount * Number(itemInfo.bigPrice);
  111. price += item.smallCount * Number(itemInfo.smallPrice);
  112. }
  113. }
  114. }
  115. return Math.round(price * 100) / 100;
  116. },
  117. //选择商品总数
  118. allCount() {
  119. // console.log(444444, this.selectList);
  120. return this.selectList.length;
  121. },
  122. /**
  123. * 描点-定位分类
  124. * */
  125. suitScrollClassId() {
  126. const curClass = this.suitInfo[this.suitClassIndex];
  127. if (curClass) {
  128. return `class_${curClass.classId}`;
  129. }
  130. return "";
  131. },
  132. /**
  133. * 多风格-选择商品总价
  134. * */
  135. allGoodsPrice() {
  136. let price = 0;
  137. if (this.selectList.goodsStyleList) {
  138. for (const item of this.selectList) {
  139. if(item.goodsStyle==0){
  140. price += item.bigCount * Number(item.price);
  141. }else{
  142. price += item.bigCount * Number(item.goodsStyleList[item.goodsStyleSelectIndex].price);
  143. }
  144. }
  145. }
  146. return Math.round(price * 100) / 100;
  147. },
  148. /**
  149. * 多风格-选择商品总数
  150. * */
  151. allGoodsCount() {
  152. let length = 0;
  153. this.selectList.forEach(element => {
  154. length += Number(element.bigCount)
  155. })
  156. return length;
  157. },
  158. /**
  159. * 当前选中商品列表中是否有没有库存的商品
  160. */
  161. hasNoStock() {
  162. let has = false;
  163. if (this.selectList) {
  164. for (const item of this.selectList) {
  165. const {
  166. bigCount,
  167. smallCount,
  168. bigNum,
  169. bigUnit,
  170. smallNum,
  171. smallUnit,
  172. ratio
  173. } = item;
  174. const ratioNum = Number(ratio);
  175. if (
  176. Number(bigCount) * ratioNum + Number(smallCount) >
  177. Number(bigNum) * ratioNum + Number(smallNum)
  178. ) {
  179. has = true;
  180. break;
  181. }
  182. }
  183. }
  184. return has;
  185. }
  186. },
  187. methods: {
  188. ...mapActions(["setSelectInfoByType", "resetSelectInfoByType"]),
  189. /**
  190. * 请求商品数据
  191. */
  192. async initData(extendInfo) {
  193. //extendInfo调用时的扩展参数 如 shopId
  194. try {
  195. let params = {
  196. py: this.py,
  197. type: this.type
  198. };
  199. if (extendInfo) {
  200. params = { ...params, ...extendInfo };
  201. }
  202. const { data } = await getProductDataApi(params);
  203. data.forEach(element => {
  204. element.child &&
  205. element.child.forEach(child => {
  206. child.classId = element.classId;
  207. child.className = element.className;
  208. });
  209. });
  210. this.productInfo = data;
  211. this.classIndex = 0;
  212. } catch (error) {
  213. } finally {
  214. uni.hideLoading();
  215. }
  216. },
  217. /**
  218. * 获取当前选中商品总价
  219. */
  220. getSelectItemPrice(item) {
  221. let price = 0;
  222. const { bigCount, smallCount, bigPrice, smallPrice } = item;
  223. let sum =
  224. Number(bigCount) * Number(bigPrice) +
  225. Number(smallCount) * Number(smallPrice);
  226. price = Math.round(sum * 100) / 100;
  227. return price;
  228. },
  229. /**
  230. * 成品-请求商品数据
  231. */
  232. async initGoodsData(extendInfo) {
  233. //extendInfo调用时的扩展参数 如 shopId
  234. try {
  235. let params = {
  236. py: this.py,
  237. type: this.type
  238. };
  239. if (extendInfo) {
  240. params = { ...params, ...extendInfo };
  241. }
  242. const { data } = await getGoodsDataApi(params);
  243. data.forEach(element => {
  244. element.child &&
  245. element.child.forEach(child => {
  246. child.classId = element.classId;
  247. child.className = element.className;
  248. });
  249. });
  250. this.suitInfo = data;
  251. this.suitClassIndex = 0;
  252. } catch (error) {
  253. } finally {
  254. uni.hideLoading();
  255. }
  256. },
  257. /**
  258. * 获取当前选中商品总价
  259. */
  260. getSelectGoodsPrice(item) {
  261. let price = 0;
  262. if(item.goodsStyle==0){
  263. price = item.bigCount * Number(item.price);
  264. }else{
  265. price = item.bigCount * Number(item.goodsStyleList[item.goodsStyleSelectIndex].price);
  266. }
  267. // let sum = 0;
  268. // if()
  269. // Number(bigCount) * Number(bigPrice) +
  270. // Number(smallCount) * Number(smallPrice);
  271. // price = Math.round(sum * 100) / 100;
  272. return price;
  273. },
  274. /**
  275. * 通过id获取当前选中的商品信息
  276. */
  277. getSelectItemById(id, classId) {
  278. let info = {
  279. bigCount: 0, //扎数
  280. smallCount: 0, //支数
  281. autoPrice: 0, //调价价格
  282. itemPrice: 0 //调价价格
  283. };
  284. const item =
  285. this.selectList &&
  286. this.selectList.find(ele => {
  287. return ele.id == id && ele.classId == classId;
  288. });
  289. if (item) {
  290. info = {
  291. ...item,
  292. bigCount: Number(item.bigCount),
  293. smallCount: Number(item.smallCount),
  294. autoPrice: Number(item.autoPrice),
  295. itemPrice: Number(item.itemPrice),
  296. };
  297. }
  298. return info;
  299. },
  300. /**
  301. * 通过id获取当前选中的商品类别信息 左边菜单角标 当前选中该类别商品多少件
  302. */
  303. getSelectClassById(classId) {
  304. let info = {
  305. bigCount: 0, //扎数
  306. smallCount: 0 //支数
  307. };
  308. const productItem =
  309. this.productInfo &&
  310. this.productInfo.find(ele => {
  311. return ele.classId == classId;
  312. });
  313. if (productItem && productItem.child) {
  314. for (const item of productItem.child) {
  315. for (const selectItem of this.selectList) {
  316. if (item.id == selectItem.id && item.classId == selectItem.classId) {
  317. info.bigCount += Number(selectItem.bigCount);
  318. info.smallCount += Number(selectItem.smallCount);
  319. info.itemPrice += selectItem.itemPrice?Number(selectItem.itemPrice):'';
  320. }
  321. }
  322. }
  323. }
  324. return info;
  325. },
  326. /**
  327. * 添加商品
  328. */
  329. addEvent(item) {
  330. const list = this.selectList;
  331. let has = false;
  332. for (let index = 0; index < list.length; index++) {
  333. const element = list[index];
  334. if (element.id == item.id && element.classId == item.classId) {
  335. has = true;
  336. if (this.isLimit) {
  337. if (element.bigCount < item.bigNum) {
  338. list[index] = { ...element, bigCount: ++element.bigCount };
  339. }
  340. // else if (element.smallCount < item.smallNum) {
  341. // list[index] = { ...element, smallCount: ++element.smallCount };
  342. // }
  343. } else {
  344. list[index] = { ...element, bigCount: ++element.bigCount };
  345. }
  346. if (this.isLimit) {
  347. const { bigCount, smallCount } = list[index];
  348. //超过库存时 设置为库存最大值
  349. const ratio = Number(item.ratio);
  350. if (
  351. Number(bigCount) * ratio + Number(smallCount) >
  352. Number(item.bigNum) * ratio + Number(item.smallNum)
  353. ) {
  354. list[index].bigCount = Number(item.bigNum);
  355. list[index].smallCount = Number(item.smallNum);
  356. this.$msg("库存不足");
  357. }
  358. }
  359. break;
  360. }
  361. }
  362. if (!has) {
  363. let info = { classId: item.classId, id: item.id };
  364. const classInfo =
  365. this.productInfo &&
  366. this.productInfo.find(element => {
  367. return element.classId == item.classId && element.child;
  368. });
  369. if (classInfo) {
  370. info = classInfo.child.find(ele => {
  371. return ele.id == item.id;
  372. });
  373. }
  374. let params = {
  375. ...info,
  376. bigCount: 1, //扎数
  377. smallCount: 0, //支数
  378. autoPrice: 0 //调价价格
  379. };
  380. if (info.bigNum && info.bigNum > 0) {
  381. list.push(params);
  382. } else {
  383. this.$msg("库存不足");
  384. }
  385. }
  386. this.setSelectInfoByType({ type: this.pageType, info: list });
  387. // this.$forceUpdate();
  388. },
  389. addCustomNumEvent(item,params1,type) {
  390. const list = this.selectList;
  391. // if (!has) {}
  392. let info = { classId: item.classId, id: item.id };
  393. const classInfo =
  394. this.productInfo &&
  395. this.productInfo.find(element => {
  396. return element.classId == item.classId && element.child;
  397. });
  398. if (classInfo) {
  399. info = classInfo.child.find(ele => {
  400. return ele.id == item.id;
  401. });
  402. }
  403. let params = {
  404. ...info,
  405. bigCount: 0, //扎数
  406. smallCount: 0, //支数
  407. autoPrice: 0 //调价价格
  408. };
  409. list.push(params);
  410. if(type!='noAdd'){
  411. this.setSelectInfoByType({ type: this.pageType, info: list });
  412. }
  413. this.$nextTick(()=>{
  414. this.customNum(params1)
  415. })
  416. // this.$forceUpdate();
  417. },
  418. /**
  419. * 减去商品
  420. */
  421. delEvent(item) {
  422. const list = this.selectList;
  423. let has = false;
  424. for (let index = 0; index < list.length; index++) {
  425. const element = list[index];
  426. if (element.id == item.id && element.classId == item.classId) {
  427. has = true;
  428. if (element.bigCount > 0) {
  429. list[index] = { ...element, bigCount: --element.bigCount };
  430. } else if (element.smallCount > 0) {
  431. list[index] = { ...element, smallCount: 0 };
  432. }
  433. if (this.isLimit) {
  434. const { bigCount, smallCount } = list[index];
  435. //超过库存时 设置为库存最大值
  436. const ratio = Number(item.ratio);
  437. if (
  438. Number(bigCount) * ratio + Number(smallCount) >
  439. Number(item.bigNum) * ratio + Number(item.smallNum)
  440. ) {
  441. list[index].bigCount = Number(item.bigNum);
  442. list[index].smallCount = Number(item.smallNum);
  443. }
  444. }
  445. if (element.bigCount == 0 && element.smallCount == 0) {
  446. list.splice(index, 1);
  447. }
  448. break;
  449. }
  450. }
  451. if (!has) {
  452. let info = { classId: item.classId, id: item.id };
  453. const classInfo =
  454. this.productInfo &&
  455. this.productInfo.find(element => {
  456. return element.classId == item.classId && element.child;
  457. });
  458. if (classInfo) {
  459. info = classInfo.child.find(ele => {
  460. return ele.id == item.id;
  461. });
  462. }
  463. let params = {
  464. ...info,
  465. bigCount: 1, //扎数
  466. smallCount: 0, //支数
  467. autoPrice: 0 //调价价格
  468. };
  469. list.push(params);
  470. }
  471. this.setSelectInfoByType({ type: this.pageType, info: list });
  472. // this.$forceUpdate();
  473. },
  474. //减去所有商品
  475. delAllEvent(item) {
  476. const list = this.selectList;
  477. for (let index = 0; index < list.length; index++) {
  478. const element = list[index];
  479. if (element.id == item.id ) {
  480. list.splice(index, 1);
  481. break;
  482. }
  483. }
  484. this.setSelectInfoByType({ type: this.pageType, info: list });
  485. },
  486. /**
  487. * 通过id获取当前选中的商品类别信息 左边菜单角标 当前选中该类别商品多少件
  488. */
  489. getSuitSelectClassById(classId) {
  490. let info = {
  491. bigCount: 0, //扎数
  492. smallCount: 0 //支数
  493. };
  494. const productItem =
  495. this.suitInfo &&
  496. this.suitInfo.find(ele => {
  497. return ele.classId == classId;
  498. });
  499. if (productItem && productItem.child) {
  500. for (const item of productItem.child) {
  501. for (const selectItem of this.selectList) {
  502. if (item.id == selectItem.id && item.classId == selectItem.classId) {
  503. info.bigCount += Number(selectItem.bigCount);
  504. info.smallCount += Number(selectItem.smallCount);
  505. info.itemPrice += selectItem.itemPrice?Number(selectItem.itemPrice):'';
  506. }
  507. }
  508. }
  509. }
  510. return info;
  511. },
  512. /**
  513. * 多风格-添加商品
  514. */
  515. addGoodsEvent(item) {
  516. const list = this.selectList;
  517. //过滤空数据
  518. if(item.bigCount==0||!item.bigCount){return}
  519. let has = false;
  520. for (let index = 0; index < list.length; index++) {
  521. const element = list[index];
  522. if (element.id == item.id && element.classId == item.classId) {
  523. has = true;
  524. list[index] = { ...element, bigCount: item.bigCount,goodsStyleSelectIndex: item.goodsStyleSelectIndex };
  525. break;
  526. }
  527. }
  528. if (!has) {
  529. let info = { classId: item.classId, id: item.id };
  530. const classInfo =
  531. this.suitInfo &&
  532. this.suitInfo.find(element => {
  533. return element.classId == item.classId && element.child;
  534. });
  535. if (classInfo) {
  536. info = classInfo.child.find(ele => {
  537. return ele.id == item.id;
  538. });
  539. }
  540. let params = {
  541. ...info,
  542. bigCount: item.bigCount, //扎数
  543. goodsStyleSelectIndex: item.goodsStyleSelectIndex, //选中的风格
  544. };
  545. list.push(params);
  546. }
  547. this.setSelectInfoByType({ type: this.pageType, info: list });
  548. // this.$forceUpdate();
  549. },
  550. /**
  551. * 多风格-添加商品
  552. */
  553. addGoods(item) {
  554. const list = this.selectList;
  555. let has = false;
  556. for (let index = 0; index < list.length; index++) {
  557. const element = list[index];
  558. if (element.id == item.id && element.classId == item.classId) {
  559. has = true;
  560. list[index] = { ...element, bigCount: ++element.bigCount };
  561. break;
  562. }
  563. }
  564. if (!has) {
  565. let info = { classId: item.classId, id: item.id };
  566. const classInfo =
  567. this.suitInfo &&
  568. this.suitInfo.find(element => {
  569. return element.classId == item.classId && element.child;
  570. });
  571. if (classInfo) {
  572. info = classInfo.child.find(ele => {
  573. return ele.id == item.id;
  574. });
  575. }
  576. let params = {
  577. ...info,
  578. bigCount: 1, //扎数
  579. smallCount: 0, //支数
  580. autoPrice: 0 //调价价格
  581. };
  582. list.push(params);
  583. }
  584. this.setSelectInfoByType({ type: this.pageType, info: list });
  585. // this.$forceUpdate();
  586. },
  587. /**
  588. * 多风格-减去商品
  589. */
  590. delGoodsEvent(item) {
  591. const list = this.selectList;
  592. for (let index = 0; index < list.length; index++) {
  593. const element = list[index];
  594. if (element.id == item.id && element.classId == item.classId) {
  595. if (element.bigCount > 0) {
  596. list[index] = { ...element, bigCount: --element.bigCount };
  597. }
  598. if (element.bigCount == 0 || !element.bigCount) {
  599. list.splice(index, 1);
  600. }
  601. break;
  602. }
  603. }
  604. this.setSelectInfoByType({ type: this.pageType, info: list });
  605. },
  606. /**
  607. * 更新商品
  608. */
  609. updateItemEvent(item) {
  610. const list = this.selectList;
  611. for (let index = 0; index < list.length; index++) {
  612. const element = list[index];
  613. if (element.id == item.id && element.classId == item.classId) {
  614. const { bigCount, smallCount } = list[index];
  615. //超过库存时 设置为库存最大值
  616. const ratio = Number(item.ratio);
  617. if (this.isLimit) {
  618. if (
  619. Number(bigCount) * ratio + Number(smallCount) >
  620. Number(item.bigNum) * ratio + Number(item.smallNum)
  621. ) {
  622. list[index].bigCount = Number(item.bigNum);
  623. list[index].smallCount = Number(item.smallNum);
  624. list[index].itemPrice = item.itemPrice?Number(item.itemPrice):'';
  625. }
  626. }
  627. list[index] = {
  628. ...element,
  629. bigCount: Number(item.bigCount),
  630. smallCount: Number(item.smallCount),
  631. itemPrice: item.itemPrice?Number(item.itemPrice):''
  632. };
  633. break;
  634. }
  635. }
  636. this.setSelectInfoByType({ type: this.pageType, info: list });
  637. // this.$forceUpdate();
  638. },
  639. /**
  640. * 切换左侧商品类别
  641. */
  642. swichClass(e) {
  643. let cur = e.currentTarget.dataset.current;
  644. if (this.classIndex == cur) {
  645. return false;
  646. } else {
  647. this.classIndex = cur;
  648. }
  649. },
  650. /**
  651. * 多风格-切换左侧商品类别
  652. */
  653. suitSwichClass(e) {
  654. let cur = e.currentTarget.dataset.current;
  655. if (this.suitClassIndex == cur) {
  656. return false;
  657. } else {
  658. this.suitClassIndex = cur;
  659. }
  660. },
  661. /**
  662. * 搜索对应花材
  663. */
  664. searchFn() {},
  665. /**
  666. * 请求课程数据
  667. */
  668. async initPxClassData() {
  669. try {
  670. const { data } = await getPxClassDataApi({pageSize:50});
  671. this.pxClassInfo = data.list;
  672. } catch (error) {
  673. } finally {
  674. uni.hideLoading();
  675. }
  676. },
  677. }
  678. };