index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const _Http = getApp().globalData.http,
  2. file = require("../../utils/FormatTheAttachment");
  3. import currency from "../../utils/currency";
  4. Page({
  5. data: {
  6. loading: true,
  7. params: {}, //请求体
  8. result: [], //返回结果
  9. radio: false, //是否为单选
  10. idname: "itemid", //idkey
  11. showName: "itemname"
  12. },
  13. onLoad(options) {
  14. if (options.params) {
  15. let params = JSON.parse(options.params);
  16. if (!params.content.pageNumber || !params.content.pageTotal) {
  17. params.content.pageNumber = 1;
  18. params.content.pageTotal = 1;
  19. }
  20. this.setData({
  21. params
  22. });
  23. };
  24. this.setData({
  25. radio: options.radio ? true : false,
  26. idname: options.idname || this.data.idname,
  27. showName: options.showName || this.data.showName,
  28. newPrice: options.newprice || "marketprice", //红色价格
  29. oldPrice: options.oldprice || "oldprice" //对比老价格
  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. oldPrice = this.data.oldPrice;
  52. res.data = res.data.map(value => {
  53. if (value.attinfos.length != 0) {
  54. value.attinfos = file.fileList(value.attinfos)
  55. let image = value.attinfos.find(v => v.fileType == "image");
  56. value.cover = image ? image.cover : "";
  57. }
  58. if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
  59. if (oldPrice) value.oldPrice = CNY(value[oldPrice] || 0);
  60. value.brandName = value.brand.map(name => name.brandname)
  61. value.tradefields = value.tradefield.map(name => name.tradefield)
  62. return value;
  63. })
  64. this.setData({
  65. 'params.content.pageNumber': res.pageNumber + 1,
  66. 'params.content.pageTotal': res.pageTotal,
  67. 'params.content.total': res.total,
  68. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  69. loading: false
  70. })
  71. })
  72. },
  73. /* 选中 */
  74. changeResult(e) {
  75. let {
  76. id
  77. } = e.currentTarget.dataset, result = this.data.result;
  78. if (this.data.radio) {
  79. result = [id];
  80. } else {
  81. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  82. }
  83. this.setData({
  84. result
  85. });
  86. if (this.data.radio) this.submit();
  87. },
  88. /* 提交 */
  89. submit() {
  90. let result = this.data.result,
  91. obj = this.data.radio ? {
  92. id: result,
  93. item: this.data.list.find(value => value[this.data.idname] == result),
  94. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  95. } : {
  96. result,
  97. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  98. value: [result.map(v => {
  99. let data = this.data.list.find(value => value[this.data.idname] == v);
  100. return data ? data[this.data.showName] : ""
  101. }), result]
  102. }
  103. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  104. },
  105. /* 预览图片 */
  106. viewImage(e) {
  107. const {
  108. file
  109. } = e.currentTarget.dataset;
  110. if (file.length) wx.previewMedia({
  111. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  112. return {
  113. url: v.url,
  114. type: v.fileType
  115. }
  116. }),
  117. current: 0,
  118. showmenu: true
  119. })
  120. },
  121. /* 开始搜索 */
  122. startSearch({
  123. detail
  124. }) {
  125. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  126. if (detail == condition) return;
  127. this.setData({
  128. 'content.where.condition': detail,
  129. 'params.content.where.condition': detail
  130. });
  131. this.getList(true);
  132. },
  133. /* 取消搜索 */
  134. onClear() {
  135. this.setData({
  136. 'content.where.condition': "",
  137. 'params.content.where.condition': ""
  138. });
  139. this.getList(true);
  140. },
  141. onReady() {
  142. this.selectComponent("#ListBox").setHeight(".total", this);
  143. },
  144. onUnload() {
  145. //回收数据
  146. getApp().globalData.handleSelect = null;
  147. }
  148. })