与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
7.6 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. import http from '../../utils/api'
  2. const baseUrl = require('../../utils/baseUrl')
  3. Page({
  4. data: {
  5. // 用户信息
  6. avatarUrl: '',
  7. userInfo: {
  8. user: {}
  9. },
  10. baseUrl: baseUrl,
  11. // 弹窗状态
  12. showFeedbackModal: false,
  13. showNicknameModal: false,
  14. showLogoutModal: false,
  15. showToast: false,
  16. // 反馈相关
  17. feedbackContent: '',
  18. canSubmit: false,
  19. isSubmitting: false,
  20. // 编辑相关
  21. newNickname: '',
  22. // 提示信息
  23. toastText: '',
  24. // 表单数据
  25. formData: {
  26. avatar: null,
  27. nickName: null
  28. },
  29. // 上传状态
  30. isUploadingAvatar: false,
  31. isUpdatingNickname: false
  32. },
  33. onLoad() {
  34. this.getUserInfo()
  35. },
  36. onShow() {
  37. this.getUserInfo()
  38. },
  39. // 获取用户信息
  40. getUserInfo() {
  41. http.UserInfo({
  42. data: {},
  43. success: (res) => {
  44. if (res.data && res.data.user) {
  45. wx.setStorageSync('userInfo', res.data)
  46. this.setData({
  47. userInfo: res.data,
  48. avatarUrl: res.data.user?.avatar ? baseUrl + res.data.user.avatar : ''
  49. })
  50. }
  51. },
  52. fail: (err) => {
  53. console.error('获取用户信息失败:', err)
  54. // 使用缓存的用户信息
  55. const cachedUserInfo = wx.getStorageSync('userInfo')
  56. if (cachedUserInfo) {
  57. this.setData({
  58. userInfo: cachedUserInfo,
  59. avatarUrl: cachedUserInfo.user?.avatar ? baseUrl + cachedUserInfo.user.avatar : ''
  60. })
  61. }
  62. }
  63. })
  64. },
  65. // 更新用户信息到服务器
  66. updateUserInfo() {
  67. const formData = this.data.formData
  68. // 过滤掉空值
  69. const dataToUpdate = {}
  70. if (formData.avatar) dataToUpdate.avatar = formData.avatar
  71. if (formData.nickName) dataToUpdate.nickName = formData.nickName
  72. if (Object.keys(dataToUpdate).length === 0) return
  73. http.revise({
  74. data: dataToUpdate,
  75. success: (res) => {
  76. // 清空表单数据
  77. this.setData({
  78. 'formData.avatar': null,
  79. 'formData.nickName': null
  80. })
  81. // 立即刷新用户信息
  82. this.getUserInfo()
  83. },
  84. fail: (err) => {
  85. console.error('更新失败:', err)
  86. this.showToast('更新失败,请重试')
  87. }
  88. })
  89. },
  90. // 选择头像
  91. onChooseAvatar(e) {
  92. if (this.data.isUploadingAvatar) {
  93. this.showToast('正在上传中...')
  94. return
  95. }
  96. const { avatarUrl } = e.detail
  97. if (!avatarUrl) {
  98. this.showToast('选择头像失败')
  99. return
  100. }
  101. this.setData({ isUploadingAvatar: true })
  102. wx.showLoading({
  103. title: '上传中...',
  104. mask: true
  105. })
  106. // 上传头像到服务器
  107. wx.uploadFile({
  108. url: baseUrl + '/common/upload',
  109. header: {
  110. 'Authorization': 'Bearer ' + wx.getStorageSync('token')
  111. },
  112. filePath: avatarUrl,
  113. name: 'file',
  114. success: (uploadRes) => {
  115. wx.hideLoading()
  116. try {
  117. const result = JSON.parse(uploadRes.data)
  118. if (result && result.fileName) {
  119. // 立即更新本地显示
  120. this.setData({
  121. avatarUrl: avatarUrl,
  122. 'formData.avatar': result.fileName,
  123. 'userInfo.user.avatar': result.fileName
  124. })
  125. // 更新到服务器
  126. this.updateUserInfo()
  127. this.showToast('头像更新成功')
  128. } else {
  129. throw new Error('上传失败')
  130. }
  131. } catch (error) {
  132. console.error('解析上传结果失败:', error)
  133. this.showToast('上传失败,请重试')
  134. }
  135. },
  136. fail: (err) => {
  137. wx.hideLoading()
  138. console.error('上传失败:', err)
  139. this.showToast('上传失败,请检查网络')
  140. },
  141. complete: () => {
  142. this.setData({ isUploadingAvatar: false })
  143. }
  144. })
  145. },
  146. // 编辑昵称
  147. editNickname() {
  148. this.setData({
  149. showNicknameModal: true,
  150. newNickname: this.data.userInfo.user?.nickName || ''
  151. })
  152. },
  153. hideNicknameModal() {
  154. this.setData({ showNicknameModal: false })
  155. },
  156. onNicknameInput(e) {
  157. this.setData({ newNickname: e.detail.value })
  158. },
  159. saveNickname() {
  160. const newNickname = this.data.newNickname.trim()
  161. if (!newNickname) {
  162. this.showToast('昵称不能为空')
  163. return
  164. }
  165. if (newNickname.length > 10) {
  166. this.showToast('昵称不能超过10个字符')
  167. return
  168. }
  169. // 如果昵称没有变化,直接关闭弹窗
  170. if (newNickname === this.data.userInfo.user?.nickName) {
  171. this.hideNicknameModal()
  172. return
  173. }
  174. this.setData({ isUpdatingNickname: true })
  175. // 立即更新本地显示
  176. this.setData({
  177. 'userInfo.user.nickName': newNickname,
  178. 'formData.nickName': newNickname
  179. })
  180. // 更新到服务器
  181. this.updateUserInfo()
  182. this.setData({
  183. showNicknameModal: false,
  184. isUpdatingNickname: false
  185. })
  186. this.showToast('昵称修改成功')
  187. },
  188. // 查看问诊消息
  189. goToConsultation() {
  190. wx.navigateTo({
  191. url: ''
  192. })
  193. },
  194. // 查看问答消息
  195. goToQA() {
  196. wx.navigateTo({
  197. url: ''
  198. })
  199. },
  200. // 实名认证
  201. goToAuth() {
  202. if (this.data.userInfo.authStatus) {
  203. this.showToast('您已完成实名认证')
  204. } else {
  205. wx.navigateTo({
  206. url: '/pagesA/pages/attestation/attestation'
  207. })
  208. }
  209. },
  210. // 显示反馈弹窗
  211. showFeedback() {
  212. this.setData({
  213. showFeedbackModal: true,
  214. feedbackContent: '',
  215. canSubmit: false,
  216. isSubmitting: false
  217. })
  218. },
  219. hideFeedback() {
  220. this.setData({ showFeedbackModal: false })
  221. },
  222. // 反馈内容输入
  223. onFeedbackInput(e) {
  224. const content = e.detail.value
  225. const canSubmit = content.trim().length > 0
  226. this.setData({
  227. feedbackContent: content,
  228. canSubmit
  229. })
  230. },
  231. // 提交反馈
  232. submitFeedback() {
  233. if (!this.data.canSubmit || this.data.isSubmitting) return
  234. const content = this.data.feedbackContent.trim()
  235. if (content.length < 5) {
  236. this.showToast('请填写详细的反馈内容')
  237. return
  238. }
  239. this.setData({ isSubmitting: true })
  240. // 提交
  241. setTimeout(() => {
  242. console.log('提交反馈:', content)
  243. http.feedback({
  244. data:{
  245. content:content
  246. },
  247. success:res=>{
  248. if(res.code==200){
  249. this.showToast('感谢您的反馈!')
  250. }else{
  251. this.showToast('反馈失败!')
  252. }
  253. this.setData({
  254. isSubmitting: false,
  255. showFeedbackModal: false
  256. })
  257. }
  258. })
  259. }, 1500)
  260. },
  261. // 退出登录相关
  262. showLogoutConfirm() {
  263. this.setData({ showLogoutModal: true })
  264. },
  265. hideLogoutModal() {
  266. this.setData({ showLogoutModal: false })
  267. },
  268. doLogout() {
  269. // 清除本地存储
  270. wx.clearStorageSync()
  271. // 跳转到登录页
  272. wx.reLaunch({
  273. url: '/pages/login/login'
  274. })
  275. this.showToast('已退出登录')
  276. },
  277. // 显示提示
  278. showToast(text) {
  279. this.setData({
  280. toastText: text,
  281. showToast: true
  282. })
  283. setTimeout(() => {
  284. this.setData({ showToast: false })
  285. }, 2000)
  286. },
  287. // 下拉刷新
  288. onPullDownRefresh() {
  289. this.getUserInfo()
  290. setTimeout(() => {
  291. wx.stopPullDownRefresh()
  292. this.showToast('刷新成功')
  293. }, 1000)
  294. },
  295. // 分享
  296. onShareAppMessage() {
  297. return {
  298. title: '健康守护 - 您的个人健康中心',
  299. path: '/pages/personal-center/index'
  300. }
  301. }
  302. })