index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. });
  30. this.getList()
  31. },
  32. getList(init = false) {
  33. console.log('发送')
  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. return value;
  61. })
  62. this.setData({
  63. 'params.content.pageNumber': res.pageNumber + 1,
  64. 'params.content.pageTotal': res.pageTotal,
  65. 'params.content.total': res.total,
  66. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  67. loading: false
  68. })
  69. })
  70. },
  71. /* 选中 */
  72. changeResult(e) {
  73. let {
  74. id
  75. } = e.currentTarget.dataset, result = this.data.result;
  76. if (this.data.radio) {
  77. result = [id];
  78. } else {
  79. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  80. }
  81. this.setData({
  82. result
  83. });
  84. if (this.data.radio) this.submit();
  85. },
  86. /* 提交 */
  87. submit() {
  88. let result = this.data.result,
  89. obj = this.data.radio ? {
  90. id: result,
  91. item: this.data.list.find(value => value[this.data.idname] == result),
  92. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  93. } : {
  94. result,
  95. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  96. value: [result.map(v => {
  97. let data = this.data.list.find(value => value[this.data.idname] == v);
  98. return data ? data[this.data.showName] : ""
  99. }), result]
  100. }
  101. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  102. },
  103. /* 预览图片 */
  104. viewImage(e) {
  105. const {
  106. file
  107. } = e.currentTarget.dataset;
  108. if (file.length) wx.previewMedia({
  109. sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
  110. return {
  111. url: v.url,
  112. type: v.fileType
  113. }
  114. }),
  115. current: 0,
  116. showmenu: true
  117. })
  118. },
  119. /* 开始搜索 */
  120. startSearch({
  121. detail
  122. }) {
  123. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  124. if (detail == condition) return;
  125. this.setData({
  126. 'content.where.condition': detail,
  127. 'params.content.where.condition': detail
  128. });
  129. this.getList(true);
  130. },
  131. /* 取消搜索 */
  132. onClear() {
  133. this.setData({
  134. 'content.where.condition': "",
  135. 'params.content.where.condition': ""
  136. });
  137. this.getList(true);
  138. },
  139. onReady() {
  140. this.selectComponent("#ListBox").setHeight(".total", this);
  141. },
  142. onUnload() {
  143. //回收数据
  144. getApp().globalData.handleSelect = null;
  145. }
  146. })