family-guard.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view v-if="visible" class="family-guard-mask" @click="close">
  3. <view class="family-guard-panel" @click.stop>
  4. <text class="family-guard-title">创建家庭后即可使用</text>
  5. <text class="family-guard-desc">家庭是任务、测评、成长档案等功能的基础,创建后邀请家人加入</text>
  6. <view class="family-guard-input-wrap">
  7. <input class="family-guard-input" v-model="familyName" placeholder="请输入家庭名称(默认:我的家庭)" maxlength="20" />
  8. </view>
  9. <view class="family-guard-actions">
  10. <button class="family-guard-btn family-guard-btn-primary" @click="doCreate" :loading="loading">去创建家庭</button>
  11. <button class="family-guard-btn family-guard-btn-plain" @click="close">暂不创建</button>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import { createFamily } from '../utils/api.js'
  18. export default {
  19. name: 'FamilyGuard',
  20. data() {
  21. return {
  22. visible: false,
  23. loading: false,
  24. familyName: ''
  25. }
  26. },
  27. methods: {
  28. show() {
  29. this.familyName = ''
  30. this.visible = true
  31. },
  32. close() {
  33. this.visible = false
  34. },
  35. doCreate() {
  36. var self = this
  37. var name = (this.familyName || '').trim() || '我的家庭'
  38. this.loading = true
  39. createFamily(name).then(function(res) {
  40. self.loading = false
  41. if (res && res.code === 200 && res.data) {
  42. // 同步 userInfo 缓存(familyId 由后端 JWT 属性注入,无需单独存储)
  43. var userInfo = uni.getStorageSync('userInfo') || {}
  44. userInfo.familyId = res.data
  45. uni.setStorageSync('userInfo', userInfo)
  46. // 同步 familyId 到 store(hasFamily getter 依赖,8ddc5b55 移除存储后此数据流断裂)
  47. if (self.$store) {
  48. self.$store.commit('setFamilyId', res.data)
  49. }
  50. self.visible = false
  51. uni.showToast({ title: '家庭创建成功', icon: 'success' })
  52. if (self.$listeners && self.$listeners.created) {
  53. self.$emit('created', res.data)
  54. }
  55. } else {
  56. uni.showToast({ title: (res && res.message) || '创建失败', icon: 'none' })
  57. }
  58. }).catch(function() {
  59. self.loading = false
  60. uni.showToast({ title: '网络异常', icon: 'none' })
  61. })
  62. }
  63. }
  64. }
  65. </script>
  66. <style scoped>
  67. .family-guard-mask {
  68. position: fixed;
  69. top: 0;
  70. left: 0;
  71. right: 0;
  72. bottom: 0;
  73. background: rgba(0, 0, 0, 0.5);
  74. z-index: 999;
  75. display: flex;
  76. align-items: center;
  77. justify-content: center;
  78. }
  79. .family-guard-panel {
  80. width: 600rpx;
  81. background: #fff;
  82. border-radius: 20rpx;
  83. padding: 40rpx 36rpx;
  84. display: flex;
  85. flex-direction: column;
  86. }
  87. .family-guard-title {
  88. font-size: 34rpx;
  89. font-weight: bold;
  90. color: #333;
  91. text-align: center;
  92. }
  93. .family-guard-desc {
  94. font-size: 26rpx;
  95. color: #999;
  96. margin-top: 16rpx;
  97. line-height: 1.6;
  98. text-align: center;
  99. }
  100. .family-guard-input-wrap {
  101. margin-top: 30rpx;
  102. }
  103. .family-guard-input {
  104. height: 80rpx;
  105. background: #f5f7fa;
  106. border-radius: 12rpx;
  107. padding: 0 24rpx;
  108. font-size: 28rpx;
  109. }
  110. .family-guard-actions {
  111. margin-top: 36rpx;
  112. display: flex;
  113. flex-direction: column;
  114. }
  115. .family-guard-btn {
  116. height: 84rpx;
  117. border-radius: 42rpx;
  118. font-size: 30rpx;
  119. display: flex;
  120. align-items: center;
  121. justify-content: center;
  122. margin-top: 16rpx;
  123. }
  124. .family-guard-btn-primary {
  125. background: #F97316;
  126. color: #fff;
  127. }
  128. .family-guard-btn-plain {
  129. background: #f5f7fa;
  130. color: #666;
  131. }
  132. </style>