index.js 9.7 KB

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