index.js 3.7 KB

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