profile.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <template>
  2. <view class="container">
  3. <!-- ===== 未登录状态 ===== -->
  4. <view v-if="!isLoggedIn" class="login-prompt">
  5. <view class="prompt-icon">👤</view>
  6. <text class="prompt-title">登录浠艾福</text>
  7. <text class="prompt-desc">登录后可查看个人资料、积分记录等</text>
  8. <button class="login-btn" @click="goLogin">登录 / 注册</button>
  9. </view>
  10. <!-- ===== 已登录状态 ===== -->
  11. <template v-else>
  12. <!-- 退出切换按钮(仅家长切换到孩子状态时显示) -->
  13. <view class="exit-switch" v-if="isSwitchedChild" @click="exitSwitch">
  14. <text>🔄 退出切换</text>
  15. </view>
  16. <view class="user-card">
  17. <view class="avatar">👤</view>
  18. <view class="user-info">
  19. <view class="nickname">{{ nickname || '未设置昵称' }}</view>
  20. <view class="role">{{ role === 'parent' ? '家长模式' : (role === 'child' ? '孩子模式' : '成长规划师模式') }}</view>
  21. <view class="system-points" v-if="role === 'parent' && children.length > 0">🏅 系统积分: {{ children[0].systemPoints || 0 }}</view>
  22. </view>
  23. <!-- 切换按钮仅在非切换状态下显示 -->
  24. <button class="btn-switch" v-if="!isSwitchedChild" @click="switchMode">切换</button>
  25. </view>
  26. <view class="menu-list">
  27. <!-- 修改个人资料:所有角色可见 -->
  28. <view class="menu-item" @click="goToEditInfo">
  29. <text>📝 修改个人资料</text>
  30. <text class="arrow">›</text>
  31. </view>
  32. <!-- 我的孩子:仅家长可见 -->
  33. <view class="menu-item" v-if="role === 'parent'" @click="goToChildren">
  34. <text>👶 我的孩子</text>
  35. <text class="arrow">›</text>
  36. </view>
  37. <!-- 家庭成员 -->
  38. <view class="menu-item" v-if="role === 'parent'" @click="goToFamilyMembers">
  39. <text>👨‍👩‍👧‍👦 家庭成员</text>
  40. <text class="arrow">›</text>
  41. </view>
  42. <view class="menu-item" @click="goToPointsLogs">
  43. <text>📊 积分记录</text>
  44. <text class="arrow">›</text>
  45. </view>
  46. <!-- 设置/重置密码:仅家长和成长规划师可见,孩子不可见 -->
  47. <view class="menu-item" v-if="role === 'parent'" @click="showPasswordModal = true">
  48. <text>🔐 {{ hasPassword ? '重置密码' : '设置密码' }}</text>
  49. <text class="arrow">›</text>
  50. </view>
  51. <button class="menu-item menu-item-btn" open-type="share" @click="inviteFamily" v-if="role === 'parent'">
  52. <text>👨‍👩‍👧‍👦 邀请家人</text>
  53. <text class="arrow">›</text>
  54. </button>
  55. <button class="menu-item menu-item-btn" open-type="share" @click="inviteTeacher" v-if="role === 'parent'">
  56. <text>👨‍🏫 邀请规划师</text>
  57. <text class="arrow">›</text>
  58. </button>
  59. <!-- 会员中心(浠艾福俱乐部) -->
  60. <view class="menu-item" @click="goToMembership">
  61. <text>👑 会员中心</text>
  62. <text class="arrow">›</text>
  63. </view>
  64. <!-- 服务商中心(仅已通过服务商可见) -->
  65. <view class="menu-item" v-if="isVendor" @click="goToVendorCenter">
  66. <text>🏪 服务商中心</text>
  67. <text class="arrow">›</text>
  68. </view>
  69. <view class="menu-item" @click="goToPromotion">
  70. <text>📢 推广中心</text>
  71. <text class="arrow">›</text>
  72. </view>
  73. <view class="menu-item" @click="logout">
  74. <text>🚪 退出登录</text>
  75. <text class="arrow">›</text>
  76. </view>
  77. </view>
  78. <!-- 设置/重置密码弹窗 -->
  79. <view class="modal-mask" v-if="showPasswordModal" @click="showPasswordModal = false">
  80. <view class="modal" @click.stop>
  81. <view class="modal-title">{{ hasPassword ? '重置密码' : '设置密码' }}</view>
  82. <view class="form-item" v-if="hasPassword">
  83. <input v-model="oldPassword" type="number" password placeholder="输入旧密码" maxlength="6" />
  84. </view>
  85. <view class="form-item">
  86. <input v-model="newPassword" type="number" password placeholder="输入新密码" maxlength="6" />
  87. </view>
  88. <view class="form-item">
  89. <input v-model="confirmPassword" type="number" password placeholder="确认新密码" maxlength="6" />
  90. </view>
  91. <view class="modal-btns">
  92. <button @click="showPasswordModal = false">取消</button>
  93. <button class="btn-primary" @click="savePassword">确定</button>
  94. </view>
  95. </view>
  96. </view>
  97. </template>
  98. </view>
  99. </template>
  100. <script>
  101. import { setPassword, verifyPassword, getChildren, vendorStatus } from '../../utils/api.js'
  102. export default {
  103. data() {
  104. return {
  105. nickname: '',
  106. role: 'parent',
  107. hasPassword: false,
  108. showPasswordModal: false,
  109. oldPassword: '',
  110. newPassword: '',
  111. confirmPassword: '',
  112. children: [],
  113. isSwitchedChild: false,
  114. shareData: null,
  115. isVendor: false
  116. }
  117. },
  118. computed: {
  119. isLoggedIn() {
  120. return !!uni.getStorageSync('token')
  121. }
  122. },
  123. onShareAppMessage() {
  124. if (this.shareData) {
  125. return {
  126. title: this.shareData.title,
  127. path: this.shareData.path,
  128. imageUrl: '/static/invite-card.png'
  129. }
  130. }
  131. },
  132. onShow() {
  133. if (!uni.getStorageSync('token')) return
  134. const currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  135. if (currentRole === 'teacher') {
  136. uni.redirectTo({ url: '/pages/teacher/teacher-profile' })
  137. return
  138. }
  139. var userInfo = uni.getStorageSync('userInfo')
  140. this.nickname = this.$store.state.nickname || (userInfo && userInfo.nickname) || ''
  141. // 使用 currentRole(视图角色),而非 role(实际角色)
  142. this.role = currentRole
  143. this.hasPassword = !!uni.getStorageSync('passwordSet')
  144. // 获取切换标记
  145. this.isSwitchedChild = uni.getStorageSync('isSwitchedChild') || false
  146. this.loadChildren()
  147. this.checkVendorStatus()
  148. },
  149. methods: {
  150. goLogin() {
  151. uni.navigateTo({ url: '/pages/login/login' })
  152. },
  153. async loadChildren() {
  154. try {
  155. const res = await getChildren()
  156. this.children = res.data || []
  157. } catch (e) {
  158. console.error('获取孩子列表失败', e)
  159. }
  160. },
  161. goToEditInfo() {
  162. uni.navigateTo({ url: '/pages/user-edit/user-edit' })
  163. },
  164. goToChildren() {
  165. uni.navigateTo({ url: '/pages/profile/children' })
  166. },
  167. goToFamilyMembers() {
  168. uni.navigateTo({ url: '/pages/profile/family-members' })
  169. },
  170. goToPointsLogs() {
  171. uni.navigateTo({ url: '/pages/points/points' })
  172. },
  173. switchMode() {
  174. if (this.role === 'child') {
  175. this.verifyPasswordForSwitch(() => {
  176. this.doSwitchMode()
  177. })
  178. } else {
  179. this.doSwitchMode()
  180. }
  181. },
  182. doSwitchMode() {
  183. const newRole = this.role === 'parent' ? 'child' : 'parent'
  184. if (newRole === 'child') {
  185. if (this.children.length === 0) {
  186. this.loadChildren()
  187. }
  188. if (this.children.length === 0) {
  189. uni.showModal({
  190. title: '提示',
  191. content: '您还没有添加孩子,是否现在去添加?',
  192. success: (res) => {
  193. if (res.confirm) {
  194. uni.navigateTo({ url: '/pages/profile/create-child' })
  195. }
  196. }
  197. })
  198. return
  199. }
  200. if (this.children.length === 1) {
  201. this.$store.commit('switchToChild', this.children[0].id)
  202. this.role = newRole
  203. uni.showToast({ title: '已切换为孩子模式', icon: 'success' })
  204. return
  205. }
  206. uni.showActionSheet({
  207. itemList: this.children.map(c => c.nickname),
  208. success: (res) => {
  209. const selectedChild = this.children[res.tapIndex]
  210. this.$store.commit('switchToChild', selectedChild.id)
  211. this.role = newRole
  212. uni.showToast({ title: `已切换为${selectedChild.nickname}模式`, icon: 'success' })
  213. }
  214. })
  215. return
  216. }
  217. this.$store.commit('switchBackToParent')
  218. this.role = newRole
  219. uni.showToast({ title: `已切换为${newRole === 'parent' ? '家长' : '孩子'}模式`, icon: 'success' })
  220. },
  221. verifyPasswordForSwitch(callback) {
  222. uni.showModal({
  223. title: '验证密码',
  224. content: '请输入家长密码',
  225. editable: true,
  226. success: async (res) => {
  227. if (res.confirm && res.content) {
  228. try {
  229. const result = await verifyPassword(res.content)
  230. if (result.code === 200) {
  231. callback && callback()
  232. } else {
  233. uni.showToast({ title: '密码错误', icon: 'none' })
  234. }
  235. } catch (e) {
  236. uni.showToast({ title: '验证失败', icon: 'none' })
  237. }
  238. }
  239. }
  240. })
  241. },
  242. async savePassword() {
  243. if (this.newPassword.length !== 6) {
  244. uni.showToast({ title: '请输入6位密码', icon: 'none' })
  245. return
  246. }
  247. if (this.newPassword !== this.confirmPassword) {
  248. uni.showToast({ title: '两次密码不一致', icon: 'none' })
  249. return
  250. }
  251. if (this.hasPassword && this.oldPassword.length !== 6) {
  252. uni.showToast({ title: '请输入旧密码', icon: 'none' })
  253. return
  254. }
  255. try {
  256. if (this.hasPassword) {
  257. const verifyRes = await verifyPassword(this.oldPassword)
  258. if (verifyRes.code !== 200) {
  259. uni.showToast({ title: '旧密码错误', icon: 'none' })
  260. return
  261. }
  262. }
  263. await setPassword(this.newPassword)
  264. uni.setStorageSync('passwordSet', true)
  265. this.hasPassword = true
  266. uni.showToast({ title: '密码设置成功', icon: 'success' })
  267. this.showPasswordModal = false
  268. this.oldPassword = ''
  269. this.newPassword = ''
  270. this.confirmPassword = ''
  271. } catch (e) {
  272. console.error('设置密码失败', e)
  273. }
  274. },
  275. logout() {
  276. uni.showModal({
  277. title: '确认退出',
  278. content: '确定要退出登录吗?',
  279. success: (res) => {
  280. if (res.confirm) {
  281. this.$store.commit('logout')
  282. uni.reLaunch({ url: '/pages/login/login' })
  283. }
  284. }
  285. })
  286. },
  287. inviteFamily() {
  288. const userInfo = uni.getStorageSync('userInfo')
  289. const familyId = uni.getStorageSync('familyId')
  290. if (!familyId) {
  291. uni.showToast({ title: '您暂未加入家庭', icon: 'none' })
  292. return
  293. }
  294. this.inviteFamilyGenerate(familyId, userInfo)
  295. },
  296. async inviteFamilyGenerate(familyId, userInfo) {
  297. try {
  298. const { generateInviteCard } = require('../../utils/api.js')
  299. const res = await generateInviteCard('family', familyId)
  300. const code = res.data.code
  301. this.shareData = {
  302. title: (userInfo && userInfo.nickname ? userInfo.nickname : '家人') + ' 邀请你加入家庭',
  303. path: '/pages/login/login?invite_code=' + code
  304. }
  305. uni.showToast({ title: '点击右上角转发给TA', icon: 'none' })
  306. } catch (e) {
  307. uni.showToast({ title: e.message || '生成邀请失败', icon: 'none' })
  308. }
  309. },
  310. inviteTeacher() {
  311. const userInfo = uni.getStorageSync('userInfo')
  312. const familyId = uni.getStorageSync('familyId')
  313. if (!familyId) {
  314. uni.showToast({ title: '您暂未加入家庭', icon: 'none' })
  315. return
  316. }
  317. this.inviteTeacherGenerate(familyId, userInfo)
  318. },
  319. async inviteTeacherGenerate(familyId, userInfo) {
  320. try {
  321. const { generateInviteCard } = require('../../utils/api.js')
  322. const res = await generateInviteCard('guide', familyId)
  323. const code = res.data.code
  324. this.shareData = {
  325. title: (userInfo && userInfo.nickname ? userInfo.nickname : '家长') + ' 邀请你成为家庭成长规划师',
  326. path: '/pages/login/login?invite_code=' + code
  327. }
  328. uni.showToast({ title: '点击右上角转发给规划师', icon: 'none' })
  329. } catch (e) {
  330. uni.showToast({ title: e.message || '生成邀请失败', icon: 'none' })
  331. }
  332. },
  333. goToMembership() {
  334. uni.showToast({ title: '即将上线', icon: 'none' })
  335. },
  336. goToVendorCenter() {
  337. uni.navigateTo({ url: '/pages/vendor/center' })
  338. },
  339. goToPromotion() {
  340. uni.navigateTo({ url: '/pages/promotion/index' })
  341. },
  342. async checkVendorStatus() {
  343. try {
  344. const res = await vendorStatus()
  345. this.isVendor = res.data && res.data.vendorStatus === 'approved'
  346. } catch (e) {
  347. this.isVendor = false
  348. }
  349. },
  350. exitSwitch() {
  351. if (!this.isSwitchedChild) return
  352. uni.showModal({
  353. title: '退出切换',
  354. content: '请输入家长密码以确认退出',
  355. editable: true,
  356. success: async (res) => {
  357. if (res.confirm && res.content) {
  358. try {
  359. const verifyResult = await verifyPassword(res.content)
  360. if (verifyResult.code === 200) {
  361. this.$store.commit('switchBackToParent')
  362. this.isSwitchedChild = false
  363. this.role = 'parent'
  364. uni.showToast({ title: '已退出切换', icon: 'success' })
  365. // 刷新首页
  366. uni.reLaunch({ url: '/pages/index/index' })
  367. } else {
  368. uni.showToast({ title: '密码错误', icon: 'none' })
  369. }
  370. } catch (e) {
  371. uni.showToast({ title: '验证失败', icon: 'none' })
  372. }
  373. }
  374. }
  375. })
  376. }
  377. }
  378. }
  379. </script>
  380. <style scoped>
  381. .container {
  382. padding: 30rpx;
  383. }
  384. .user-card {
  385. background: linear-gradient(135deg, #5B9BD5 0%, #3A7CC4 100%);
  386. border-radius: 20rpx;
  387. padding: 40rpx;
  388. display: flex;
  389. align-items: center;
  390. color: #fff;
  391. }
  392. .avatar {
  393. font-size: 80rpx;
  394. margin-right: 30rpx;
  395. }
  396. .user-info {
  397. flex: 1;
  398. }
  399. .nickname {
  400. font-size: 36rpx;
  401. font-weight: bold;
  402. }
  403. .role {
  404. font-size: 24rpx;
  405. opacity: 0.8;
  406. }
  407. .system-points {
  408. font-size: 22rpx;
  409. color: #D6EAF8;
  410. margin-top: 8rpx;
  411. font-weight: 500;
  412. }
  413. .btn-switch {
  414. background: rgba(255, 255, 255, 0.2);
  415. color: #fff;
  416. font-size: 24rpx;
  417. padding: 10rpx 20rpx;
  418. border-radius: 20rpx;
  419. }
  420. .menu-list {
  421. margin-top: 30rpx;
  422. background: #fff;
  423. border-radius: 16rpx;
  424. }
  425. .menu-item {
  426. padding: 30rpx;
  427. border-bottom: 1rpx solid #f0f0f0;
  428. display: flex;
  429. justify-content: space-between;
  430. font-size: 28rpx;
  431. }
  432. .menu-item:last-child {
  433. border-bottom: none;
  434. }
  435. .arrow {
  436. color: #ccc;
  437. font-size: 32rpx;
  438. }
  439. .menu-item-btn {
  440. display: flex;
  441. justify-content: space-between;
  442. align-items: center;
  443. width: 100%;
  444. padding: 28rpx 24rpx;
  445. border: none;
  446. background: transparent;
  447. font-size: 28rpx;
  448. color: #333;
  449. }
  450. .menu-item-btn::after { border: none; }
  451. .modal-mask {
  452. position: fixed;
  453. top: 0;
  454. left: 0;
  455. right: 0;
  456. bottom: 0;
  457. background: rgba(0, 0, 0, 0.5);
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. }
  462. .modal {
  463. background: #fff;
  464. border-radius: 20rpx;
  465. padding: 40rpx;
  466. width: 80%;
  467. }
  468. .modal-title {
  469. font-size: 32rpx;
  470. font-weight: bold;
  471. margin-bottom: 30rpx;
  472. text-align: center;
  473. }
  474. .form-item {
  475. margin-bottom: 20rpx;
  476. }
  477. .form-item input {
  478. border: 1rpx solid #ddd;
  479. border-radius: 10rpx;
  480. padding: 20rpx;
  481. font-size: 28rpx;
  482. }
  483. .modal-btns {
  484. display: flex;
  485. gap: 20rpx;
  486. margin-top: 30rpx;
  487. }
  488. .modal-btns button {
  489. flex: 1;
  490. }
  491. /* ===== 未登录提示 ===== */
  492. .login-prompt {
  493. display: flex;
  494. flex-direction: column;
  495. align-items: center;
  496. justify-content: center;
  497. min-height: 80vh;
  498. padding: 60rpx;
  499. }
  500. .prompt-icon {
  501. font-size: 120rpx;
  502. margin-bottom: 30rpx;
  503. width: 160rpx;
  504. height: 160rpx;
  505. line-height: 160rpx;
  506. text-align: center;
  507. background: #f5f5f5;
  508. border-radius: 50%;
  509. }
  510. .prompt-title {
  511. font-size: 36rpx;
  512. font-weight: bold;
  513. color: #333;
  514. margin-bottom: 16rpx;
  515. }
  516. .prompt-desc {
  517. font-size: 26rpx;
  518. color: #999;
  519. text-align: center;
  520. margin-bottom: 40rpx;
  521. }
  522. .login-prompt .login-btn {
  523. width: 60%;
  524. height: 80rpx;
  525. line-height: 80rpx;
  526. background: linear-gradient(135deg, #5B9BD5, #3A7CC4);
  527. color: #fff;
  528. font-size: 30rpx;
  529. font-weight: bold;
  530. border-radius: 40rpx;
  531. text-align: center;
  532. border: none;
  533. }
  534. .login-prompt .login-btn::after {
  535. border: none;
  536. }
  537. </style>