index.js 4.2 KB

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