productContrapose.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // import { getProductDataApi } from "@/api/product/index";
  2. import { getProductDataApi,getAllProductDataApi } from "@/api/product/index";
  3. import { mapGetters, mapActions } from "vuex";
  4. import { copyObject } from "@/utils/util";
  5. export default {
  6. data() {
  7. return {
  8. pageType: "bill", //选择商品页面类别 用于区分不同功能的数据
  9. productInfo: [], //商品信息
  10. classIndex: 0, //当前商品类别下标
  11. py: "", //拼音搜索条件
  12. type: "", //库存预警时,固定为waring,其他情况不传或者传空
  13. autoLoad: true, //自动获取商品信息
  14. isLimit: true //选择数量不能大于库存限制
  15. };
  16. },
  17. onLoad(options) {
  18. const { pageType } = options;
  19. if (pageType) {
  20. this.pageType = pageType;
  21. }
  22. if (this.autoLoad) {
  23. this.initDataContrapose();
  24. }
  25. },
  26. onUnload() {},
  27. computed: {
  28. ...mapGetters(["getSelectInfo"]),
  29. filterProductInfoContrapose() {
  30. let list = copyObject(this.productInfo);
  31. return list;
  32. },
  33. /**
  34. * 当前页面选中的商品列表
  35. */
  36. selectList() {
  37. return this.getSelectInfo[this.pageType] || [];
  38. },
  39. scrollClassId() {
  40. const curClass = this.productInfo[this.classIndex];
  41. if (curClass) {
  42. return `class_${curClass.classId}`;
  43. }
  44. return "";
  45. },
  46. //选择商品总价
  47. allPrice() {
  48. let price = 0;
  49. if (this.selectList) {
  50. for (const item of this.selectList) {
  51. const itemInfo = this.getSelectItemById(item.id, item.classId);
  52. if (
  53. itemInfo &&
  54. !this.$util.isEmpty(itemInfo.bigPrice) &&
  55. !this.$util.isEmpty(itemInfo.smallPrice)
  56. ) {
  57. price += item.bigCount * Number(itemInfo.bigPrice);
  58. price += item.smallCount * Number(itemInfo.smallPrice);
  59. }
  60. }
  61. }
  62. return Math.round(price * 100) / 100;
  63. },
  64. //选择商品总数
  65. allCount() {
  66. // console.log(444444, this.selectList);
  67. return this.selectList.length;
  68. },
  69. /**
  70. * 当前选中商品列表中是否有没有库存的商品
  71. */
  72. hasNoStock() {
  73. let has = false;
  74. if (this.selectList) {
  75. for (const item of this.selectList) {
  76. const {
  77. bigCount,
  78. smallCount,
  79. bigNum,
  80. bigUnit,
  81. smallNum,
  82. smallUnit,
  83. ratio
  84. } = item;
  85. const ratioNum = Number(ratio);
  86. if (
  87. Number(bigCount) * ratioNum + Number(smallCount) >
  88. Number(bigNum) * ratioNum + Number(smallNum)
  89. ) {
  90. has = true;
  91. break;
  92. }
  93. }
  94. }
  95. return has;
  96. }
  97. },
  98. methods: {
  99. ...mapActions(["setSelectInfoByType", "resetSelectInfoByType"]),
  100. /**
  101. * 请求商品数据
  102. */
  103. async initDataContrapose(extendInfo) {
  104. //extendInfo调用时的扩展参数 如 shopId
  105. try {
  106. let params = {
  107. py: this.py,
  108. type: this.type
  109. };
  110. if (extendInfo) {
  111. params = { ...params, ...extendInfo };
  112. }
  113. // const { data } = await getProductDataApi(params);
  114. const { data } = await getAllProductDataApi(params);
  115. // data.forEach(element => {
  116. // element.child && element.child.forEach(child => {
  117. // child.classId = element.classId;
  118. // child.className = element.className;
  119. // });
  120. // });
  121. this.productInfo = data.list;
  122. this.classIndex = 0;
  123. uni.stopPullDownRefresh();
  124. } catch (error) {
  125. } finally {
  126. uni.hideLoading();
  127. }
  128. },
  129. /**
  130. * 获取当前选中商品总价
  131. */
  132. getSelectItemPrice(item) {
  133. let price = 0;
  134. const { bigCount, smallCount, bigPrice, smallPrice } = item;
  135. let sum =
  136. Number(bigCount) * Number(bigPrice) +
  137. Number(smallCount) * Number(smallPrice);
  138. price = Math.round(sum * 100) / 100;
  139. return price;
  140. },
  141. /**
  142. * 通过id获取当前选中的商品信息
  143. */
  144. getSelectItemById(id) {
  145. let info = {
  146. bigCount: 0, //扎数
  147. smallCount: 0, //支数
  148. autoPrice: 0 //调价价格
  149. };
  150. const item =
  151. this.selectList && this.selectList.find(ele => {
  152. return ele.id == id;
  153. });
  154. if (item) {
  155. info = {
  156. ...item,
  157. bigCount: Number(item.bigCount),
  158. smallCount: Number(item.smallCount),
  159. autoPrice: Number(item.autoPrice)
  160. };
  161. }
  162. return info;
  163. },
  164. /**
  165. * 通过id获取当前选中的商品类别信息 左边菜单角标 当前选中该类别商品多少件
  166. */
  167. getSelectClassById(classId) {
  168. let info = {
  169. bigCount: 0, //扎数
  170. smallCount: 0 //支数
  171. };
  172. const productItem =
  173. this.productInfo &&
  174. this.productInfo.find(ele => {
  175. return ele.classId == classId;
  176. });
  177. if (productItem && productItem.child) {
  178. for (const item of productItem.child) {
  179. for (const selectItem of this.selectList) {
  180. if (item.id == selectItem.id && item.classId == selectItem.classId) {
  181. info.bigCount += Number(selectItem.bigCount);
  182. info.smallCount += Number(selectItem.smallCount);
  183. }
  184. }
  185. }
  186. }
  187. return info;
  188. },
  189. /**
  190. * 添加商品
  191. */
  192. addEvent(item) {
  193. const list = this.selectList;
  194. let has = false;
  195. for (let index = 0; index < list.length; index++) {
  196. const element = list[index];
  197. if (element.id == item.id) {
  198. has = true;
  199. if (this.isLimit) {
  200. if (element.bigCount < item.bigNum) {
  201. list[index] = { ...element, bigCount: ++element.bigCount };
  202. }
  203. // else if (element.smallCount < item.smallNum) {
  204. // list[index] = { ...element, smallCount: ++element.smallCount };
  205. // }
  206. } else {
  207. list[index] = { ...element, bigCount: ++element.bigCount };
  208. }
  209. if (this.isLimit) {
  210. const { bigCount, smallCount } = list[index];
  211. //超过库存时 设置为库存最大值
  212. const ratio = Number(item.ratio);
  213. if (
  214. Number(bigCount) * ratio + Number(smallCount) >
  215. Number(item.bigNum) * ratio + Number(item.smallNum)
  216. ) {
  217. list[index].bigCount = Number(item.bigNum);
  218. list[index].smallCount = Number(item.smallNum);
  219. this.$msg("库存不足");
  220. }
  221. }
  222. break;
  223. }
  224. }
  225. if (!has) {
  226. let info = {};
  227. info =
  228. this.productInfo && this.productInfo.find(element => {
  229. return element.id == item.id;
  230. });
  231. let params = {
  232. ...info,
  233. bigCount: 1, //扎数
  234. smallCount: 0, //支数
  235. autoPrice: 0 //调价价格
  236. };
  237. if (info.bigNum && info.bigNum > 0) {
  238. list.push(params);
  239. } else {
  240. this.$msg("库存不足");
  241. }
  242. }
  243. this.setSelectInfoByType({ type: this.pageType, info: list });
  244. },
  245. /**
  246. * 减去商品
  247. */
  248. delEvent(item) {
  249. const list = this.selectList;
  250. let has = false;
  251. for (let index = 0; index < list.length; index++) {
  252. const element = list[index];
  253. if (element.id == item.id && element.classId == item.classId) {
  254. has = true;
  255. if (element.bigCount > 0) {
  256. list[index] = { ...element, bigCount: --element.bigCount };
  257. } else if (element.smallCount > 0) {
  258. list[index] = { ...element, smallCount: 0 };
  259. }
  260. if (this.isLimit) {
  261. const { bigCount, smallCount } = list[index];
  262. //超过库存时 设置为库存最大值
  263. const ratio = Number(item.ratio);
  264. if (
  265. Number(bigCount) * ratio + Number(smallCount) >
  266. Number(item.bigNum) * ratio + Number(item.smallNum)
  267. ) {
  268. list[index].bigCount = Number(item.bigNum);
  269. list[index].smallCount = Number(item.smallNum);
  270. }
  271. }
  272. if (element.bigCount == 0 && element.smallCount == 0) {
  273. list.splice(index, 1);
  274. }
  275. break;
  276. }
  277. }
  278. if (!has) {
  279. let info = { classId: item.classId, id: item.id };
  280. const classInfo =
  281. this.productInfo &&
  282. this.productInfo.find(element => {
  283. return element.classId == item.classId && element.child;
  284. });
  285. if (classInfo) {
  286. info = classInfo.child.find(ele => {
  287. return ele.id == item.id;
  288. });
  289. }
  290. let params = {
  291. ...info,
  292. bigCount: 1, //扎数
  293. smallCount: 0, //支数
  294. autoPrice: 0 //调价价格
  295. };
  296. list.push(params);
  297. }
  298. this.setSelectInfoByType({ type: this.pageType, info: list });
  299. // this.$forceUpdate();
  300. },
  301. // /**
  302. // * 删除商品
  303. // */
  304. // removeEvent(item) {
  305. // const list = this.selectList;
  306. // for (let index = list.length - 1; index >= 0; index--) {
  307. // const element = list[index];
  308. // if (element.id == item.id && element.classId == item.classId) {
  309. // list.splice(index, 1);
  310. // break;
  311. // }
  312. // }
  313. // this.setSelectInfoByType({ type: this.pageType, info: list });
  314. // // this.$forceUpdate();
  315. // },
  316. /**
  317. * 更新商品
  318. */
  319. updateItemEvent(item) {
  320. const list = this.selectList;
  321. for (let index = 0; index < list.length; index++) {
  322. const element = list[index];
  323. if (element.id == item.id && element.classId == item.classId) {
  324. const { bigCount, smallCount } = list[index];
  325. //超过库存时 设置为库存最大值
  326. const ratio = Number(item.ratio);
  327. if (this.isLimit) {
  328. if (
  329. Number(bigCount) * ratio + Number(smallCount) >
  330. Number(item.bigNum) * ratio + Number(item.smallNum)
  331. ) {
  332. list[index].bigCount = Number(item.bigNum);
  333. list[index].smallCount = Number(item.smallNum);
  334. }
  335. }
  336. list[index] = {
  337. ...element,
  338. bigCount: Number(item.bigCount),
  339. smallCount: Number(item.smallCount)
  340. };
  341. break;
  342. }
  343. }
  344. this.setSelectInfoByType({ type: this.pageType, info: list });
  345. // this.$forceUpdate();
  346. },
  347. /**
  348. * 切换左侧商品类别
  349. */
  350. swichClass(e) {
  351. let cur = e.currentTarget.dataset.current;
  352. if (this.classIndex == cur) {
  353. return false;
  354. } else {
  355. this.classIndex = cur;
  356. }
  357. },
  358. /**
  359. * 搜索对应花材
  360. */
  361. searchFn() {}
  362. }
  363. };