index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. classList = [];
  177. if (results.length) results = results.filter(v => {
  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. getApp().globalData.num = getApp().globalData.num - 1;
  195. }
  196. return item
  197. });
  198. let sa_brandid = results.length ? this.data.sa_brandid : null;
  199. wx.setStorageSync('shopping', {
  200. results,
  201. sa_brandid
  202. })
  203. let isAll = false;
  204. if (sa_brandid) {
  205. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  206. isAll = brand.results.length == results.length
  207. }
  208. this.setData({
  209. sum: CNY(sum),
  210. isAll,
  211. results,
  212. sa_brandid,
  213. classList
  214. });
  215. this.setLogisticsMsg(sum)
  216. },
  217. /* 免运费信息 */
  218. setLogisticsMsg(sum) {
  219. let logistics = null,
  220. figure = this.data.figure;
  221. if (figure == -1) {
  222. logistics = '到付'
  223. } else if (figure == 0) {
  224. logistics = '预付'
  225. } else {
  226. logistics = sum >= figure ? '免运费' : '差' + currency(currency(figure).subtract(sum).value, {
  227. symbol: "",
  228. precision: 2
  229. }).format() + '元免运费';
  230. }
  231. this.setData({
  232. logistics
  233. })
  234. },
  235. /* 删除产品 */
  236. deteleItem(e) {
  237. const {
  238. item
  239. } = e.currentTarget.dataset;
  240. _Http.basic({
  241. "id": 20220924095202,
  242. "content": {
  243. "sa_shoppingcartids": [item.sa_shoppingcartid]
  244. }
  245. }).then(res => {
  246. if (res.msg != '成功') return wx.showToast({
  247. title: res.msg,
  248. icon: "none"
  249. });
  250. let index = this.data.allBrandList.findIndex(v => v.sa_brandid == item.sa_brandid)
  251. this.setData({
  252. list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid),
  253. [`allBrandList[${index}].results`]: this.data.allBrandList[index].results.filter(v => v != item.sa_shoppingcartid)
  254. })
  255. if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
  256. item
  257. }, true);
  258. getApp().globalData.num = getApp().globalData.num - 1;
  259. })
  260. },
  261. /* 步进器输入框失去焦点 */
  262. inputBlur(e) {
  263. const {
  264. index
  265. } = e.currentTarget.dataset;
  266. let item = this.data.list[index];
  267. let qty = 0;
  268. if (item.orderminqty > e.detail.value) {
  269. wx.showToast({
  270. title: '输入数量低于最低起订量!',
  271. icon: "none"
  272. })
  273. qty = item.orderminqty;
  274. } else if (item.orderminqty < e.detail.value) {
  275. var currencyRounding = value => currency(value, {
  276. increment: item.orderaddqty
  277. });
  278. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  279. } else {
  280. qty = e.detail.value;
  281. }
  282. this.setData({
  283. [`list[${index}].qty`]: 0
  284. });
  285. this.setData({
  286. [`list[${index}].qty`]: qty
  287. });
  288. this.computeSum();
  289. clearTimeout(downCount['count' + index])
  290. downCount['count' + index] = setTimeout(() => {
  291. _Http.basic({
  292. "id": 20220924104302,
  293. "content": {
  294. "sa_shoppingcartid": item.sa_shoppingcartid,
  295. "qty": item.qty
  296. },
  297. }, false).then(res => {
  298. console.log("修改数量", res)
  299. })
  300. }, 2000)
  301. },
  302. stepperChange(e) {
  303. const {
  304. index
  305. } = e.currentTarget.dataset;
  306. let item = this.data.list[index];
  307. if (e.type == 'plus') {
  308. item.qty += item.orderaddqty
  309. } else {
  310. item.qty -= item.orderaddqty
  311. }
  312. this.setData({
  313. [`list[${index}]`]: item
  314. })
  315. this.computeSum();
  316. clearTimeout(downCount['count' + index])
  317. downCount['count' + index] = setTimeout(() => {
  318. _Http.basic({
  319. "id": 20220924104302,
  320. "content": {
  321. "sa_shoppingcartid": item.sa_shoppingcartid,
  322. "qty": item.qty
  323. },
  324. }, false).then(res => {
  325. console.log("修改数量", res)
  326. })
  327. }, 2000)
  328. }
  329. }
  330. })