myEdit.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import httpRequestApi from '../../../utils/APIClient';
  2. import {
  3. formatDate
  4. } from '../../../utils/util';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. user: {},
  11. sexs: [],
  12. occupations: [],
  13. date: '',
  14. msg: '',
  15. nickname: '',
  16. gradeIndex:0,
  17. gradeArray:['小学一年级','小学二年级','小学三年级','学前班'],
  18. gradeCode:'',
  19. gradeObject: [{
  20. code: 'PRIMARY_FIRST_GRADE',
  21. name: '小学一年级'
  22. },
  23. {
  24. code: 'PRIMARY_SECOND_GRADE',
  25. name: '小学二年级'
  26. },
  27. {
  28. code: 'PRIMARY_THREE_GRADE',
  29. name: '小学三年级'
  30. },
  31. {
  32. code: 'PRESCHOOL',
  33. name: '学前班'
  34. }
  35. ],
  36. },
  37. chooseGrade: function (e) {
  38. console.log('选择年级')
  39. },
  40. saveNickName: function (e) {
  41. let nickname = e.detail.value;
  42. this.setData({
  43. nicename: nickname
  44. });
  45. },
  46. saveAddress: function (e) {
  47. let msg = e.detail.value;
  48. this.setData({
  49. msg: msg
  50. });
  51. },
  52. bindDateChange: function (e) {
  53. console.log(e.detail.value)
  54. this.setData({
  55. date: e.detail.value
  56. });
  57. },
  58. optionAction: function (e) {
  59. console.log(e.detail.value);
  60. },
  61. optionSetting: function (e) {
  62. console.log(e.detail.value);
  63. },
  64. changeAvatar: function (e) {
  65. const _this = this;
  66. wx.chooseImage({
  67. count: 1,
  68. sizeType: ['compressed'],
  69. sourceType: ['album', 'camera'],
  70. success(res) {
  71. // tempFilePath可以作为img标签的src属性显示图片
  72. const localImage = res.tempFilePaths[0];
  73. wx.uploadFile({
  74. url: 'https://reader.lingjiao.cn/readerBase/file/upload',
  75. filePath: localImage,
  76. name: '头像',
  77. header: {
  78. uid: wx.getStorageSync('uid')
  79. },
  80. success: (res) => {
  81. // console.log(JSON.parse(res.data));
  82. const newAvatar = JSON.parse(res.data).data;
  83. const str = 'user.avatar'
  84. _this.setData({
  85. [str]: newAvatar
  86. })
  87. }
  88. })
  89. }
  90. })
  91. },
  92. formSubmit: function (e) {
  93. console.log(e.detail.value)
  94. console.log(e)
  95. if (e.detail.value.nickname === '') {
  96. return false
  97. }
  98. // const uid = wx.getStorageSync('uid');
  99. const data = {
  100. nickName: e.detail.value.nickname || '',
  101. mobile: '',
  102. gender: e.detail.value.radioGroup1,
  103. schoolProvince: '',
  104. schoolCity: '',
  105. schoolName: e.detail.value.address || '',
  106. profession: e.detail.value.radioGroup2 || '',
  107. birthday: e.detail.value.picker || '',
  108. avatar: this.data.user.avatar,
  109. grade: this.data.gradeCode
  110. }
  111. httpRequestApi.settingUserInfo(data).success(res => {
  112. console.log(res.data.data); //修改资料成功后 调用 getUserInfo
  113. // wx.redirectTo({
  114. // url: '/pages/index/index?index=3'
  115. // });
  116. wx.setStorageSync('user', res.data.data);
  117. wx.navigateBack({
  118. delta: 1
  119. })
  120. });
  121. },
  122. bindGradeChange: function(e){
  123. console.log('年级选择',e)
  124. this.setData({
  125. gradeIndex: e.detail.value,
  126. gradeCode: this.data.gradeObject[e.detail.value].code
  127. },()=>{
  128. console.log(this.data.gradeCode)
  129. })
  130. },
  131. /**
  132. * 生命周期函数--监听页面加载
  133. */
  134. getUserInfo: function () {
  135. },
  136. onLoad: function (option) {
  137. httpRequestApi.getUserInfo().success(res => {
  138. const user = res.data.data;
  139. let gradeIndex = 0;
  140. this.data.gradeObject.forEach((item,index)=>{
  141. console.log('年级index',item)
  142. console.log('年级index',item)
  143. if(item.code === user.grade){
  144. console.log('年级index',index)
  145. gradeIndex = index
  146. }
  147. console.log(user)
  148. const birthday = user.birthday ? formatDate(user.birthday, 2) : '2018-01-01';
  149. console.log(birthday)
  150. this.setData({
  151. user: user,
  152. date: birthday,
  153. sexs: [{
  154. value: 2,
  155. checked: user.gender === 2 ? true : false,
  156. sex: '女'
  157. },
  158. {
  159. value: 1,
  160. checked: user.gender === 1 ? true : false,
  161. sex: '男'
  162. }
  163. ],
  164. occupations: [{
  165. value: '家长',
  166. checked: user.profession === '家长' ? true : false
  167. },
  168. {
  169. value: '老师',
  170. checked: user.profession === '老师' ? true : false
  171. },
  172. {
  173. value: '学生',
  174. checked: user.profession === '学生' ? true : false
  175. }
  176. ],
  177. gradeIndex
  178. })
  179. })
  180. console.log(this.data.date)
  181. if (option.title) {
  182. wx.setNavigationBarTitle({
  183. title: option.title //页面标题为路由参数
  184. });
  185. this.setData({
  186. title: option.title
  187. });
  188. }
  189. httpRequestApi.userIntoPage('pages/user/myEdit/myEdit', '修改资料页面').success((res) => {
  190. })
  191. });
  192. },
  193. /**
  194. * 生命周期函数--监听页面初次渲染完成
  195. */
  196. onReady: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面显示
  200. */
  201. onShow: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面隐藏
  205. */
  206. onHide: function () {
  207. },
  208. /**
  209. * 生命周期函数--监听页面卸载
  210. */
  211. onUnload: function () {
  212. },
  213. /**
  214. * 页面相关事件处理函数--监听用户下拉动作
  215. */
  216. onPullDownRefresh: function () {
  217. },
  218. /**
  219. * 页面上拉触底事件的处理函数
  220. */
  221. onReachBottom: function () {
  222. },
  223. /**
  224. * 用户点击右上角分享
  225. */
  226. onShareAppMessage: function () {
  227. }
  228. })