ProfileHeader.vue 7.8 KB

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