index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. loading: true,
  5. params: {}, //请求体
  6. result: [], //返回结果
  7. radio: false, //是否为单选
  8. idname: "sys_enterpriseid", //idkey
  9. showName: "enterprisename"
  10. },
  11. onLoad(options) {
  12. if (options.params) {
  13. let params = JSON.parse(options.params);
  14. if (!params.content.pageNumber || !params.content.pageTotal) {
  15. params.content.pageNumber = 1;
  16. params.content.pageTotal = 1;
  17. }
  18. this.setData({
  19. params
  20. });
  21. }
  22. this.setData({
  23. radio: options.radio ? true : false,
  24. idname: options.idname || this.data.idname,
  25. showName: options.showName || this.data.showName,
  26. });
  27. this.getList()
  28. },
  29. getList(init = false) {
  30. //init 用于初始化分页
  31. if (init.detail != undefined) init = init.detail;
  32. let params = this.data.params;
  33. if (init) params.content.pageNumber = 1
  34. if (params.content.pageNumber > params.content.pageTotal) return;
  35. _Http.basic(params).then(res => {
  36. console.log("选择经销商列表", res)
  37. this.selectComponent('#ListBox').RefreshToComplete();
  38. if (res.msg != '成功') return wx.showToast({
  39. title: res.msg,
  40. icon: "none"
  41. })
  42. this.setData({
  43. 'params.content.pageNumber': res.pageNumber + 1,
  44. 'params.content.pageTotal': res.pageTotal,
  45. 'params.content.total': res.total,
  46. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  47. loading: false
  48. })
  49. })
  50. },
  51. /* 选中 */
  52. changeResult(e) {
  53. let {
  54. id
  55. } = e.currentTarget.dataset, result = this.data.result;
  56. if (this.data.radio) {
  57. result = [id];
  58. } else {
  59. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  60. }
  61. this.setData({
  62. result
  63. });
  64. if (this.data.radio) this.submit();
  65. },
  66. /* 提交 */
  67. submit() {
  68. let result = this.data.result,
  69. obj = this.data.radio ? {
  70. id: result,
  71. item: this.data.list.find(value => value[this.data.idname] == result),
  72. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  73. } : {
  74. result,
  75. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  76. value: [result.map(v => {
  77. let data = this.data.list.find(value => value[this.data.idname] == v);
  78. return data ? data[this.data.showName] : ""
  79. }), result]
  80. }
  81. console.log(obj);
  82. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  83. },
  84. /* 开始搜索 */
  85. startSearch({
  86. detail
  87. }) {
  88. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  89. if (detail == condition) return;
  90. this.setData({
  91. 'content.where.condition': detail,
  92. 'params.content.where.condition': detail
  93. });
  94. this.getList(true);
  95. },
  96. /* 取消搜索 */
  97. onClear() {
  98. this.setData({
  99. 'content.where.condition': "",
  100. 'params.content.where.condition': ""
  101. });
  102. this.getList(true);
  103. },
  104. onReady() {
  105. this.selectComponent("#ListBox").setHeight(".total", this);
  106. },
  107. onUnload() {
  108. //回收数据
  109. getApp().globalData.handleSelect = null;
  110. }
  111. })