select.js 5.9 KB

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