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

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