index.js 12 KB

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