join-form.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view class="join-form-container" :style="{ '--font-scale': fontScale }">
  3. <view class="header">
  4. <text class="title">加入家庭</text>
  5. <text class="subtitle">请填写您的真实姓名和家庭身份,提交后等待管理员审批</text>
  6. </view>
  7. <view class="form-section">
  8. <!-- 真实姓名(必填) -->
  9. <view class="form-item">
  10. <text class="label">真实姓名 <text class="required">*</text></text>
  11. <input type="text" v-model="form.realName" placeholder="请输入真实姓名" class="input" maxlength="20" />
  12. </view>
  13. <!-- 家庭身份(必填) -->
  14. <view class="form-item">
  15. <text class="label">家庭身份 <text class="required">*</text></text>
  16. <picker mode="selector" :range="familyRoleOptions" :value="familyRoleIndex" @change="onFamilyRoleChange">
  17. <view class="picker-input">{{ form.familyRole || '请选择(如:爸爸、妈妈)' }}</view>
  18. </picker>
  19. </view>
  20. <!-- 昵称(选填,默认用户+手机尾号) -->
  21. <view class="form-item">
  22. <text class="label">昵称</text>
  23. <input type="text" v-model="form.nickname" placeholder="选填,默认:用户+手机尾号" class="input" maxlength="20" />
  24. </view>
  25. <!-- 头像(选填) -->
  26. <view class="form-item">
  27. <text class="label">头像</text>
  28. <view class="avatar-upload" @click="chooseAvatar">
  29. <image v-if="form.avatar" :src="form.avatar" class="avatar-preview" mode="aspectFill"></image>
  30. <view v-else class="avatar-placeholder">
  31. <text class="plus-icon">+</text>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. <view class="actions">
  37. <button class="btn-primary" @click="submit" :loading="submitting" :disabled="submitting">
  38. <text v-if="submitting">提交中...</text>
  39. <text v-else>提交加入申请</text>
  40. </button>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { updateUserInfo, requestJoinByCode } from '@/utils/api.js'
  46. import { pickImage } from '@/utils/image-picker.js'
  47. import fontScaleMixin from '@/mixins/font-scale.js'
  48. export default {
  49. mixins: [fontScaleMixin],
  50. data() {
  51. return {
  52. familyInviteCode: '',
  53. familyRoleOptions: ['爸爸', '妈妈', '爷爷', '奶奶', '姥爷', '姥姥', '其他'],
  54. form: {
  55. realName: '',
  56. familyRole: '',
  57. nickname: '',
  58. avatar: ''
  59. },
  60. submitting: false
  61. }
  62. },
  63. computed: {
  64. familyRoleIndex: {
  65. get() {
  66. return this.familyRoleOptions.indexOf(this.form.familyRole)
  67. },
  68. set(val) {
  69. this.form.familyRole = this.familyRoleOptions[val] || ''
  70. }
  71. }
  72. },
  73. onLoad(options) {
  74. // 接收家庭邀请码:join.vue 传 familyInviteCode;登录页衔接传 pendingFamilyInviteCode 对应值
  75. var code = (options && options.familyInviteCode) || uni.getStorageSync('pendingFamilyInviteCode') || ''
  76. this.familyInviteCode = code
  77. if (code) {
  78. uni.setStorageSync('pendingFamilyInviteCode', code)
  79. }
  80. // 已有资料预填(如有昵称/头像/真实姓名则带出)
  81. this.prefill()
  82. },
  83. methods: {
  84. prefill() {
  85. var userInfo = uni.getStorageSync('userInfo') || {}
  86. if (userInfo.realName) this.form.realName = userInfo.realName
  87. if (userInfo.nickname) this.form.nickname = userInfo.nickname
  88. if (userInfo.avatar) this.form.avatar = userInfo.avatar
  89. },
  90. chooseAvatar() {
  91. var self = this
  92. pickImage({
  93. count: 1,
  94. success: function(res) {
  95. self.form.avatar = res.tempFilePaths[0]
  96. }
  97. })
  98. },
  99. onFamilyRoleChange(e) {
  100. this.form.familyRole = this.familyRoleOptions[e.detail.value] || ''
  101. },
  102. async submit() {
  103. if (this.submitting) return
  104. if (!this.form.realName || !this.form.realName.trim()) {
  105. uni.showToast({ title: '请输入真实姓名', icon: 'none' })
  106. return
  107. }
  108. if (!this.form.familyRole) {
  109. uni.showToast({ title: '请选择家庭身份', icon: 'none' })
  110. return
  111. }
  112. if (!this.familyInviteCode) {
  113. uni.showToast({ title: '缺少家庭邀请码', icon: 'none' })
  114. return
  115. }
  116. this.submitting = true
  117. try {
  118. // 先保存基本信息(真实姓名/昵称/头像),失败不阻塞加入申请
  119. try {
  120. await updateUserInfo({
  121. realName: this.form.realName.trim(),
  122. nickname: this.form.nickname ? this.form.nickname.trim() : '',
  123. avatar: this.form.avatar
  124. })
  125. } catch (e) {
  126. console.log('[join-form] 保存基本信息失败,继续提交加入申请', e)
  127. }
  128. // 提交加入家庭申请
  129. await requestJoinByCode(this.familyInviteCode, this.form.familyRole)
  130. uni.removeStorageSync('pendingFamilyInviteCode')
  131. uni.showToast({ title: '加入家庭申请已提交,等待管理员审批', icon: 'success' })
  132. setTimeout(function() {
  133. uni.reLaunch({ url: '/pages/index-home/index' })
  134. }, 1500)
  135. } catch (e) {
  136. uni.showToast({ title: (e && e.message) || '提交失败', icon: 'none' })
  137. } finally {
  138. this.submitting = false
  139. }
  140. }
  141. }
  142. }
  143. </script>
  144. <style scoped lang="scss">
  145. .join-form-container {
  146. min-height: 100vh;
  147. background: $facade-fill-2;
  148. padding: $uni-space-page;
  149. box-sizing: border-box;
  150. }
  151. .header {
  152. margin-bottom: 40rpx;
  153. text-align: center;
  154. }
  155. .title {
  156. font-size: calc(40rpx * var(--font-scale, 1));
  157. font-weight: bold;
  158. color: $facade-text-strong;
  159. display: block;
  160. margin-bottom: 10rpx;
  161. }
  162. .subtitle {
  163. font-size: calc(26rpx * var(--font-scale, 1));
  164. color: $facade-text-muted;
  165. line-height: 1.6;
  166. display: block;
  167. }
  168. .form-section {
  169. background: $app-surface;
  170. border-radius: 16rpx;
  171. padding: $uni-space-page;
  172. margin-bottom: $uni-space-lg;
  173. }
  174. .form-item {
  175. margin-bottom: 40rpx;
  176. }
  177. .form-item:last-child {
  178. margin-bottom: 0;
  179. }
  180. .label {
  181. display: block;
  182. font-size: calc(28rpx * var(--font-scale, 1));
  183. color: $facade-text-strong;
  184. margin-bottom: 16rpx;
  185. }
  186. .required {
  187. color: $mat-red;
  188. }
  189. .input, .picker-input {
  190. padding: 28rpx 24rpx;
  191. background: $facade-fill-2;
  192. border-radius: 16rpx;
  193. font-size: calc(30rpx * var(--font-scale, 1));
  194. box-sizing: border-box;
  195. width: 100%;
  196. min-height: 96rpx;
  197. line-height: 1.4;
  198. }
  199. .avatar-upload {
  200. width: 150rpx;
  201. height: 150rpx;
  202. }
  203. .avatar-preview {
  204. width: 150rpx;
  205. height: 150rpx;
  206. border-radius: 50%;
  207. }
  208. .avatar-placeholder {
  209. width: 150rpx;
  210. height: 150rpx;
  211. background: $facade-fill-2;
  212. border-radius: 50%;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. }
  217. .plus-icon {
  218. font-size: calc(64rpx * var(--font-scale, 1));
  219. color: $facade-text-ghost;
  220. }
  221. .actions {
  222. padding-bottom: 40rpx;
  223. }
  224. .btn-primary {
  225. background: $mat-green;
  226. color: $app-surface;
  227. border-radius: 24rpx;
  228. font-size: calc(30rpx * var(--font-scale, 1));
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. padding: 24rpx 0;
  233. }
  234. </style>