index.js 5.4 KB

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