index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import currency from "../../../utils/currency";
  2. const _Http = getApp().globalData.http,
  3. file = require("../../../utils/FormatTheAttachment"),
  4. {
  5. getItem
  6. } = require("./calculatePrice")
  7. Page({
  8. data: {
  9. loading: true,
  10. params: {}, //请求体
  11. result: [], //返回结果
  12. idname: "itemid", //idkey
  13. showName: "itemname",
  14. newPrice: "price",
  15. oldPrice: "oldprice",
  16. resultList: []
  17. },
  18. onLoad(options) {
  19. if (options.params) {
  20. let params = JSON.parse(options.params);
  21. if (!params.content.pageNumber || !params.content.pageTotal) {
  22. params.content.pageNumber = 1;
  23. params.content.pageTotal = 1;
  24. }
  25. this.setData({
  26. params,
  27. butText: options.butText || '生成订单'
  28. });
  29. };
  30. this.setData({
  31. idname: options.idname || this.data.idname
  32. });
  33. this.getList()
  34. getApp().globalData.customizedProduct = this.customizedProduct.bind(this);
  35. },
  36. customization(e) {
  37. let {
  38. item
  39. } = e.target.dataset;
  40. item.pitchOn = false;
  41. if (item) this.selectComponent("#Custom").onClick(item)
  42. },
  43. customizedProduct(item) {
  44. item = getItem(item);
  45. item = getItem(item, 'newOldPrice', 'oldprice');
  46. return new Promise((resolve) => {
  47. let index = this.data.list.findIndex(v => v[this.data.idname] == item[this.data.idname]),
  48. resultList = this.data.resultList,
  49. result = this.data.result;
  50. if (index != -1) this.data.list[index] = item;
  51. let i = resultList.findIndex(v => v[this.data.idname] == item[this.data.idname]);
  52. if (item.pitchOn) {
  53. if (i == -1) {
  54. resultList.push(item);
  55. result.push(item[this.data.idname])
  56. } else {
  57. resultList[i] = item;
  58. }
  59. } else {
  60. resultList[i] = item;
  61. }
  62. this.setData({
  63. list: this.data.list,
  64. result,
  65. resultList
  66. })
  67. resolve(true)
  68. })
  69. },
  70. getList(init = false) {
  71. //init 用于初始化分页
  72. if (init.detail != undefined) init = init.detail;
  73. let params = this.data.params;
  74. if (init) params.content.pageNumber = 1
  75. if (params.content.pageNumber > params.content.pageTotal) return;
  76. _Http.basic(params).then(res => {
  77. console.log("选择产品列表", res)
  78. this.selectComponent('#ListBox').RefreshToComplete();
  79. if (res.msg != '成功') return wx.showToast({
  80. title: res.msg,
  81. icon: "none"
  82. })
  83. const CNY = num => currency(num, {
  84. symbol: "¥",
  85. precision: 2
  86. }).format();
  87. let newPrice = this.data.newPrice,
  88. oldPrice = this.data.oldPrice;
  89. res.data = res.data.map(value => {
  90. if (value.attinfos.length != 0) {
  91. value.attinfos = file.fileList(value.attinfos)
  92. let image = value.attinfos.find(v => v.fileType == "image");
  93. if (image) {
  94. try {
  95. value.cover = image.subfiles.find(v => v.type == "thumbnail").url;
  96. } catch (error) {
  97. value.cover = image.url;
  98. }
  99. }
  100. }
  101. if (value.islimit == 0) value.groupqty = 0;
  102. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  103. if (oldPrice) value.oldPrice = CNY(value[oldPrice] || 0);
  104. //value.orderminqty = value.packageqty || value.orderminqty; 起订量优先取包装数量
  105. if (!value.saledqty) value.saledqty = 0;
  106. value.maxQty = value.groupqty == 0 ? "" : value.groupqty - value.saledqty; //有限购 设置最高可订购数量
  107. value.qty = value.orderminqty;
  108. return value;
  109. })
  110. this.setData({
  111. 'params.content.pageNumber': res.pageNumber + 1,
  112. 'params.content.pageTotal': res.pageTotal,
  113. 'params.content.total': res.total,
  114. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  115. loading: false
  116. })
  117. })
  118. },
  119. /* 选中 */
  120. changeResult(e) {
  121. let {
  122. id,
  123. item
  124. } = e.currentTarget.dataset, result = this.data.result, resultList = this.data.resultList;
  125. if (this.data.radio) {
  126. result = [id];
  127. resultList = [item]
  128. } else {
  129. let isAdd = result.some(v => v == id);
  130. if (!isAdd) {
  131. if (item.iscustomsize == 1) {
  132. if ((item.widthschemeid && !item.width) || (item.lengthschemeid && !item.length)) {
  133. item.pitchOn = true;
  134. if (item) this.selectComponent("#Custom").onClick(item)
  135. } else {
  136. result.push(id)
  137. resultList.push(item)
  138. }
  139. } else {
  140. result.push(id)
  141. resultList.push(item)
  142. }
  143. } else {
  144. result = result.filter(v => v != id)
  145. resultList = resultList.filter(v => v[this.data.idname] != id)
  146. }
  147. }
  148. this.setData({
  149. result,
  150. resultList
  151. });
  152. },
  153. /* 提交 */
  154. submit() {
  155. let result = this.data.result,
  156. resultList = this.data.resultList,
  157. obj = {
  158. result,
  159. list: resultList,
  160. value: [result.map(v => {
  161. let data = this.data.list.find(value => value[this.data.idname] == v);
  162. return data ? data[this.data.showName] : ""
  163. }), result]
  164. }
  165. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  166. },
  167. /* 预览图片 */
  168. viewImage(e) {
  169. const {
  170. file
  171. } = e.currentTarget.dataset;
  172. if (file.length) wx.previewMedia({
  173. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  174. return {
  175. url: v.url,
  176. type: v.fileType
  177. }
  178. }),
  179. current: 0,
  180. showmenu: true
  181. })
  182. },
  183. /* 开始搜索 */
  184. startSearch({
  185. detail
  186. }) {
  187. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  188. if (detail == condition) return;
  189. this.setData({
  190. 'content.where.condition': detail,
  191. 'params.content.where.condition': detail
  192. });
  193. this.getList(true);
  194. },
  195. /* 取消搜索 */
  196. onClear() {
  197. this.setData({
  198. 'content.where.condition': "",
  199. 'params.content.where.condition': ""
  200. });
  201. this.getList(true);
  202. },
  203. onReady() {
  204. this.selectComponent("#ListBox").setHeight(".search", this);
  205. },
  206. /* 步进器输入框失去焦点 */
  207. inputBlur(e) {
  208. const {
  209. index
  210. } = e.currentTarget.dataset;
  211. let item = this.data.list[index];
  212. let qty = 0;
  213. if (item.orderminqty > e.detail.value) {
  214. wx.showToast({
  215. title: '输入数量低于最低起订量!',
  216. icon: "none"
  217. })
  218. qty = item.orderminqty;
  219. } else if (item.orderminqty < e.detail.value) {
  220. var currencyRounding = value => currency(value, {
  221. increment: item.orderaddqty
  222. });
  223. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  224. } else {
  225. qty = e.detail.value;
  226. }
  227. this.setData({
  228. [`list[${index}].qty`]: 0
  229. });
  230. this.setData({
  231. [`list[${index}].qty`]: qty
  232. });
  233. },
  234. stepperChange(e) {
  235. const {
  236. index
  237. } = e.currentTarget.dataset;
  238. let item = this.data.list[index];
  239. if (e.type == 'plus') {
  240. item.qty += (item.orderaddqty) - 0
  241. } else {
  242. item.qty -= item.orderaddqty
  243. }
  244. this.setData({
  245. [`list[${index}]`]: item
  246. })
  247. },
  248. onUnload() {
  249. //回收数据
  250. getApp().globalData.handleSelect = null;
  251. }
  252. })