ProfileHeader.vue 7.2 KB

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