cascader.js 7.6 KB

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