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