profile.vue 16 KB

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