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

488 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. this.setData({
  176. avatarUrl: uploadedFilePath,
  177. })
  178. // 更新缓存中的用户信息
  179. const cachedUserInfo = wx.getStorageSync('userInfo') || {}
  180. if (!cachedUserInfo.user) {
  181. cachedUserInfo.user = {}
  182. }
  183. cachedUserInfo.user.avatar = uploadedFilePath
  184. wx.setStorageSync('userInfo', cachedUserInfo)
  185. // 更新头像的API
  186. http.revise({
  187. data: {
  188. avatar: uploadedFilePath
  189. },
  190. success: (res) => {
  191. console.log('头像更新成功')
  192. wx.hideLoading()
  193. this.showToast('头像更新成功')
  194. // 4. 重新获取用户信息以确保数据同步
  195. setTimeout(() => {
  196. this.getUserInfo()
  197. }, 500)
  198. },
  199. fail: (err) => {
  200. console.error('头像更新失败:', err)
  201. wx.hideLoading()
  202. this.showToast('头像保存失败,请重试')
  203. }
  204. })
  205. } else {
  206. throw new Error('上传失败:返回数据格式错误')
  207. }
  208. },
  209. fail: (err) => {
  210. wx.hideLoading()
  211. console.error('上传失败:', err)
  212. this.showToast('上传失败,请检查网络')
  213. },
  214. complete: () => {
  215. this.setData({
  216. isUploadingAvatar: false
  217. })
  218. }
  219. })
  220. },
  221. // 编辑昵称
  222. editNickname() {
  223. this.setData({
  224. showNicknameModal: true,
  225. newNickname: this.data.userInfo.user?.nickName || this.data.displayNickName || ''
  226. })
  227. },
  228. hideNicknameModal() {
  229. this.setData({
  230. showNicknameModal: false
  231. })
  232. },
  233. onNicknameInput(e) {
  234. this.setData({
  235. newNickname: e.detail.value
  236. })
  237. },
  238. saveNickname() {
  239. const newNickname = this.data.newNickname.trim()
  240. if (!newNickname) {
  241. this.showToast('昵称不能为空')
  242. return
  243. }
  244. if (newNickname.length > 10) {
  245. this.showToast('昵称不能超过10个字符')
  246. return
  247. }
  248. // 如果昵称没有变化,直接关闭弹窗
  249. const currentNickName = this.data.userInfo.user?.nickName || this.data.displayNickName
  250. if (newNickname === currentNickName) {
  251. this.hideNicknameModal()
  252. return
  253. }
  254. this.setData({
  255. isUpdatingNickname: true
  256. })
  257. // 立即更新本地显示
  258. this.setData({
  259. 'userInfo.user.nickName': newNickname,
  260. displayNickName: newNickname,
  261. 'formData.nickName': newNickname
  262. })
  263. // 更新缓存
  264. const cachedUserInfo = wx.getStorageSync('userInfo') || {}
  265. if (!cachedUserInfo.user) {
  266. cachedUserInfo.user = {}
  267. }
  268. cachedUserInfo.user.nickName = newNickname
  269. wx.setStorageSync('userInfo', cachedUserInfo)
  270. // 更新到服务器
  271. http.revise({
  272. data: {
  273. nickName: newNickname
  274. },
  275. success: (res) => {
  276. console.log('昵称更新成功')
  277. this.setData({
  278. showNicknameModal: false,
  279. isUpdatingNickname: false,
  280. 'formData.nickName': null
  281. })
  282. this.showToast('昵称修改成功')
  283. // 重新获取用户信息以确保数据同步
  284. setTimeout(() => {
  285. this.getUserInfo()
  286. }, 500)
  287. },
  288. fail: (err) => {
  289. console.error('昵称更新失败:', err)
  290. this.setData({
  291. isUpdatingNickname: false
  292. })
  293. this.showToast('修改失败,请重试')
  294. }
  295. })
  296. },
  297. // 查看问诊消息
  298. goToConsultation() {
  299. wx.navigateTo({
  300. url: '/pagesA/pages/todayInquiry/todayInquiry'
  301. })
  302. },
  303. // 查看问答消息
  304. goToQA() {
  305. wx.navigateTo({
  306. url: '/pagesA/pages/chatting/chatting'
  307. })
  308. },
  309. // 实名认证
  310. goToAuth() {
  311. if (this.data.userInfo.authStatus == '已认证') {
  312. this.showToast('您已完成实名认证')
  313. } else {
  314. wx.navigateTo({
  315. url: '/pagesA/pages/attestation/attestation'
  316. })
  317. }
  318. },
  319. // 显示反馈弹窗
  320. showFeedback() {
  321. // 刷新反馈类型数据
  322. this.getfwpj()
  323. this.setData({
  324. showFeedbackModal: true,
  325. feedbackContent: '',
  326. canSubmit: false,
  327. isSubmitting: false,
  328. textareaFocus: true
  329. })
  330. },
  331. hideFeedback() {
  332. this.setData({
  333. showFeedbackModal: false,
  334. textareaFocus: false
  335. })
  336. },
  337. // 反馈内容输入
  338. onFeedbackInput(e) {
  339. const content = e.detail.value
  340. const canSubmit = content.trim().length > 0 && this.data.currentFeedbackType
  341. this.setData({
  342. feedbackContent: content,
  343. canSubmit
  344. })
  345. },
  346. // 提交反馈
  347. submitFeedback() {
  348. if (!this.data.canSubmit || this.data.isSubmitting) return
  349. const content = this.data.feedbackContent.trim()
  350. if (content.length < 5) {
  351. this.showToast('请填写详细的反馈内容')
  352. return
  353. }
  354. if (!this.data.currentFeedbackType) {
  355. this.showToast('请选择反馈类型')
  356. return
  357. }
  358. this.setData({
  359. isSubmitting: true
  360. })
  361. http.feedback({
  362. data: {
  363. content: content,
  364. feedbackType: this.data.currentFeedbackType // 添加反馈类型字段
  365. },
  366. success: res => {
  367. if (res.code == 200) {
  368. this.showToast('感谢您的反馈!')
  369. } else {
  370. this.showToast('反馈失败!')
  371. }
  372. this.setData({
  373. isSubmitting: false,
  374. showFeedbackModal: false,
  375. textareaFocus: false,
  376. feedbackContent: '',
  377. currentFeedbackType: this.data.feedbackTypes.length > 0 ? this.data.feedbackTypes[0].dictValue : '',
  378. selectedFeedbackTypeLabel: this.data.feedbackTypes.length > 0 ? this.data.feedbackTypes[0].dictLabel : ''
  379. })
  380. },
  381. fail: (err) => {
  382. console.error('反馈失败:', err)
  383. this.showToast('反馈失败,请重试')
  384. this.setData({
  385. isSubmitting: false
  386. })
  387. }
  388. })
  389. },
  390. // 退出登录相关
  391. showLogoutConfirm() {
  392. this.setData({
  393. showLogoutModal: true
  394. })
  395. },
  396. hideLogoutModal() {
  397. this.setData({
  398. showLogoutModal: false
  399. })
  400. },
  401. doLogout() {
  402. // 清除本地存储
  403. wx.clearStorageSync()
  404. // 跳转到登录页
  405. wx.reLaunch({
  406. url: '/pages/login/login'
  407. })
  408. this.showToast('已退出登录')
  409. },
  410. // 显示提示
  411. showToast(text) {
  412. this.setData({
  413. toastText: text,
  414. showToast: true
  415. })
  416. setTimeout(() => {
  417. this.setData({
  418. showToast: false
  419. })
  420. }, 2000)
  421. },
  422. // 下拉刷新
  423. onPullDownRefresh() {
  424. this.getUserInfo()
  425. this.gettoday() // 刷新消息数
  426. this.getfwpj() // 刷新反馈类型
  427. setTimeout(() => {
  428. wx.stopPullDownRefresh()
  429. this.showToast('刷新成功')
  430. }, 1000)
  431. }
  432. })