与牧同行-小程序用户端
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.

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