family-guard.vue 3.1 KB

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