index.js 8.9 KB

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