index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <template>
  2. <view class="profile-page">
  3. <nav-bar title="个人信息" bgColor="#fff" color="#000"></nav-bar>
  4. <view class="form-container" v-if="!loading">
  5. <view class="avatar-section" @click="handleChooseAvatar">
  6. <image :src="formData.avatar || '/static/images/profile.png'" class="avatar-img" mode="aspectFill"></image>
  7. <view class="avatar-tip">点击更换头像</view>
  8. </view>
  9. <view class="form-group">
  10. <view class="form-item">
  11. <text class="form-label require">用户昵称</text>
  12. <input class="form-input" v-model="formData.nickName" placeholder="请输入昵称" placeholder-class="input-placeholder" />
  13. </view>
  14. <view class="line"></view>
  15. <view class="form-item">
  16. <text class="form-label require">手机号码</text>
  17. <input class="form-input" type="number" v-model="formData.phonenumber" placeholder="请输入手机号码" placeholder-class="input-placeholder" />
  18. </view>
  19. <view class="line"></view>
  20. <view class="form-item">
  21. <text class="form-label">邮箱</text>
  22. <input class="form-input" v-model="formData.email" placeholder="请输入邮箱" placeholder-class="input-placeholder" />
  23. </view>
  24. <view class="line"></view>
  25. <view class="form-item">
  26. <text class="form-label">性别</text>
  27. <picker :range="sexOptions" range-key="label" @change="onSexChange">
  28. <view class="picker-value" :class="{'placeholder': formData.sex === undefined || formData.sex === ''}">
  29. {{ getSexLabel }}
  30. </view>
  31. </picker>
  32. </view>
  33. </view>
  34. <view class="btn-group">
  35. <button class="submit-btn" @click="submit">保存修改</button>
  36. </view>
  37. </view>
  38. <!-- 自定义权限弹窗 -->
  39. <view class="permission-modal-mask" v-if="showPermissionModal" @touchmove.stop.prevent>
  40. <view class="permission-modal-content">
  41. <view class="permission-modal-header">
  42. <view class="shield-icon-box">
  43. <uni-icons type="info-filled" size="30" color="#ff9500"></uni-icons>
  44. </view>
  45. <text class="permission-modal-title">权限申请提示</text>
  46. </view>
  47. <view class="permission-modal-body">
  48. <text class="permission-desc-main">为了正常更换头像,我们需要获取您的以下权限:</text>
  49. <view class="permission-list-box">
  50. <view class="permission-item-box">
  51. <view class="icon-circle">
  52. <uni-icons type="images-filled" size="20" color="#ff9500"></uni-icons>
  53. </view>
  54. <view class="item-text-box">
  55. <text class="item-name-text">存储与相册权限</text>
  56. <text class="item-desc-text">用于从相册中选择已有照片作为头像</text>
  57. </view>
  58. </view>
  59. <view class="permission-item-box">
  60. <view class="icon-circle">
  61. <uni-icons type="camera-filled" size="20" color="#ff9500"></uni-icons>
  62. </view>
  63. <view class="item-text-box">
  64. <text class="item-name-text">相机拍照权限</text>
  65. <text class="item-desc-text">用于拍摄新照片并上传作为头像</text>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. <view class="permission-modal-footer">
  71. <button class="btn-decline" @click="declinePermission">暂不授权</button>
  72. <button class="btn-grant" @click="confirmPermission">同意并授权</button>
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. </template>
  78. <script setup>
  79. // @Author: Antigravity
  80. import { ref, reactive, computed } from 'vue'
  81. import { onLoad } from '@dcloudio/uni-app'
  82. import navBar from '@/components/nav-bar/index.vue'
  83. import { getInfo, updateUserProfile, uploadAvatar } from '@/api/system/user'
  84. const loading = ref(true)
  85. const showPermissionModal = ref(false)
  86. const formData = reactive({
  87. avatar: '',
  88. nickName: '',
  89. phonenumber: '',
  90. email: '',
  91. sex: ''
  92. })
  93. const sexOptions = [
  94. { label: '男', value: '0' },
  95. { label: '女', value: '1' },
  96. { label: '未知', value: '2' }
  97. ]
  98. onLoad(async () => {
  99. try {
  100. const res = await getInfo()
  101. if (res && res.user) {
  102. formData.avatar = res.user.avatarUrl || ''
  103. formData.nickName = res.user.nickName || ''
  104. formData.phonenumber = res.user.phonenumber || ''
  105. formData.email = res.user.email || ''
  106. formData.sex = res.user.sex || '2'
  107. }
  108. } catch (e) {
  109. console.error('获取个人信息失败', e)
  110. uni.showToast({ title: typeof e === 'string' ? e : '获取个人信息失败', icon: 'none' })
  111. } finally {
  112. loading.value = false
  113. }
  114. })
  115. const getSexLabel = computed(() => {
  116. const opt = sexOptions.find(o => o.value === String(formData.sex))
  117. return opt ? opt.label : '请选择'
  118. })
  119. const onSexChange = (e) => {
  120. formData.sex = sexOptions[e.detail.value].value
  121. }
  122. // 选择并上传头像
  123. const handleChooseAvatar = () => {
  124. const hasShown = uni.getStorageSync('has_shown_avatar_permission')
  125. if (!hasShown) {
  126. showPermissionModal.value = true
  127. } else {
  128. chooseAvatarImage()
  129. }
  130. }
  131. // 实际执行图片选择与上传
  132. const chooseAvatarImage = () => {
  133. uni.chooseImage({
  134. count: 1,
  135. sizeType: ['compressed'],
  136. sourceType: ['album', 'camera'],
  137. success: async (res) => {
  138. const tempFilePaths = res.tempFilePaths;
  139. try {
  140. uni.showLoading({ title: '上传中...' });
  141. const uploadRes = await uploadAvatar(tempFilePaths[0]);
  142. formData.avatar = uploadRes.imgUrl;
  143. uni.hideLoading();
  144. uni.showToast({ title: '头像上传成功', icon: 'success' });
  145. } catch (e) {
  146. uni.hideLoading();
  147. console.error('上传头像失败', e);
  148. uni.showToast({ title: typeof e === 'string' ? e : '上传头像失败', icon: 'none' });
  149. }
  150. }
  151. });
  152. }
  153. const confirmPermission = () => {
  154. showPermissionModal.value = false
  155. uni.setStorageSync('has_shown_avatar_permission', true)
  156. chooseAvatarImage()
  157. }
  158. const declinePermission = () => {
  159. showPermissionModal.value = false
  160. uni.setStorageSync('has_shown_avatar_permission', true)
  161. }
  162. const validate = () => {
  163. if (!formData.nickName || !formData.nickName.trim()) {
  164. uni.showToast({ title: '请输入昵称', icon: 'none' })
  165. return false
  166. }
  167. if (!formData.phonenumber || !formData.phonenumber.trim()) {
  168. uni.showToast({ title: '请输入手机号码', icon: 'none' })
  169. return false
  170. }
  171. if (!/^1[3-9]\d{9}$/.test(formData.phonenumber)) {
  172. uni.showToast({ title: '请输入正确的手机号码', icon: 'none' })
  173. return false
  174. }
  175. if (formData.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) {
  176. uni.showToast({ title: '请输入正确的邮箱格式', icon: 'none' })
  177. return false
  178. }
  179. return true
  180. }
  181. const submit = async () => {
  182. if (!validate()) return
  183. try {
  184. uni.showLoading({ title: '保存中...' })
  185. await updateUserProfile(formData)
  186. uni.hideLoading()
  187. uni.showToast({ title: '个人信息修改成功', icon: 'success' })
  188. setTimeout(() => {
  189. uni.navigateBack()
  190. }, 1000)
  191. } catch (err) {
  192. uni.hideLoading()
  193. console.log('保存失败', err)
  194. uni.showToast({ title: typeof err === 'string' ? err : '保存失败', icon: 'none' })
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. .profile-page {
  200. min-height: 100vh;
  201. background-color: #f7f8fa;
  202. }
  203. .form-container {
  204. padding-top: 0;
  205. }
  206. .avatar-section {
  207. display: flex;
  208. flex-direction: column;
  209. align-items: center;
  210. padding: 60rpx 0;
  211. background-color: #fff;
  212. margin-bottom: 20rpx;
  213. border-bottom: 2rpx solid #f0f0f0;
  214. .avatar-img {
  215. width: 160rpx;
  216. height: 160rpx;
  217. border-radius: 80rpx;
  218. background-color: #f7f8fa;
  219. }
  220. .avatar-tip {
  221. margin-top: 20rpx;
  222. font-size: 24rpx;
  223. color: #999;
  224. }
  225. }
  226. .form-group {
  227. background: #fff;
  228. margin: 0 24rpx;
  229. border-radius: 20rpx;
  230. padding: 0 32rpx;
  231. }
  232. .form-item {
  233. display: flex;
  234. align-items: center;
  235. padding: 24rpx 0;
  236. }
  237. .form-label {
  238. width: 200rpx;
  239. font-size: 28rpx;
  240. color: #333;
  241. flex-shrink: 0;
  242. &::before {
  243. content: '*';
  244. color: transparent;
  245. margin-right: 4rpx;
  246. }
  247. &.require::before {
  248. color: #f56c6c;
  249. }
  250. }
  251. .form-input {
  252. flex: 1;
  253. height: 48rpx;
  254. line-height: 48rpx;
  255. font-size: 28rpx;
  256. color: #333;
  257. text-align: right;
  258. }
  259. .input-placeholder {
  260. color: #c0c4cc;
  261. font-size: 28rpx;
  262. }
  263. .line {
  264. height: 2rpx;
  265. background: #eee;
  266. margin: 0;
  267. }
  268. .btn-group {
  269. margin: 60rpx 32rpx 32rpx;
  270. }
  271. .submit-btn {
  272. height: 88rpx;
  273. line-height: 88rpx;
  274. background: linear-gradient(90deg, #ffd53f, #ff9500);
  275. color: #fff;
  276. border: none;
  277. border-radius: 44rpx;
  278. font-size: 30rpx;
  279. font-weight: bold;
  280. &::after { border: none; }
  281. }
  282. .picker-value {
  283. flex: 1;
  284. font-size: 28rpx;
  285. color: #333;
  286. text-align: right;
  287. height: 48rpx;
  288. line-height: 48rpx;
  289. padding-right: 20rpx;
  290. }
  291. .picker-value.placeholder {
  292. color: #c0c4cc;
  293. }
  294. /* ==================== 权限弹窗样式 ==================== */
  295. .permission-modal-mask {
  296. position: fixed;
  297. top: 0;
  298. left: 0;
  299. right: 0;
  300. bottom: 0;
  301. background: rgba(0, 0, 0, 0.6);
  302. backdrop-filter: blur(10rpx);
  303. display: flex;
  304. align-items: center;
  305. justify-content: center;
  306. z-index: 9999;
  307. padding: 50rpx;
  308. }
  309. .permission-modal-content {
  310. width: 100%;
  311. background: #ffffff;
  312. border-radius: 36rpx;
  313. display: flex;
  314. flex-direction: column;
  315. overflow: hidden;
  316. box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.15);
  317. animation: modalFadeIn 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  318. }
  319. @keyframes modalFadeIn {
  320. from {
  321. opacity: 0;
  322. transform: scale(0.9);
  323. }
  324. to {
  325. opacity: 1;
  326. transform: scale(1);
  327. }
  328. }
  329. .permission-modal-header {
  330. display: flex;
  331. flex-direction: column;
  332. align-items: center;
  333. padding: 48rpx 40rpx 20rpx;
  334. .shield-icon-box {
  335. width: 90rpx;
  336. height: 90rpx;
  337. border-radius: 45rpx;
  338. background: #fff8eb;
  339. display: flex;
  340. align-items: center;
  341. justify-content: center;
  342. margin-bottom: 16rpx;
  343. }
  344. .permission-modal-title {
  345. font-size: 32rpx;
  346. font-weight: 800;
  347. color: #1a1a1a;
  348. }
  349. }
  350. .permission-modal-body {
  351. padding: 0 40rpx 30rpx;
  352. .permission-desc-main {
  353. font-size: 26rpx;
  354. color: #666;
  355. line-height: 1.5;
  356. display: block;
  357. text-align: center;
  358. margin-bottom: 24rpx;
  359. }
  360. }
  361. .permission-list-box {
  362. display: flex;
  363. flex-direction: column;
  364. gap: 16rpx;
  365. }
  366. .permission-item-box {
  367. display: flex;
  368. align-items: center;
  369. background: #fdfdfd;
  370. border: 2rpx solid #f6f7f9;
  371. border-radius: 20rpx;
  372. padding: 20rpx;
  373. .icon-circle {
  374. width: 64rpx;
  375. height: 64rpx;
  376. border-radius: 32rpx;
  377. background: #fff5e6;
  378. display: flex;
  379. align-items: center;
  380. justify-content: center;
  381. margin-right: 16rpx;
  382. flex-shrink: 0;
  383. }
  384. .item-text-box {
  385. flex: 1;
  386. .item-name-text {
  387. font-size: 26rpx;
  388. font-weight: 700;
  389. color: #333;
  390. display: block;
  391. margin-bottom: 4rpx;
  392. text-align: left;
  393. }
  394. .item-desc-text {
  395. font-size: 22rpx;
  396. color: #999;
  397. display: block;
  398. text-align: left;
  399. }
  400. }
  401. }
  402. .permission-modal-footer {
  403. padding: 24rpx 40rpx 40rpx;
  404. display: flex;
  405. gap: 20rpx;
  406. border-top: 2rpx solid #f5f6f8;
  407. background: #ffffff;
  408. }
  409. .btn-decline {
  410. flex: 1;
  411. height: 80rpx;
  412. line-height: 80rpx;
  413. font-size: 26rpx;
  414. color: #666;
  415. background: #f5f6f8;
  416. border-radius: 40rpx;
  417. border: none;
  418. font-weight: 600;
  419. text-align: center;
  420. &::after { border: none; }
  421. }
  422. .btn-grant {
  423. flex: 1.2;
  424. height: 80rpx;
  425. line-height: 80rpx;
  426. font-size: 26rpx;
  427. color: #fff;
  428. background: linear-gradient(90deg, #ffd53f, #ff9500);
  429. border-radius: 40rpx;
  430. border: none;
  431. font-weight: 700;
  432. box-shadow: 0 6rpx 16rpx rgba(255, 149, 0, 0.2);
  433. text-align: center;
  434. &::after { border: none; }
  435. }
  436. </style>