index.js 6.4 KB

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