myInfo.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // component/myInfo/myInfo.ts
  2. import { ConstsData } from "../../utils/const"
  3. import { httpUtil } from "../../utils/restful";
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. },
  10. /**
  11. * 组件的初始数据
  12. */
  13. data: {
  14. regionOne: Array<String>(),
  15. regionTwo: Array<String>(),
  16. regionThree: Array<String>(),
  17. regionOneIndex: 0,
  18. regionTwoIndex: 0,
  19. regionThreeIndex: 0,
  20. region: [
  21. Array<String>(),
  22. Array<String>(),
  23. Array<String>()
  24. ],
  25. regionIndex: [0, 0, 0],
  26. schoolArray: [['暂无学校']],
  27. schoolIndex: 0,
  28. classArray: ['暂无班级'],
  29. classIndex: 0,
  30. provinceValue: '',//省份value
  31. regionValue: '',//地区value
  32. cityValue: '',
  33. schoolData: [{ 'id': '' }],
  34. schoolId: '',
  35. classData: [{ 'id': '' }],
  36. classId: '',
  37. isShowPassWord: true,
  38. userName: "",
  39. userPwd: "",
  40. initFirst: true
  41. },
  42. lifetimes: {
  43. attached: function () {
  44. // 在组件实例被从页面节点树添加时执行
  45. //获取用户信息
  46. this.getMyInfo()
  47. },
  48. detached: function () {
  49. // 在组件实例被从页面节点树移除时执行
  50. },
  51. },
  52. /**
  53. * 组件的方法列表
  54. */
  55. methods: {
  56. getMyInfo: function () {
  57. if (ConstsData.AppData.myInfoData.id == 0) {
  58. //没有用户信息,请求接口
  59. httpUtil.wxGet(httpUtil.interfaces.getUserById, null).then((res: any) => {
  60. ConstsData.AppData.myInfoData = res.data.data
  61. //获取地区和学校班级等信息
  62. this.changeOrganizeInfo()
  63. }).catch((res) => {
  64. console.log("获取我的信息失败:", res)
  65. })
  66. } else {
  67. //有用户信息,直接设置地区啥的
  68. this.changeOrganizeInfo()
  69. }
  70. },
  71. changeOrganizeInfo: function () {
  72. //设置名字
  73. this.setData({
  74. userName: ConstsData.AppData.myInfoData.name
  75. })
  76. //切换地区
  77. this.setData({
  78. regionOne: [],
  79. regionTwo: [],
  80. regionThree: []
  81. })
  82. ConstsData.AppData.organizeInfo.data.children.forEach((item, index) => {
  83. if (this.data.initFirst) {
  84. if (item.value == ConstsData.AppData.myInfoData.province) {
  85. //如果是当前的
  86. this.setData({
  87. regionOneIndex: index
  88. })
  89. }
  90. }
  91. this.data.regionOne.push(item.title)
  92. })
  93. ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children.forEach((item, index) => {
  94. if (this.data.initFirst) {
  95. if (item.value == ConstsData.AppData.myInfoData.city) {
  96. this.setData({
  97. regionTwoIndex: index
  98. })
  99. }
  100. }
  101. this.data.regionTwo.push(item.title)
  102. })
  103. ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children[this.data.regionTwoIndex].children.forEach((item, index) => {
  104. if (this.data.initFirst) {
  105. if (item.value == ConstsData.AppData.myInfoData.region) {
  106. this.setData({
  107. regionThreeIndex: index
  108. })
  109. }
  110. }
  111. this.data.regionThree.push(item.title)
  112. })
  113. this.setData({
  114. region: [this.data.regionOne, this.data.regionTwo, this.data.regionThree],
  115. regionIndex: [this.data.regionOneIndex, this.data.regionTwoIndex, this.data.regionThreeIndex],
  116. provinceValue: ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].value.toString(),
  117. cityValue: ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children[this.data.regionTwoIndex].value.toString(),
  118. regionValue: ConstsData.AppData.organizeInfo.data.children[this.data.regionOneIndex].children[this.data.regionTwoIndex].children[this.data.regionThreeIndex].value.toString(),
  119. initFirst: false
  120. })
  121. //切换学校
  122. this.changeSchool();
  123. },
  124. //查询学校
  125. changeSchool: function () {
  126. //查询学校
  127. let params = {
  128. city: this.data.cityValue,
  129. province: this.data.provinceValue,
  130. region: this.data.regionValue,
  131. type: 1,
  132. }
  133. httpUtil.wxGet(httpUtil.interfaces.getSchoolOrClass, params).then((res: any) => {
  134. this.setData({
  135. schoolArray: [],
  136. schoolIndex: 0,
  137. schoolData: res.data.data,
  138. schoolId: res.data.data[0].id
  139. })
  140. let newArray: string[] = [];
  141. res.data.data.forEach((element, index) => {
  142. if (ConstsData.AppData.myInfoData.schoolId == element.id) {
  143. this.setData({
  144. schoolIndex: index,
  145. schoolId: this.data.schoolData[index].id
  146. })
  147. }
  148. newArray.push(element.title)
  149. });
  150. this.data.schoolArray.push(newArray)
  151. this.setData({
  152. schoolArray: this.data.schoolArray
  153. })
  154. //切换班级
  155. this.changeClass()
  156. }).catch(res => {
  157. console.log("查询学校error:", res)
  158. this.setData({
  159. schoolArray: [['暂无学校']],
  160. schoolId: '',
  161. classArray: ['暂无班级'],
  162. classId: ''
  163. })
  164. })
  165. },
  166. //查询班级
  167. changeClass: function () {
  168. let params = {
  169. "city": this.data.cityValue,
  170. "father": this.data.schoolId,//学校ID
  171. "province": this.data.provinceValue,
  172. "region": this.data.regionValue,
  173. "type": 2,
  174. }
  175. httpUtil.wxGet(httpUtil.interfaces.getSchoolOrClass, params).then((res: any) => {
  176. this.setData({
  177. classArray: [],
  178. classIndex: 0
  179. })
  180. this.setData({
  181. classData: res.data.data,
  182. classId: res.data.data[0].id
  183. })
  184. console.log("classData:", this.data.classData)
  185. res.data.data.forEach((element: { id: number; title: string; }, index: number) => {
  186. if (element.id == ConstsData.AppData.myInfoData.classId) {
  187. this.setData({
  188. classIndex: index,
  189. classId: res.data.data[index].id
  190. })
  191. console.log("this.data.classIndex:", this.data.classIndex)
  192. }
  193. this.data.classArray.push(element.title)
  194. });
  195. this.setData({
  196. classArray: this.data.classArray,
  197. })
  198. }).catch(res => {
  199. console.log("查询班级error:", res)
  200. this.setData({
  201. classArray: ['暂无班级'],
  202. classId: ''
  203. })
  204. })
  205. },
  206. bindMultiPickerColumnChange: function (event: any) {
  207. //
  208. switch (Number(event.detail.column)) {
  209. case 0:
  210. //代表第一列
  211. this.setData({
  212. regionOneIndex: event.detail.value,
  213. })
  214. break;
  215. case 1:
  216. //代表第二列
  217. this.setData({
  218. regionTwoIndex: event.detail.value,
  219. })
  220. break;
  221. case 2:
  222. //代表第三列
  223. this.setData({
  224. regionThreeIndex: event.detail.value
  225. })
  226. break;
  227. }
  228. this.changeOrganizeInfo();
  229. },
  230. bindRegionChange: function (event: any) {
  231. this.setData({
  232. regionIndex: event.detail.value
  233. })
  234. },
  235. bindSchoolPicker: function (event: any) {
  236. console.log(" event.detail.value:", event.detail.value)
  237. this.setData({
  238. schoolIndex: event.detail.value,
  239. schoolId: this.data.schoolData[event.detail.value].id
  240. })
  241. this.changeClass();
  242. },
  243. bindClssPicker: function (event: any) {
  244. this.setData({
  245. classIndex: event.detail.value,
  246. classId: this.data.classData[event.detail.value].id
  247. })
  248. },
  249. showPwdTab: function () {
  250. if (this.data.isShowPassWord) {
  251. this.setData({
  252. isShowPassWord: false
  253. })
  254. } else {
  255. this.setData({
  256. isShowPassWord: true
  257. })
  258. }
  259. },
  260. bindUserNameInput: function (event: any) {
  261. this.setData({
  262. userName: event.detail.value
  263. })
  264. },
  265. bindUserPwdInput: function (event: any) {
  266. this.setData({
  267. userPwd: event.detail.value
  268. })
  269. },
  270. submitMyInfo: function () {
  271. if (!this.data.userName) {
  272. wx.showToast({
  273. title: '请输入姓名',
  274. icon: 'none',
  275. duration: 1000
  276. })
  277. return;
  278. }
  279. let params = {}
  280. if (!this.data.userPwd) {
  281. params = {
  282. id: httpUtil.httpData.userId,
  283. city: this.data.cityValue,
  284. classId: this.data.classId,
  285. name: this.data.userName,
  286. province: this.data.provinceValue,//省份
  287. region: this.data.regionValue,//地区
  288. schoolId: this.data.schoolId,
  289. }
  290. } else {
  291. params = {
  292. id: httpUtil.httpData.userId,
  293. city: this.data.cityValue,
  294. classId: this.data.classId,
  295. name: this.data.userName,
  296. password: this.data.userPwd,
  297. province: this.data.provinceValue,//省份
  298. region: this.data.regionValue,//地区
  299. schoolId: this.data.schoolId,
  300. }
  301. }
  302. httpUtil.wxPut(httpUtil.interfaces.updateUserInfo, params).then((res: any) => {
  303. console.log("更新个人信息成功:", res)
  304. ConstsData.AppData.myInfoData = res.data.data
  305. wx.showToast({
  306. title: '更新成功',
  307. icon: 'none'
  308. })
  309. }).catch((res) => {
  310. console.log("更新个人信息失败:", res)
  311. })
  312. }
  313. }
  314. })