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

355 lines
7.7 KiB

  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. showLogoutConfirm() {
  266. this.setData({
  267. showLogoutModal: true
  268. })
  269. },
  270. hideLogoutModal() {
  271. this.setData({
  272. showLogoutModal: false
  273. })
  274. },
  275. doLogout() {
  276. // 清除本地存储
  277. wx.clearStorageSync()
  278. // 跳转到登录页
  279. wx.reLaunch({
  280. url: '/pages/login/login'
  281. })
  282. this.showToast('已退出登录')
  283. },
  284. // 显示提示
  285. showToast(text) {
  286. this.setData({
  287. toastText: text,
  288. showToast: true
  289. })
  290. setTimeout(() => {
  291. this.setData({
  292. showToast: false
  293. })
  294. }, 2000)
  295. },
  296. // 下拉刷新
  297. onPullDownRefresh() {
  298. this.getUserInfo()
  299. this.gettoday() // 刷新消息数
  300. setTimeout(() => {
  301. wx.stopPullDownRefresh()
  302. this.showToast('刷新成功')
  303. }, 1000)
  304. },
  305. })