index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. loading: true,
  5. params: {}, //请求体
  6. result: [], //返回结果
  7. radio: false, //是否为单选
  8. idname: "contactsid", //idkey
  9. showName: "name"
  10. },
  11. onLoad(options) {
  12. if (options.item) {
  13. let item = JSON.parse(options.item);
  14. this.setData({
  15. item,
  16. params: item.params
  17. });
  18. }
  19. if (options.params) this.setData({
  20. params: JSON.parse(options.params)
  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. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  82. },
  83. /* 开始搜索 */
  84. startSearch({
  85. detail
  86. }) {
  87. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  88. if (detail == condition) return;
  89. this.setData({
  90. 'content.where.condition': detail,
  91. 'params.content.where.condition': detail
  92. });
  93. this.getList(true);
  94. },
  95. /* 取消搜索 */
  96. onClear() {
  97. this.setData({
  98. 'content.where.condition': "",
  99. 'params.content.where.condition': ""
  100. });
  101. this.getList(true);
  102. },
  103. onReady() {
  104. this.selectComponent("#ListBox").setHeight(".total", this);
  105. },
  106. onUnload() {
  107. //回收数据
  108. getApp().globalData.handleSelect = null;
  109. }
  110. })