index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. const _Http = getApp().globalData.http,
  2. currency = require("../../../utils/currency"),
  3. CNY = sum => currency(sum, {
  4. symbol: "¥",
  5. precision: 2
  6. }).format();
  7. let downCount = {};
  8. import {
  9. getCustomText
  10. } from "../../../utils/customItemType";
  11. Component({
  12. options: {
  13. addGlobalClass: true,
  14. },
  15. properties: {},
  16. data: {
  17. list: [],
  18. results: [], //选中结果
  19. sa_brandid: null, //当前选中品牌id
  20. classList: [], //生成订单时所选
  21. sum: 0, //价格合
  22. allBrandList: [],
  23. hidePrice: wx.getStorageSync('hidePrice'),
  24. },
  25. methods: {
  26. init() {
  27. this.getList()
  28. getApp().globalData.customizedProduct = (item, custom) => {
  29. return new Promise((resolve) => {
  30. wx.showLoading({
  31. title: '修改中...',
  32. })
  33. _Http.basic({
  34. "id": 20220924104302,
  35. "content": {
  36. "sa_shoppingcartid": item.sa_shoppingcartid,
  37. "qty": item.qty,
  38. ...custom
  39. },
  40. }).then(res => {
  41. console.log("修改定制", res)
  42. wx.showToast({
  43. title: res.msg != '成功' ? res.msg : '修改成功',
  44. icon: "none"
  45. });
  46. this.getList()
  47. resolve(true)
  48. })
  49. })
  50. }
  51. return true;
  52. },
  53. /* 获取列表 */
  54. getList() {
  55. _Http.basic({
  56. "id": 20220924095302,
  57. "content": {
  58. nocache: true,
  59. istool: 0,
  60. "pageNumber": 1,
  61. "pageSize": getApp().globalData.collectCount + 5,
  62. "where": {
  63. "condition": ""
  64. }
  65. }
  66. }).then(res => {
  67. console.log('购物车列表', res)
  68. this.selectComponent('#ListBox').RefreshToComplete();
  69. this.selectComponent("#ListBox").setHeight(".head", this);
  70. if (res.msg != '成功') return wx.showToast({
  71. title: res.msg,
  72. icon: "none"
  73. })
  74. let list = [],
  75. allBrandList = [];
  76. list = res.data.map(v => {
  77. v.showPrice = CNY(v.gradeprice)
  78. let obj = allBrandList.find(s => s.sa_brandid == v.sa_brandid);
  79. if (obj) {
  80. obj.results.push(v.sa_shoppingcartid)
  81. } else {
  82. allBrandList.push({
  83. brandname: v.brandname,
  84. sa_brandid: v.sa_brandid,
  85. results: [v.sa_shoppingcartid],
  86. })
  87. }
  88. if (v.iscustomsize) v.customText = getCustomText(v);
  89. return v
  90. });
  91. this.setData({
  92. list,
  93. allBrandList,
  94. isGet: true
  95. });
  96. if (wx.getStorageSync('shopping')) {
  97. this.setData({
  98. ...wx.getStorageSync('shopping')
  99. });
  100. this.computeSum();
  101. }
  102. })
  103. },
  104. clickBut(e) {
  105. this.data.classList.length >= 2 ? wx.showToast({
  106. title: '请选择订单领域(订单只允许同品牌/同领域的商品)',
  107. icon: "none",
  108. duration: 3000
  109. }) : this.handleSubmit(0);
  110. },
  111. /* 提交 */
  112. submit(e) {
  113. this.handleSubmit(e.detail.value)
  114. },
  115. handleSubmit(index) {
  116. let data = this.data.classList[index];
  117. _Http.basic({
  118. "id": 20221128183202,
  119. "content": {
  120. istool: 0,
  121. type: "标准订单",
  122. "tradefield": data.type, //必选
  123. "items": data.list.map(v => {
  124. return {
  125. "sa_orderitemsid": 0,
  126. "itemid": v.itemid,
  127. "sa_brandid": v.sa_brandid,
  128. "qty": v.qty,
  129. width: v.width || 0,
  130. length: v.length || 0,
  131. sa_shoppingcartid: v.sa_shoppingcartid || 0
  132. }
  133. })
  134. }
  135. }).then(res => {
  136. console.log("转化订单", res)
  137. if (res.msg != '成功') return wx.showToast({
  138. title: res.msg,
  139. icon: "none"
  140. });
  141. getApp().globalData.getCollectCount()
  142. wx.showModal({
  143. title: '提示',
  144. content: '生成成功!是否立即前往',
  145. complete: (s) => {
  146. if (s.confirm) {
  147. wx.navigateTo({
  148. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  149. })
  150. }
  151. }
  152. });
  153. this.getList();
  154. })
  155. },
  156. /* 是否选择全部 */
  157. setIsAll() {
  158. let isAll = this.data.isAll;
  159. //取消全选
  160. if (isAll) {
  161. this.setData({
  162. sa_brandid: null,
  163. results: []
  164. })
  165. } else {
  166. //已选品牌产品情况下
  167. if (this.data.sa_brandid) {
  168. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  169. this.setData({
  170. results: obj.results
  171. })
  172. } else {
  173. if (this.data.allBrandList.length == 0) return;
  174. this.setData({
  175. sa_brandid: this.data.allBrandList[0].sa_brandid,
  176. results: this.data.allBrandList[0].results
  177. })
  178. }
  179. };
  180. this.computeSum();
  181. },
  182. /* 切换选中项 */
  183. changeResults(e, my = false) {
  184. const {
  185. item
  186. } = my ? e : e.currentTarget.dataset;
  187. let results = this.data.results,
  188. sa_brandid = this.data.sa_brandid;
  189. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  190. if (results.length == 0) {
  191. results.push(item.sa_shoppingcartid);
  192. sa_brandid = item.sa_brandid;
  193. } else {
  194. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  195. if (index == -1) {
  196. results.push(item.sa_shoppingcartid);
  197. } else {
  198. results.splice(index, 1);
  199. if (results.length == 0) sa_brandid = null;
  200. }
  201. };
  202. this.setData({
  203. results,
  204. sa_brandid
  205. })
  206. this.computeSum();
  207. },
  208. customization(e) {
  209. const {
  210. item
  211. } = e.target.dataset;
  212. if (item) this.selectComponent("#Custom").onClick(item)
  213. },
  214. /* 计算总价/产品领域分类 */
  215. computeSum() {
  216. let results = this.data.results,
  217. sum = 0,
  218. classList = [];
  219. if (results.length) results = results.filter(v => {
  220. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  221. if (item) {
  222. sum = currency(sum).add(currency(item.qty).multiply(item.gradeprice)).value;
  223. /* 领域分类 */
  224. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  225. if (index == -1) {
  226. classList.push({
  227. type: item.tradefield_shoppingcart,
  228. list: [item],
  229. name: item.tradefield_shoppingcart + "(1件商品)"
  230. })
  231. } else {
  232. classList[index].list.push(item)
  233. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  234. }
  235. };
  236. return item
  237. });
  238. let sa_brandid = results.length ? this.data.sa_brandid : null;
  239. wx.setStorageSync('shopping', {
  240. results,
  241. sa_brandid
  242. })
  243. let isAll = false;
  244. if (sa_brandid) {
  245. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  246. isAll = brand.results.length == results.length
  247. }
  248. console.log(results)
  249. this.setData({
  250. sum: CNY(sum),
  251. isAll,
  252. results,
  253. sa_brandid,
  254. classList
  255. });
  256. },
  257. /* 删除产品 */
  258. deteleItem(e) {
  259. const {
  260. item
  261. } = e.currentTarget.dataset;
  262. wx.showModal({
  263. title: '提示',
  264. content: `是否确认删除${item.itemname}?`,
  265. complete: ({
  266. confirm
  267. }) => {
  268. e.detail.instance.close();
  269. if (confirm) _Http.basic({
  270. "id": 20220924095202,
  271. "content": {
  272. "sa_shoppingcartids": [item.sa_shoppingcartid]
  273. }
  274. }).then(res => {
  275. wx.showToast({
  276. title: res.msg != '成功' ? res.msg : "删除成功",
  277. icon: "none"
  278. });
  279. if (res.msg != '成功') return;
  280. this.getList(true)
  281. getApp().globalData.getCollectCount()
  282. })
  283. }
  284. })
  285. },
  286. /* 输入框失去焦点调整数量 */
  287. inputBlur(e) {
  288. const {
  289. index
  290. } = e.currentTarget.dataset;
  291. let item = this.data.list[index];
  292. let qty = 0;
  293. if (item.orderminqty > e.detail.value) {
  294. wx.showToast({
  295. title: '输入数量低于最低起订量!',
  296. icon: "none"
  297. })
  298. qty = item.orderminqty;
  299. } else if (item.orderminqty < e.detail.value) {
  300. var currencyRounding = value => currency(value, {
  301. increment: item.orderaddqty
  302. });
  303. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  304. } else {
  305. qty = e.detail.value;
  306. }
  307. this.setData({
  308. [`list[${index}].qty`]: 0
  309. });
  310. this.setData({
  311. [`list[${index}].qty`]: qty
  312. });
  313. this.computeSum();
  314. clearTimeout(downCount['count' + index])
  315. downCount['count' + index] = setTimeout(() => {
  316. _Http.basic({
  317. "id": 20220924104302,
  318. "content": {
  319. "sa_shoppingcartid": item.sa_shoppingcartid,
  320. "qty": item.qty,
  321. "width": item.width || 0,
  322. "length": item.length || 0
  323. },
  324. }, false).then(res => {
  325. console.log("修改数量", res)
  326. })
  327. }, 2000)
  328. },
  329. /* 步进器调整数量 */
  330. stepperChange(e) {
  331. const {
  332. index
  333. } = e.currentTarget.dataset;
  334. let item = this.data.list[index];
  335. if (e.type == 'plus') {
  336. item.qty += item.orderaddqty
  337. } else {
  338. item.qty -= item.orderaddqty
  339. }
  340. this.setData({
  341. [`list[${index}]`]: item
  342. })
  343. this.computeSum();
  344. clearTimeout(downCount['count' + index])
  345. downCount['count' + index] = setTimeout(() => {
  346. _Http.basic({
  347. "id": 20220924104302,
  348. "content": {
  349. "sa_shoppingcartid": item.sa_shoppingcartid,
  350. "qty": item.qty,
  351. "width": item.width || 0,
  352. "length": item.length || 0
  353. },
  354. }, false).then(res => {
  355. console.log("修改数量", res)
  356. })
  357. }, 2000)
  358. }
  359. }
  360. })