index.js 9.1 KB

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