index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const _Http = getApp().globalData.http,
  2. file = require("../../utils/FormatTheAttachment");
  3. import currency from "../../utils/currency";
  4. Page({
  5. data: {
  6. hidePrice: wx.getStorageSync('hidePrice'),
  7. loading: true,
  8. params: {}, //请求体
  9. result: [], //返回结果
  10. radio: false, //是否为单选
  11. idname: "itemid", //idkey
  12. showName: "itemname"
  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. radio: options.radio ? true : false,
  27. idname: options.idname || this.data.idname,
  28. showName: options.showName || this.data.showName,
  29. newPrice: options.newprice || "price", //红色价格
  30. });
  31. this.getList()
  32. },
  33. getList(init = false) {
  34. //init 用于初始化分页
  35. if (init.detail != undefined) init = init.detail;
  36. let params = this.data.params;
  37. if (init) params.content.pageNumber = 1
  38. if (params.content.pageNumber > params.content.pageTotal) return;
  39. _Http.basic(params).then(res => {
  40. console.log("选择产品列表", res)
  41. this.selectComponent('#ListBox').RefreshToComplete();
  42. if (res.msg != '成功') return wx.showToast({
  43. title: res.msg,
  44. icon: "none"
  45. })
  46. const CNY = num => currency(num, {
  47. symbol: "¥",
  48. precision: 2
  49. }).format();
  50. let newPrice = this.data.newPrice;
  51. res.data = res.data.map(value => {
  52. if (value.attinfos.length != 0) {
  53. value.attinfos = file.fileList(value.attinfos)
  54. let image = value.attinfos.find(v => v.fileType == "image");
  55. value.cover = image ? image.cover : "";
  56. }
  57. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  58. value.brandName = value.brand.map(name => name.brandname)
  59. value.tradefields = value.tradefield.map(name => name.tradefield)
  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. let {
  75. id
  76. } = e.currentTarget.dataset, result = this.data.result;
  77. if (this.data.radio) {
  78. result = [id];
  79. } else {
  80. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  81. }
  82. this.setData({
  83. result
  84. });
  85. if (this.data.radio) this.submit();
  86. },
  87. /* 提交 */
  88. submit() {
  89. let result = this.data.result,
  90. obj = this.data.radio ? {
  91. id: result,
  92. item: this.data.list.find(value => value[this.data.idname] == result),
  93. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  94. } : {
  95. result,
  96. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  97. value: [result.map(v => {
  98. let data = this.data.list.find(value => value[this.data.idname] == v);
  99. return data ? data[this.data.showName] : ""
  100. }), result]
  101. }
  102. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  103. },
  104. /* 预览图片 */
  105. viewImage(e) {
  106. const {
  107. file
  108. } = e.currentTarget.dataset;
  109. if (file.length) wx.previewMedia({
  110. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  111. return {
  112. url: v.url,
  113. type: v.fileType
  114. }
  115. }),
  116. current: 0,
  117. showmenu: true
  118. })
  119. },
  120. /* 开始搜索 */
  121. startSearch({
  122. detail
  123. }) {
  124. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  125. if (detail == condition) return;
  126. this.setData({
  127. 'content.where.condition': detail,
  128. 'params.content.where.condition': detail
  129. });
  130. this.getList(true);
  131. },
  132. /* 取消搜索 */
  133. onClear() {
  134. this.setData({
  135. 'content.where.condition': "",
  136. 'params.content.where.condition': ""
  137. });
  138. this.getList(true);
  139. },
  140. onReady() {
  141. this.selectComponent("#ListBox").setHeight(".total", this);
  142. },
  143. /* 步进器输入框失去焦点 */
  144. inputBlur(e) {
  145. const {
  146. index
  147. } = e.currentTarget.dataset;
  148. let item = this.data.list[index];
  149. let qty = 0;
  150. if (item.orderminqty > e.detail.value) {
  151. wx.showToast({
  152. title: '输入数量低于最低起订量!',
  153. icon: "none"
  154. })
  155. qty = item.orderminqty;
  156. } else if (item.orderminqty < e.detail.value) {
  157. var currencyRounding = value => currency(value, {
  158. increment: item.orderaddqty
  159. });
  160. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  161. } else {
  162. qty = e.detail.value;
  163. }
  164. this.setData({
  165. [`list[${index}].qty`]: 0
  166. });
  167. this.setData({
  168. [`list[${index}].qty`]: qty
  169. });
  170. },
  171. stepperChange(e) {
  172. const {
  173. index
  174. } = e.currentTarget.dataset;
  175. let item = this.data.list[index];
  176. if (e.type == 'plus') {
  177. item.qty += (item.orderaddqty) - 0
  178. } else {
  179. item.qty -= item.orderaddqty
  180. }
  181. this.setData({
  182. [`list[${index}]`]: item
  183. })
  184. },
  185. onUnload() {
  186. getApp().globalData.handleSelect = null;
  187. }
  188. })