index.js 9.1 KB

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