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