index.js 12 KB

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