index.js 5.4 KB

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