index.js 10.0 KB

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