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

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