index.js 8.7 KB

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