index.js 12 KB

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