index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import currency from "../../../utils/currency";
  2. const _Http = getApp().globalData.http,
  3. file = require("../../../utils/FormatTheAttachment"),
  4. {
  5. getItem
  6. } = require("../selectProduct/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. packageList: [],
  23. condition: "",
  24. searchList: [],
  25. dialogShow: false,
  26. submitQty: 1,
  27. packagetypemxrows: [],
  28. modification: false, //是否为修改套餐数量
  29. },
  30. onLoad(options) {
  31. if (options.params) {
  32. let params = JSON.parse(options.params);
  33. if (!params.content.pageNumber || !params.content.pageTotal) {
  34. params.content.pageNumber = 1;
  35. params.content.pageTotal = 1;
  36. }
  37. console.log(params)
  38. _Http.basic({
  39. id: 2024080614131103,
  40. content: {
  41. sa_promotionid: params.content.sa_promotionid,
  42. packagetype: params.content.packagetype
  43. }
  44. }).then(res => {
  45. console.log('套餐分类', res)
  46. if (res.msg != '成功') return wx.showToast({
  47. title: res.msg,
  48. icon: "none"
  49. })
  50. let packageList = []
  51. try {
  52. let packagetypemxrows = [];
  53. if (options.packagetypemxrows) {
  54. this.data.packagetypemxrows = JSON.parse(options.packagetypemxrows);
  55. packagetypemxrows = this.data.packagetypemxrows.map(v => v.packagetypemx);
  56. }
  57. packageList = res.data.find(v => v.value == params.content.packagetype).subvalues;
  58. if (packagetypemxrows.length != 0) {
  59. if (options.butText == '调整套数') {
  60. packageList = packagetypemxrows
  61. this.setData({
  62. "submitQty": this.data.packagetypemxrows[0].packagetypeqty
  63. })
  64. } else {
  65. packageList = packageList.filter(v => !packagetypemxrows.includes(v))
  66. }
  67. }
  68. } catch (error) {
  69. console.error("error", error)
  70. }
  71. let getListPa = "";
  72. if (options.getListPa) {
  73. getListPa = JSON.parse(options.getListPa);
  74. getListPa.content.pageNumber = 1;
  75. getListPa.content.pageSize = 9999;
  76. getListPa.content.onlyquery = 1;
  77. getListPa.content.where = {
  78. packagetypemx: ''
  79. }
  80. delete getListPa.content.pageTotal;
  81. }
  82. if (packageList.length) params.content.where.packagetypemx = packageList[0]
  83. this.setData({
  84. params,
  85. getListPa,
  86. butText: options.butText || '生成订单',
  87. packageList
  88. });
  89. this.getList()
  90. this.selectComponent("#ListBox").setHeight(".search", this);
  91. })
  92. this.setData({
  93. params,
  94. butText: options.butText || '生成订单'
  95. });
  96. };
  97. try {
  98. let privacyFieldC = wx.getStorageSync('auth').wdiscounts.forms.wdiscounts.formcols.map(v => v.title);
  99. this.setData({
  100. privacyFieldC
  101. })
  102. } catch (error) {
  103. console.error(error)
  104. }
  105. this.setData({
  106. idname: options.idname || this.data.idname
  107. });
  108. },
  109. customization(e) {
  110. let {
  111. item
  112. } = e.target.dataset;
  113. this.bindChangeFun();
  114. if (item) this.selectComponent("#Custom").onClick(item)
  115. },
  116. bindChangeFun() {
  117. getApp().globalData.customizedProduct = function (item, custom) {
  118. item = getItem(getItem(Object.assign(item, custom)), 'newOldPrice', 'oldprice');
  119. item.customText = getCustomText(item);
  120. return new Promise((resolve) => {
  121. let index = this.data.list.findIndex(v => v.itemid == item.itemid),
  122. i = this.data.searchList.findIndex(v => v.itemid == item.itemid);
  123. item.CompleteCustomization = true;
  124. if (index != -1) this.setData({
  125. [`list[${index}]`]: item
  126. })
  127. if (i != -1) this.setData({
  128. [`searchList[${i}]`]: item,
  129. })
  130. resolve(true)
  131. })
  132. }.bind(this)
  133. },
  134. async 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. params.content.pageSize = 9999;
  140. if (params.content.pageNumber > params.content.pageTotal) return;
  141. let res = {},
  142. CompleteCustomization = false,
  143. modification = false;
  144. if (params.content.where.packagetypemx == '') return this.setData({
  145. loading: false
  146. })
  147. wx.showLoading({
  148. title: '加载中...',
  149. mask: true
  150. })
  151. if (this.data.getListPa) {
  152. this.data.getListPa.content.where.packagetypemx = params.content.where.packagetypemx;
  153. res = await _Http.basic(this.data.getListPa);
  154. CompleteCustomization = true;
  155. modification = true;
  156. if (!res.data.length) {
  157. res = await _Http.basic(params);
  158. CompleteCustomization = false;
  159. modification = false;
  160. }
  161. } else {
  162. res = await _Http.basic(params);
  163. }
  164. wx.hideLoading()
  165. console.log("选择产品列表", res)
  166. this.selectComponent('#ListBox').RefreshToComplete();
  167. this.selectComponent("#ListBox").setHeight(".search", this);
  168. if (res.msg != '成功') return wx.showToast({
  169. title: res.msg,
  170. icon: "none"
  171. })
  172. const CNY = num => currency(num, {
  173. symbol: "¥",
  174. precision: 2
  175. }).format();
  176. let newPrice = this.data.newPrice,
  177. oldPrice = this.data.oldPrice;
  178. res.data = res.data.map(value => {
  179. if (value.attinfos.length != 0) {
  180. value.attinfos = file.fileList(value.attinfos)
  181. let image = value.attinfos.find(v => v.fileType == "image");
  182. if (image) {
  183. try {
  184. value.cover = image.subfiles.find(v => v.type == "thumbnail").url;
  185. } catch (error) {
  186. value.cover = image.url;
  187. }
  188. }
  189. }
  190. if (value.islimit == 0) value.groupqty = 0;
  191. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  192. if (oldPrice) value.oldPrice = CNY(value[oldPrice] || 0);
  193. //value.orderminqty = value.packageqty || value.orderminqty; 起订量优先取包装数量
  194. if (!value.saledqty) value.saledqty = 0;
  195. value.maxQty = value.groupqty == 0 ? "" : value.groupqty - value.saledqty; //有限购 设置最高可订购数量
  196. value.qty = value.orderminqty;
  197. if (value.iscustomsize) {
  198. value.customText = getCustomText(value);
  199. value.CompleteCustomization = CompleteCustomization;
  200. }
  201. return value;
  202. })
  203. this.setData({
  204. 'params.content.pageNumber': res.pageNumber + 1,
  205. 'params.content.pageTotal': res.pageTotal,
  206. 'params.content.total': res.total,
  207. modification,
  208. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  209. loading: false
  210. })
  211. this.handleSearch()
  212. },
  213. tabsChange(e) {
  214. this.setData({
  215. "params.content.where.packagetypemx": e.detail.name
  216. })
  217. try {
  218. this.setData({
  219. "submitQty": this.data.packagetypemxrows.find(v => v.packagetypemx == e.detail.name).packagetypeqty || 1
  220. })
  221. } catch (error) {
  222. }
  223. this.getList(true)
  224. },
  225. changeQtySte({
  226. detail
  227. }) {
  228. if (detail == '-' || detail <= 0) detail = 1;
  229. this.setData({
  230. submitQty: detail
  231. })
  232. },
  233. /* 提交 */
  234. submit() {
  235. const customsize = this.data.list.find(v => (v.iscustomsize && !v.CompleteCustomization));
  236. let that = this;
  237. if (customsize) {
  238. return wx.showModal({
  239. title: '提示',
  240. content: `请先完成"${customsize.itemname}"产品的定制化信息`,
  241. // confirmText: "查看产品",
  242. showCancel: false,
  243. complete: ({
  244. confirm
  245. }) => {
  246. if (confirm) {
  247. /* that.setData({
  248. condition: customsize.itemname
  249. })
  250. that.handleSearch() */
  251. }
  252. }
  253. })
  254. }
  255. /* if (this.data.modification) {
  256. wx.showModal({
  257. title: '提示',
  258. content: '您已添加过此套餐,是否需要重新调整套数?',
  259. confirmText: '是',
  260. cancelText: "否",
  261. complete: ({
  262. confirm
  263. }) => {
  264. if (confirm) handle()
  265. }
  266. })
  267. } else {
  268. handle()
  269. } */
  270. handle()
  271. function handle() {
  272. that.setData({
  273. dialogShow: true
  274. })
  275. that.selectComponent("#Dialog").data.beforeClose = (action) => new Promise((resolve) => {
  276. if (action == 'cancel') {
  277. resolve()
  278. that.setData({
  279. dialogShow: false
  280. })
  281. return;
  282. }
  283. getApp().globalData.handleSelect && getApp().globalData.handleSelect({
  284. list: that.data.list.map(v => {
  285. v.qty = v.qty * that.data.submitQty;
  286. return v
  287. })
  288. }).then(res => {
  289. resolve()
  290. that.setData({
  291. dialogShow: false
  292. })
  293. })
  294. });
  295. }
  296. },
  297. /* 预览图片 */
  298. viewImage(e) {
  299. const {
  300. file
  301. } = e.currentTarget.dataset;
  302. if (file.length) wx.previewMedia({
  303. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  304. return {
  305. url: v.url,
  306. type: v.fileType
  307. }
  308. }),
  309. current: 0,
  310. showmenu: true
  311. })
  312. },
  313. /* 开始搜索 */
  314. startSearch({
  315. detail
  316. }) {
  317. this.data.condition = detail;
  318. this.handleSearch()
  319. },
  320. /* 取消搜索 */
  321. onClear() {
  322. this.data.condition = '';
  323. this.handleSearch()
  324. },
  325. handleSearch() {
  326. let searchList = this.data.list;
  327. if (this.data.condition.length) searchList = searchList.filter(v => {
  328. return ['itemname', 'itemno', 'standards', 'model', 'spec'].some(s => {
  329. try {
  330. return v[s].includes(this.data.condition)
  331. } catch (error) {
  332. return false
  333. }
  334. })
  335. })
  336. this.setData({
  337. searchList
  338. })
  339. },
  340. onReady() {
  341. this.selectComponent("#ListBox").setHeight(".search", this);
  342. },
  343. /* 步进器输入框失去焦点 */
  344. inputBlur(e) {
  345. const {
  346. index
  347. } = e.currentTarget.dataset;
  348. let item = this.data.list[index];
  349. let qty = 0;
  350. if (item.orderminqty > e.detail.value) {
  351. wx.showToast({
  352. title: '输入数量低于最低起订量!',
  353. icon: "none"
  354. })
  355. qty = item.orderminqty;
  356. } else if (item.orderminqty < e.detail.value) {
  357. var currencyRounding = value => currency(value, {
  358. increment: item.orderaddqty
  359. });
  360. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  361. } else {
  362. qty = e.detail.value;
  363. }
  364. this.setData({
  365. [`list[${index}].qty`]: 0
  366. });
  367. this.setData({
  368. [`list[${index}].qty`]: qty
  369. });
  370. let i = this.data.resultList.findIndex(v => v.itemid == item.itemid);
  371. if (i !== -1) this.data.resultList[i].qty = qty;
  372. },
  373. stepperChange(e) {
  374. const {
  375. index
  376. } = e.currentTarget.dataset;
  377. let item = this.data.list[index];
  378. if (e.type == 'plus') {
  379. item.qty += (item.orderaddqty) - 0
  380. } else {
  381. item.qty -= item.orderaddqty
  382. }
  383. this.setData({
  384. [`list[${index}]`]: item
  385. })
  386. let i = this.data.resultList.findIndex(v => v.itemid == item.itemid);
  387. if (i !== -1) this.data.resultList[i] = item;
  388. },
  389. onUnload() {
  390. //回收数据
  391. getApp().globalData.handleSelect = null;
  392. }
  393. })