index.js 10 KB

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