user-edit.vue 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. <template>
  2. <view class="user-edit-container">
  3. <view class="header">
  4. <text class="title">{{ isEdit ? '完善个人信息' : '选择角色' }}</text>
  5. <text class="subtitle">{{ isEdit ? '以下信息可帮助您更好地使用服务' : '请选择您的身份类型' }}</text>
  6. </view>
  7. <!-- 角色选择(新用户首次登录时显示) -->
  8. <view class="role-select-section" v-if="!isEdit && showRoleSelect">
  9. <view
  10. class="role-card"
  11. :class="{ active: form.role === 'parent' }"
  12. @click="selectRole('parent')"
  13. >
  14. <text class="role-icon">👨‍👩‍👧</text>
  15. <text class="role-name">家长</text>
  16. <text class="role-desc">管理孩子任务、审核奖励</text>
  17. </view>
  18. <view
  19. class="role-card"
  20. :class="{ active: form.role === 'child' }"
  21. @click="selectRole('child')"
  22. >
  23. <text class="role-icon">👶</text>
  24. <text class="role-name">孩子</text>
  25. <text class="role-desc">执行任务、赚取积分</text>
  26. </view>
  27. <view
  28. class="role-card"
  29. :class="{ active: form.role === 'teacher' }"
  30. @click="selectRole('teacher')"
  31. >
  32. <text class="role-icon">👨‍🏫</text>
  33. <text class="role-name">成长规划师</text>
  34. <text class="role-desc">DAN测评、制定成长方案</text>
  35. </view>
  36. <button class="btn-next" @click="confirmRole" :disabled="!form.role">
  37. 下一步
  38. </button>
  39. </view>
  40. <!-- 详细信息填写(选择角色后显示) -->
  41. <view v-if="isEdit && (!isNewUserFlow || hasFamily)">
  42. <view class="form-section">
  43. <!-- 头像 -->
  44. <view class="form-item avatar-item">
  45. <text class="label">头像</text>
  46. <view class="avatar-upload" @click="chooseAvatar">
  47. <image v-if="form.avatar" :src="form.avatar" class="avatar-preview"></image>
  48. <view v-else class="avatar-placeholder">
  49. <text class="iconfont icon-plus">+</text>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 昵称 -->
  54. <view class="form-item">
  55. <text class="label">昵称 <text class="required">*</text></text>
  56. <input type="text" v-model="form.nickname" placeholder="请输入昵称" class="input" maxlength="20" />
  57. </view>
  58. <!-- 新用户流程:仅展示头像+昵称,其余字段按需在功能页补充 -->
  59. <view v-if="!isNewUserFlow">
  60. <!-- 真实姓名 -->
  61. <view class="form-item">
  62. <text class="label">真实姓名</text>
  63. <input type="text" v-model="form.realName" placeholder="选填,用于家庭管理" class="input" maxlength="20" />
  64. </view>
  65. <!-- 性别(成长规划师必填) -->
  66. <view class="form-item">
  67. <text class="label">性别 <text class="required" v-if="form.role === 'teacher'">*</text></text>
  68. <radio-group class="role-select" @change="onGenderChange">
  69. <label class="role-option">
  70. <radio value="male" :checked="form.gender === 'male'" />
  71. <text>男</text>
  72. </label>
  73. <label class="role-option">
  74. <radio value="female" :checked="form.gender === 'female'" />
  75. <text>女</text>
  76. </label>
  77. </radio-group>
  78. </view>
  79. <!-- 身份证号(成长规划师必填) -->
  80. <view class="form-item">
  81. <text class="label">身份证号 <text class="required" v-if="form.role === 'teacher'">*</text></text>
  82. <input type="idcard" v-model="form.idCard" placeholder="选填,填写后自动生成生日" class="input" maxlength="18" @input="onIdCardInput" />
  83. </view>
  84. <!-- 生日 -->
  85. <view class="form-item">
  86. <text class="label">生日</text>
  87. <picker mode="date" :value="form.birthday" @change="onBirthdayChange">
  88. <view class="picker-input">{{ form.birthday || '请选择生日' }}</view>
  89. </picker>
  90. </view>
  91. <!-- 出生时辰 -->
  92. <view class="form-item">
  93. <text class="label">出生时辰</text>
  94. <picker mode="selector" :range="birthHourOptions" :value="birthHourIndex" @change="onBirthHourChange">
  95. <view class="picker-input">{{ form.birthHour || '请选择出生时辰' }}</view>
  96. </picker>
  97. </view>
  98. <!-- 民族 -->
  99. <view class="form-item">
  100. <text class="label">民族</text>
  101. <picker mode="selector" :range="ethnicityOptions" :value="ethnicityIndex" @change="onEthnicityChange">
  102. <view class="picker-input">{{ form.ethnicity || '请选择民族' }}</view>
  103. </picker>
  104. </view>
  105. <!-- 血型 -->
  106. <view class="form-item">
  107. <text class="label">血型</text>
  108. <picker mode="selector" :range="bloodTypeOptions" :value="bloodTypeIndex" @change="onBloodTypeChange">
  109. <view class="picker-input">{{ form.bloodType || '请选择血型' }}</view>
  110. </picker>
  111. </view>
  112. <!-- 最高学历 -->
  113. <view class="form-item">
  114. <text class="label">最高学历</text>
  115. <picker mode="selector" :range="educationOptions" :value="educationIndex" @change="onEducationChange">
  116. <view class="picker-input">{{ form.highestEducation || '请选择最高学历' }}</view>
  117. </picker>
  118. </view>
  119. <!-- 婚姻状态 -->
  120. <view class="form-item">
  121. <text class="label">婚姻状态</text>
  122. <picker mode="selector" :range="maritalOptions" :value="maritalIndex" @change="onMaritalChange">
  123. <view class="picker-input">{{ form.maritalStatus || '请选择婚姻状态' }}</view>
  124. </picker>
  125. </view>
  126. <!-- 所在地区(省市区三级联动,与订单收货人地址一致) -->
  127. <view class="form-item address-row" @click="openRegionPicker">
  128. <text class="label">所在地区 <text class="required" v-if="isNewUserFlow">*</text></text>
  129. <view class="address-value-wrap">
  130. <text v-if="form.address" class="address-value">{{ form.address }}</text>
  131. <text v-else class="address-placeholder">请选择省市区</text>
  132. <text class="address-arrow">›</text>
  133. </view>
  134. </view>
  135. <!-- 详细地址 -->
  136. <view class="form-item">
  137. <text class="label">详细地址</text>
  138. <input type="text" v-model="form.street" placeholder="请输入详细地址(选填)" class="input" maxlength="100" @input="updateFullAddress" />
  139. </view>
  140. <!-- 兴趣爱好 -->
  141. <view class="form-item">
  142. <text class="label">兴趣爱好</text>
  143. <input type="text" v-model="form.hobbies" placeholder="如: 阅读、运动、音乐" class="input" maxlength="200" />
  144. </view>
  145. <!-- 饮食偏好 -->
  146. <view class="form-item">
  147. <text class="label">饮食偏好</text>
  148. <input type="text" v-model="form.dietPreferences" placeholder="如: 清淡、无辣、低糖" class="input" maxlength="200" />
  149. </view>
  150. </view>
  151. <!-- 成长规划师额外字段 -->
  152. <view class="form-section-title" v-if="form.role === 'teacher'">成长规划师信息</view>
  153. <!-- 成长规划师证书号(成长规划师必填) -->
  154. <view class="form-item" v-if="form.role === 'teacher'">
  155. <text class="label">成长规划师证书号 <text class="required">*</text></text>
  156. <input type="text" v-model="form.teacherNo" placeholder="请输入成长规划师证书号" class="input" maxlength="50" />
  157. </view>
  158. <!-- 成长规划师照片(成长规划师必填) -->
  159. <view class="form-item" v-if="form.role === 'teacher'">
  160. <text class="label">成长规划师照片 <text class="required">*</text></text>
  161. <view class="photo-upload" @click="chooseTeacherPhoto">
  162. <image v-if="form.teacherPhoto" :src="form.teacherPhoto" class="photo-preview" mode="aspectFill"></image>
  163. <view v-else class="photo-placeholder">
  164. <text class="plus-icon">+</text>
  165. <text class="placeholder-text">上传照片</text>
  166. </view>
  167. </view>
  168. </view>
  169. <!-- 成长规划师证书照片(成长规划师必填) -->
  170. <view class="form-item" v-if="form.role === 'teacher'">
  171. <text class="label">成长规划师证书 <text class="required">*</text></text>
  172. <view class="photo-upload" @click="chooseCertificate">
  173. <image v-if="form.certificateImage" :src="form.certificateImage" class="photo-preview" mode="aspectFill"></image>
  174. <view v-else class="photo-placeholder">
  175. <text class="plus-icon">+</text>
  176. <text class="placeholder-text">上传证书</text>
  177. </view>
  178. </view>
  179. </view>
  180. </view>
  181. <!-- AI助手选择(已屏蔽)
  182. <view class="family-section" v-if="isEdit">
  183. <view class="section-title">🤖 选择AI助手</view>
  184. <view class="mascot-cards">
  185. <view class="mascot-card" :class="{ active: form.mascot === 'xibao' }" @click="selectMascot('xibao')">
  186. <view class="mascot-card-icon xibao-bg">🌟</view>
  187. <view class="mascot-card-info">
  188. <text class="mascot-card-name">浠宝</text>
  189. <text class="mascot-card-desc">温柔活泼的阳光女孩</text>
  190. </view>
  191. </view>
  192. <view class="mascot-card" :class="{ active: form.mascot === 'fubao' }" @click="selectMascot('fubao')">
  193. <view class="mascot-card-icon fubao-bg">💪</view>
  194. <view class="mascot-card-info">
  195. <text class="mascot-card-name">福宝</text>
  196. <text class="mascot-card-desc">开朗自信的阳光男孩</text>
  197. </view>
  198. </view>
  199. </view>
  200. </view> -->
  201. <!-- 新用户流程:保存资料 / 跳过(不再创建或加入家庭) -->
  202. <view class="family-section" v-if="isEdit && form.role !== 'teacher' && isNewUserFlow">
  203. <view class="family-actions">
  204. <button class="btn btn-primary" @click="saveUserInfo" :loading="loading">
  205. 保存资料
  206. </button>
  207. <button class="btn btn-skip" @click="skip">
  208. 跳过
  209. </button>
  210. </view>
  211. </view>
  212. <!-- 修改个人资料保存按钮(非新用户流程时显示) -->
  213. <view class="family-section" v-if="isEdit && !isNewUserFlow && form.role !== 'teacher'">
  214. <view class="family-actions">
  215. <button class="btn btn-primary" @click="saveUserInfo" :loading="loading">
  216. 保存修改
  217. </button>
  218. </view>
  219. </view>
  220. <!-- 成长规划师提交按钮 -->
  221. <view class="family-section" v-if="isEdit && form.role === 'teacher' && isNewUserFlow">
  222. <view class="family-actions">
  223. <button class="btn btn-primary" @click="submitTeacherInfo" :loading="loading">
  224. 提交申请
  225. </button>
  226. </view>
  227. </view>
  228. </view>
  229. <view v-else-if="isEdit">
  230. <FamilyEmptyState type="card" />
  231. </view>
  232. <!-- 区域选择弹窗(省市区三级联动) -->
  233. <view class="region-mask" v-if="showRegionPicker" @click="cancelRegion">
  234. <view class="region-picker" @click.stop>
  235. <view class="region-header">
  236. <text class="region-header-btn" @click="cancelRegion">取消</text>
  237. <text class="region-header-title">选择地区</text>
  238. <text class="region-header-btn region-confirm" @click="confirmRegion">确定</text>
  239. </view>
  240. <view class="region-breadcrumb">
  241. <text
  242. v-for="(name, i) in regionBreadcrumb"
  243. :key="i"
  244. :class="['crumb-item', i === currentRegionLevel ? 'crumb-active' : '']"
  245. @click="jumpRegionLevel(i)"
  246. >{{ name || '请选择' }}</text>
  247. <text class="crumb-arrow" v-if="currentRegionLevel < 2">›</text>
  248. </view>
  249. <scroll-view class="region-list" scroll-y>
  250. <view
  251. v-for="(item, idx) in currentRegionItems"
  252. :key="idx"
  253. :class="['region-item', isRegionSelected(item) ? 'region-item-selected' : '']"
  254. @click="selectRegionItem(item)"
  255. >
  256. <text class="region-item-text">{{ getRegionName(item) }}</text>
  257. <text class="region-item-check" v-if="isRegionSelected(item)">✓</text>
  258. </view>
  259. <view v-if="regionLoading" class="region-loading">
  260. <text>加载中...</text>
  261. </view>
  262. </scroll-view>
  263. </view>
  264. </view>
  265. </view>
  266. </template>
  267. <script>
  268. import { updateUserInfo, createFamily, directRegister, getUserInfo, requestJoinByCode } from '../../utils/api.js'
  269. import config from '@/config.js'
  270. export default {
  271. data() {
  272. return {
  273. isEdit: false,
  274. showRoleSelect: true,
  275. isNewUserFlow: false, // 是否是新用户注册流程
  276. form: {
  277. role: '',
  278. avatar: '',
  279. nickname: '',
  280. realName: '',
  281. gender: '',
  282. idCard: '',
  283. birthday: '',
  284. birthHour: '',
  285. inviteCode: '',
  286. mascot: '',
  287. phone: '',
  288. teacherNo: '',
  289. teacherPhoto: '',
  290. certificateImage: '',
  291. ethnicity: '',
  292. bloodType: '',
  293. highestEducation: '',
  294. maritalStatus: '',
  295. hobbies: '',
  296. dietPreferences: '',
  297. address: '',
  298. province: '',
  299. city: '',
  300. district: '',
  301. street: ''
  302. },
  303. // 区域选择弹窗(省市区三级联动,与订单收货人地址一致)
  304. showRegionPicker: false,
  305. currentRegionLevel: 0, // 0=省份, 1=城市, 2=区县
  306. regionLoading: false,
  307. provinceList: [],
  308. cityList: [],
  309. districtList: [],
  310. tempProvince: null,
  311. tempCity: null,
  312. tempDistrict: null,
  313. ethnicityOptions: ['汉族','蒙古族','回族','藏族','维吾尔族','苗族','彝族','壮族','布依族','朝鲜族','满族','侗族','瑶族','白族','土家族','哈尼族','哈萨克族','傣族','黎族','其他'],
  314. bloodTypeOptions: ['A','B','AB','O','未知'],
  315. birthHourOptions: ['子时(23-01)','丑时(01-03)','寅时(03-05)','卯时(05-07)','辰时(07-09)','巳时(09-11)','午时(11-13)','未时(13-15)','申时(15-17)','酉时(17-19)','戌时(19-21)','亥时(21-23)'],
  316. birthHourValues: ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'],
  317. educationOptions: ['小学','初中','高中','中专','大专','本科','硕士','博士'],
  318. maritalOptions: ['未婚','已婚','离异','丧偶'],
  319. loading: false,
  320. userId: '',
  321. token: ''
  322. }
  323. },
  324. computed: {
  325. ethnicityIndex() {
  326. return this.ethnicityOptions.indexOf(this.form.ethnicity)
  327. },
  328. bloodTypeIndex() {
  329. return this.bloodTypeOptions.indexOf(this.form.bloodType)
  330. },
  331. birthHourIndex() {
  332. return this.birthHourValues.indexOf(this.form.birthHour)
  333. },
  334. educationIndex() {
  335. return this.educationOptions.indexOf(this.form.highestEducation)
  336. },
  337. maritalIndex() {
  338. return this.maritalOptions.indexOf(this.form.maritalStatus)
  339. },
  340. hasFamily: function() {
  341. if (this.token) return true
  342. return this.$store.getters.hasFamily
  343. },
  344. regionBreadcrumb: function() {
  345. var names = []
  346. names.push(this.tempProvince ? this.getRegionName(this.tempProvince) : '省份')
  347. names.push(this.tempCity ? this.getRegionName(this.tempCity) : '城市')
  348. names.push(this.tempDistrict ? this.getRegionName(this.tempDistrict) : '区县')
  349. return names
  350. },
  351. currentRegionItems: function() {
  352. if (this.currentRegionLevel === 0) return this.provinceList
  353. if (this.currentRegionLevel === 1) return this.cityList
  354. if (this.currentRegionLevel === 2) return this.districtList
  355. return []
  356. },
  357. },
  358. onLoad(options) {
  359. if (options.token && options.userId) {
  360. this.token = options.token
  361. this.userId = options.userId
  362. if (options.autoParent === 'true') {
  363. // 首次登录:跳过角色选择,默认 parent,直接进入信息补充页
  364. this.showRoleSelect = false
  365. this.isEdit = true
  366. this.isNewUserFlow = true
  367. this.form.role = 'parent'
  368. } else {
  369. // 检查是否需要选择角色(新用户)- 只有明确传递 needsRoleSelect=true 才显示选择
  370. this.showRoleSelect = options.needsRoleSelect === 'true'
  371. this.isEdit = !this.showRoleSelect
  372. this.isNewUserFlow = options.needsRoleSelect === 'true'
  373. }
  374. } else if (options.phone && (options.autoParent === 'true' || options.needsRoleSelect === 'true')) {
  375. // 测试环境:没有token,只有手机号,是新用户注册流程
  376. this.showRoleSelect = options.needsRoleSelect === 'true'
  377. this.isEdit = !this.showRoleSelect
  378. this.isNewUserFlow = true
  379. if (options.autoParent === 'true') {
  380. this.form.role = 'parent'
  381. }
  382. // 将手机号传递给表单
  383. this.form.phone = options.phone
  384. } else {
  385. // 其他情况:默认进入编辑模式(如从"我的"页面进入)
  386. this.showRoleSelect = false
  387. this.isEdit = true
  388. this.isNewUserFlow = false
  389. }
  390. // 非新用户流程时,加载已有用户信息
  391. if (this.isEdit && !this.isNewUserFlow) {
  392. this.loadUserInfo()
  393. }
  394. },
  395. methods: {
  396. selectRole(role) {
  397. this.form.role = role
  398. },
  399. confirmRole() {
  400. if (!this.form.role) {
  401. uni.showToast({ title: '请选择角色', icon: 'none' })
  402. return
  403. }
  404. this.showRoleSelect = false
  405. this.isEdit = true
  406. // 测试环境:直接注册用户(不需要验证码)
  407. if (!this.token && this.form.phone) {
  408. // 自动进入家庭绑定流程
  409. }
  410. },
  411. chooseAvatar() {
  412. uni.chooseImage({
  413. count: 1,
  414. success: (res) => {
  415. this.form.avatar = res.tempFilePaths[0]
  416. }
  417. })
  418. },
  419. chooseTeacherPhoto() {
  420. uni.chooseImage({
  421. count: 1,
  422. success: (res) => {
  423. this.form.teacherPhoto = res.tempFilePaths[0]
  424. }
  425. })
  426. },
  427. chooseCertificate() {
  428. uni.chooseImage({
  429. count: 1,
  430. success: (res) => {
  431. this.form.certificateImage = res.tempFilePaths[0]
  432. }
  433. })
  434. },
  435. onGenderChange(e) {
  436. this.form.gender = e.detail.value
  437. },
  438. onIdCardInput(e) {
  439. const idCard = e.detail.value
  440. if (idCard.length === 18) {
  441. // 根据身份证号自动提取生日
  442. const year = idCard.substring(6, 10)
  443. const month = idCard.substring(10, 12)
  444. const day = idCard.substring(12, 14)
  445. this.form.birthday = `${year}-${month}-${day}`
  446. }
  447. },
  448. onBirthdayChange(e) {
  449. this.form.birthday = e.detail.value
  450. },
  451. onEthnicityChange(e) {
  452. this.form.ethnicity = this.ethnicityOptions[e.detail.value]
  453. },
  454. onBloodTypeChange(e) {
  455. this.form.bloodType = this.bloodTypeOptions[e.detail.value]
  456. },
  457. onBirthHourChange(e) {
  458. this.form.birthHour = this.birthHourValues[e.detail.value]
  459. },
  460. onEducationChange(e) {
  461. this.form.highestEducation = this.educationOptions[e.detail.value]
  462. },
  463. onMaritalChange(e) {
  464. this.form.maritalStatus = this.maritalOptions[e.detail.value]
  465. },
  466. selectMascot(code) {
  467. this.form.mascot = code
  468. },
  469. // ===== 省市区三级联动弹窗(与订单收货人地址一致) =====
  470. openRegionPicker() {
  471. this.tempProvince = this.form.province ? { name: this.form.province, id: null } : null
  472. this.tempCity = this.form.city ? { name: this.form.city, id: null } : null
  473. this.tempDistrict = this.form.district ? { name: this.form.district, id: null } : null
  474. this.currentRegionLevel = 0
  475. this.showRegionPicker = true
  476. this.loadRegionProvinces()
  477. },
  478. loadRegionProvinces() {
  479. var self = this
  480. self.regionLoading = true
  481. uni.request({
  482. url: config.api('/api/region/provinces'),
  483. method: 'POST',
  484. header: {
  485. 'Content-Type': 'application/json',
  486. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  487. },
  488. success: function(res) {
  489. self.regionLoading = false
  490. if (res.data && res.data.code === 200) {
  491. self.provinceList = res.data.data || []
  492. }
  493. },
  494. fail: function() {
  495. self.regionLoading = false
  496. }
  497. })
  498. },
  499. loadRegionCities(parentId) {
  500. if (!parentId) return
  501. var self = this
  502. self.regionLoading = true
  503. uni.request({
  504. url: config.api('/api/region/cities'),
  505. method: 'POST',
  506. data: { parentId: parentId },
  507. header: {
  508. 'Content-Type': 'application/json',
  509. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  510. },
  511. success: function(res) {
  512. self.regionLoading = false
  513. if (res.data && res.data.code === 200) {
  514. self.cityList = res.data.data || []
  515. }
  516. },
  517. fail: function() {
  518. self.regionLoading = false
  519. }
  520. })
  521. },
  522. loadRegionDistricts(parentId) {
  523. if (!parentId) return
  524. var self = this
  525. self.regionLoading = true
  526. uni.request({
  527. url: config.api('/api/region/districts'),
  528. method: 'POST',
  529. data: { parentId: parentId },
  530. header: {
  531. 'Content-Type': 'application/json',
  532. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  533. },
  534. success: function(res) {
  535. self.regionLoading = false
  536. if (res.data && res.data.code === 200) {
  537. self.districtList = res.data.data || []
  538. }
  539. },
  540. fail: function() {
  541. self.regionLoading = false
  542. }
  543. })
  544. },
  545. selectRegionItem(item) {
  546. if (this.currentRegionLevel === 0) {
  547. this.tempProvince = item
  548. this.tempCity = null
  549. this.tempDistrict = null
  550. this.cityList = []
  551. this.districtList = []
  552. this.currentRegionLevel = 1
  553. this.loadRegionCities(this.getRegionId(item))
  554. } else if (this.currentRegionLevel === 1) {
  555. this.tempCity = item
  556. this.tempDistrict = null
  557. this.districtList = []
  558. this.currentRegionLevel = 2
  559. this.loadRegionDistricts(this.getRegionId(item))
  560. } else if (this.currentRegionLevel === 2) {
  561. this.tempDistrict = item
  562. this.confirmRegion()
  563. }
  564. },
  565. confirmRegion() {
  566. if (this.tempProvince) {
  567. this.form.province = this.getRegionName(this.tempProvince)
  568. }
  569. if (this.tempCity) {
  570. this.form.city = this.getRegionName(this.tempCity)
  571. }
  572. if (this.tempDistrict) {
  573. this.form.district = this.getRegionName(this.tempDistrict)
  574. }
  575. this.updateFullAddress()
  576. this.showRegionPicker = false
  577. },
  578. cancelRegion() {
  579. this.showRegionPicker = false
  580. },
  581. jumpRegionLevel(level) {
  582. if (level < this.currentRegionLevel) {
  583. this.currentRegionLevel = level
  584. }
  585. },
  586. getRegionName(item) {
  587. return item && (item.name || item.province || item.city || item.district || '')
  588. },
  589. getRegionId(item) {
  590. return item && (item.id || item.code)
  591. },
  592. isRegionSelected(item) {
  593. var name = this.getRegionName(item)
  594. if (this.currentRegionLevel === 0) return this.tempProvince && this.getRegionName(this.tempProvince) === name
  595. if (this.currentRegionLevel === 1) return this.tempCity && this.getRegionName(this.tempCity) === name
  596. if (this.currentRegionLevel === 2) return this.tempDistrict && this.getRegionName(this.tempDistrict) === name
  597. return false
  598. },
  599. updateFullAddress() {
  600. this.form.address = (this.form.province || '') + (this.form.city || '') + (this.form.district || '') + (this.form.street || '')
  601. },
  602. // 加载已有用户信息
  603. async loadUserInfo() {
  604. try {
  605. const res = await getUserInfo()
  606. if (res.code === 200 && res.data) {
  607. const userData = res.data
  608. this.form.nickname = userData.nickname || ''
  609. this.form.realName = userData.realName || ''
  610. this.form.gender = userData.gender || ''
  611. this.form.idCard = userData.idCard || ''
  612. // 生日只取日期部分,去掉时间和 ISO 格式的 T 分隔符
  613. this.form.birthday = userData.birthday ? String(userData.birthday).split(/[T ]/)[0] : ''
  614. this.form.avatar = userData.avatar || ''
  615. this.form.role = userData.role || ''
  616. this.form.phone = userData.phone || ''
  617. this.form.ethnicity = userData.ethnicity || ''
  618. this.form.bloodType = userData.bloodType || ''
  619. this.form.highestEducation = userData.highestEducation || ''
  620. this.form.maritalStatus = userData.maritalStatus || ''
  621. this.form.hobbies = userData.hobbies || ''
  622. this.form.dietPreferences = userData.dietPreferences || ''
  623. this.form.mascot = userData.mascot || ''
  624. this.form.address = userData.address || ''
  625. this.form.province = userData.province || ''
  626. this.form.city = userData.city || ''
  627. this.form.district = userData.district || ''
  628. this.form.street = userData.street || ''
  629. // 若无完整地址文本但有省市区,则拼接
  630. if (!this.form.address && (this.form.province || this.form.city || this.form.district)) {
  631. this.updateFullAddress()
  632. }
  633. }
  634. } catch (e) {
  635. console.error('加载用户信息失败', e)
  636. }
  637. },
  638. // 保存修改
  639. async saveUserInfo() {
  640. // 新用户流程:昵称可跳过(跳过时用默认昵称);老用户流程:昵称必填
  641. if (!this.isNewUserFlow && !this.form.nickname) {
  642. uni.showToast({ title: '请输入昵称', icon: 'none' })
  643. return
  644. }
  645. if (this.isNewUserFlow && !this.form.nickname) {
  646. var phone = this.form.phone || ''
  647. this.form.nickname = phone ? '用户' + phone.substring(phone.length - 4) : '微信用户'
  648. }
  649. // 从省市区街道字段同步完整地址文本
  650. this.updateFullAddress()
  651. this.loading = true
  652. try {
  653. await updateUserInfo({
  654. nickname: this.form.nickname,
  655. realName: this.form.realName,
  656. gender: this.form.gender,
  657. idCard: this.form.idCard,
  658. birthday: this.form.birthday,
  659. avatar: this.form.avatar,
  660. mascot: this.form.mascot,
  661. ethnicity: this.form.ethnicity,
  662. bloodType: this.form.bloodType,
  663. highestEducation: this.form.highestEducation,
  664. maritalStatus: this.form.maritalStatus,
  665. hobbies: this.form.hobbies,
  666. dietPreferences: this.form.dietPreferences,
  667. address: this.form.address,
  668. province: this.form.province,
  669. city: this.form.city,
  670. district: this.form.district,
  671. street: this.form.street
  672. })
  673. uni.showToast({ title: '保存成功', icon: 'success' })
  674. setTimeout(() => {
  675. if (this.isNewUserFlow) {
  676. this.navigateToHome()
  677. } else {
  678. uni.navigateBack()
  679. }
  680. }, 1500)
  681. } catch (e) {
  682. uni.showToast({ title: '保存失败', icon: 'none' })
  683. } finally {
  684. this.loading = false
  685. }
  686. },
  687. async submitTeacherInfo() {
  688. // 验证必填字段
  689. if (!this.form.nickname) {
  690. uni.showToast({ title: '请输入昵称', icon: 'none' })
  691. return
  692. }
  693. if (!this.form.gender) {
  694. uni.showToast({ title: '请选择性别', icon: 'none' })
  695. return
  696. }
  697. if (!this.form.idCard) {
  698. uni.showToast({ title: '请输入身份证号', icon: 'none' })
  699. return
  700. }
  701. if (!this.form.teacherNo) {
  702. uni.showToast({ title: '请输入成长规划师证书号', icon: 'none' })
  703. return
  704. }
  705. if (!this.form.teacherPhoto) {
  706. uni.showToast({ title: '请上传成长规划师照片', icon: 'none' })
  707. return
  708. }
  709. if (!this.form.certificateImage) {
  710. uni.showToast({ title: '请上传成长规划师证书', icon: 'none' })
  711. return
  712. }
  713. this.loading = true
  714. try {
  715. await updateUserInfo({
  716. ...this.form,
  717. roles: this.form.role
  718. })
  719. uni.showToast({ title: '提交成功,请等待审核' })
  720. setTimeout(() => {
  721. uni.switchTab({ url: '/pages/index-home/index' })
  722. }, 1500)
  723. } catch (e) {
  724. uni.showToast({ title: '提交失败', icon: 'none' })
  725. } finally {
  726. this.loading = false
  727. }
  728. },
  729. async joinFamilyAction() {
  730. if (!this.form.inviteCode) {
  731. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  732. return
  733. }
  734. if (!this.form.nickname) {
  735. uni.showToast({ title: '请输入昵称', icon: 'none' })
  736. return
  737. }
  738. this.loading = true
  739. try {
  740. let userId = this.userId
  741. // 测试环境:没有token,需要先注册用户
  742. if (!this.token && this.form.phone) {
  743. const regRes = await directRegister({
  744. phone: this.form.phone,
  745. nickname: this.form.nickname,
  746. avatar: this.form.avatar,
  747. role: this.form.role,
  748. mascot: this.form.mascot,
  749. birthday: this.form.birthday,
  750. gender: this.form.gender,
  751. inviteCode: this.form.inviteCode
  752. })
  753. userId = regRes.data.userId
  754. // 保存登录信息
  755. uni.setStorageSync('token', regRes.data.token)
  756. uni.setStorageSync('userId', userId)
  757. uni.setStorageSync('role', regRes.data.role)
  758. // 邀请码注册不写入 familyId(加入需管理员审批)
  759. } else {
  760. await updateUserInfo(this.form)
  761. await requestJoinByCode(this.form.inviteCode)
  762. }
  763. uni.showToast({ title: '申请已提交,等待管理员审批' })
  764. setTimeout(() => {
  765. this.navigateToHome()
  766. }, 1500)
  767. } catch (e) {
  768. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  769. } finally {
  770. this.loading = false
  771. }
  772. },
  773. async createFamilyAction() {
  774. if (!this.form.nickname) {
  775. uni.showToast({ title: '请输入昵称', icon: 'none' })
  776. return
  777. }
  778. this.loading = true
  779. try {
  780. let userId = this.userId
  781. // 测试环境:没有token,需要先注册用户
  782. if (!this.token && this.form.phone) {
  783. const regRes = await directRegister({
  784. phone: this.form.phone,
  785. nickname: this.form.nickname,
  786. avatar: this.form.avatar,
  787. role: this.form.role,
  788. mascot: this.form.mascot,
  789. birthday: this.form.birthday,
  790. gender: this.form.gender
  791. })
  792. userId = regRes.data.userId
  793. // 保存登录信息
  794. uni.setStorageSync('token', regRes.data.token)
  795. uni.setStorageSync('userId', userId)
  796. uni.setStorageSync('role', regRes.data.role)
  797. // 注册不写入 familyId(后端根据 userId 获取)
  798. } else {
  799. await updateUserInfo(this.form)
  800. const famRes = await createFamily(this.form.nickname + '的家庭')
  801. // 同步 familyId 到 store(hasFamily getter 依赖)
  802. if (famRes && famRes.data) {
  803. this.$store.commit('setFamilyId', famRes.data)
  804. }
  805. }
  806. uni.showToast({ title: '创建成功' })
  807. setTimeout(() => {
  808. this.navigateToHome()
  809. }, 1500)
  810. } catch (e) {
  811. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  812. } finally {
  813. this.loading = false
  814. }
  815. },
  816. skip() {
  817. this.navigateToHome()
  818. },
  819. navigateToHome() {
  820. // 保存 mascot 到 userInfo 存储,供 AI 聊天页读取
  821. if (this.form.mascot) {
  822. try {
  823. const userInfo = uni.getStorageSync('userInfo') || {}
  824. userInfo.mascot = this.form.mascot
  825. uni.setStorageSync('userInfo', userInfo)
  826. } catch (e) {
  827. console.error('保存 mascot 失败', e)
  828. }
  829. }
  830. // 用 switchTab 跳转带底部 TabBar 的首页(行/身/智/心/富)
  831. // parent-index/child-index 是分包页面(非 TabBar 页),用 navigateTo 会丢失底部 TabBar
  832. uni.switchTab({ url: '/pages/index-home/index' })
  833. }
  834. }
  835. }
  836. </script>
  837. <style scoped>
  838. .user-edit-container {
  839. min-height: 100vh;
  840. background: #f5f5f5;
  841. padding: 30rpx;
  842. }
  843. .header {
  844. margin-bottom: 40rpx;
  845. text-align: center;
  846. }
  847. .title {
  848. font-size: 40rpx;
  849. font-weight: bold;
  850. color: #333;
  851. display: block;
  852. margin-bottom: 10rpx;
  853. }
  854. .subtitle {
  855. font-size: 28rpx;
  856. color: #999;
  857. }
  858. /* 角色选择 */
  859. .role-select-section {
  860. margin-bottom: 40rpx;
  861. }
  862. .role-card {
  863. background: #fff;
  864. border-radius: 16rpx;
  865. padding: 40rpx;
  866. margin-bottom: 20rpx;
  867. display: flex;
  868. flex-direction: column;
  869. align-items: center;
  870. border: 2rpx solid transparent;
  871. transition: all 0.3s;
  872. }
  873. .role-card.active {
  874. border-color: #4CAF50;
  875. background: #f8fdf8;
  876. }
  877. .role-icon {
  878. font-size: 80rpx;
  879. margin-bottom: 16rpx;
  880. }
  881. .role-name {
  882. font-size: 32rpx;
  883. font-weight: bold;
  884. color: #333;
  885. margin-bottom: 8rpx;
  886. }
  887. .role-desc {
  888. font-size: 24rpx;
  889. color: #999;
  890. }
  891. .btn-next {
  892. background: #4CAF50;
  893. color: #fff;
  894. border-radius: 40rpx;
  895. margin-top: 20rpx;
  896. }
  897. /* 表单 */
  898. .form-section {
  899. background: #fff;
  900. border-radius: 16rpx;
  901. padding: 30rpx;
  902. margin-bottom: 30rpx;
  903. }
  904. .form-section-title {
  905. font-size: 30rpx;
  906. font-weight: bold;
  907. color: #333;
  908. margin-bottom: 20rpx;
  909. padding-bottom: 16rpx;
  910. border-bottom: 1rpx solid #f0f0f0;
  911. }
  912. .form-item {
  913. margin-bottom: 30rpx;
  914. }
  915. .label {
  916. display: block;
  917. font-size: 28rpx;
  918. color: #333;
  919. margin-bottom: 16rpx;
  920. }
  921. .required {
  922. color: #f44336;
  923. }
  924. .input, .picker-input {
  925. padding: 20rpx;
  926. background: #f5f5f5;
  927. border-radius: 12rpx;
  928. font-size: 28rpx;
  929. }
  930. .role-select {
  931. display: flex;
  932. gap: 40rpx;
  933. }
  934. .role-option {
  935. display: flex;
  936. align-items: center;
  937. gap: 8rpx;
  938. }
  939. /* 头像上传 */
  940. .avatar-upload {
  941. width: 150rpx;
  942. height: 150rpx;
  943. }
  944. .avatar-preview {
  945. width: 150rpx;
  946. height: 150rpx;
  947. border-radius: 50%;
  948. }
  949. .avatar-placeholder {
  950. width: 150rpx;
  951. height: 150rpx;
  952. background: #f5f5f5;
  953. border-radius: 50%;
  954. display: flex;
  955. align-items: center;
  956. justify-content: center;
  957. font-size: 60rpx;
  958. color: #ccc;
  959. }
  960. /* 照片上传 */
  961. .photo-upload {
  962. width: 300rpx;
  963. height: 200rpx;
  964. }
  965. .photo-preview {
  966. width: 300rpx;
  967. height: 200rpx;
  968. border-radius: 12rpx;
  969. }
  970. .photo-placeholder {
  971. width: 300rpx;
  972. height: 200rpx;
  973. background: #f5f5f5;
  974. border-radius: 12rpx;
  975. display: flex;
  976. flex-direction: column;
  977. align-items: center;
  978. justify-content: center;
  979. }
  980. .plus-icon {
  981. font-size: 60rpx;
  982. color: #ccc;
  983. }
  984. .placeholder-text {
  985. font-size: 24rpx;
  986. color: #999;
  987. margin-top: 8rpx;
  988. }
  989. /* AI助手选择 */
  990. .mascot-cards {
  991. display: flex;
  992. gap: 20rpx;
  993. margin-top: 16rpx;
  994. }
  995. .mascot-card {
  996. flex: 1;
  997. background: #f8f8f8;
  998. border-radius: 16rpx;
  999. padding: 24rpx;
  1000. display: flex;
  1001. align-items: center;
  1002. gap: 16rpx;
  1003. border: 2rpx solid transparent;
  1004. transition: all 0.3s;
  1005. }
  1006. .mascot-card.active {
  1007. border-color: #4CAF50;
  1008. background: #f0fdf4;
  1009. }
  1010. .mascot-card-icon {
  1011. width: 72rpx;
  1012. height: 72rpx;
  1013. border-radius: 50%;
  1014. display: flex;
  1015. align-items: center;
  1016. justify-content: center;
  1017. font-size: 36rpx;
  1018. flex-shrink: 0;
  1019. }
  1020. .xibao-bg {
  1021. background: linear-gradient(135deg, #FFF0DB, #FFD6A5);
  1022. }
  1023. .fubao-bg {
  1024. background: linear-gradient(135deg, #FFF8E1, #FCD34D);
  1025. }
  1026. .mascot-card-info {
  1027. display: flex;
  1028. flex-direction: column;
  1029. gap: 4rpx;
  1030. }
  1031. .mascot-card-name {
  1032. font-size: 30rpx;
  1033. font-weight: bold;
  1034. color: #333;
  1035. }
  1036. .mascot-card-desc {
  1037. font-size: 22rpx;
  1038. color: #999;
  1039. }
  1040. /* 家庭绑定 */
  1041. .family-section {
  1042. background: #fff;
  1043. border-radius: 16rpx;
  1044. padding: 30rpx;
  1045. }
  1046. .section-title {
  1047. font-size: 30rpx;
  1048. font-weight: bold;
  1049. color: #333;
  1050. margin-bottom: 20rpx;
  1051. }
  1052. .family-actions {
  1053. display: flex;
  1054. flex-direction: column;
  1055. gap: 20rpx;
  1056. margin-top: 30rpx;
  1057. }
  1058. .btn {
  1059. border-radius: 40rpx;
  1060. font-size: 30rpx;
  1061. }
  1062. .btn-primary {
  1063. background: #4CAF50;
  1064. color: #fff;
  1065. }
  1066. .btn-skip {
  1067. background: #f5f5f5;
  1068. color: #999;
  1069. }
  1070. /* ===== 区域选择弹窗 ===== */
  1071. .region-mask {
  1072. position: fixed;
  1073. top: 0;
  1074. left: 0;
  1075. right: 0;
  1076. bottom: 0;
  1077. background: rgba(0, 0, 0, 0.5);
  1078. z-index: 999;
  1079. display: flex;
  1080. align-items: flex-end;
  1081. }
  1082. .region-picker {
  1083. background: #fff;
  1084. border-radius: 20rpx 20rpx 0 0;
  1085. width: 100%;
  1086. max-height: 70vh;
  1087. display: flex;
  1088. flex-direction: column;
  1089. }
  1090. .region-header {
  1091. display: flex;
  1092. justify-content: space-between;
  1093. align-items: center;
  1094. padding: 24rpx 30rpx;
  1095. border-bottom: 1rpx solid #f0f0f0;
  1096. }
  1097. .region-header-btn {
  1098. font-size: 28rpx;
  1099. color: #999;
  1100. }
  1101. .region-confirm {
  1102. color: #F97316;
  1103. font-weight: bold;
  1104. }
  1105. .region-header-title {
  1106. font-size: 30rpx;
  1107. font-weight: bold;
  1108. color: #333;
  1109. }
  1110. .region-breadcrumb {
  1111. display: flex;
  1112. align-items: center;
  1113. padding: 20rpx 30rpx;
  1114. border-bottom: 1rpx solid #f5f5f5;
  1115. }
  1116. .crumb-item {
  1117. font-size: 26rpx;
  1118. color: #999;
  1119. padding: 8rpx 12rpx;
  1120. border-radius: 8rpx;
  1121. }
  1122. .crumb-item.crumb-active {
  1123. color: #F97316;
  1124. font-weight: bold;
  1125. }
  1126. .crumb-arrow {
  1127. font-size: 24rpx;
  1128. color: #ccc;
  1129. margin: 0 8rpx;
  1130. }
  1131. .region-list {
  1132. max-height: 50vh;
  1133. overflow-y: auto;
  1134. }
  1135. .region-item {
  1136. display: flex;
  1137. justify-content: space-between;
  1138. align-items: center;
  1139. padding: 28rpx 30rpx;
  1140. border-bottom: 1rpx solid #f5f5f5;
  1141. }
  1142. .region-item:active {
  1143. background: #FFF7ED;
  1144. }
  1145. .region-item-text {
  1146. font-size: 28rpx;
  1147. color: #333;
  1148. }
  1149. .region-item-check {
  1150. font-size: 28rpx;
  1151. color: #F97316;
  1152. font-weight: bold;
  1153. }
  1154. .region-item-selected {
  1155. background: #FFF7ED;
  1156. }
  1157. .region-item-selected .region-item-text {
  1158. color: #F97316;
  1159. font-weight: bold;
  1160. }
  1161. .region-loading {
  1162. padding: 40rpx;
  1163. text-align: center;
  1164. font-size: 26rpx;
  1165. color: #999;
  1166. }
  1167. /* ===== 地址行(所在地区) ===== */
  1168. .address-row {
  1169. display: flex;
  1170. justify-content: space-between;
  1171. align-items: center;
  1172. }
  1173. .address-value-wrap {
  1174. display: flex;
  1175. align-items: center;
  1176. flex: 1;
  1177. justify-content: flex-end;
  1178. }
  1179. .address-value {
  1180. font-size: 28rpx;
  1181. color: #333;
  1182. }
  1183. .address-placeholder {
  1184. font-size: 28rpx;
  1185. color: #999;
  1186. }
  1187. .address-arrow {
  1188. font-size: 28rpx;
  1189. color: #ccc;
  1190. margin-left: 8rpx;
  1191. }
  1192. </style>