cascader.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import Api from "../../model/api";
  2. import {getDataSet, toast} from "../../utils/utils";
  3. import {studentTypes} from "../../model/enum";
  4. import Config from "../../model/config";
  5. Component({
  6. properties: {
  7. contactPhone: String, //联系电话
  8. perType: String,
  9. parentIds: String,
  10. },
  11. observers: {
  12. parentIds: async function (parentIds) {
  13. if (!parentIds) {
  14. this.setData({
  15. area: ''
  16. })
  17. }
  18. await this.getTabData({id: ""}, -1);
  19. },
  20. },
  21. data: {
  22. // contactPhone: '', //联系电话
  23. show: false,
  24. active: 0,
  25. loading: false,
  26. tabs: [],
  27. dict: {
  28. 0: "省级",
  29. 1: "市级",
  30. 2: "区县级",
  31. 3: "街道(镇)级",
  32. 4: "社区(村)级",
  33. },
  34. area: ''
  35. },
  36. methods: {
  37. async restoreData(index) {
  38. if (!this.data.parentIds) {
  39. return;
  40. }
  41. let areaIdArray = this.data.parentIds.split(",");
  42. if (areaIdArray && areaIdArray.length > index) {
  43. let id = areaIdArray[index];
  44. const cell = this.findCell(id, index);
  45. let event = {currentTarget: {dataset: {}}};
  46. event.currentTarget.dataset.cell = cell;
  47. event.currentTarget.dataset.index = index;
  48. await this.onClick(event, true);
  49. }
  50. this.setData({
  51. area: this.data.tabs.map(item => item.title).filter(item=>item).join("/")
  52. })
  53. },
  54. findCell(id, index, key = 'id') {
  55. if (this.data.tabs) {
  56. return this.data.tabs[index].data.find(item => item[key] == id)
  57. }
  58. },
  59. onChange(e) {
  60. },
  61. async handleClick(event) {
  62. const {cell, index} = getDataSet(event);
  63. this.data.tabs[index].title = cell.name;
  64. this.data.tabs[index].code = cell.code;
  65. this.setData({
  66. tabs: this.data.tabs,
  67. });
  68. //加载新一级别的数据 级联组件 正常加载这个即可
  69. // await this.getTabData(cell, index);
  70. //以下属于该app变种规则扩展
  71. if (this.data.perType == studentTypes.COMMUNITY_WORKER) {
  72. if (this.data.tabs[0].title == "吉林省") {
  73. if (index == 3) { // 村一级单独请求
  74. this.setData({
  75. loading: true
  76. })
  77. this.setData({
  78. tabs: this.data.tabs.slice(0, index + 1),
  79. }, () => {
  80. this.selectComponent("#tabs")?.resize()
  81. })
  82. let res = await Api.getUnitList({
  83. parentId: cell.id
  84. })
  85. this.setData({
  86. loading: false
  87. })
  88. res.data = res.data.map(item => {
  89. return {
  90. name: item.unitname,
  91. code: item.unitcode,
  92. id: item.unitcode,
  93. }
  94. })
  95. this.changeTab(res.data, index);
  96. return;
  97. }
  98. } else {
  99. if (index == 1) {
  100. return;
  101. }
  102. }
  103. }
  104. await this.getTabData(cell, index);
  105. },
  106. async onClick(event, isCode) {
  107. if (!isCode && this.data.parentIds) {
  108. //让 父id变化不走观察者
  109. this.data.parentIds = "";
  110. }
  111. await this.handleClick(event);
  112. },
  113. click() {
  114. let code = '';
  115. console.log("传出去的code1", code)
  116. if (this.data.perType == studentTypes.COMMUNITY_WORKER) {
  117. if (this.data.tabs[0].title == "吉林省") {
  118. if (this.data.tabs.length >= 5) {
  119. code = this.data.tabs[4].code;
  120. }
  121. } else { //非吉林省社区工作者 只需要选择省市
  122. if (this.data.tabs.length >= 2) {
  123. code = this.data.tabs[1].code;
  124. }
  125. }
  126. } else {
  127. code = this.data.tabs[this.data.tabs.length - 1].code || this.data.tabs[this.data.tabs.length - 2].code;
  128. }
  129. this.setData({
  130. show: false,
  131. area: this.data.tabs.map(item => item.title).filter(item=>item).join("/")
  132. })
  133. console.log("传出去的code", code)
  134. this.triggerEvent("change", {
  135. 'formData.code': code
  136. });
  137. },
  138. showArea() {
  139. this.setData({
  140. show: true
  141. })
  142. },
  143. close() {
  144. this.setData({show: false})
  145. },
  146. changeTab(data, index) {
  147. let oldTabs = this.data.tabs.slice(0, index + 1);
  148. let indexObj = {};
  149. if (data && data.length > 0) {
  150. oldTabs = [
  151. ...oldTabs,
  152. {data: data.reverse()}
  153. ];
  154. indexObj = {active: index + 1}
  155. this.setData({
  156. tabs: oldTabs,
  157. ...indexObj
  158. }, () => {
  159. this.selectComponent("#tabs")?.resize()
  160. this.restoreData(indexObj.active);
  161. })
  162. } else {
  163. this.setData({
  164. tabs: oldTabs,
  165. ...indexObj
  166. }, () => {
  167. this.selectComponent("#tabs")?.resize()
  168. })
  169. }
  170. },
  171. async getTabData(cell, index) {
  172. //抽取出来可扩展
  173. //获取多级数据的实现方法,这里是网络请求,根据业务也可以本地获取全部数据,分级在这个方法提供
  174. this.setData({
  175. loading: true
  176. })
  177. this.setData({
  178. tabs: this.data.tabs.slice(0, index + 1),
  179. }, () => {
  180. this.selectComponent("#tabs")?.resize()
  181. })
  182. const res = await Api.getRegionList({parentId: cell.id})
  183. this.setData({
  184. loading: false
  185. })
  186. this.changeTab(res.data, index);
  187. },
  188. async callPhone(){
  189. try {
  190. await wx.makePhoneCall({
  191. phoneNumber: this.data.contactPhone
  192. });
  193. } catch (e) {
  194. console.log(e)
  195. toast("拨打电话失败");
  196. }
  197. },
  198. },
  199. async attached() {
  200. await this.getTabData({id: ""}, -1);
  201. //取客服电话
  202. // const appHelpInfo = await Api.getHelpInfo(Config.APP_ID, true);
  203. // // console.log('appHelpInfo', appHelpInfo.data.appTelephone);
  204. // this.setData({
  205. // contactPhone: appHelpInfo.data.appTelephone
  206. // });
  207. //客服电话end
  208. //取班主任电话
  209. // if (this.data.teamId && this.data.teamId != 'undefined') {
  210. // const res = await Api.getTeamTeacherPhone({
  211. // teamId: this.data.teamId
  212. // });
  213. // this.setData({
  214. // contactPhone: res.data.teacherPhone
  215. // });
  216. // }
  217. },
  218. });