myEdit.js 5.7 KB

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