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

353 lines
7.5 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. // 服务评价
  41. showFeedback(){
  42. wx.navigateTo({
  43. url: '/pagesA/pages/serviceEvaluation/serviceEvaluation',
  44. })
  45. },
  46. onShow() {
  47. console.log('个人中心页面显示,重新获取用户信息')
  48. // 每次显示页面都从服务器获取最新数据
  49. this.getUserInfo()
  50. // 刷新消息数
  51. this.gettoday()
  52. },
  53. // 获取用户信息
  54. getUserInfo() {
  55. http.UserInfo({
  56. data: {},
  57. success: (res) => {
  58. console.log('获取用户信息成功', res)
  59. // 更新数据
  60. this.setData({
  61. userInfo: res.data,
  62. displayNickName: res.data.user.nickName,
  63. avatarUrl: baseUrl + res.data.user.avatar
  64. })
  65. // 更新缓存
  66. wx.setStorageSync('userInfo', res.data)
  67. },
  68. fail: (err) => {
  69. console.error('获取用户信息失败:', err)
  70. }
  71. })
  72. },
  73. // 问诊消息 - 获取今日消息数
  74. gettoday() {
  75. http.today({
  76. data: {},
  77. success: res => {
  78. if (res.rows && res.rows[0]) {
  79. const num = res.rows[0].totalTodayReplyCount || 0
  80. this.setData({
  81. totalToday: num
  82. })
  83. }
  84. },
  85. fail: err => {
  86. console.error('获取消息数失败', err)
  87. }
  88. })
  89. },
  90. // 选择头像
  91. onChooseAvatar(e) {
  92. console.log(7788, e);
  93. // 防止重复上传
  94. if (this.data.isUploadingAvatar) {
  95. this.showToast('正在上传中...')
  96. return
  97. }
  98. const {
  99. avatarUrl
  100. } = e.detail
  101. if (!avatarUrl) {
  102. this.showToast('选择头像失败')
  103. return
  104. }
  105. this.setData({
  106. isUploadingAvatar: true
  107. })
  108. wx.showLoading({
  109. title: '上传中...',
  110. mask: true
  111. })
  112. // 上传头像到服务器
  113. wx.uploadFile({
  114. url: baseUrl + '/common/upload',
  115. header: {
  116. 'Authorization': 'Bearer ' + wx.getStorageSync('token')
  117. },
  118. filePath: avatarUrl,
  119. name: 'file',
  120. success: (uploadRes) => {
  121. const result = JSON.parse(uploadRes.data)
  122. console.log('上传结果', result)
  123. if (result && result.fileName) {
  124. // 获取上传后的文件路径
  125. const uploadedFilePath = result.fileName
  126. this.setData({
  127. avatarUrl: uploadedFilePath,
  128. })
  129. // 更新头像的API
  130. http.revise({
  131. data: {
  132. avatar: uploadedFilePath
  133. },
  134. success: (res) => {
  135. console.log('头像更新成功')
  136. wx.hideLoading()
  137. this.showToast('头像更新成功')
  138. // 4. 重新获取用户信息以确保数据同步
  139. setTimeout(() => {
  140. this.getUserInfo()
  141. }, 500)
  142. },
  143. fail: (err) => {
  144. console.error('头像更新失败:', err)
  145. wx.hideLoading()
  146. this.showToast('头像保存失败,请重试')
  147. }
  148. })
  149. } else {
  150. throw new Error('上传失败:返回数据格式错误')
  151. }
  152. },
  153. fail: (err) => {
  154. wx.hideLoading()
  155. console.error('上传失败:', err)
  156. this.showToast('上传失败,请检查网络')
  157. },
  158. complete: () => {
  159. this.setData({
  160. isUploadingAvatar: false
  161. })
  162. }
  163. })
  164. },
  165. // 编辑昵称
  166. editNickname() {
  167. this.setData({
  168. showNicknameModal: true,
  169. newNickname: this.data.userInfo.user?.nickName || this.data.displayNickName || ''
  170. })
  171. },
  172. hideNicknameModal() {
  173. this.setData({
  174. showNicknameModal: false
  175. })
  176. },
  177. onNicknameInput(e) {
  178. this.setData({
  179. newNickname: e.detail.value
  180. })
  181. },
  182. saveNickname() {
  183. const newNickname = this.data.newNickname.trim()
  184. if (!newNickname) {
  185. this.showToast('昵称不能为空')
  186. return
  187. }
  188. if (newNickname.length > 10) {
  189. this.showToast('昵称不能超过10个字符')
  190. return
  191. }
  192. // 如果昵称没有变化,直接关闭弹窗
  193. const currentNickName = this.data.userInfo.user?.nickName || this.data.displayNickName
  194. if (newNickname === currentNickName) {
  195. this.hideNicknameModal()
  196. return
  197. }
  198. this.setData({
  199. isUpdatingNickname: true
  200. })
  201. // 立即更新本地显示
  202. this.setData({
  203. 'userInfo.user.nickName': newNickname,
  204. displayNickName: newNickname,
  205. 'formData.nickName': newNickname
  206. })
  207. // 更新缓存
  208. const cachedUserInfo = wx.getStorageSync('userInfo') || {}
  209. if (!cachedUserInfo.user) {
  210. cachedUserInfo.user = {}
  211. }
  212. cachedUserInfo.user.nickName = newNickname
  213. wx.setStorageSync('userInfo', cachedUserInfo)
  214. // 更新到服务器
  215. http.revise({
  216. data: {
  217. nickName: newNickname
  218. },
  219. success: (res) => {
  220. console.log('昵称更新成功')
  221. this.setData({
  222. showNicknameModal: false,
  223. isUpdatingNickname: false,
  224. 'formData.nickName': null
  225. })
  226. this.showToast('昵称修改成功')
  227. // 重新获取用户信息以确保数据同步
  228. setTimeout(() => {
  229. this.getUserInfo()
  230. }, 500)
  231. },
  232. fail: (err) => {
  233. console.error('昵称更新失败:', err)
  234. this.setData({
  235. isUpdatingNickname: false
  236. })
  237. this.showToast('修改失败,请重试')
  238. }
  239. })
  240. },
  241. // 查看问诊消息
  242. goToConsultation() {
  243. wx.navigateTo({
  244. url: '/pagesA/pages/todayInquiry/todayInquiry'
  245. })
  246. },
  247. // 查看问答消息
  248. goToQA() {
  249. wx.navigateTo({
  250. url: '' // 请填写实际的问答消息页面路径
  251. })
  252. },
  253. // 实名认证
  254. goToAuth() {
  255. if (this.data.userInfo.authStatus == '已认证') {
  256. this.showToast('您已完成实名认证')
  257. } else {
  258. wx.navigateTo({
  259. url: '/pagesA/pages/attestation/attestation'
  260. })
  261. }
  262. },
  263. // 退出登录相关
  264. showLogoutConfirm() {
  265. this.setData({
  266. showLogoutModal: true
  267. })
  268. },
  269. hideLogoutModal() {
  270. this.setData({
  271. showLogoutModal: false
  272. })
  273. },
  274. doLogout() {
  275. // 清除本地存储
  276. wx.clearStorageSync()
  277. // 跳转到登录页
  278. wx.reLaunch({
  279. url: '/pages/login/login'
  280. })
  281. this.showToast('已退出登录')
  282. },
  283. // 显示提示
  284. showToast(text) {
  285. this.setData({
  286. toastText: text,
  287. showToast: true
  288. })
  289. setTimeout(() => {
  290. this.setData({
  291. showToast: false
  292. })
  293. }, 2000)
  294. },
  295. // 下拉刷新
  296. onPullDownRefresh() {
  297. this.getUserInfo()
  298. this.gettoday() // 刷新消息数
  299. setTimeout(() => {
  300. wx.stopPullDownRefresh()
  301. this.showToast('刷新成功')
  302. }, 1000)
  303. },
  304. })