myEdit.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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-test.efunbox.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. gender: e.detail.value.radioGroup1,
  102. schoolName: e.detail.value.address || '',
  103. birthday: e.detail.value.picker || '',
  104. avatar: this.data.user.avatar,
  105. grade: this.data.gradeCode
  106. }
  107. httpRequestApi.settingUserInfo(data).success(res => {
  108. console.log(res.data.data); //修改资料成功后 调用 getUserInfo
  109. // wx.redirectTo({
  110. // url: '/pages/index/index?index=3'
  111. // });
  112. wx.setStorageSync('user', res.data.data);
  113. wx.setStorageSync('grade', res.data.data.grade);
  114. wx.navigateBack({
  115. delta: 1
  116. })
  117. });
  118. },
  119. bindGradeChange: function(e){
  120. console.log('年级选择',e)
  121. this.setData({
  122. gradeIndex: e.detail.value,
  123. gradeCode: this.data.gradeObject[e.detail.value].code
  124. },()=>{
  125. console.log(this.data.gradeCode)
  126. })
  127. },
  128. /**
  129. * 生命周期函数--监听页面加载
  130. */
  131. getUserInfo: function () {
  132. },
  133. onLoad: function (option) {
  134. httpRequestApi.getUserInfo().success(res => {
  135. const user = res.data.data;
  136. let gradeIndex = 0;
  137. this.data.gradeObject.forEach((item,index)=>{
  138. console.log('年级index',item)
  139. console.log('年级index',item)
  140. if(item.code === user.grade){
  141. console.log('年级index',index)
  142. gradeIndex = index
  143. }
  144. console.log(user)
  145. const birthday = user.birthday ? formatDate(user.birthday, 2) : '2018-01-01';
  146. console.log(birthday)
  147. this.setData({
  148. user: user,
  149. date: birthday,
  150. sexs: [{
  151. value: 2,
  152. checked: user.gender === 2 ? true : false,
  153. sex: '女'
  154. },
  155. {
  156. value: 1,
  157. checked: user.gender === 1 ? true : false,
  158. sex: '男'
  159. }
  160. ],
  161. occupations: [{
  162. value: '家长',
  163. checked: user.profession === '家长' ? true : false
  164. },
  165. {
  166. value: '老师',
  167. checked: user.profession === '老师' ? true : false
  168. },
  169. {
  170. value: '学生',
  171. checked: user.profession === '学生' ? true : false
  172. }
  173. ],
  174. gradeIndex
  175. })
  176. })
  177. console.log(this.data.date)
  178. if (option.title) {
  179. wx.setNavigationBarTitle({
  180. title: option.title //页面标题为路由参数
  181. });
  182. this.setData({
  183. title: option.title
  184. });
  185. }
  186. httpRequestApi.userIntoPage('pages/user/myEdit/myEdit', '修改资料页面').success((res) => {
  187. })
  188. });
  189. },
  190. /**
  191. * 生命周期函数--监听页面初次渲染完成
  192. */
  193. onReady: function () {
  194. },
  195. /**
  196. * 生命周期函数--监听页面显示
  197. */
  198. onShow: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面隐藏
  202. */
  203. onHide: function () {
  204. },
  205. /**
  206. * 生命周期函数--监听页面卸载
  207. */
  208. onUnload: function () {
  209. },
  210. /**
  211. * 页面相关事件处理函数--监听用户下拉动作
  212. */
  213. onPullDownRefresh: function () {
  214. },
  215. /**
  216. * 页面上拉触底事件的处理函数
  217. */
  218. onReachBottom: function () {
  219. },
  220. /**
  221. * 用户点击右上角分享
  222. */
  223. onShareAppMessage: function () {
  224. }
  225. })