ProfileHeader.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, switchToFamilyMember } 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. async 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-extra/family-members' })
  88. }
  89. }
  90. })
  91. return
  92. }
  93. }
  94. // 优先显示可切换成员(isSwitchable 由后端计算:非本人即可切换,含可登录成员)
  95. var switchable = this.children.filter(function(m) { return m.isSwitchable })
  96. if (switchable.length === 0) {
  97. uni.showToast({ title: '暂无可切换成员', icon: 'none' })
  98. return
  99. }
  100. if (switchable.length === 1) {
  101. var member = switchable[0]
  102. uni.showModal({
  103. title: '切换确认',
  104. content: '切换到"' + member.nickname + '"后,退出时需要输入您的密码。是否继续?',
  105. success: async (res) => {
  106. if (!res.confirm) return
  107. try {
  108. var result = await switchToFamilyMember(member.id, member.source || 'family_member')
  109. if (result.code === 200 && result.data) {
  110. this.$store.commit('switchToMember', {
  111. memberId: result.data.memberId,
  112. effectiveRole: result.data.effectiveRole
  113. })
  114. this.isSwitchedChild = true
  115. this.role = result.data.effectiveRole
  116. uni.showToast({ title: '已切换', icon: 'success' })
  117. } else {
  118. uni.showToast({ title: result.message || '切换失败', icon: 'none' })
  119. }
  120. } catch (e) {
  121. uni.showToast({ title: '切换失败', icon: 'none' })
  122. }
  123. }
  124. })
  125. return
  126. }
  127. uni.showActionSheet({
  128. itemList: switchable.map(function(m) { return m.nickname }),
  129. success: (res) => {
  130. if (res.tapIndex < 0) return
  131. var member = switchable[res.tapIndex]
  132. this.$store.commit('switchToMember', {
  133. memberId: member.id,
  134. effectiveRole: member.effectiveRole
  135. })
  136. this.isSwitchedChild = true
  137. this.role = member.effectiveRole
  138. uni.showToast({ title: '已切换', icon: 'success' })
  139. }
  140. })
  141. },
  142. async onExitSwitch() {
  143. if (!this.isSwitchedChild) return
  144. var self = this
  145. uni.showModal({
  146. title: '退出切换',
  147. content: '请输入原用户密码以确认退出',
  148. editable: true,
  149. success: async function(res) {
  150. if (!res.confirm || !res.content) return
  151. try {
  152. var ok = await self.$store.dispatch('switchBackWithPassword', res.content)
  153. if (ok) {
  154. self.isSwitchedChild = false
  155. self.role = 'parent'
  156. uni.showToast({ title: '已退出切换', icon: 'success' })
  157. uni.reLaunch({ url: '/pages/index-home/index' })
  158. } else {
  159. uni.showToast({ title: '密码错误', icon: 'none' })
  160. }
  161. } catch (e) {
  162. uni.showToast({ title: '验证失败', icon: 'none' })
  163. }
  164. }
  165. })
  166. }
  167. }
  168. }
  169. </script>
  170. <style scoped>
  171. .exit-switch {
  172. display: flex;
  173. align-items: center;
  174. justify-content: center;
  175. margin: 0 30rpx 16rpx;
  176. background: linear-gradient(135deg, #667eea, #764ba2);
  177. color: #fff;
  178. padding: 16rpx;
  179. border-radius: 12rpx;
  180. font-size: 26rpx;
  181. }
  182. .user-card {
  183. display: flex;
  184. align-items: center;
  185. background: #fff;
  186. margin: 0 30rpx 20rpx;
  187. border-radius: 20rpx;
  188. padding: 30rpx;
  189. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  190. }
  191. .avatar {
  192. width: 100rpx;
  193. height: 100rpx;
  194. border-radius: 50%;
  195. background: linear-gradient(135deg, #667eea, #764ba2);
  196. display: flex;
  197. align-items: center;
  198. justify-content: center;
  199. font-size: 48rpx;
  200. margin-right: 24rpx;
  201. flex-shrink: 0;
  202. }
  203. .user-info { flex: 1; }
  204. .nickname { font-size: 34rpx; font-weight: bold; color: #333; margin-bottom: 8rpx; }
  205. .role { font-size: 24rpx; color: #999; margin-bottom: 6rpx; }
  206. .system-points { font-size: 24rpx; color: #F97316; margin-top: 4rpx; }
  207. .btn-switch {
  208. background: #667eea;
  209. color: #fff;
  210. font-size: 24rpx;
  211. padding: 10rpx 30rpx;
  212. border-radius: 30rpx;
  213. line-height: 1.8;
  214. }
  215. </style>