add-member.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <view class="container">
  3. <view class="form-container">
  4. <view class="page-title">{{ isEdit ? '编辑成员' : '添加家庭成员' }}</view>
  5. <!-- 昵称(必填) -->
  6. <view class="form-item required">
  7. <text class="label"><text class="required-mark">*</text>成员昵称</text>
  8. <input class="input" v-model="form.nickname" placeholder="请输入成员昵称" maxlength="20" />
  9. </view>
  10. <!-- 辈分关系(必填,替代关系类型) -->
  11. <view class="form-item required">
  12. <text class="label"><text class="required-mark">*</text>辈分关系</text>
  13. <generation-picker ref="generationPicker" v-model:generationLevel="form.generationLevel" v-model:peerType="form.peerType" :showLabel="false" />
  14. </view>
  15. <!-- 性别(必填) -->
  16. <view class="form-item required">
  17. <text class="label"><text class="required-mark">*</text>性别</text>
  18. <picker :range="genderOptions" range-key="label" @change="onGenderChange">
  19. <view class="picker">
  20. {{ genderLabel || '请选择性别' }}
  21. </view>
  22. </picker>
  23. </view>
  24. <!-- 出生日期(必填) -->
  25. <view class="form-item required">
  26. <text class="label"><text class="required-mark">*</text>出生日期</text>
  27. <picker mode="date" :value="form.birthday" @change="onBirthdayChange" :end="todayDate">
  28. <view class="picker">
  29. {{ form.birthday || '请选择出生日期' }}
  30. </view>
  31. </picker>
  32. </view>
  33. <!-- 年龄(自动计算) -->
  34. <view class="form-item">
  35. <text class="label">年龄</text>
  36. <view class="age-display">{{ calculatedAge || '根据出生日期自动计算' }}</view>
  37. </view>
  38. <!-- 出生时辰(选填) -->
  39. <view class="form-item">
  40. <text class="label">出生时辰(选填)</text>
  41. <picker :range="birthHourOptions" @change="onBirthHourChange">
  42. <view class="picker">
  43. {{ form.birthHourName || '请选择出生时辰' }}
  44. </view>
  45. </picker>
  46. </view>
  47. <!-- 出生体重(选填) -->
  48. <view class="form-item">
  49. <text class="label">出生体重(选填,克)</text>
  50. <input class="input" type="number" v-model="form.birthWeight" placeholder="如: 3500" maxlength="5" />
  51. </view>
  52. <!-- 出生地(选填) -->
  53. <view class="form-item">
  54. <text class="label">出生地(选填)</text>
  55. <input class="input" v-model="form.birthPlace" placeholder="如: 北京市" maxlength="30" />
  56. </view>
  57. <!-- 是否剖腹产(选填) -->
  58. <view class="form-item">
  59. <text class="label">是否剖腹产(选填)</text>
  60. <picker :range="cesareanOptions" range-key="label" @change="onCesareanChange">
  61. <view class="picker">
  62. {{ form.cesareanLabel || '请选择' }}
  63. </view>
  64. </picker>
  65. </view>
  66. <!-- 手机号(选填) -->
  67. <view class="form-item">
  68. <text class="label">手机号(选填)</text>
  69. <input class="input" type="number" v-model="form.phone" placeholder="填写手机号可关联已有账号" maxlength="11" />
  70. <text class="hint">若手机号已注册,将提示是否邀请该用户加入家庭</text>
  71. </view>
  72. <!-- 保存按钮 -->
  73. <button class="btn-primary" :loading="submitting" @click="submit">保存</button>
  74. <!-- 监护人同意弹窗 -->
  75. <view class="modal-overlay" v-if="showConsentModal">
  76. <view class="modal-content">
  77. <text class="modal-title">儿童个人信息保护确认</text>
  78. <text class="modal-desc">您添加的家庭成员年龄小于14周岁。根据《中华人民共和国个人信息保护法》及《儿童个人信息网络保护规定》,作为法定监护人,您需要确认同意我们收集和使用该儿童的个人信息(包括姓名、性别、出生日期、身高体重、学校信息等),用于提供成长测评、健康分析、任务管理等相关服务。</text>
  79. <text class="modal-link" @click="viewPrivacy">查看完整《隐私政策》中儿童个人信息保护章节</text>
  80. <view class="modal-checkbox">
  81. <label>
  82. <checkbox :checked="consentChecked" @click="consentChecked = !consentChecked" />
  83. <text class="checkbox-label">我已阅读并同意上述内容,作为法定监护人授权平台处理该儿童的个人信息</text>
  84. </label>
  85. </view>
  86. <view class="modal-buttons">
  87. <button class="modal-btn-cancel" @click="showConsentModal = false">取消</button>
  88. <button class="modal-btn-confirm" :disabled="!consentChecked" @click="confirmConsent">确认并继续</button>
  89. </view>
  90. </view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </template>
  96. <script>
  97. import { addFamilyMember, updateFamilyMember, recordConsent, checkPhone } from '../../utils/api.js'
  98. import GenerationPicker from '@/components/GenerationPicker.vue'
  99. import { parseDate } from '../../utils/format.js'
  100. export default {
  101. components: { GenerationPicker },
  102. data() {
  103. return {
  104. submitting: false,
  105. isEdit: false,
  106. editMemberId: null,
  107. form: {
  108. nickname: '',
  109. generationLevel: '',
  110. peerType: '',
  111. gender: '',
  112. birthday: '',
  113. phone: '',
  114. birthHour: '',
  115. birthHourName: '',
  116. birthWeight: '',
  117. birthPlace: '',
  118. isCesarean: null,
  119. cesareanLabel: ''
  120. },
  121. genderOptions: [
  122. { value: 'male', label: '男' },
  123. { value: 'female', label: '女' }
  124. ],
  125. birthHourOptions: ['', '子时(23-1点)', '丑时(1-3点)', '寅时(3-5点)', '卯时(5-7点)', '辰时(7-9点)', '巳时(9-11点)', '午时(11-13点)', '未时(13-15点)', '申时(15-17点)', '酉时(17-19点)', '戌时(19-21点)', '亥时(21-23点)'],
  126. cesareanOptions: [
  127. { value: 0, label: '顺产' },
  128. { value: 1, label: '剖腹产' }
  129. ],
  130. todayDate: '',
  131. showConsentModal: false,
  132. consentChecked: false,
  133. pendingSubmitPayload: null
  134. }
  135. },
  136. computed: {
  137. genderLabel() {
  138. var gender = this.genderOptions.find(function(g) { return g.value === this.form.gender }.bind(this))
  139. return gender ? gender.label : ''
  140. },
  141. calculatedAge() {
  142. if (!this.form.birthday) return ''
  143. var birth = parseDate(this.form.birthday)
  144. if (!birth) return ''
  145. var today = new Date()
  146. var age = today.getFullYear() - birth.getFullYear()
  147. var monthDiff = today.getMonth() - birth.getMonth()
  148. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  149. age--
  150. }
  151. return age >= 0 ? age + '岁' : ''
  152. }
  153. },
  154. onLoad(options) {
  155. var today = new Date()
  156. this.todayDate = today.getFullYear() + '-' + String(today.getMonth() + 1).padStart(2, '0') + '-' + String(today.getDate()).padStart(2, '0')
  157. if (options && options.memberId) {
  158. this.isEdit = true
  159. this.editMemberId = Number(options.memberId)
  160. if (options.nickname) this.form.nickname = decodeURIComponent(options.nickname)
  161. if (options.gender) this.form.gender = options.gender
  162. if (options.phone) this.form.phone = options.phone
  163. if (options.birthday) this.form.birthday = options.birthday
  164. if (options.generationLevel) this.form.generationLevel = options.generationLevel
  165. if (options.peerType) this.form.peerType = options.peerType
  166. }
  167. },
  168. methods: {
  169. onGenderChange(e) {
  170. this.form.gender = this.genderOptions[e.detail.value].value
  171. },
  172. onBirthdayChange(e) {
  173. this.form.birthday = e.detail.value
  174. },
  175. onBirthHourChange(e) {
  176. var idx = e.detail.value
  177. if (idx === 0) {
  178. this.form.birthHour = ''
  179. this.form.birthHourName = ''
  180. } else {
  181. this.form.birthHour = this.birthHourOptions[idx]
  182. this.form.birthHourName = this.birthHourOptions[idx]
  183. }
  184. },
  185. onCesareanChange(e) {
  186. var idx = e.detail.value
  187. this.form.isCesarean = this.cesareanOptions[idx].value
  188. this.form.cesareanLabel = this.cesareanOptions[idx].label
  189. },
  190. async submit() {
  191. if (this.submitting) return
  192. if (!this.form.nickname || !this.form.nickname.trim()) {
  193. uni.showToast({ title: '请输入成员昵称', icon: 'none' })
  194. return
  195. }
  196. if (!this.isEdit && (!this.$refs.generationPicker || !this.$refs.generationPicker.selectedGen)) {
  197. uni.showToast({ title: '请选择辈分关系', icon: 'none' })
  198. return
  199. }
  200. if (!this.form.gender) {
  201. uni.showToast({ title: '请选择性别', icon: 'none' })
  202. return
  203. }
  204. if (!this.form.birthday) {
  205. uni.showToast({ title: '请选择出生日期', icon: 'none' })
  206. return
  207. }
  208. if (this.form.phone && !/^1[3-9]\d{9}$/.test(this.form.phone)) {
  209. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  210. return
  211. }
  212. // 手机号已注册:提示是否邀请该用户加入家庭(不自动关联)
  213. if (this.form.phone && !this.isEdit) {
  214. try {
  215. var phoneCheck = await checkPhone(this.form.phone)
  216. if (phoneCheck && phoneCheck.code === 200 && phoneCheck.data === true) {
  217. var phoneConfirmed = await new Promise(function(resolve) {
  218. uni.showModal({
  219. title: '手机号已注册',
  220. content: '该手机号已被其他用户注册,是否邀请该用户加入家庭?',
  221. confirmText: '去邀请',
  222. cancelText: '继续添加',
  223. success: function(res2) {
  224. resolve(res2.confirm)
  225. }
  226. })
  227. })
  228. if (phoneConfirmed) {
  229. // 原「邀请分享」页已删除,家庭邀请统一走家庭成员页的分享卡片流程
  230. uni.navigateTo({ url: '/pages/profile-extra/family-members' })
  231. return
  232. }
  233. }
  234. } catch (e) {
  235. console.log('手机号存在性检查失败,忽略', e)
  236. }
  237. }
  238. // 检查是否为14岁以下儿童 -> 显示监护人同意弹窗
  239. if (!this.isEdit) {
  240. var genLevel = (this.$refs.generationPicker && this.$refs.generationPicker.selectedGen) || this.form.generationLevel
  241. // 辈分负值表示晚辈(孩子),且年龄 < 14
  242. var age = this.calculatedAge ? parseInt(this.calculatedAge) : 0
  243. if (genLevel < 0 && age < 14) {
  244. this.pendingSubmitPayload = null
  245. this.consentChecked = false
  246. this.showConsentModal = true
  247. return
  248. }
  249. }
  250. this.doSubmit()
  251. },
  252. viewPrivacy() {
  253. uni.navigateTo({ url: '/pages/policy/privacy' })
  254. },
  255. async confirmConsent() {
  256. if (!this.consentChecked) return
  257. this.showConsentModal = false
  258. try {
  259. await recordConsent({ consentType: 'children_data', version: '2026-08-v1' })
  260. } catch (e) {
  261. console.log('记录同意失败', e)
  262. }
  263. this.doSubmit()
  264. },
  265. async doSubmit() {
  266. try {
  267. this.submitting = true
  268. if (this.isEdit) {
  269. var updatePayload = {
  270. memberId: this.editMemberId,
  271. nickname: this.form.nickname.trim()
  272. }
  273. if (this.form.gender) updatePayload.gender = this.form.gender
  274. if (this.form.birthday) updatePayload.birthday = this.form.birthday
  275. if (this.form.phone) updatePayload.phone = this.form.phone
  276. var editGen = (this.$refs.generationPicker && this.$refs.generationPicker.selectedGen) || this.form.generationLevel
  277. if (editGen) updatePayload.generationLevel = editGen
  278. if (this.form.peerType) updatePayload.peerType = this.form.peerType
  279. var res = await updateFamilyMember(updatePayload)
  280. if (res.code === 200) {
  281. uni.showToast({ title: '已更新', icon: 'success' })
  282. setTimeout(function() { uni.navigateBack() }, 1500)
  283. } else {
  284. uni.showToast({ title: res.message || '更新失败', icon: 'none' })
  285. }
  286. } else {
  287. var genLevel = (this.$refs.generationPicker && this.$refs.generationPicker.selectedGen) || this.form.generationLevel
  288. var genPeerType = (this.$refs.generationPicker && this.$refs.generationPicker.selectedPeer) || this.form.peerType
  289. var payload = {
  290. nickname: this.form.nickname.trim(),
  291. generationLevel: genLevel,
  292. peerType: genPeerType || null,
  293. gender: this.form.gender,
  294. birthday: this.form.birthday
  295. }
  296. if (this.form.phone) {
  297. payload.phone = this.form.phone
  298. }
  299. if (this.form.birthHour) {
  300. payload.birthHour = this.form.birthHour
  301. }
  302. if (this.form.birthWeight) {
  303. payload.birthWeight = parseInt(this.form.birthWeight)
  304. }
  305. if (this.form.birthPlace) {
  306. payload.birthPlace = this.form.birthPlace
  307. }
  308. if (this.form.isCesarean !== null && this.form.isCesarean !== undefined) {
  309. payload.isCesarean = this.form.isCesarean
  310. }
  311. var res = await addFamilyMember(payload)
  312. if (res.code === 200) {
  313. uni.showToast({ title: '添加成功,已发放优惠券', icon: 'none', duration: 2000 })
  314. setTimeout(function() { uni.navigateBack() }, 1600)
  315. } else {
  316. uni.showToast({ title: res.message || '添加失败', icon: 'none' })
  317. }
  318. }
  319. } catch (e) {
  320. uni.showToast({ title: this.isEdit ? '更新失败' : '添加失败', icon: 'none' })
  321. } finally {
  322. this.submitting = false
  323. }
  324. }
  325. }
  326. }
  327. </script>
  328. <style scoped>
  329. .container { padding: 30rpx; }
  330. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  331. .page-title { font-size: 34rpx; font-weight: bold; color: #333; margin-bottom: 30rpx; }
  332. .form-item { margin-bottom: 30rpx; }
  333. .form-item.required .label { font-weight: bold; }
  334. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  335. .required-mark { color: #F97316; margin-right: 4rpx; }
  336. .input { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; }
  337. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  338. .age-display { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #999; background: #f9f9f9; }
  339. .hint { font-size: 24rpx; color: #999; margin-top: 8rpx; display: block; }
  340. .btn-primary { background: #F97316; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  341. /* 监护人同意弹窗 */
  342. .modal-overlay {
  343. position: fixed;
  344. top: 0; left: 0; right: 0; bottom: 0;
  345. background: rgba(0,0,0,0.5);
  346. display: flex;
  347. justify-content: center;
  348. align-items: center;
  349. z-index: 999;
  350. }
  351. .modal-content {
  352. background: #fff;
  353. border-radius: 20rpx;
  354. padding: 40rpx;
  355. margin: 40rpx;
  356. max-height: 80vh;
  357. overflow-y: auto;
  358. }
  359. .modal-title {
  360. font-size: 32rpx;
  361. font-weight: bold;
  362. color: #333;
  363. display: block;
  364. text-align: center;
  365. margin-bottom: 20rpx;
  366. }
  367. .modal-desc {
  368. font-size: 26rpx;
  369. color: #666;
  370. display: block;
  371. line-height: 1.7;
  372. margin-bottom: 16rpx;
  373. }
  374. .modal-link {
  375. font-size: 24rpx;
  376. color: #F97316;
  377. display: block;
  378. text-align: center;
  379. margin-bottom: 24rpx;
  380. text-decoration: underline;
  381. }
  382. .modal-checkbox {
  383. margin-bottom: 32rpx;
  384. }
  385. .checkbox-label {
  386. font-size: 24rpx;
  387. color: #333;
  388. margin-left: 8rpx;
  389. }
  390. .modal-buttons {
  391. display: flex;
  392. gap: 20rpx;
  393. }
  394. .modal-btn-cancel {
  395. flex: 1;
  396. background: #f5f5f5;
  397. color: #666;
  398. border-radius: 44rpx;
  399. font-size: 28rpx;
  400. padding: 20rpx;
  401. text-align: center;
  402. }
  403. .modal-btn-confirm {
  404. flex: 1;
  405. background: #F97316;
  406. color: #fff;
  407. border-radius: 44rpx;
  408. font-size: 28rpx;
  409. padding: 20rpx;
  410. text-align: center;
  411. }
  412. .modal-btn-confirm[disabled] {
  413. background: #ccc;
  414. }
  415. </style>