ProfileHeader.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view :style="{ '--font-scale': fontScale }">
  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. import fontScaleMixin from '@/mixins/font-scale.js'
  22. export default {
  23. mixins: [fontScaleMixin],
  24. name: 'ProfileHeader',
  25. data() {
  26. return {
  27. nickname: '',
  28. role: 'parent',
  29. children: [],
  30. isSwitchedChild: false
  31. }
  32. },
  33. computed: {
  34. isLoggedIn() {
  35. return !!uni.getStorageSync('token')
  36. }
  37. },
  38. onShow() {
  39. if (!uni.getStorageSync('token')) return
  40. const currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  41. const userInfo = uni.getStorageSync('userInfo')
  42. this.nickname = this.$store.state.nickname || (userInfo && userInfo.nickname) || ''
  43. this.role = currentRole
  44. this.isSwitchedChild = uni.getStorageSync('isSwitchedChild') || false
  45. this.loadChildren()
  46. },
  47. methods: {
  48. async loadChildren() {
  49. try {
  50. const res = await getChildren()
  51. this.children = res.data || []
  52. } catch (e) {
  53. console.error('获取成员列表失败', e)
  54. }
  55. },
  56. onSwitchMode() {
  57. if (this.role === 'child') {
  58. uni.showModal({
  59. title: '验证密码',
  60. content: '请输入家庭主账号密码',
  61. editable: true,
  62. success: (res) => {
  63. if (res.confirm && res.content) {
  64. verifyPassword(res.content).then((result) => {
  65. if (result.code === 200) {
  66. this.doSwitchMode()
  67. } else {
  68. uni.showToast({ title: '密码错误', icon: 'none' })
  69. }
  70. }).catch(() => {
  71. uni.showToast({ title: '验证失败', icon: 'none' })
  72. })
  73. }
  74. }
  75. })
  76. } else {
  77. this.doSwitchMode()
  78. }
  79. },
  80. async doSwitchMode() {
  81. // 成员视角(已切换到不可登录成员)下不可再切换其他成员,只能退出切换
  82. if (this.$store.state.currentView === 'member') {
  83. uni.showToast({ title: '当前为成员视角,请先退出切换', icon: 'none' })
  84. return
  85. }
  86. if (this.children.length === 0) {
  87. this.loadChildren()
  88. if (this.children.length === 0) {
  89. uni.showModal({
  90. title: '提示',
  91. content: '您还没有添加成员,请先在家庭成员中添加',
  92. success: (res) => {
  93. if (res.confirm) {
  94. uni.navigateTo({ url: '/pages/profile-extra/family-members' })
  95. }
  96. }
  97. })
  98. return
  99. }
  100. }
  101. // 优先显示可切换成员(不可登录成员),过滤掉可通过微信登录的成员
  102. var switchable = this.children.filter(function(m) { return m.isSwitchable })
  103. if (switchable.length === 0) {
  104. uni.showToast({ title: '暂无可切换成员', icon: 'none' })
  105. return
  106. }
  107. if (switchable.length === 1) {
  108. var member = switchable[0]
  109. uni.showModal({
  110. title: '切换确认',
  111. content: '切换到"' + member.nickname + '"后,退出时需要输入您的密码。是否继续?',
  112. success: async (res) => {
  113. if (!res.confirm) return
  114. try {
  115. var result = await switchToFamilyMember(member.id, member.source || 'family_member')
  116. if (result.code === 200 && result.data) {
  117. this.$store.commit('switchToMember', {
  118. memberId: result.data.memberId,
  119. effectiveRole: result.data.effectiveRole
  120. })
  121. this.isSwitchedChild = true
  122. this.role = result.data.effectiveRole
  123. uni.showToast({ title: '已切换', icon: 'success' })
  124. } else {
  125. uni.showToast({ title: result.message || '切换失败', icon: 'none' })
  126. }
  127. } catch (e) {
  128. uni.showToast({ title: '切换失败', icon: 'none' })
  129. }
  130. }
  131. })
  132. return
  133. }
  134. uni.showActionSheet({
  135. itemList: switchable.map(function(m) { return m.nickname }),
  136. success: (res) => {
  137. if (res.tapIndex < 0) return
  138. var member = switchable[res.tapIndex]
  139. this.$store.commit('switchToMember', {
  140. memberId: member.id,
  141. effectiveRole: member.effectiveRole
  142. })
  143. this.isSwitchedChild = true
  144. this.role = member.effectiveRole
  145. uni.showToast({ title: '已切换', icon: 'success' })
  146. }
  147. })
  148. },
  149. async onExitSwitch() {
  150. if (!this.isSwitchedChild) return
  151. var self = this
  152. uni.showModal({
  153. title: '退出切换',
  154. content: '请输入原用户密码以确认退出',
  155. editable: true,
  156. success: async function(res) {
  157. if (!res.confirm || !res.content) return
  158. try {
  159. var ok = await self.$store.dispatch('switchBackWithPassword', res.content)
  160. if (ok) {
  161. self.isSwitchedChild = false
  162. self.role = 'parent'
  163. uni.showToast({ title: '已退出切换', icon: 'success' })
  164. uni.reLaunch({ url: '/pages/index-home/index' })
  165. } else {
  166. uni.showToast({ title: '密码错误', icon: 'none' })
  167. }
  168. } catch (e) {
  169. uni.showToast({ title: '验证失败', icon: 'none' })
  170. }
  171. }
  172. })
  173. }
  174. }
  175. }
  176. </script>
  177. <style scoped lang="scss">
  178. .exit-switch {
  179. display: flex;
  180. align-items: center;
  181. justify-content: center;
  182. margin: 0 $uni-space-lg 16rpx;
  183. background: linear-gradient(135deg, $accent-indigo, $accent-indigo-deep);
  184. color: $app-surface;
  185. padding: 16rpx;
  186. border-radius: 16rpx;
  187. font-size: calc(26rpx * var(--font-scale, 1));
  188. }
  189. .user-card {
  190. display: flex;
  191. align-items: center;
  192. background: $app-surface;
  193. margin: 0 $uni-space-lg 20rpx;
  194. border-radius: 24rpx;
  195. padding: $uni-space-lg;
  196. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  197. }
  198. .avatar {
  199. width: 100rpx;
  200. height: 100rpx;
  201. border-radius: 50%;
  202. background: linear-gradient(135deg, $accent-indigo, $accent-indigo-deep);
  203. display: flex;
  204. align-items: center;
  205. justify-content: center;
  206. font-size: calc(48rpx * var(--font-scale, 1));
  207. margin-right: 24rpx;
  208. flex-shrink: 0;
  209. }
  210. .user-info { flex: 1; }
  211. .nickname { font-size: calc(34rpx * var(--font-scale, 1)); font-weight: bold; color: $facade-text-strong; margin-bottom: 8rpx; }
  212. .role { font-size: calc(24rpx * var(--font-scale, 1)); color: $facade-text-muted; margin-bottom: 6rpx; }
  213. .system-points { font-size: calc(24rpx * var(--font-scale, 1)); color: $brand-primary; margin-top: 4rpx; }
  214. .btn-switch {
  215. background: $accent-indigo;
  216. color: $app-surface;
  217. font-size: calc(24rpx * var(--font-scale, 1));
  218. padding: 10rpx $uni-space-page;
  219. border-radius: 24rpx;
  220. line-height: 1.8;
  221. }
  222. </style>