add-member.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 } 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. }
  165. },
  166. methods: {
  167. onGenderChange(e) {
  168. this.form.gender = this.genderOptions[e.detail.value].value
  169. },
  170. onBirthdayChange(e) {
  171. this.form.birthday = e.detail.value
  172. },
  173. onBirthHourChange(e) {
  174. var idx = e.detail.value
  175. if (idx === 0) {
  176. this.form.birthHour = ''
  177. this.form.birthHourName = ''
  178. } else {
  179. this.form.birthHour = this.birthHourOptions[idx]
  180. this.form.birthHourName = this.birthHourOptions[idx]
  181. }
  182. },
  183. onCesareanChange(e) {
  184. var idx = e.detail.value
  185. this.form.isCesarean = this.cesareanOptions[idx].value
  186. this.form.cesareanLabel = this.cesareanOptions[idx].label
  187. },
  188. async submit() {
  189. if (this.submitting) return
  190. if (!this.form.nickname || !this.form.nickname.trim()) {
  191. uni.showToast({ title: '请输入成员昵称', icon: 'none' })
  192. return
  193. }
  194. if (!this.isEdit && (!this.$refs.generationPicker || !this.$refs.generationPicker.selectedGen)) {
  195. uni.showToast({ title: '请选择辈分关系', icon: 'none' })
  196. return
  197. }
  198. if (!this.form.gender) {
  199. uni.showToast({ title: '请选择性别', icon: 'none' })
  200. return
  201. }
  202. if (!this.form.birthday) {
  203. uni.showToast({ title: '请选择出生日期', icon: 'none' })
  204. return
  205. }
  206. if (this.form.phone && !/^1[3-9]\d{9}$/.test(this.form.phone)) {
  207. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  208. return
  209. }
  210. // 检查是否为14岁以下儿童 -> 显示监护人同意弹窗
  211. if (!this.isEdit) {
  212. var genLevel = (this.$refs.generationPicker && this.$refs.generationPicker.selectedGen) || this.form.generationLevel
  213. // 辈分负值表示晚辈(孩子),且年龄 < 14
  214. var age = this.calculatedAge ? parseInt(this.calculatedAge) : 0
  215. if (genLevel < 0 && age < 14) {
  216. this.pendingSubmitPayload = null
  217. this.consentChecked = false
  218. this.showConsentModal = true
  219. return
  220. }
  221. }
  222. this.doSubmit()
  223. },
  224. viewPrivacy() {
  225. uni.navigateTo({ url: '/pages/policy/privacy' })
  226. },
  227. async confirmConsent() {
  228. if (!this.consentChecked) return
  229. this.showConsentModal = false
  230. try {
  231. await recordConsent({ consentType: 'children_data', version: '2026-08-v1' })
  232. } catch (e) {
  233. console.log('记录同意失败', e)
  234. }
  235. this.doSubmit()
  236. },
  237. async doSubmit() {
  238. try {
  239. this.submitting = true
  240. if (this.isEdit) {
  241. var updatePayload = {
  242. memberId: this.editMemberId,
  243. nickname: this.form.nickname.trim()
  244. }
  245. if (this.form.gender) updatePayload.gender = this.form.gender
  246. if (this.form.birthday) updatePayload.birthday = this.form.birthday
  247. if (this.form.phone) updatePayload.phone = this.form.phone
  248. var res = await updateFamilyMember(updatePayload)
  249. if (res.code === 200) {
  250. uni.showToast({ title: '已更新', icon: 'success' })
  251. setTimeout(function() { uni.navigateBack() }, 1500)
  252. } else {
  253. uni.showToast({ title: res.message || '更新失败', icon: 'none' })
  254. }
  255. } else {
  256. var genLevel = (this.$refs.generationPicker && this.$refs.generationPicker.selectedGen) || this.form.generationLevel
  257. var genPeerType = (this.$refs.generationPicker && this.$refs.generationPicker.selectedPeer) || this.form.peerType
  258. var payload = {
  259. nickname: this.form.nickname.trim(),
  260. generationLevel: genLevel,
  261. peerType: genPeerType || null,
  262. gender: this.form.gender,
  263. birthday: this.form.birthday
  264. }
  265. if (this.form.phone) {
  266. payload.phone = this.form.phone
  267. }
  268. if (this.form.birthHour) {
  269. payload.birthHour = this.form.birthHour
  270. }
  271. if (this.form.birthWeight) {
  272. payload.birthWeight = parseInt(this.form.birthWeight)
  273. }
  274. if (this.form.birthPlace) {
  275. payload.birthPlace = this.form.birthPlace
  276. }
  277. if (this.form.isCesarean !== null && this.form.isCesarean !== undefined) {
  278. payload.isCesarean = this.form.isCesarean
  279. }
  280. var res = await addFamilyMember(payload)
  281. if (res.code === 200) {
  282. uni.showToast({ title: '添加成功,已发放优惠券', icon: 'none', duration: 2000 })
  283. setTimeout(function() { uni.navigateBack() }, 1600)
  284. } else {
  285. uni.showToast({ title: res.message || '添加失败', icon: 'none' })
  286. }
  287. }
  288. } catch (e) {
  289. uni.showToast({ title: this.isEdit ? '更新失败' : '添加失败', icon: 'none' })
  290. } finally {
  291. this.submitting = false
  292. }
  293. }
  294. }
  295. }
  296. </script>
  297. <style scoped>
  298. .container { padding: 30rpx; }
  299. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  300. .page-title { font-size: 34rpx; font-weight: bold; color: #333; margin-bottom: 30rpx; }
  301. .form-item { margin-bottom: 30rpx; }
  302. .form-item.required .label { font-weight: bold; }
  303. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  304. .required-mark { color: #F97316; margin-right: 4rpx; }
  305. .input { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; }
  306. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  307. .age-display { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #999; background: #f9f9f9; }
  308. .hint { font-size: 24rpx; color: #999; margin-top: 8rpx; display: block; }
  309. .btn-primary { background: #F97316; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  310. /* 监护人同意弹窗 */
  311. .modal-overlay {
  312. position: fixed;
  313. top: 0; left: 0; right: 0; bottom: 0;
  314. background: rgba(0,0,0,0.5);
  315. display: flex;
  316. justify-content: center;
  317. align-items: center;
  318. z-index: 999;
  319. }
  320. .modal-content {
  321. background: #fff;
  322. border-radius: 20rpx;
  323. padding: 40rpx;
  324. margin: 40rpx;
  325. max-height: 80vh;
  326. overflow-y: auto;
  327. }
  328. .modal-title {
  329. font-size: 32rpx;
  330. font-weight: bold;
  331. color: #333;
  332. display: block;
  333. text-align: center;
  334. margin-bottom: 20rpx;
  335. }
  336. .modal-desc {
  337. font-size: 26rpx;
  338. color: #666;
  339. display: block;
  340. line-height: 1.7;
  341. margin-bottom: 16rpx;
  342. }
  343. .modal-link {
  344. font-size: 24rpx;
  345. color: #667eea;
  346. display: block;
  347. text-align: center;
  348. margin-bottom: 24rpx;
  349. text-decoration: underline;
  350. }
  351. .modal-checkbox {
  352. margin-bottom: 32rpx;
  353. }
  354. .checkbox-label {
  355. font-size: 24rpx;
  356. color: #333;
  357. margin-left: 8rpx;
  358. }
  359. .modal-buttons {
  360. display: flex;
  361. gap: 20rpx;
  362. }
  363. .modal-btn-cancel {
  364. flex: 1;
  365. background: #f5f5f5;
  366. color: #666;
  367. border-radius: 44rpx;
  368. font-size: 28rpx;
  369. padding: 20rpx;
  370. text-align: center;
  371. }
  372. .modal-btn-confirm {
  373. flex: 1;
  374. background: #F97316;
  375. color: #fff;
  376. border-radius: 44rpx;
  377. font-size: 28rpx;
  378. padding: 20rpx;
  379. text-align: center;
  380. }
  381. .modal-btn-confirm[disabled] {
  382. background: #ccc;
  383. }
  384. </style>