select.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. tabChange(e) {
  54. this.data.params.content.where.type = e.detail.title == '客户' ? 7 : 5;
  55. this.getList(true);
  56. },
  57. /* 获取列表标签 */
  58. getTags() {
  59. let list = this.data.list,
  60. ownerids = list.map(v => v.sa_customersid);
  61. _Http.basic({
  62. "id": 20221018102001,
  63. "content": {
  64. nocache: true,
  65. "ownertable": "sa_customers",
  66. ownerids
  67. }
  68. }).then(res => {
  69. console.log("标签", res)
  70. if (res.data == '失败') return this.getTags1();
  71. for (let key in res.data) {
  72. let index = list.findIndex(v => v.sa_customersid == key);
  73. list[index].tags = res.data[key]
  74. };
  75. this.setData({
  76. list
  77. })
  78. })
  79. },
  80. /* 竞争对手标签 */
  81. getTags1() {
  82. let list = this.data.list,
  83. ownerids = list.map(v => v.sa_competitorid);
  84. _Http.basic({
  85. "id": 20221018102001,
  86. "content": {
  87. nocache: true,
  88. "ownertable": "sa_competitor",
  89. ownerids
  90. }
  91. }).then(res => {
  92. console.log("标签1", res)
  93. for (let key in res.data) {
  94. let index = list.findIndex(v => v.sa_competitorid == key);
  95. list[index].tags = res.data[key]
  96. };
  97. this.setData({
  98. list
  99. })
  100. })
  101. },
  102. /* 删除项 */
  103. deteleItem(id) {
  104. this.setData({
  105. list: this.data.list.filter(v => v[this.data.idname] != id),
  106. "params.content.total": this.data.params.content.total - 1
  107. })
  108. },
  109. /* 选中 */
  110. changeResult(e) {
  111. let {
  112. id
  113. } = e.currentTarget.dataset, result = this.data.result;
  114. if (this.data.radio) {
  115. result = [id];
  116. } else {
  117. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  118. }
  119. this.setData({
  120. result
  121. });
  122. if (this.data.radio) this.submit();
  123. },
  124. /* 提交 */
  125. submit() {
  126. let result = this.data.result,
  127. obj = this.data.radio ? {
  128. id: result,
  129. item: this.data.list.find(value => value[this.data.idname] == result),
  130. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  131. } : {
  132. result,
  133. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  134. value: [result.map(v => {
  135. let data = this.data.list.find(value => value[this.data.idname] == v);
  136. return data ? data[this.data.showName] : ""
  137. }), result]
  138. }
  139. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  140. },
  141. /* 开始搜索 */
  142. startSearch({
  143. detail
  144. }) {
  145. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  146. if (detail == condition) return;
  147. this.setData({
  148. 'content.where.condition': detail,
  149. 'params.content.where.condition': detail
  150. });
  151. this.getList(true);
  152. },
  153. /* 取消搜索 */
  154. onClear() {
  155. this.setData({
  156. 'content.where.condition': "",
  157. 'params.content.where.condition': ""
  158. });
  159. this.getList(true);
  160. },
  161. onReady() {
  162. this.selectComponent("#ListBox").setHeight(".total", this);
  163. },
  164. onUnload() {
  165. //回收数据
  166. getApp().globalData.handleSelect = null;
  167. }
  168. })