index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. item: null, //自定义表单组件使用
  5. result: [], //选中结果
  6. radio: false, //是否为单选模式
  7. idName: 'contactsid', //选择ID
  8. list: []
  9. },
  10. onLoad(options) {
  11. if (options.item) {
  12. let item = JSON.parse(options.item);
  13. this.setData({
  14. item,
  15. params: item.params,
  16. result: item.value.length ? item.value[1] : [],
  17. idName:item.idName
  18. })
  19. };
  20. if (options.params) this.setData({
  21. params: JSON.parse(options.params)
  22. })
  23. if (options.idName) this.setData({
  24. idName: options.idName
  25. })
  26. this.setData({
  27. radio: options.radio ? true : false
  28. })
  29. this.getList();
  30. },
  31. /* 提交 */
  32. submit() {
  33. let pages = getCurrentPages(), //页面
  34. item = this.data.item, //表单
  35. result = this.data.result, //选中结果
  36. list = this.data.result.map(value => this.data.list.find(v => v[this.data.idName] == value)); //选中列表
  37. try {
  38. if (item) { //判断是否表单调用,表单的话吧数据交还给表单
  39. let page = pages[pages.length - 2].selectComponent(item.model || "#Form");
  40. item.value = this.data.radio ? [list[0].name, result] : [list.map(v => v.name), result];
  41. page.handleRoute(item);
  42. } else if (this.data.params.model) {
  43. //判断是否上级页面中的组件调用
  44. let page = pages[pages.length - 2].selectComponent(this.data.params.model);
  45. page.handleSelectContacts(result, list)
  46. } else {
  47. //上级页面调用
  48. pages[pages.length - 2].handleSelectContacts(result, list)
  49. }
  50. } catch (e) {
  51. console.log(e)
  52. wx.showToast({
  53. title: '操作失败',
  54. icon: "none"
  55. })
  56. }
  57. },
  58. /* 开始搜索 */
  59. startSearch({
  60. detail
  61. }) {
  62. let condition = this.data.content?this.data.content.where.condition:this.data.params.content.where.condition;
  63. if (detail == condition) return;
  64. this.setData({
  65. 'content.where.condition': detail,
  66. 'params.content.where.condition': detail
  67. });
  68. this.getList(true);
  69. },
  70. /* 取消搜索 */
  71. onClear() {
  72. this.setData({
  73. 'content.where.condition': "",
  74. 'params.content.where.condition': ""
  75. });
  76. this.getList(true);
  77. },
  78. /* 选中 */
  79. onChange(e) {
  80. const id = e.currentTarget.dataset.item[this.data.idName] + "";
  81. if (!id) return;
  82. let result = this.data.result;
  83. if (this.data.radio) {
  84. result = [id]
  85. } else {
  86. if (result.some(v => v == id)) {
  87. result = result.filter(v => v != id)
  88. } else {
  89. result.push(id)
  90. }
  91. }
  92. this.setData({
  93. result
  94. })
  95. if (this.data.radio) this.submit();
  96. },
  97. //获取列表
  98. getList(init = false) {
  99. let params = this.data.params;
  100. if (init) params.content.pageNumber = 1;
  101. if (params.content.pageNumber > params.content.pageTotal) return;
  102. _Http.basic(params).then(res => {
  103. console.log("联系人", res)
  104. if (res.msg != '成功') return wx.showToast({
  105. title: res.data,
  106. icon: "none"
  107. });
  108. this.setData({
  109. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  110. "params.content.pageNumber": res.pageNumber + 1,
  111. "params.content.pageTotal": res.pageTotal,
  112. })
  113. })
  114. },
  115. onReachBottom() {
  116. this.getList();
  117. }
  118. })