index.js 12 KB

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