info.nvue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view class="page">
  3. <!-- 头像区 -->
  4. <view class="section">
  5. <view class="info-item" @click="changeAvatar">
  6. <text class="item-label">头像</text>
  7. <view class="item-right">
  8. <image class="avatar-preview" :src="avatar" mode="aspectFill"></image>
  9. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  10. </view>
  11. </view>
  12. </view>
  13. <!-- 基本信息 -->
  14. <view class="section">
  15. <view class="info-item" @click="editNickname">
  16. <text class="item-label">昵称</text>
  17. <view class="item-right">
  18. <text class="item-value">{{nickname}}</text>
  19. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  20. </view>
  21. </view>
  22. <view class="info-item" @click="editGender">
  23. <text class="item-label">性别</text>
  24. <view class="item-right">
  25. <text class="item-value">{{gender === 1 ? '男' : '女'}}</text>
  26. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  27. </view>
  28. </view>
  29. <view class="info-item" @click="editNumberField('age')">
  30. <text class="item-label">年龄</text>
  31. <view class="item-right">
  32. <text class="item-value">{{age ? age + '岁' : '去设置'}}</text>
  33. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  34. </view>
  35. </view>
  36. <view class="info-item" @click="editNumberField('shoulderHeight')">
  37. <text class="item-label">肩高</text>
  38. <view class="item-right">
  39. <text class="item-value">{{shoulderHeight ? shoulderHeight + 'cm' : '去设置'}}</text>
  40. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  41. </view>
  42. </view>
  43. <view class="info-item" @click="editNumberField('shoulderWidth')">
  44. <text class="item-label">肩宽</text>
  45. <view class="item-right">
  46. <text class="item-value">{{shoulderWidth ? shoulderWidth + 'cm' : '去设置'}}</text>
  47. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  48. </view>
  49. </view>
  50. <view class="info-item" @click="editNumberField('weight')">
  51. <text class="item-label">体重</text>
  52. <view class="item-right">
  53. <text class="item-value">{{weight ? weight + 'kg' : '去设置'}}</text>
  54. <image class="item-arrow" src="/static/my/arrowright.png" mode="aspectFill"></image>
  55. </view>
  56. </view>
  57. <view class="info-item">
  58. <text class="item-label">手机号</text>
  59. <view class="item-right">
  60. <text class="item-value">{{phone}}</text>
  61. </view>
  62. </view>
  63. <view class="info-item" style="border-bottom-width: 0;">
  64. <text class="item-label">AppId</text>
  65. <view class="item-right">
  66. <text class="item-value">{{appid}}</text>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 修改昵称弹窗 -->
  71. <view class="nickname-overlay" v-if="showNicknamePopup" @click="closeNicknamePopup">
  72. <view class="nickname-dialog" @click.stop="" :style="{marginBottom: keyboardHeight + 'px'}">
  73. <text class="dialog-title">修改昵称</text>
  74. <view class="input-box">
  75. <input class="nickname-input" v-model="nicknameInput" placeholder="请输入昵称" placeholder-class="input-placeholder"
  76. maxlength="12" :adjust-position="false" :focus="showNicknamePopup"
  77. @keyboardheightchange="onKeyboardHeight" />
  78. </view>
  79. <view class="dialog-btns">
  80. <view class="dialog-btn" @click="closeNicknamePopup">
  81. <text class="dialog-cancel">取消</text>
  82. </view>
  83. <view class="dialog-divider"></view>
  84. <view class="dialog-btn" @click="saveNickname">
  85. <text class="dialog-confirm">确定</text>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. <!-- 数字输入弹窗 -->
  91. <view class="nickname-overlay" v-if="showNumberPopup" @click="closeNumberPopup">
  92. <view class="nickname-dialog" @click.stop="" :style="{marginBottom: keyboardHeight + 'px'}">
  93. <text class="dialog-title">修改{{ getNumberFieldLabel() }}</text>
  94. <view class="input-box">
  95. <input class="nickname-input" v-model="numberInput" :type="currentEditField === 'age' ? 'number' : 'digit'" :placeholder="'请输入' + getNumberFieldLabel()" placeholder-class="input-placeholder"
  96. :adjust-position="false" :focus="showNumberPopup"
  97. @keyboardheightchange="onKeyboardHeight" />
  98. </view>
  99. <view class="dialog-btns">
  100. <view class="dialog-btn" @click="closeNumberPopup">
  101. <text class="dialog-cancel">取消</text>
  102. </view>
  103. <view class="dialog-divider"></view>
  104. <view class="dialog-btn" @click="saveNumberField">
  105. <text class="dialog-confirm">确定</text>
  106. </view>
  107. </view>
  108. </view>
  109. </view>
  110. <ay-toast ref="ayToast" />
  111. </view>
  112. </template>
  113. <script>
  114. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  115. import { getProfile, uploadAvatar as uploadAvatarApi, updateProfile } from '@/utils/api/user.js'
  116. export default {
  117. components: {
  118. ayToast
  119. },
  120. data() {
  121. return {
  122. avatar: '/static/144x144.png',
  123. nickname: '用户',
  124. phone: '',
  125. appid: '',
  126. gender: 1, // 1男 2女
  127. age: '',
  128. shoulderHeight: '',
  129. shoulderWidth: '',
  130. weight: '',
  131. showNicknamePopup: false,
  132. showGenderPopup: false,
  133. showNumberPopup: false,
  134. nicknameInput: '',
  135. numberInput: '',
  136. currentEditField: '', // 'age' | 'shoulderHeight' | 'shoulderWidth' | 'weight'
  137. keyboardHeight: 0
  138. }
  139. },
  140. onLoad() {
  141. this.loadUserInfo()
  142. },
  143. methods: {
  144. async loadUserInfo() {
  145. try {
  146. const profile = await getProfile()
  147. this.phone = profile.accountPhone || profile.phone || ''
  148. this.nickname = profile.nickname || '用户'
  149. this.appid = profile.appId || ''
  150. this.avatar = profile.avatar || '/static/144x144.png'
  151. uni.setStorageSync('phone', this.phone)
  152. uni.setStorageSync('nickname', this.nickname)
  153. uni.setStorageSync('appId', this.appid)
  154. uni.setStorageSync('avatar', this.avatar)
  155. uni.setStorageSync('current_manage_user', profile)
  156. uni.setStorageSync('current_manage_user_id', profile.profileId || profile.id)
  157. return
  158. } catch (e) {
  159. console.error('加载个人资料失败,使用本地缓存', e)
  160. }
  161. const phone = uni.getStorageSync('phone') || '138****8888'
  162. const nickname = uni.getStorageSync('nickname') || '用户'
  163. const appid = uni.getStorageSync('appId') || ''
  164. const avatar = uni.getStorageSync('avatar') || '/static/144x144.png'
  165. this.phone = phone
  166. this.nickname = nickname
  167. this.appid = appid
  168. this.avatar = avatar
  169. this.gender = uni.getStorageSync('gender') || 1
  170. this.age = uni.getStorageSync('age') || ''
  171. this.shoulderHeight = uni.getStorageSync('shoulderHeight') || ''
  172. this.shoulderWidth = uni.getStorageSync('shoulderWidth') || ''
  173. this.weight = uni.getStorageSync('weight') || ''
  174. },
  175. changeAvatar() {
  176. uni.chooseImage({
  177. count: 1,
  178. sizeType: ['compressed'],
  179. sourceType: ['album', 'camera'],
  180. success: (res) => {
  181. const tempPath = res.tempFilePaths[0]
  182. this.uploadAvatar(tempPath)
  183. }
  184. })
  185. },
  186. async uploadAvatar(filePath) {
  187. try {
  188. // 上传图片到 OSS
  189. const res = await uploadAvatarApi(filePath)
  190. const avatarUrl = res.url
  191. // 更新后端用户资料
  192. await updateProfile({ avatar: avatarUrl })
  193. // 更新本地存储和页面显示
  194. this.avatar = avatarUrl
  195. uni.setStorageSync('avatar', avatarUrl)
  196. this.$refs.ayToast.success('头像修改成功')
  197. } catch (e) {
  198. // 错误已由 http 层统一提示
  199. }
  200. },
  201. editNickname() {
  202. this.nicknameInput = this.nickname
  203. this.showNicknamePopup = true
  204. },
  205. closeNicknamePopup() {
  206. this.showNicknamePopup = false
  207. this.keyboardHeight = 0
  208. },
  209. onKeyboardHeight(e) {
  210. this.keyboardHeight = e.detail.height || 0
  211. },
  212. async saveNickname() {
  213. const val = this.nicknameInput.trim()
  214. if (!val) {
  215. this.$refs.ayToast.error('昵称不能为空')
  216. return
  217. }
  218. try {
  219. // 更新后端用户资料
  220. await updateProfile({ nickname: val })
  221. this.nickname = val
  222. uni.setStorageSync('nickname', val)
  223. this.showNicknamePopup = false
  224. this.keyboardHeight = 0
  225. this.$refs.ayToast.success('修改成功')
  226. } catch (e) {
  227. // 错误已由 http 层统一提示
  228. }
  229. },
  230. editGender() {
  231. uni.showActionSheet({
  232. itemList: ['男', '女'],
  233. success: (res) => {
  234. const val = res.tapIndex === 0 ? 1 : 2
  235. this.gender = val
  236. uni.setStorageSync('gender', val)
  237. // TODO: 如果后端支持,在这里调用 updateProfile({ gender: val })
  238. this.$refs.ayToast.success('修改成功')
  239. }
  240. })
  241. },
  242. editNumberField(field) {
  243. this.currentEditField = field
  244. this.numberInput = String(this[field] || '')
  245. this.showNumberPopup = true
  246. },
  247. closeNumberPopup() {
  248. this.showNumberPopup = false
  249. this.keyboardHeight = 0
  250. },
  251. getNumberFieldLabel() {
  252. const map = {
  253. age: '年龄',
  254. shoulderHeight: '肩高',
  255. shoulderWidth: '肩宽',
  256. weight: '体重'
  257. }
  258. return map[this.currentEditField] || ''
  259. },
  260. saveNumberField() {
  261. const val = this.numberInput.trim()
  262. if (!val) {
  263. this.$refs.ayToast.error(`请输入${this.getNumberFieldLabel()}`)
  264. return
  265. }
  266. const num = Number(val)
  267. if (isNaN(num) || num <= 0) {
  268. this.$refs.ayToast.error('请输入有效的数字')
  269. return
  270. }
  271. if (this.currentEditField === 'shoulderHeight' && (num < 50 || num > 250)) {
  272. this.$refs.ayToast.error('肩高范围为50-250cm')
  273. return
  274. }
  275. if (this.currentEditField === 'shoulderWidth' && (num < 20 || num > 80)) {
  276. this.$refs.ayToast.error('肩宽范围为20-80cm')
  277. return
  278. }
  279. if (this.currentEditField === 'weight' && (num < 10 || num > 300)) {
  280. this.$refs.ayToast.error('体重范围为10-300kg')
  281. return
  282. }
  283. if (this.currentEditField === 'age' && (num < 1 || num > 150)) {
  284. this.$refs.ayToast.error('年龄范围为1-150岁')
  285. return
  286. }
  287. this[this.currentEditField] = num
  288. uni.setStorageSync(this.currentEditField, num)
  289. // TODO: 如果后端支持,在这里调用 updateProfile({ [this.currentEditField]: num })
  290. this.showNumberPopup = false
  291. this.keyboardHeight = 0
  292. this.$refs.ayToast.success('修改成功')
  293. }
  294. },
  295. onShow() {
  296. // 返回时刷新用户信息
  297. this.loadUserInfo()
  298. }
  299. }
  300. </script>
  301. <style>
  302. .page {
  303. flex: 1;
  304. background-color: #F5F6F8;
  305. }
  306. /* 分组 */
  307. .section {
  308. background-color: #FFFFFF;
  309. margin-top: 20rpx;
  310. border-radius: 20rpx;
  311. margin-left: 24rpx;
  312. margin-right: 24rpx;
  313. overflow: hidden;
  314. }
  315. /* 信息项 */
  316. .info-item {
  317. flex-direction: row;
  318. align-items: center;
  319. justify-content: space-between;
  320. height: 110rpx;
  321. padding-left: 32rpx;
  322. padding-right: 32rpx;
  323. border-bottom-width: 1px;
  324. border-bottom-color: #F0F0F0;
  325. border-bottom-style: solid;
  326. }
  327. .item-label {
  328. font-size: 30rpx;
  329. color: #333333;
  330. }
  331. .item-right {
  332. flex-direction: row;
  333. align-items: center;
  334. }
  335. .item-value {
  336. font-size: 30rpx;
  337. color: #999999;
  338. margin-right: 16rpx;
  339. }
  340. .item-arrow {
  341. width: 32rpx;
  342. height: 32rpx;
  343. }
  344. .avatar-preview {
  345. width: 80rpx;
  346. height: 80rpx;
  347. border-radius: 80rpx;
  348. margin-right: 16rpx;
  349. }
  350. /* 昵称修改弹窗 */
  351. .nickname-overlay {
  352. position: fixed;
  353. top: 0;
  354. left: 0;
  355. right: 0;
  356. bottom: 0;
  357. background-color: rgba(0, 0, 0, 0.4);
  358. justify-content: center;
  359. align-items: center;
  360. }
  361. .nickname-dialog {
  362. width: 600rpx;
  363. background-color: #FFFFFF;
  364. border-radius: 28rpx;
  365. align-items: center;
  366. overflow: hidden;
  367. }
  368. .dialog-title {
  369. font-size: 34rpx;
  370. color: #333333;
  371. font-weight: bold;
  372. margin-top: 48rpx;
  373. margin-bottom: 40rpx;
  374. }
  375. .input-box {
  376. width: 520rpx;
  377. height: 88rpx;
  378. background-color: #F5F6F8;
  379. border-radius: 16rpx;
  380. justify-content: center;
  381. padding-left: 24rpx;
  382. padding-right: 24rpx;
  383. margin-bottom: 48rpx;
  384. }
  385. .nickname-input {
  386. font-size: 30rpx;
  387. color: #333333;
  388. }
  389. .dialog-btns {
  390. flex-direction: row;
  391. align-self: stretch;
  392. border-top-width: 1px;
  393. border-top-color: #F0F0F0;
  394. border-top-style: solid;
  395. height: 96rpx;
  396. align-items: center;
  397. }
  398. .dialog-btn {
  399. flex: 1;
  400. height: 96rpx;
  401. justify-content: center;
  402. align-items: center;
  403. }
  404. .dialog-cancel {
  405. font-size: 32rpx;
  406. color: #999999;
  407. }
  408. .dialog-divider {
  409. width: 1px;
  410. height: 96rpx;
  411. background-color: #F0F0F0;
  412. }
  413. .dialog-confirm {
  414. font-size: 32rpx;
  415. color: #389588;
  416. font-weight: bold;
  417. }
  418. </style>