family-guard.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. self.visible = false
  47. uni.showToast({ title: '家庭创建成功', icon: 'success' })
  48. if (self.$listeners && self.$listeners.created) {
  49. self.$emit('created', res.data)
  50. }
  51. } else {
  52. uni.showToast({ title: (res && res.message) || '创建失败', icon: 'none' })
  53. }
  54. }).catch(function() {
  55. self.loading = false
  56. uni.showToast({ title: '网络异常', icon: 'none' })
  57. })
  58. }
  59. }
  60. }
  61. </script>
  62. <style scoped>
  63. .family-guard-mask {
  64. position: fixed;
  65. top: 0;
  66. left: 0;
  67. right: 0;
  68. bottom: 0;
  69. background: rgba(0, 0, 0, 0.5);
  70. z-index: 999;
  71. display: flex;
  72. align-items: center;
  73. justify-content: center;
  74. }
  75. .family-guard-panel {
  76. width: 600rpx;
  77. background: #fff;
  78. border-radius: 20rpx;
  79. padding: 40rpx 36rpx;
  80. display: flex;
  81. flex-direction: column;
  82. }
  83. .family-guard-title {
  84. font-size: 34rpx;
  85. font-weight: bold;
  86. color: #333;
  87. text-align: center;
  88. }
  89. .family-guard-desc {
  90. font-size: 26rpx;
  91. color: #999;
  92. margin-top: 16rpx;
  93. line-height: 1.6;
  94. text-align: center;
  95. }
  96. .family-guard-input-wrap {
  97. margin-top: 30rpx;
  98. }
  99. .family-guard-input {
  100. height: 80rpx;
  101. background: #f5f7fa;
  102. border-radius: 12rpx;
  103. padding: 0 24rpx;
  104. font-size: 28rpx;
  105. }
  106. .family-guard-actions {
  107. margin-top: 36rpx;
  108. display: flex;
  109. flex-direction: column;
  110. }
  111. .family-guard-btn {
  112. height: 84rpx;
  113. border-radius: 42rpx;
  114. font-size: 30rpx;
  115. display: flex;
  116. align-items: center;
  117. justify-content: center;
  118. margin-top: 16rpx;
  119. }
  120. .family-guard-btn-primary {
  121. background: #F97316;
  122. color: #fff;
  123. }
  124. .family-guard-btn-plain {
  125. background: #f5f7fa;
  126. color: #666;
  127. }
  128. </style>