profile.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <template>
  2. <view class="container">
  3. <!-- 退出切换按钮(仅家长切换到孩子状态时显示) -->
  4. <view class="exit-switch" v-if="isSwitchedChild" @click="exitSwitch">
  5. <text>🔄 退出切换</text>
  6. </view>
  7. <view class="user-card">
  8. <view class="avatar">👤</view>
  9. <view class="user-info">
  10. <view class="nickname">{{ nickname || '未设置昵称' }}</view>
  11. <view class="role">{{ role === 'parent' ? '家长模式' : (role === 'child' ? '孩子模式' : '成长规划师模式') }}</view>
  12. <view class="system-points" v-if="role === 'parent' && children.length > 0">🏅 系统积分: {{ children[0].systemPoints || 0 }}</view>
  13. </view>
  14. <!-- 切换按钮仅在非切换状态下显示 -->
  15. <button class="btn-switch" v-if="!isSwitchedChild" @click="switchMode">切换</button>
  16. </view>
  17. <view class="menu-list">
  18. <!-- 修改个人资料:所有角色可见 -->
  19. <view class="menu-item" @click="goToEditInfo">
  20. <text>📝 修改个人资料</text>
  21. <text class="arrow">›</text>
  22. </view>
  23. <!-- 我的孩子:仅家长可见 -->
  24. <view class="menu-item" v-if="role === 'parent'" @click="goToChildren">
  25. <text>👶 我的孩子</text>
  26. <text class="arrow">›</text>
  27. </view>
  28. <!-- 家庭成员 -->
  29. <view class="menu-item" v-if="role === 'parent'" @click="goToFamilyMembers">
  30. <text>👨‍👩‍👧‍👦 家庭成员</text>
  31. <text class="arrow">›</text>
  32. </view>
  33. <view class="menu-item" @click="goToPointsLogs">
  34. <text>📊 积分记录</text>
  35. <text class="arrow">›</text>
  36. </view>
  37. <!-- 设置/重置密码:仅家长和成长规划师可见,孩子不可见 -->
  38. <view class="menu-item" v-if="role === 'parent'" @click="showPasswordModal = true">
  39. <text>🔐 {{ hasPassword ? '重置密码' : '设置密码' }}</text>
  40. <text class="arrow">›</text>
  41. </view>
  42. <button class="menu-item menu-item-btn" open-type="share" @click="inviteFamily" v-if="role === 'parent'">
  43. <text>👨‍👩‍👧‍👦 邀请家人</text>
  44. <text class="arrow">›</text>
  45. </button>
  46. <button class="menu-item menu-item-btn" open-type="share" @click="inviteTeacher" v-if="role === 'parent'">
  47. <text>👨‍🏫 邀请规划师</text>
  48. <text class="arrow">›</text>
  49. </button>
  50. <!-- 会员中心(汐福俱乐部) -->
  51. <view class="menu-item" @click="goToMembership">
  52. <text>👑 会员中心</text>
  53. <text class="arrow">›</text>
  54. </view>
  55. <!-- 服务商中心(仅已通过服务商可见) -->
  56. <view class="menu-item" v-if="isVendor" @click="goToVendorCenter">
  57. <text>🏪 服务商中心</text>
  58. <text class="arrow">›</text>
  59. </view>
  60. <view class="menu-item" @click="logout">
  61. <text>🚪 退出登录</text>
  62. <text class="arrow">›</text>
  63. </view>
  64. </view>
  65. <!-- 设置/重置密码弹窗 -->
  66. <view class="modal-mask" v-if="showPasswordModal" @click="showPasswordModal = false">
  67. <view class="modal" @click.stop>
  68. <view class="modal-title">{{ hasPassword ? '重置密码' : '设置密码' }}</view>
  69. <view class="form-item" v-if="hasPassword">
  70. <input v-model="oldPassword" type="number" password placeholder="输入旧密码" maxlength="6" />
  71. </view>
  72. <view class="form-item">
  73. <input v-model="newPassword" type="number" password placeholder="输入新密码" maxlength="6" />
  74. </view>
  75. <view class="form-item">
  76. <input v-model="confirmPassword" type="number" password placeholder="确认新密码" maxlength="6" />
  77. </view>
  78. <view class="modal-btns">
  79. <button @click="showPasswordModal = false">取消</button>
  80. <button class="btn-primary" @click="savePassword">确定</button>
  81. </view>
  82. </view>
  83. </view>
  84. </view>
  85. </template>
  86. <script>
  87. import { setPassword, verifyPassword, getChildren, vendorStatus } from '../../utils/api.js'
  88. export default {
  89. data() {
  90. return {
  91. nickname: '',
  92. role: 'parent',
  93. hasPassword: false,
  94. showPasswordModal: false,
  95. oldPassword: '',
  96. newPassword: '',
  97. confirmPassword: '',
  98. children: [],
  99. isSwitchedChild: false,
  100. shareData: null,
  101. isVendor: false
  102. }
  103. },
  104. onShareAppMessage() {
  105. if (this.shareData) {
  106. return {
  107. title: this.shareData.title,
  108. path: this.shareData.path,
  109. imageUrl: '/static/invite-card.png'
  110. }
  111. }
  112. },
  113. onShow() {
  114. const currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  115. if (currentRole === 'teacher') {
  116. uni.redirectTo({ url: '/pages/teacher/teacher-profile' })
  117. return
  118. }
  119. var userInfo = uni.getStorageSync('userInfo')
  120. this.nickname = this.$store.state.nickname || (userInfo && userInfo.nickname) || ''
  121. // 使用 currentRole(视图角色),而非 role(实际角色)
  122. this.role = currentRole
  123. this.hasPassword = !!uni.getStorageSync('passwordSet')
  124. // 获取切换标记
  125. this.isSwitchedChild = uni.getStorageSync('isSwitchedChild') || false
  126. this.loadChildren()
  127. this.checkVendorStatus()
  128. },
  129. methods: {
  130. async loadChildren() {
  131. try {
  132. const res = await getChildren()
  133. this.children = res.data || []
  134. } catch (e) {
  135. console.error('获取孩子列表失败', e)
  136. }
  137. },
  138. goToEditInfo() {
  139. uni.navigateTo({ url: '/pages/user-edit/user-edit' })
  140. },
  141. goToChildren() {
  142. uni.navigateTo({ url: '/pages/profile/children' })
  143. },
  144. goToFamilyMembers() {
  145. uni.navigateTo({ url: '/pages/profile/family-members' })
  146. },
  147. goToPointsLogs() {
  148. uni.navigateTo({ url: '/pages/points/points' })
  149. },
  150. switchMode() {
  151. if (this.role === 'child') {
  152. this.verifyPasswordForSwitch(() => {
  153. this.doSwitchMode()
  154. })
  155. } else {
  156. this.doSwitchMode()
  157. }
  158. },
  159. doSwitchMode() {
  160. const newRole = this.role === 'parent' ? 'child' : 'parent'
  161. if (newRole === 'child') {
  162. if (this.children.length === 0) {
  163. this.loadChildren()
  164. }
  165. if (this.children.length === 0) {
  166. uni.showModal({
  167. title: '提示',
  168. content: '您还没有添加孩子,是否现在去添加?',
  169. success: (res) => {
  170. if (res.confirm) {
  171. uni.navigateTo({ url: '/pages/profile/create-child' })
  172. }
  173. }
  174. })
  175. return
  176. }
  177. if (this.children.length === 1) {
  178. this.$store.commit('switchToChild', this.children[0].id)
  179. this.role = newRole
  180. uni.showToast({ title: '已切换为孩子模式', icon: 'success' })
  181. return
  182. }
  183. uni.showActionSheet({
  184. itemList: this.children.map(c => c.nickname),
  185. success: (res) => {
  186. const selectedChild = this.children[res.tapIndex]
  187. this.$store.commit('switchToChild', selectedChild.id)
  188. this.role = newRole
  189. uni.showToast({ title: `已切换为${selectedChild.nickname}模式`, icon: 'success' })
  190. }
  191. })
  192. return
  193. }
  194. this.$store.commit('switchBackToParent')
  195. this.role = newRole
  196. uni.showToast({ title: `已切换为${newRole === 'parent' ? '家长' : '孩子'}模式`, icon: 'success' })
  197. },
  198. verifyPasswordForSwitch(callback) {
  199. uni.showModal({
  200. title: '验证密码',
  201. content: '请输入家长密码',
  202. editable: true,
  203. success: async (res) => {
  204. if (res.confirm && res.content) {
  205. try {
  206. const result = await verifyPassword(res.content)
  207. if (result.code === 200) {
  208. callback && callback()
  209. } else {
  210. uni.showToast({ title: '密码错误', icon: 'none' })
  211. }
  212. } catch (e) {
  213. uni.showToast({ title: '验证失败', icon: 'none' })
  214. }
  215. }
  216. }
  217. })
  218. },
  219. async savePassword() {
  220. if (this.newPassword.length !== 6) {
  221. uni.showToast({ title: '请输入6位密码', icon: 'none' })
  222. return
  223. }
  224. if (this.newPassword !== this.confirmPassword) {
  225. uni.showToast({ title: '两次密码不一致', icon: 'none' })
  226. return
  227. }
  228. if (this.hasPassword && this.oldPassword.length !== 6) {
  229. uni.showToast({ title: '请输入旧密码', icon: 'none' })
  230. return
  231. }
  232. try {
  233. if (this.hasPassword) {
  234. const verifyRes = await verifyPassword(this.oldPassword)
  235. if (verifyRes.code !== 200) {
  236. uni.showToast({ title: '旧密码错误', icon: 'none' })
  237. return
  238. }
  239. }
  240. await setPassword(this.newPassword)
  241. uni.setStorageSync('passwordSet', true)
  242. this.hasPassword = true
  243. uni.showToast({ title: '密码设置成功', icon: 'success' })
  244. this.showPasswordModal = false
  245. this.oldPassword = ''
  246. this.newPassword = ''
  247. this.confirmPassword = ''
  248. } catch (e) {
  249. console.error('设置密码失败', e)
  250. }
  251. },
  252. logout() {
  253. uni.showModal({
  254. title: '确认退出',
  255. content: '确定要退出登录吗?',
  256. success: (res) => {
  257. if (res.confirm) {
  258. this.$store.commit('logout')
  259. uni.reLaunch({ url: '/pages/login/login' })
  260. }
  261. }
  262. })
  263. },
  264. inviteFamily() {
  265. const userInfo = uni.getStorageSync('userInfo')
  266. const familyId = uni.getStorageSync('familyId')
  267. if (!familyId) {
  268. uni.showToast({ title: '您暂未加入家庭', icon: 'none' })
  269. return
  270. }
  271. this.inviteFamilyGenerate(familyId, userInfo)
  272. },
  273. async inviteFamilyGenerate(familyId, userInfo) {
  274. try {
  275. const { generateInviteCard } = require('../../utils/api.js')
  276. const res = await generateInviteCard('family', familyId)
  277. const code = res.data.code
  278. this.shareData = {
  279. title: (userInfo && userInfo.nickname ? userInfo.nickname : '家人') + ' 邀请你加入家庭',
  280. path: '/pages/login/login?invite_code=' + code
  281. }
  282. uni.showToast({ title: '点击右上角转发给TA', icon: 'none' })
  283. } catch (e) {
  284. uni.showToast({ title: e.message || '生成邀请失败', icon: 'none' })
  285. }
  286. },
  287. inviteTeacher() {
  288. const userInfo = uni.getStorageSync('userInfo')
  289. const familyId = uni.getStorageSync('familyId')
  290. if (!familyId) {
  291. uni.showToast({ title: '您暂未加入家庭', icon: 'none' })
  292. return
  293. }
  294. this.inviteTeacherGenerate(familyId, userInfo)
  295. },
  296. async inviteTeacherGenerate(familyId, userInfo) {
  297. try {
  298. const { generateInviteCard } = require('../../utils/api.js')
  299. const res = await generateInviteCard('guide', familyId)
  300. const code = res.data.code
  301. this.shareData = {
  302. title: (userInfo && userInfo.nickname ? userInfo.nickname : '家长') + ' 邀请你成为家庭成长规划师',
  303. path: '/pages/login/login?invite_code=' + code
  304. }
  305. uni.showToast({ title: '点击右上角转发给规划师', icon: 'none' })
  306. } catch (e) {
  307. uni.showToast({ title: e.message || '生成邀请失败', icon: 'none' })
  308. }
  309. },
  310. goToMembership() {
  311. uni.showToast({ title: '即将上线', icon: 'none' })
  312. },
  313. goToVendorCenter() {
  314. uni.navigateTo({ url: '/pages/vendor/center' })
  315. },
  316. async checkVendorStatus() {
  317. try {
  318. const res = await vendorStatus()
  319. this.isVendor = res.data && res.data.vendorStatus === 'approved'
  320. } catch (e) {
  321. this.isVendor = false
  322. }
  323. },
  324. exitSwitch() {
  325. if (!this.isSwitchedChild) return
  326. uni.showModal({
  327. title: '退出切换',
  328. content: '请输入家长密码以确认退出',
  329. editable: true,
  330. success: async (res) => {
  331. if (res.confirm && res.content) {
  332. try {
  333. const verifyResult = await verifyPassword(res.content)
  334. if (verifyResult.code === 200) {
  335. this.$store.commit('switchBackToParent')
  336. this.isSwitchedChild = false
  337. this.role = 'parent'
  338. uni.showToast({ title: '已退出切换', icon: 'success' })
  339. // 刷新首页
  340. uni.reLaunch({ url: '/pages/index/index' })
  341. } else {
  342. uni.showToast({ title: '密码错误', icon: 'none' })
  343. }
  344. } catch (e) {
  345. uni.showToast({ title: '验证失败', icon: 'none' })
  346. }
  347. }
  348. }
  349. })
  350. }
  351. }
  352. }
  353. </script>
  354. <style scoped>
  355. .container {
  356. padding: 30rpx;
  357. }
  358. .user-card {
  359. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  360. border-radius: 20rpx;
  361. padding: 40rpx;
  362. display: flex;
  363. align-items: center;
  364. color: #fff;
  365. }
  366. .avatar {
  367. font-size: 80rpx;
  368. margin-right: 30rpx;
  369. }
  370. .user-info {
  371. flex: 1;
  372. }
  373. .nickname {
  374. font-size: 36rpx;
  375. font-weight: bold;
  376. }
  377. .role {
  378. font-size: 24rpx;
  379. opacity: 0.8;
  380. }
  381. .system-points {
  382. font-size: 22rpx;
  383. color: #667eea;
  384. margin-top: 8rpx;
  385. font-weight: 500;
  386. }
  387. .btn-switch {
  388. background: rgba(255, 255, 255, 0.2);
  389. color: #fff;
  390. font-size: 24rpx;
  391. padding: 10rpx 20rpx;
  392. border-radius: 20rpx;
  393. }
  394. .menu-list {
  395. margin-top: 30rpx;
  396. background: #fff;
  397. border-radius: 16rpx;
  398. }
  399. .menu-item {
  400. padding: 30rpx;
  401. border-bottom: 1rpx solid #f0f0f0;
  402. display: flex;
  403. justify-content: space-between;
  404. font-size: 28rpx;
  405. }
  406. .menu-item:last-child {
  407. border-bottom: none;
  408. }
  409. .arrow {
  410. color: #ccc;
  411. font-size: 32rpx;
  412. }
  413. .menu-item-btn {
  414. display: flex;
  415. justify-content: space-between;
  416. align-items: center;
  417. width: 100%;
  418. padding: 28rpx 24rpx;
  419. border: none;
  420. background: transparent;
  421. font-size: 28rpx;
  422. color: #333;
  423. }
  424. .menu-item-btn::after { border: none; }
  425. .modal-mask {
  426. position: fixed;
  427. top: 0;
  428. left: 0;
  429. right: 0;
  430. bottom: 0;
  431. background: rgba(0, 0, 0, 0.5);
  432. display: flex;
  433. align-items: center;
  434. justify-content: center;
  435. }
  436. .modal {
  437. background: #fff;
  438. border-radius: 20rpx;
  439. padding: 40rpx;
  440. width: 80%;
  441. }
  442. .modal-title {
  443. font-size: 32rpx;
  444. font-weight: bold;
  445. margin-bottom: 30rpx;
  446. text-align: center;
  447. }
  448. .form-item {
  449. margin-bottom: 20rpx;
  450. }
  451. .form-item input {
  452. border: 1rpx solid #ddd;
  453. border-radius: 10rpx;
  454. padding: 20rpx;
  455. font-size: 28rpx;
  456. }
  457. .modal-btns {
  458. display: flex;
  459. gap: 20rpx;
  460. margin-top: 30rpx;
  461. }
  462. .modal-btns button {
  463. flex: 1;
  464. }
  465. </style>