ProfileHeader.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <view>
  3. <!-- 退出切换按钮(仅家长切换到孩子状态时显示) -->
  4. <view class="exit-switch" v-if="isSwitchedChild" @click="onExitSwitch">
  5. <text>🔄 退出切换</text>
  6. </view>
  7. <view class="user-card">
  8. <view class="avatar" @click="$emit('avatar-click')">👤</view>
  9. <view class="user-info">
  10. <view class="nickname">{{ nickname || '未设置昵称' }}</view>
  11. <view class="role">{{ role === 'parent' ? '家长模式' : (role === 'child' ? '孩子模式' : '成长规划师模式') }}</view>
  12. <view class="system-points" v-if="role === 'parent' && children.length > 0">🏅 系统积分: {{ children[0].systemPoints || 0 }}</view>
  13. </view>
  14. <!-- 切换按钮仅在非切换状态下显示 -->
  15. <button class="btn-switch" v-if="!isSwitchedChild" @click="onSwitchMode">切换</button>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import { getChildren, verifyPassword } from '../../../utils/api.js'
  21. export default {
  22. name: 'ProfileHeader',
  23. data() {
  24. return {
  25. nickname: '',
  26. role: 'parent',
  27. children: [],
  28. isSwitchedChild: false
  29. }
  30. },
  31. computed: {
  32. isLoggedIn() {
  33. return !!uni.getStorageSync('token')
  34. }
  35. },
  36. onShow() {
  37. if (!uni.getStorageSync('token')) return
  38. const currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  39. const userInfo = uni.getStorageSync('userInfo')
  40. this.nickname = this.$store.state.nickname || (userInfo && userInfo.nickname) || ''
  41. this.role = currentRole
  42. this.isSwitchedChild = uni.getStorageSync('isSwitchedChild') || false
  43. this.loadChildren()
  44. },
  45. methods: {
  46. async loadChildren() {
  47. try {
  48. const res = await getChildren()
  49. this.children = res.data || []
  50. } catch (e) {
  51. console.error('获取孩子列表失败', e)
  52. }
  53. },
  54. onSwitchMode() {
  55. if (this.role === 'child') {
  56. uni.showModal({
  57. title: '验证密码',
  58. content: '请输入家长密码',
  59. editable: true,
  60. success: (res) => {
  61. if (res.confirm && res.content) {
  62. verifyPassword(res.content).then((result) => {
  63. if (result.code === 200) {
  64. this.doSwitchMode()
  65. } else {
  66. uni.showToast({ title: '密码错误', icon: 'none' })
  67. }
  68. }).catch(() => {
  69. uni.showToast({ title: '验证失败', icon: 'none' })
  70. })
  71. }
  72. }
  73. })
  74. } else {
  75. this.doSwitchMode()
  76. }
  77. },
  78. doSwitchMode() {
  79. if (this.children.length === 0) {
  80. this.loadChildren()
  81. if (this.children.length === 0) {
  82. uni.showModal({
  83. title: '提示',
  84. content: '您还没有添加孩子,是否现在去添加?',
  85. success: (res) => {
  86. if (res.confirm) {
  87. uni.navigateTo({ url: '/pages/profile/create-child' })
  88. }
  89. }
  90. })
  91. return
  92. }
  93. }
  94. if (this.children.length === 1) {
  95. this.$store.commit('switchToChild', this.children[0].id)
  96. this.role = 'child'
  97. this.isSwitchedChild = true
  98. uni.showToast({ title: '已切换为孩子模式', icon: 'success' })
  99. return
  100. }
  101. uni.showActionSheet({
  102. itemList: this.children.map(c => c.nickname),
  103. success: (res) => {
  104. const selectedChild = this.children[res.tapIndex]
  105. this.$store.commit('switchToChild', selectedChild.id)
  106. this.role = 'child'
  107. this.isSwitchedChild = true
  108. uni.showToast({ title: `已切换为${selectedChild.nickname}模式`, icon: 'success' })
  109. }
  110. })
  111. },
  112. onExitSwitch() {
  113. if (!this.isSwitchedChild) return
  114. uni.showModal({
  115. title: '退出切换',
  116. content: '请输入家长密码以确认退出',
  117. editable: true,
  118. success: (res) => {
  119. if (res.confirm && res.content) {
  120. verifyPassword(res.content).then((result) => {
  121. if (result.code === 200) {
  122. this.$store.commit('switchBackToParent')
  123. this.isSwitchedChild = false
  124. this.role = 'parent'
  125. uni.showToast({ title: '已退出切换', icon: 'success' })
  126. uni.reLaunch({ url: '/pages/index/index' })
  127. } else {
  128. uni.showToast({ title: '密码错误', icon: 'none' })
  129. }
  130. }).catch(() => {
  131. uni.showToast({ title: '验证失败', icon: 'none' })
  132. })
  133. }
  134. }
  135. })
  136. }
  137. }
  138. }
  139. </script>
  140. <style scoped>
  141. .exit-switch {
  142. display: flex;
  143. align-items: center;
  144. justify-content: center;
  145. margin: 0 30rpx 16rpx;
  146. background: linear-gradient(135deg, #667eea, #764ba2);
  147. color: #fff;
  148. padding: 16rpx;
  149. border-radius: 12rpx;
  150. font-size: 26rpx;
  151. }
  152. .user-card {
  153. display: flex;
  154. align-items: center;
  155. background: #fff;
  156. margin: 0 30rpx 20rpx;
  157. border-radius: 20rpx;
  158. padding: 30rpx;
  159. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  160. }
  161. .avatar {
  162. width: 100rpx;
  163. height: 100rpx;
  164. border-radius: 50%;
  165. background: linear-gradient(135deg, #667eea, #764ba2);
  166. display: flex;
  167. align-items: center;
  168. justify-content: center;
  169. font-size: 48rpx;
  170. margin-right: 24rpx;
  171. flex-shrink: 0;
  172. }
  173. .user-info { flex: 1; }
  174. .nickname { font-size: 34rpx; font-weight: bold; color: #333; margin-bottom: 8rpx; }
  175. .role { font-size: 24rpx; color: #999; margin-bottom: 6rpx; }
  176. .system-points { font-size: 24rpx; color: #F97316; margin-top: 4rpx; }
  177. .btn-switch {
  178. background: #667eea;
  179. color: #fff;
  180. font-size: 24rpx;
  181. padding: 10rpx 30rpx;
  182. border-radius: 30rpx;
  183. line-height: 1.8;
  184. }
  185. </style>