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

420 lines
9.1 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. import http from '../../utils/api'
  2. const baseUrl = require('../../utils/baseUrl')
  3. Page({
  4. data: {
  5. // 用户信息
  6. avatarUrl: '', // 原始头像路径(相对路径)
  7. avatarFullUrl: '', // 完整的头像URL(用于显示)
  8. userInfo: {
  9. user: {}
  10. },
  11. baseUrl: baseUrl,
  12. displayNickName: '', // 显示用的昵称
  13. // 弹窗状态
  14. showFeedbackModal: false,
  15. showNicknameModal: false,
  16. showLogoutModal: false,
  17. showToast: false,
  18. // 反馈相关
  19. feedbackContent: '',
  20. canSubmit: false,
  21. isSubmitting: false,
  22. // 编辑相关
  23. newNickname: '',
  24. // 提示信息
  25. toastText: '',
  26. // 表单数据
  27. formData: {
  28. avatar: null,
  29. nickName: null
  30. },
  31. // 上传状态
  32. isUploadingAvatar: false,
  33. isUpdatingNickname: false,
  34. // 消息数字
  35. totalToday: 0
  36. },
  37. onLoad() {
  38. this.gettoday()
  39. },
  40. onShow() {
  41. console.log('个人中心页面显示,重新获取用户信息')
  42. // 每次显示页面都从服务器获取最新数据
  43. this.getUserInfo()
  44. // 刷新消息数
  45. this.gettoday()
  46. },
  47. // 获取用户信息
  48. getUserInfo() {
  49. http.UserInfo({
  50. data: {},
  51. success: (res) => {
  52. console.log('获取用户信息成功', res)
  53. // 更新数据
  54. this.setData({
  55. userInfo: res.data,
  56. displayNickName: res.data.user.nickName,
  57. avatarUrl: baseUrl + res.data.user.avatar
  58. })
  59. // 更新缓存
  60. wx.setStorageSync('userInfo', res.data)
  61. },
  62. fail: (err) => {
  63. console.error('获取用户信息失败:', err)
  64. }
  65. })
  66. },
  67. // 问诊消息 - 获取今日消息数
  68. gettoday() {
  69. http.today({
  70. data: {},
  71. success: res => {
  72. if (res.rows && res.rows[0]) {
  73. const num = res.rows[0].totalTodayReplyCount || 0
  74. this.setData({
  75. totalToday: num
  76. })
  77. }
  78. },
  79. fail: err => {
  80. console.error('获取消息数失败', err)
  81. }
  82. })
  83. },
  84. // 选择头像
  85. onChooseAvatar(e) {
  86. console.log(7788, e);
  87. // 防止重复上传
  88. if (this.data.isUploadingAvatar) {
  89. this.showToast('正在上传中...')
  90. return
  91. }
  92. const {
  93. avatarUrl
  94. } = e.detail
  95. if (!avatarUrl) {
  96. this.showToast('选择头像失败')
  97. return
  98. }
  99. this.setData({
  100. isUploadingAvatar: true
  101. })
  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. const result = JSON.parse(uploadRes.data)
  116. console.log('上传结果', result)
  117. if (result && result.fileName) {
  118. // 获取上传后的文件路径
  119. const uploadedFilePath = result.fileName
  120. this.setData({
  121. avatarUrl: uploadedFilePath,
  122. })
  123. // 更新缓存中的用户信息
  124. const cachedUserInfo = wx.getStorageSync('userInfo') || {}
  125. if (!cachedUserInfo.user) {
  126. cachedUserInfo.user = {}
  127. }
  128. cachedUserInfo.user.avatar = uploadedFilePath
  129. wx.setStorageSync('userInfo', cachedUserInfo)
  130. // 更新头像的API
  131. http.revise({
  132. data: {
  133. avatar: uploadedFilePath
  134. },
  135. success: (res) => {
  136. console.log('头像更新成功')
  137. wx.hideLoading()
  138. this.showToast('头像更新成功')
  139. // 4. 重新获取用户信息以确保数据同步
  140. setTimeout(() => {
  141. this.getUserInfo()
  142. }, 500)
  143. },
  144. fail: (err) => {
  145. console.error('头像更新失败:', err)
  146. wx.hideLoading()
  147. this.showToast('头像保存失败,请重试')
  148. }
  149. })
  150. } else {
  151. throw new Error('上传失败:返回数据格式错误')
  152. }
  153. },
  154. fail: (err) => {
  155. wx.hideLoading()
  156. console.error('上传失败:', err)
  157. this.showToast('上传失败,请检查网络')
  158. },
  159. complete: () => {
  160. this.setData({
  161. isUploadingAvatar: false
  162. })
  163. }
  164. })
  165. },
  166. // 编辑昵称
  167. editNickname() {
  168. this.setData({
  169. showNicknameModal: true,
  170. newNickname: this.data.userInfo.user?.nickName || this.data.displayNickName || ''
  171. })
  172. },
  173. hideNicknameModal() {
  174. this.setData({
  175. showNicknameModal: false
  176. })
  177. },
  178. onNicknameInput(e) {
  179. this.setData({
  180. newNickname: e.detail.value
  181. })
  182. },
  183. saveNickname() {
  184. const newNickname = this.data.newNickname.trim()
  185. if (!newNickname) {
  186. this.showToast('昵称不能为空')
  187. return
  188. }
  189. if (newNickname.length > 10) {
  190. this.showToast('昵称不能超过10个字符')
  191. return
  192. }
  193. // 如果昵称没有变化,直接关闭弹窗
  194. const currentNickName = this.data.userInfo.user?.nickName || this.data.displayNickName
  195. if (newNickname === currentNickName) {
  196. this.hideNicknameModal()
  197. return
  198. }
  199. this.setData({
  200. isUpdatingNickname: true
  201. })
  202. // 立即更新本地显示
  203. this.setData({
  204. 'userInfo.user.nickName': newNickname,
  205. displayNickName: newNickname,
  206. 'formData.nickName': newNickname
  207. })
  208. // 更新缓存
  209. const cachedUserInfo = wx.getStorageSync('userInfo') || {}
  210. if (!cachedUserInfo.user) {
  211. cachedUserInfo.user = {}
  212. }
  213. cachedUserInfo.user.nickName = newNickname
  214. wx.setStorageSync('userInfo', cachedUserInfo)
  215. // 更新到服务器
  216. http.revise({
  217. data: {
  218. nickName: newNickname
  219. },
  220. success: (res) => {
  221. console.log('昵称更新成功')
  222. this.setData({
  223. showNicknameModal: false,
  224. isUpdatingNickname: false,
  225. 'formData.nickName': null
  226. })
  227. this.showToast('昵称修改成功')
  228. // 重新获取用户信息以确保数据同步
  229. setTimeout(() => {
  230. this.getUserInfo()
  231. }, 500)
  232. },
  233. fail: (err) => {
  234. console.error('昵称更新失败:', err)
  235. this.setData({
  236. isUpdatingNickname: false
  237. })
  238. this.showToast('修改失败,请重试')
  239. }
  240. })
  241. },
  242. // 查看问诊消息
  243. goToConsultation() {
  244. wx.navigateTo({
  245. url: '/pagesA/pages/todayInquiry/todayInquiry'
  246. })
  247. },
  248. // 查看问答消息
  249. goToQA() {
  250. wx.navigateTo({
  251. url: '' // 请填写实际的问答消息页面路径
  252. })
  253. },
  254. // 实名认证
  255. goToAuth() {
  256. if (this.data.userInfo.authStatus == '已认证') {
  257. this.showToast('您已完成实名认证')
  258. } else {
  259. wx.navigateTo({
  260. url: '/pagesA/pages/attestation/attestation'
  261. })
  262. }
  263. },
  264. // 显示反馈弹窗
  265. showFeedback() {
  266. this.setData({
  267. showFeedbackModal: true,
  268. feedbackContent: '',
  269. canSubmit: false,
  270. isSubmitting: false,
  271. textareaFocus: true
  272. })
  273. },
  274. hideFeedback() {
  275. this.setData({
  276. showFeedbackModal: false,
  277. textareaFocus: false
  278. })
  279. },
  280. // 反馈内容输入
  281. onFeedbackInput(e) {
  282. const content = e.detail.value
  283. const canSubmit = content.trim().length > 0
  284. this.setData({
  285. feedbackContent: content,
  286. canSubmit
  287. })
  288. },
  289. // 提交反馈
  290. submitFeedback() {
  291. if (!this.data.canSubmit || this.data.isSubmitting) return
  292. const content = this.data.feedbackContent.trim()
  293. if (content.length < 5) {
  294. this.showToast('请填写详细的反馈内容')
  295. return
  296. }
  297. this.setData({
  298. isSubmitting: true
  299. })
  300. http.feedback({
  301. data: {
  302. content: content
  303. },
  304. success: res => {
  305. if (res.code == 200) {
  306. this.showToast('感谢您的反馈!')
  307. } else {
  308. this.showToast('反馈失败!')
  309. }
  310. this.setData({
  311. isSubmitting: false,
  312. showFeedbackModal: false,
  313. textareaFocus: false
  314. })
  315. },
  316. fail: (err) => {
  317. console.error('反馈失败:', err)
  318. this.showToast('反馈失败,请重试')
  319. this.setData({
  320. isSubmitting: false
  321. })
  322. }
  323. })
  324. },
  325. // 退出登录相关
  326. showLogoutConfirm() {
  327. this.setData({
  328. showLogoutModal: true
  329. })
  330. },
  331. hideLogoutModal() {
  332. this.setData({
  333. showLogoutModal: false
  334. })
  335. },
  336. doLogout() {
  337. // 清除本地存储
  338. wx.clearStorageSync()
  339. // 跳转到登录页
  340. wx.reLaunch({
  341. url: '/pages/login/login'
  342. })
  343. this.showToast('已退出登录')
  344. },
  345. // 显示提示
  346. showToast(text) {
  347. this.setData({
  348. toastText: text,
  349. showToast: true
  350. })
  351. setTimeout(() => {
  352. this.setData({
  353. showToast: false
  354. })
  355. }, 2000)
  356. },
  357. // 下拉刷新
  358. onPullDownRefresh() {
  359. this.getUserInfo()
  360. this.gettoday() // 刷新消息数
  361. setTimeout(() => {
  362. wx.stopPullDownRefresh()
  363. this.showToast('刷新成功')
  364. }, 1000)
  365. },
  366. })