select.js 5.4 KB

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