index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. return {
  87. "sa_brandid": this.data.params.content.sa_brandid,
  88. "sa_promotionid": this.data.params.content.sa_promotionid,
  89. "sa_promotion_itemsid": v.sa_promotion_itemsid,
  90. "qty": v.qty,
  91. "itemno": v.itemno,
  92. "tradefield": v.tradefield.map(s => s.tradefield).join(","),
  93. sa_promotion_itemsid: v.sa_promotion_itemsid,
  94. itemid: v.itemid,
  95. "width": v.width || 0,
  96. "length": v.length || 0,
  97. }
  98. })
  99. },
  100. }).then(res => {
  101. console.log("批量添加购物车", res)
  102. wx.showToast({
  103. title: res.msg == '成功' ? '添加成功' : res.msg,
  104. icon: "none"
  105. })
  106. if (res.msg == '成功') {
  107. this.setData({
  108. result: [],
  109. resultList: [],
  110. })
  111. this.getCollectCount();
  112. }
  113. })
  114. }
  115. },
  116. customization(e) {
  117. let {
  118. item
  119. } = e.target.dataset;
  120. item.pitchOn = false;
  121. this.bindChangeFun();
  122. if (item) this.selectComponent("#Custom").onClick(item)
  123. },
  124. bindChangeFun() {
  125. getApp().globalData.customizedProduct = (item, custom) => {
  126. item = getItem(getItem(Object.assign(item, custom)), 'newOldPrice', 'oldprice');
  127. item.customText = getCustomText(item);
  128. return new Promise((resolve) => {
  129. let index = this.data.list.findIndex(v => v[this.data.idname] == item[this.data.idname]),
  130. resultList = this.data.resultList,
  131. result = this.data.result;
  132. if (index != -1) this.data.list[index] = item;
  133. let i = resultList.findIndex(v => v[this.data.idname] == item[this.data.idname]);
  134. if (item.pitchOn) {
  135. if (i == -1) {
  136. resultList.push(item);
  137. result.push(item[this.data.idname])
  138. } else {
  139. resultList[i] = item;
  140. }
  141. } else {
  142. resultList[i] = item;
  143. }
  144. this.setData({
  145. list: this.data.list,
  146. result,
  147. resultList
  148. })
  149. resolve(true)
  150. })
  151. }
  152. },
  153. getList(init = false) {
  154. //init 用于初始化分页
  155. if (init.detail != undefined) init = init.detail;
  156. let params = this.data.params;
  157. if (init) params.content.pageNumber = 1
  158. if (params.content.pageNumber > params.content.pageTotal) return;
  159. _Http.basic(params).then(res => {
  160. console.log("选择产品列表", res)
  161. this.selectComponent('#ListBox').RefreshToComplete();
  162. if (res.msg != '成功') return wx.showToast({
  163. title: res.msg,
  164. icon: "none"
  165. })
  166. const CNY = num => currency(num, {
  167. symbol: "¥",
  168. precision: 2
  169. }).format();
  170. let newPrice = this.data.newPrice,
  171. oldPrice = this.data.oldPrice;
  172. res.data = res.data.map(value => {
  173. if (value.attinfos.length != 0) {
  174. value.attinfos = file.fileList(value.attinfos)
  175. let image = value.attinfos.find(v => v.fileType == "image");
  176. if (image) {
  177. try {
  178. value.cover = image.subfiles.find(v => v.type == "thumbnail").url;
  179. } catch (error) {
  180. value.cover = image.url;
  181. }
  182. }
  183. }
  184. if (value.islimit == 0) value.groupqty = 0;
  185. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  186. if (oldPrice) value.oldPrice = CNY(value[oldPrice] || 0);
  187. //value.orderminqty = value.packageqty || value.orderminqty; 起订量优先取包装数量
  188. if (!value.saledqty) value.saledqty = 0;
  189. value.maxQty = value.groupqty == 0 ? "" : value.groupqty - value.saledqty; //有限购 设置最高可订购数量
  190. value.qty = value.orderminqty;
  191. if (value.iscustomsize) {
  192. value.customText = getCustomText(value);
  193. value.pitchOn = false;
  194. }
  195. return value;
  196. })
  197. this.setData({
  198. 'params.content.pageNumber': res.pageNumber + 1,
  199. 'params.content.pageTotal': res.pageTotal,
  200. 'params.content.total': res.total,
  201. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  202. loading: false
  203. })
  204. })
  205. },
  206. /* 选中 */
  207. changeResult(e) {
  208. let {
  209. id,
  210. item
  211. } = e.currentTarget.dataset, result = this.data.result, resultList = this.data.resultList;
  212. if (this.data.radio) {
  213. result = [id];
  214. resultList = [item]
  215. } else {
  216. let isAdd = result.some(v => v == id);
  217. if (!isAdd) {
  218. if (item.iscustomsize == 1) {
  219. if ((item.widthschemeid && !item.width) || (item.lengthschemeid && !item.length)) {
  220. item.pitchOn = true;
  221. this.bindChangeFun();
  222. if (item) this.selectComponent("#Custom").onClick(item)
  223. } else {
  224. result.push(id)
  225. resultList.push(item)
  226. }
  227. } else {
  228. result.push(id)
  229. resultList.push(item)
  230. }
  231. } else {
  232. result = result.filter(v => v != id)
  233. resultList = resultList.filter(v => v[this.data.idname] != id)
  234. }
  235. }
  236. this.setData({
  237. result,
  238. resultList
  239. });
  240. },
  241. /* 提交 */
  242. submit() {
  243. let result = this.data.result,
  244. resultList = this.data.resultList,
  245. obj = {
  246. result,
  247. list: resultList,
  248. value: [result.map(v => {
  249. let data = this.data.list.find(value => value[this.data.idname] == v);
  250. return data ? data[this.data.showName] : ""
  251. }), result]
  252. }
  253. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  254. },
  255. /* 预览图片 */
  256. viewImage(e) {
  257. const {
  258. file
  259. } = e.currentTarget.dataset;
  260. if (file.length) wx.previewMedia({
  261. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  262. return {
  263. url: v.url,
  264. type: v.fileType
  265. }
  266. }),
  267. current: 0,
  268. showmenu: true
  269. })
  270. },
  271. /* 开始搜索 */
  272. startSearch({
  273. detail
  274. }) {
  275. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  276. if (detail == condition) return;
  277. this.setData({
  278. 'content.where.condition': detail,
  279. 'params.content.where.condition': detail
  280. });
  281. this.getList(true);
  282. },
  283. /* 取消搜索 */
  284. onClear() {
  285. this.setData({
  286. 'content.where.condition': "",
  287. 'params.content.where.condition': ""
  288. });
  289. this.getList(true);
  290. },
  291. onReady() {
  292. this.selectComponent("#ListBox").setHeight(".search", this);
  293. },
  294. /* 步进器输入框失去焦点 */
  295. inputBlur(e) {
  296. const {
  297. index
  298. } = e.currentTarget.dataset;
  299. let item = this.data.list[index];
  300. let qty = 0;
  301. if (item.orderminqty > e.detail.value) {
  302. wx.showToast({
  303. title: '输入数量低于最低起订量!',
  304. icon: "none"
  305. })
  306. qty = item.orderminqty;
  307. } else if (item.orderminqty < e.detail.value) {
  308. var currencyRounding = value => currency(value, {
  309. increment: item.orderaddqty
  310. });
  311. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  312. } else {
  313. qty = e.detail.value;
  314. }
  315. this.setData({
  316. [`list[${index}].qty`]: 0
  317. });
  318. this.setData({
  319. [`list[${index}].qty`]: qty
  320. });
  321. let i = this.data.resultList.findIndex(v => v.sa_promotion_itemsid == item.sa_promotion_itemsid);
  322. if (i !== -1) this.data.resultList[i].qty = qty;
  323. },
  324. stepperChange(e) {
  325. const {
  326. index
  327. } = e.currentTarget.dataset;
  328. let item = this.data.list[index];
  329. if (e.type == 'plus') {
  330. item.qty += (item.orderaddqty) - 0
  331. } else {
  332. item.qty -= item.orderaddqty
  333. }
  334. this.setData({
  335. [`list[${index}]`]: item
  336. })
  337. let i = this.data.resultList.findIndex(v => v.sa_promotion_itemsid == item.sa_promotion_itemsid);
  338. if (i !== -1) this.data.resultList[i] = item;
  339. },
  340. onUnload() {
  341. //回收数据
  342. getApp().globalData.handleSelect = null;
  343. }
  344. })