myEdit.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. onChooseAvatar(e) {
  73. const {
  74. avatarUrl
  75. } = e.detail
  76. this.setData({
  77. ['user.avatar']: avatarUrl,
  78. })
  79. },
  80. changeAvatar: function(e) {
  81. const _this = this;
  82. wx.chooseImage({
  83. count: 1,
  84. sizeType: ['compressed'],
  85. sourceType: ['album', 'camera'],
  86. success(res) {
  87. // tempFilePath可以作为img标签的src属性显示图片
  88. const localImage = res.tempFilePaths[0];
  89. wx.uploadFile({
  90. url: 'https://reader-api.ai160.com/file/upload',
  91. filePath: localImage,
  92. name: '头像',
  93. header: {
  94. uid: wx.getStorageSync('uid')
  95. },
  96. success: (res) => {
  97. // console.log(JSON.parse(res.data));
  98. const newAvatar = JSON.parse(res.data).data;
  99. const str = 'user.avatar'
  100. _this.setData({
  101. [str]: newAvatar
  102. })
  103. }
  104. })
  105. }
  106. })
  107. },
  108. formSubmit: function(e) {
  109. if (e.detail.value.nickname === '') {
  110. wx.showToast({
  111. title: '请填写昵称',
  112. icon: "none"
  113. })
  114. return false
  115. }
  116. // const uid = wx.getStorageSync('uid');
  117. const data = {
  118. nickName: e.detail.value.nickname || '',
  119. gender: e.detail.value.radioGroup1,
  120. schoolName: e.detail.value.address || '',
  121. birthday: e.detail.value.picker || '',
  122. avatar: this.data.user.avatar,
  123. grade: this.data.gradeCode
  124. }
  125. httpRequestApi.settingUserInfo(data).success(res => {
  126. console.log(res.data.data); //修改资料成功后 调用 getUserInfo
  127. // wx.redirectTo({
  128. // url: '/pages/index/index?index=3'
  129. // });
  130. wx.setStorageSync('user', res.data.data);
  131. wx.setStorageSync('grade', res.data.data.grade);
  132. wx.navigateBack({
  133. delta: 1
  134. })
  135. });
  136. },
  137. bindGradeChange: function(e) {
  138. console.log('年级选择', e)
  139. this.setData({
  140. gradeIndex: e.detail.value,
  141. gradeCode: this.data.gradeObject[e.detail.value].code
  142. }, () => {
  143. console.log(this.data.gradeCode)
  144. })
  145. },
  146. /**
  147. * 生命周期函数--监听页面加载
  148. */
  149. getUserInfo: function() {
  150. },
  151. onLoad: function(option) {
  152. httpRequestApi.getUserInfo().success(res => {
  153. const user = res.data.data;
  154. let gradeIndex = 0;
  155. this.data.gradeObject.forEach((item, index) => {
  156. console.log('年级index', item)
  157. console.log('年级index', item)
  158. if (item.code === user.grade) {
  159. console.log('年级index', index)
  160. gradeIndex = index
  161. }
  162. console.log(user)
  163. const birthday = user.birthday ? formatDate(user.birthday, 2) : '2018-01-01';
  164. console.log(birthday)
  165. this.setData({
  166. user: user,
  167. date: birthday,
  168. sexs: [{
  169. value: 2,
  170. checked: user.gender === 2 ? true : false,
  171. sex: '女'
  172. },
  173. {
  174. value: 1,
  175. checked: user.gender === 1 ? true : false,
  176. sex: '男'
  177. }
  178. ],
  179. occupations: [{
  180. value: '家长',
  181. checked: user.profession === '家长' ? true : false
  182. },
  183. {
  184. value: '老师',
  185. checked: user.profession === '老师' ? true : false
  186. },
  187. {
  188. value: '学生',
  189. checked: user.profession === '学生' ? true : false
  190. }
  191. ],
  192. gradeIndex
  193. })
  194. })
  195. console.log(this.data.date)
  196. if (option.title) {
  197. wx.setNavigationBarTitle({
  198. title: option.title //页面标题为路由参数
  199. });
  200. this.setData({
  201. title: option.title
  202. });
  203. }
  204. httpRequestApi.userIntoPage('pages/user/myEdit/myEdit', '修改资料页面').success((res) => {
  205. })
  206. });
  207. },
  208. /**
  209. * 生命周期函数--监听页面初次渲染完成
  210. */
  211. onReady: function() {
  212. },
  213. /**
  214. * 生命周期函数--监听页面显示
  215. */
  216. onShow: function() {
  217. },
  218. /**
  219. * 生命周期函数--监听页面隐藏
  220. */
  221. onHide: function() {
  222. },
  223. /**
  224. * 生命周期函数--监听页面卸载
  225. */
  226. onUnload: function() {
  227. },
  228. /**
  229. * 页面相关事件处理函数--监听用户下拉动作
  230. */
  231. onPullDownRefresh: function() {
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom: function() {
  237. },
  238. /**
  239. * 用户点击右上角分享
  240. */
  241. onShareAppMessage() {
  242. return {
  243. title: '课文朗读,从未如此有趣。',
  244. path: `/pages/index/index?&uid=${wx.getStorageSync('uid')}`,
  245. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  246. }
  247. }
  248. })