select.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. params: {}, //请求体
  5. result: [], //返回结果
  6. radio: false, //是否为单选
  7. idname: "sa_customersid", //idkey
  8. showName: "enterprisename", //表单用 显示名称
  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. if (options.title) wx.setNavigationBarTitle({
  22. title: options.title,
  23. })
  24. this.setData({
  25. radio: options.radio ? true : false,
  26. idname: options.idname || this.data.idname,
  27. showName: options.showName || this.data.showName,
  28. });
  29. this.getList()
  30. },
  31. getList(init = false) {
  32. //init 用于初始化分页
  33. if (init.detail != undefined) init = init.detail;
  34. let params = this.data.params;
  35. if (init) params.content.pageNumber = 1;
  36. if (params.content.pageNumber > params.content.pageTotal) return;
  37. _Http.basic(params).then(res => {
  38. console.log("选择客户列表", res)
  39. this.selectComponent('#ListBox').RefreshToComplete();
  40. if (res.msg != '成功') return wx.showToast({
  41. title: res.data,
  42. icon: "none"
  43. })
  44. this.setData({
  45. 'params.content.pageNumber': res.pageNumber + 1,
  46. 'params.content.pageTotal': res.pageTotal,
  47. 'params.content.total': res.total,
  48. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
  49. })
  50. this.getTags();
  51. })
  52. },
  53. /* 获取列表标签 */
  54. getTags() {
  55. let list = this.data.list,
  56. ownerids = list.map(v => v.sa_customersid);
  57. _Http.basic({
  58. "id": 20221018102001,
  59. "content": {
  60. nocache: true,
  61. "ownertable": "sa_customers",
  62. ownerids
  63. }
  64. }).then(res => {
  65. console.log("标签", res)
  66. if (res.data == '失败') return this.getTags1();
  67. for (let key in res.data) {
  68. let index = list.findIndex(v => v.sa_customersid == key);
  69. list[index].tags = res.data[key]
  70. };
  71. this.setData({
  72. list
  73. })
  74. })
  75. },
  76. /* 竞争对手标签 */
  77. getTags1() {
  78. let list = this.data.list,
  79. ownerids = list.map(v => v.sa_competitorid);
  80. _Http.basic({
  81. "id": 20221018102001,
  82. "content": {
  83. nocache: true,
  84. "ownertable": "sa_competitor",
  85. ownerids
  86. }
  87. }).then(res => {
  88. console.log("标签1", res)
  89. for (let key in res.data) {
  90. let index = list.findIndex(v => v.sa_competitorid == key);
  91. list[index].tags = res.data[key]
  92. };
  93. this.setData({
  94. list
  95. })
  96. })
  97. },
  98. /* 删除项 */
  99. deteleItem(id) {
  100. this.setData({
  101. list: this.data.list.filter(v => v[this.data.idname] != id),
  102. "params.content.total": this.data.params.content.total - 1
  103. })
  104. },
  105. /* 选中 */
  106. changeResult(e) {
  107. let {
  108. id
  109. } = e.currentTarget.dataset, result = this.data.result;
  110. if (this.data.radio) {
  111. result = [id];
  112. } else {
  113. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  114. }
  115. this.setData({
  116. result
  117. });
  118. if (this.data.radio) this.submit();
  119. },
  120. /* 提交 */
  121. submit() {
  122. let result = this.data.result,
  123. obj = this.data.radio ? {
  124. id: result,
  125. item: this.data.list.find(value => value[this.data.idname] == result),
  126. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  127. } : {
  128. result,
  129. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  130. value: [result.map(v => {
  131. let data = this.data.list.find(value => value[this.data.idname] == v);
  132. return data ? data[this.data.showName] : ""
  133. }), result]
  134. }
  135. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  136. },
  137. /* 开始搜索 */
  138. startSearch({
  139. detail
  140. }) {
  141. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  142. if (detail == condition) return;
  143. this.setData({
  144. 'content.where.condition': detail,
  145. 'params.content.where.condition': detail
  146. });
  147. this.getList(true);
  148. },
  149. /* 取消搜索 */
  150. onClear() {
  151. this.setData({
  152. 'content.where.condition': "",
  153. 'params.content.where.condition': ""
  154. });
  155. this.getList(true);
  156. },
  157. onReady() {
  158. this.selectComponent("#ListBox").setHeight(".total", this);
  159. },
  160. onUnload() {
  161. //回收数据
  162. getApp().globalData.handleSelect = null;
  163. }
  164. })