index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import currency from "../../../utils/currency";
  2. const _Http = getApp().globalData.http,
  3. file = require("../../../utils/FormatTheAttachment"),
  4. {
  5. getItem
  6. } = require("./calculatePrice");
  7. import {
  8. getCustomText
  9. } from "../../../utils/customItemType";
  10. Page({
  11. data: {
  12. loading: true,
  13. params: {}, //请求体
  14. result: [], //返回结果
  15. idname: "sa_promotion_itemsid", //idkey
  16. showName: "itemname",
  17. newPrice: "price",
  18. oldPrice: "oldprice",
  19. resultList: [],
  20. sa_brandid: null,
  21. privacyFieldC: []
  22. },
  23. onLoad(options) {
  24. console.log(options)
  25. if (options.params) {
  26. let params = JSON.parse(options.params);
  27. if (!params.content.pageNumber || !params.content.pageTotal) {
  28. params.content.pageNumber = 1;
  29. params.content.pageTotal = 1;
  30. }
  31. this.setData({
  32. params,
  33. butText: options.butText || '生成订单'
  34. });
  35. };
  36. try {
  37. let privacyFieldC = wx.getStorageSync('auth').wdiscounts.forms.wdiscounts.formcols.map(v => v.title);
  38. this.setData({
  39. privacyFieldC
  40. })
  41. console.log("privacyFieldC", privacyFieldC)
  42. } catch (error) {
  43. console.error(error)
  44. }
  45. this.setData({
  46. idname: options.idname || this.data.idname
  47. });
  48. this.getList()
  49. },
  50. onShow() {
  51. this.getCollectCount()
  52. },
  53. getCollectCount() {
  54. return _Http.basic({
  55. "id": 20220927093202,
  56. "content": {
  57. sa_promotionid: this.data.params.content.sa_promotionid
  58. }
  59. }).then(res => {
  60. console.log("促销购物车数量", res.data)
  61. this.selectComponent("#Footer").setData({
  62. badge: res.data.num || 0
  63. })
  64. });
  65. },
  66. updateCart({
  67. detail
  68. }) {
  69. if (detail == "toindex") {
  70. wx.navigateTo({
  71. url: `/packageA/activity/selectProduct/cart/index?data=${
  72. JSON.stringify({
  73. "sa_brandid": this.data.params.content.sa_brandid,
  74. "sa_promotionid": this.data.params.content.sa_promotionid,
  75. })
  76. }`,
  77. })
  78. } else if (detail == "submit") {
  79. this.submit()
  80. } else {
  81. _Http.basic({
  82. "id": 20231024110003,
  83. "content": {
  84. sa_promotionid: this.data.params.content.sa_promotionid,
  85. items: this.data.resultList.map(v => {
  86. console.log(v)
  87. return {
  88. "sa_brandid": this.data.params.content.sa_brandid,
  89. "sa_promotionid": this.data.params.content.sa_promotionid,
  90. "sa_promotion_itemsid": v.sa_promotion_itemsid,
  91. "qty": v.qty,
  92. "itemno": v.itemno,
  93. "tradefield": v.tradefield.map(s => s.tradefield).join(","),
  94. sa_promotion_itemsid: v.sa_promotion_itemsid,
  95. itemid: v.itemid,
  96. "width": v.width || 0,
  97. "length": v.length || 0,
  98. }
  99. })
  100. },
  101. }).then(res => {
  102. console.log("批量添加购物车", res)
  103. wx.showToast({
  104. title: res.msg == '成功' ? '添加成功' : res.msg,
  105. icon: "none"
  106. })
  107. if (res.msg == '成功') {
  108. this.setData({
  109. result: [],
  110. resultList: [],
  111. })
  112. this.getCollectCount();
  113. }
  114. })
  115. }
  116. },
  117. customization(e) {
  118. let {
  119. item
  120. } = e.target.dataset;
  121. item.pitchOn = false;
  122. this.bindChangeFun();
  123. if (item) this.selectComponent("#Custom").onClick(item)
  124. },
  125. bindChangeFun() {
  126. getApp().globalData.customizedProduct = (item, custom) => {
  127. item = getItem(getItem(Object.assign(item, custom)), 'newOldPrice', 'oldprice');
  128. item.customText = getCustomText(item);
  129. return new Promise((resolve) => {
  130. let index = this.data.list.findIndex(v => v[this.data.idname] == item[this.data.idname]),
  131. resultList = this.data.resultList,
  132. result = this.data.result;
  133. if (index != -1) this.data.list[index] = item;
  134. let i = resultList.findIndex(v => v[this.data.idname] == item[this.data.idname]);
  135. if (item.pitchOn) {
  136. if (i == -1) {
  137. resultList.push(item);
  138. result.push(item[this.data.idname])
  139. } else {
  140. resultList[i] = item;
  141. }
  142. } else {
  143. resultList[i] = item;
  144. }
  145. this.setData({
  146. list: this.data.list,
  147. result,
  148. resultList
  149. })
  150. resolve(true)
  151. })
  152. }
  153. },
  154. getList(init = false) {
  155. //init 用于初始化分页
  156. if (init.detail != undefined) init = init.detail;
  157. let params = this.data.params;
  158. if (init) params.content.pageNumber = 1
  159. if (params.content.pageNumber > params.content.pageTotal) return;
  160. _Http.basic(params).then(res => {
  161. console.log("选择产品列表", res)
  162. this.selectComponent('#ListBox').RefreshToComplete();
  163. if (res.msg != '成功') return wx.showToast({
  164. title: res.msg,
  165. icon: "none"
  166. })
  167. const CNY = num => currency(num, {
  168. symbol: "¥",
  169. precision: 2
  170. }).format();
  171. let newPrice = this.data.newPrice,
  172. oldPrice = this.data.oldPrice;
  173. res.data = res.data.map(value => {
  174. if (value.attinfos.length != 0) {
  175. value.attinfos = file.fileList(value.attinfos)
  176. let image = value.attinfos.find(v => v.fileType == "image");
  177. if (image) {
  178. try {
  179. value.cover = image.subfiles.find(v => v.type == "thumbnail").url;
  180. } catch (error) {
  181. value.cover = image.url;
  182. }
  183. }
  184. }
  185. if (value.islimit == 0) value.groupqty = 0;
  186. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  187. if (oldPrice) value.oldPrice = CNY(value[oldPrice] || 0);
  188. //value.orderminqty = value.packageqty || value.orderminqty; 起订量优先取包装数量
  189. if (!value.saledqty) value.saledqty = 0;
  190. value.maxQty = value.groupqty == 0 ? "" : value.groupqty - value.saledqty; //有限购 设置最高可订购数量
  191. value.qty = value.orderminqty;
  192. if (value.iscustomsize) {
  193. value.customText = getCustomText(value);
  194. value.pitchOn = false;
  195. }
  196. return value;
  197. })
  198. this.setData({
  199. 'params.content.pageNumber': res.pageNumber + 1,
  200. 'params.content.pageTotal': res.pageTotal,
  201. 'params.content.total': res.total,
  202. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  203. loading: false
  204. })
  205. })
  206. },
  207. /* 选中 */
  208. changeResult(e) {
  209. let {
  210. id,
  211. item
  212. } = e.currentTarget.dataset, result = this.data.result, resultList = this.data.resultList;
  213. if (this.data.radio) {
  214. result = [id];
  215. resultList = [item]
  216. } else {
  217. let isAdd = result.some(v => v == id);
  218. if (!isAdd) {
  219. if (item.iscustomsize == 1) {
  220. if ((item.widthschemeid && !item.width) || (item.lengthschemeid && !item.length)) {
  221. item.pitchOn = true;
  222. this.bindChangeFun();
  223. if (item) this.selectComponent("#Custom").onClick(item)
  224. } else {
  225. result.push(id)
  226. resultList.push(item)
  227. }
  228. } else {
  229. result.push(id)
  230. resultList.push(item)
  231. }
  232. } else {
  233. result = result.filter(v => v != id)
  234. resultList = resultList.filter(v => v[this.data.idname] != id)
  235. }
  236. }
  237. this.setData({
  238. result,
  239. resultList
  240. });
  241. },
  242. /* 提交 */
  243. submit() {
  244. let result = this.data.result,
  245. resultList = this.data.resultList,
  246. obj = {
  247. result,
  248. list: resultList,
  249. value: [result.map(v => {
  250. let data = this.data.list.find(value => value[this.data.idname] == v);
  251. return data ? data[this.data.showName] : ""
  252. }), result]
  253. }
  254. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  255. },
  256. /* 预览图片 */
  257. viewImage(e) {
  258. const {
  259. file
  260. } = e.currentTarget.dataset;
  261. if (file.length) wx.previewMedia({
  262. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  263. return {
  264. url: v.url,
  265. type: v.fileType
  266. }
  267. }),
  268. current: 0,
  269. showmenu: true
  270. })
  271. },
  272. /* 开始搜索 */
  273. startSearch({
  274. detail
  275. }) {
  276. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  277. if (detail == condition) return;
  278. this.setData({
  279. 'content.where.condition': detail,
  280. 'params.content.where.condition': detail
  281. });
  282. this.getList(true);
  283. },
  284. /* 取消搜索 */
  285. onClear() {
  286. this.setData({
  287. 'content.where.condition': "",
  288. 'params.content.where.condition': ""
  289. });
  290. this.getList(true);
  291. },
  292. onReady() {
  293. this.selectComponent("#ListBox").setHeight(".search", this);
  294. },
  295. /* 步进器输入框失去焦点 */
  296. inputBlur(e) {
  297. const {
  298. index
  299. } = e.currentTarget.dataset;
  300. let item = this.data.list[index];
  301. let qty = 0;
  302. if (item.orderminqty > e.detail.value) {
  303. wx.showToast({
  304. title: '输入数量低于最低起订量!',
  305. icon: "none"
  306. })
  307. qty = item.orderminqty;
  308. } else if (item.orderminqty < e.detail.value) {
  309. var currencyRounding = value => currency(value, {
  310. increment: item.orderaddqty
  311. });
  312. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  313. } else {
  314. qty = e.detail.value;
  315. }
  316. this.setData({
  317. [`list[${index}].qty`]: 0
  318. });
  319. this.setData({
  320. [`list[${index}].qty`]: qty
  321. });
  322. let i = this.data.resultList.findIndex(v => v.sa_promotion_itemsid == item.sa_promotion_itemsid);
  323. if (i !== -1) this.data.resultList[i].qty = qty;
  324. },
  325. stepperChange(e) {
  326. const {
  327. index
  328. } = e.currentTarget.dataset;
  329. let item = this.data.list[index];
  330. if (e.type == 'plus') {
  331. item.qty += (item.orderaddqty) - 0
  332. } else {
  333. item.qty -= item.orderaddqty
  334. }
  335. this.setData({
  336. [`list[${index}]`]: item
  337. })
  338. let i = this.data.resultList.findIndex(v => v.sa_promotion_itemsid == item.sa_promotion_itemsid);
  339. if (i !== -1) this.data.resultList[i] = item;
  340. },
  341. onUnload() {
  342. //回收数据
  343. getApp().globalData.handleSelect = null;
  344. }
  345. })