index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. params: {}, //请求体
  5. result: [], //返回结果
  6. radio: false, //是否为单选
  7. idname: "sat_campaignid", //idkey
  8. showName: "name", //表单用 显示名称
  9. },
  10. onLoad(options) {
  11. if (options.params) {
  12. let params = JSON.parse(options.params);
  13. if (!params.content.pageNumber || !params.content.pageTotal) {
  14. params.content.pageNumber = 1;
  15. params.content.pageTotal = 1;
  16. }
  17. this.setData({
  18. params
  19. });
  20. }
  21. this.setData({
  22. radio: options.radio ? true : false,
  23. idname: options.idname || this.data.idname,
  24. showName: options.showName || this.data.showName,
  25. });
  26. getApp().globalData.Language.getLanguagePackage(this, options.title || '选择市场活动');
  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.code != '1') return wx.showToast({
  39. title: res.data,
  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. })
  48. })
  49. },
  50. /* 删除项 */
  51. deteleItem(id) {
  52. this.setData({
  53. list: this.data.list.filter(v => v[this.data.idname] != id),
  54. "params.content.total": this.data.params.content.total - 1
  55. })
  56. },
  57. /* 选中 */
  58. changeResult(e) {
  59. let {
  60. id
  61. } = e.currentTarget.dataset, result = this.data.result;
  62. if (this.data.radio) {
  63. result = [id];
  64. } else {
  65. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  66. }
  67. this.setData({
  68. result
  69. });
  70. if (this.data.radio) this.submit();
  71. },
  72. /* 提交 */
  73. submit() {
  74. let result = this.data.result,
  75. obj = this.data.radio ? {
  76. id: result,
  77. item: this.data.list.find(value => value[this.data.idname] == result),
  78. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  79. } : {
  80. result,
  81. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  82. value: [result.map(v => {
  83. let data = this.data.list.find(value => value[this.data.idname] == v);
  84. return data ? data[this.data.showName] : ""
  85. }), result]
  86. }
  87. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  88. },
  89. /* 开始搜索 */
  90. startSearch({
  91. detail
  92. }) {
  93. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  94. if (detail == condition) return;
  95. this.setData({
  96. 'content.where.condition': detail,
  97. 'params.content.where.condition': detail
  98. });
  99. this.getList(true);
  100. },
  101. /* 取消搜索 */
  102. onClear() {
  103. this.setData({
  104. 'content.where.condition': "",
  105. 'params.content.where.condition': ""
  106. });
  107. this.getList(true);
  108. },
  109. onReady() {
  110. this.selectComponent("#ListBox").setHeight(".total", this);
  111. },
  112. onUnload() {
  113. //回收数据
  114. getApp().globalData.handleSelect = null;
  115. }
  116. })