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. lifetimes: {
  27. attached: function () {
  28. getApp().globalData.Language.getLanguagePackage(this)
  29. }
  30. },
  31. methods: {
  32. /* 获取列表 */
  33. getList() {
  34. _Http.basic({
  35. "id": 20220924095302,
  36. "content": {
  37. nocache: true,
  38. istool: 0,
  39. "pageNumber": 1,
  40. "pageSize": getApp().globalData.num + 5,
  41. "where": {
  42. "condition": ""
  43. }
  44. }
  45. }).then(res => {
  46. console.log('购物车列表', res)
  47. if (res.code != '1') return wx.showToast({
  48. title: res.msg,
  49. icon: "none"
  50. })
  51. let list = [],
  52. allBrandList = [];
  53. list = res.data.map(v => {
  54. v.showPrice = CNY(v.oldprice)
  55. let obj = allBrandList.find(s => s.sa_brandid == v.sa_brandid);
  56. if (obj) {
  57. obj.results.push(v.sa_shoppingcartid)
  58. } else {
  59. allBrandList.push({
  60. brandname: v.brandname,
  61. sa_brandid: v.sa_brandid,
  62. results: [v.sa_shoppingcartid],
  63. })
  64. }
  65. return v
  66. });
  67. this.setData({
  68. list,
  69. allBrandList,
  70. isGet: true
  71. });
  72. if (wx.getStorageSync('shopping')) {
  73. this.setData({
  74. ...wx.getStorageSync('shopping')
  75. });
  76. this.computeSum();
  77. }
  78. })
  79. },
  80. clickBut(e) {
  81. this.data.classList.length >= 2 ? getApp().globalData.Language.showToast('请选择订单领域(订单只允许同品牌/同领域的商品)') : this.handleSubmit(0);
  82. },
  83. /* 提交 */
  84. submit(e) {
  85. this.handleSubmit(e.detail.value)
  86. },
  87. handleSubmit(index) {
  88. let data = this.data.classList[index];
  89. _Http.basic({
  90. "id": 20221128183202,
  91. "content": {
  92. istool: 0,
  93. "tradefield": data.type, //必选
  94. "items": data.list.map(v => {
  95. return {
  96. "sa_orderitemsid": 0,
  97. "itemid": v.itemid,
  98. "sa_brandid": v.sa_brandid,
  99. "qty": v.qty
  100. }
  101. })
  102. }
  103. }).then(res => {
  104. console.log("转化订单", res)
  105. if (res.code != '1') return wx.showToast({
  106. title: res.msg,
  107. icon: "none"
  108. });
  109. wx.showModal({
  110. cancelText: getApp().globalData.Language.getMapText('取消'),
  111. confirmText: getApp().globalData.Language.getMapText('确定'),
  112. title: getApp().globalData.Language.getMapText('提示'),
  113. content: getApp().globalData.Language.getMapText('生成成功!是否立即前往'),
  114. complete: (s) => {
  115. if (s.confirm) {
  116. wx.navigateTo({
  117. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  118. })
  119. }
  120. }
  121. });
  122. this.getList();
  123. })
  124. },
  125. /* 是否选择全部 */
  126. setIsAll() {
  127. let isAll = this.data.isAll;
  128. //取消全选
  129. if (isAll) {
  130. this.setData({
  131. sa_brandid: null,
  132. results: []
  133. })
  134. } else {
  135. //已选品牌产品情况下
  136. if (this.data.sa_brandid) {
  137. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  138. this.setData({
  139. results: obj.results
  140. })
  141. } else {
  142. if (this.data.allBrandList.length == 0) return;
  143. this.setData({
  144. sa_brandid: this.data.allBrandList[0].sa_brandid,
  145. results: this.data.allBrandList[0].results
  146. })
  147. }
  148. };
  149. this.computeSum();
  150. },
  151. /* 切换选中项 */
  152. changeResults(e, my = false) {
  153. const {
  154. item
  155. } = my ? e : e.currentTarget.dataset;
  156. let results = this.data.results,
  157. sa_brandid = this.data.sa_brandid;
  158. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  159. if (results.length == 0) {
  160. results.push(item.sa_shoppingcartid);
  161. sa_brandid = item.sa_brandid;
  162. } else {
  163. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  164. if (index == -1) {
  165. results.push(item.sa_shoppingcartid);
  166. } else {
  167. results.splice(index, 1);
  168. if (results.length == 0) sa_brandid = null;
  169. }
  170. };
  171. this.setData({
  172. results,
  173. sa_brandid
  174. })
  175. this.computeSum();
  176. },
  177. /* 计算总价/产品领域分类 */
  178. computeSum() {
  179. let results = this.data.results,
  180. sum = 0,
  181. classList = [];
  182. if (results.length) results = results.filter(v => {
  183. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  184. if (item) {
  185. sum = currency(sum).add(currency(item.qty).multiply(item.oldprice)).value;
  186. /* 领域分类 */
  187. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  188. if (index == -1) {
  189. classList.push({
  190. type: item.tradefield_shoppingcart,
  191. list: [item],
  192. name: item.tradefield_shoppingcart + getApp().globalData.Language.getMapText('(1件商品)')
  193. })
  194. } else {
  195. classList[index].list.push(item)
  196. classList[index].name = classList[index].type + `(${classList[index].list.length + getApp().globalData.Language.getMapText('件商品')})`
  197. }
  198. } else {
  199. getApp().globalData.num = getApp().globalData.num - 1;
  200. }
  201. return item
  202. });
  203. let sa_brandid = results.length ? this.data.sa_brandid : null;
  204. wx.setStorageSync('shopping', {
  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 ? '免运费' : getApp().globalData.Language.getMapText('差') + currency(currency(figure).subtract(sum).value, {
  232. symbol: "",
  233. precision: 2
  234. }).format() + getApp().globalData.Language.getMapText('元免运费');
  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.code != '1') 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. getApp().globalData.Language.showToast('输入数量低于最低起订量!')
  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. })