index.js 9.9 KB

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