info.nvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 { 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. loadUserInfo() {
  145. const phone = uni.getStorageSync('phone') || '138****8888'
  146. const nickname = uni.getStorageSync('nickname') || '用户'
  147. const appid = uni.getStorageSync('appId') || ''
  148. const avatar = uni.getStorageSync('avatar') || '/static/144x144.png'
  149. this.phone = phone
  150. this.nickname = nickname
  151. this.appid = appid
  152. this.avatar = avatar
  153. this.gender = uni.getStorageSync('gender') || 1
  154. this.age = uni.getStorageSync('age') || ''
  155. this.shoulderHeight = uni.getStorageSync('shoulderHeight') || ''
  156. this.shoulderWidth = uni.getStorageSync('shoulderWidth') || ''
  157. this.weight = uni.getStorageSync('weight') || ''
  158. },
  159. changeAvatar() {
  160. uni.chooseImage({
  161. count: 1,
  162. sizeType: ['compressed'],
  163. sourceType: ['album', 'camera'],
  164. success: (res) => {
  165. const tempPath = res.tempFilePaths[0]
  166. this.uploadAvatar(tempPath)
  167. }
  168. })
  169. },
  170. async uploadAvatar(filePath) {
  171. try {
  172. // 上传图片到 OSS
  173. const res = await uploadAvatarApi(filePath)
  174. const avatarUrl = res.url
  175. // 更新后端用户资料
  176. await updateProfile({ avatar: avatarUrl })
  177. // 更新本地存储和页面显示
  178. this.avatar = avatarUrl
  179. uni.setStorageSync('avatar', avatarUrl)
  180. this.$refs.ayToast.success('头像修改成功')
  181. } catch (e) {
  182. // 错误已由 http 层统一提示
  183. }
  184. },
  185. editNickname() {
  186. this.nicknameInput = this.nickname
  187. this.showNicknamePopup = true
  188. },
  189. closeNicknamePopup() {
  190. this.showNicknamePopup = false
  191. this.keyboardHeight = 0
  192. },
  193. onKeyboardHeight(e) {
  194. this.keyboardHeight = e.detail.height || 0
  195. },
  196. async saveNickname() {
  197. const val = this.nicknameInput.trim()
  198. if (!val) {
  199. this.$refs.ayToast.error('昵称不能为空')
  200. return
  201. }
  202. try {
  203. // 更新后端用户资料
  204. await updateProfile({ nickname: val })
  205. this.nickname = val
  206. uni.setStorageSync('nickname', val)
  207. this.showNicknamePopup = false
  208. this.keyboardHeight = 0
  209. this.$refs.ayToast.success('修改成功')
  210. } catch (e) {
  211. // 错误已由 http 层统一提示
  212. }
  213. },
  214. editGender() {
  215. uni.showActionSheet({
  216. itemList: ['男', '女'],
  217. success: (res) => {
  218. const val = res.tapIndex === 0 ? 1 : 2
  219. this.gender = val
  220. uni.setStorageSync('gender', val)
  221. // TODO: 如果后端支持,在这里调用 updateProfile({ gender: val })
  222. this.$refs.ayToast.success('修改成功')
  223. }
  224. })
  225. },
  226. editNumberField(field) {
  227. this.currentEditField = field
  228. this.numberInput = String(this[field] || '')
  229. this.showNumberPopup = true
  230. },
  231. closeNumberPopup() {
  232. this.showNumberPopup = false
  233. this.keyboardHeight = 0
  234. },
  235. getNumberFieldLabel() {
  236. const map = {
  237. age: '年龄',
  238. shoulderHeight: '肩高',
  239. shoulderWidth: '肩宽',
  240. weight: '体重'
  241. }
  242. return map[this.currentEditField] || ''
  243. },
  244. saveNumberField() {
  245. const val = this.numberInput.trim()
  246. if (!val) {
  247. this.$refs.ayToast.error(`请输入${this.getNumberFieldLabel()}`)
  248. return
  249. }
  250. const num = Number(val)
  251. if (isNaN(num) || num <= 0) {
  252. this.$refs.ayToast.error('请输入有效的数字')
  253. return
  254. }
  255. if (this.currentEditField === 'shoulderHeight' && (num < 50 || num > 250)) {
  256. this.$refs.ayToast.error('肩高范围为50-250cm')
  257. return
  258. }
  259. if (this.currentEditField === 'shoulderWidth' && (num < 20 || num > 80)) {
  260. this.$refs.ayToast.error('肩宽范围为20-80cm')
  261. return
  262. }
  263. if (this.currentEditField === 'weight' && (num < 10 || num > 300)) {
  264. this.$refs.ayToast.error('体重范围为10-300kg')
  265. return
  266. }
  267. if (this.currentEditField === 'age' && (num < 1 || num > 150)) {
  268. this.$refs.ayToast.error('年龄范围为1-150岁')
  269. return
  270. }
  271. this[this.currentEditField] = num
  272. uni.setStorageSync(this.currentEditField, num)
  273. // TODO: 如果后端支持,在这里调用 updateProfile({ [this.currentEditField]: num })
  274. this.showNumberPopup = false
  275. this.keyboardHeight = 0
  276. this.$refs.ayToast.success('修改成功')
  277. }
  278. },
  279. onShow() {
  280. // 返回时刷新用户信息
  281. this.loadUserInfo()
  282. }
  283. }
  284. </script>
  285. <style>
  286. .page {
  287. flex: 1;
  288. background-color: #F5F6F8;
  289. }
  290. /* 分组 */
  291. .section {
  292. background-color: #FFFFFF;
  293. margin-top: 20rpx;
  294. border-radius: 20rpx;
  295. margin-left: 24rpx;
  296. margin-right: 24rpx;
  297. overflow: hidden;
  298. }
  299. /* 信息项 */
  300. .info-item {
  301. flex-direction: row;
  302. align-items: center;
  303. justify-content: space-between;
  304. height: 110rpx;
  305. padding-left: 32rpx;
  306. padding-right: 32rpx;
  307. border-bottom-width: 1px;
  308. border-bottom-color: #F0F0F0;
  309. border-bottom-style: solid;
  310. }
  311. .item-label {
  312. font-size: 30rpx;
  313. color: #333333;
  314. }
  315. .item-right {
  316. flex-direction: row;
  317. align-items: center;
  318. }
  319. .item-value {
  320. font-size: 30rpx;
  321. color: #999999;
  322. margin-right: 16rpx;
  323. }
  324. .item-arrow {
  325. width: 32rpx;
  326. height: 32rpx;
  327. }
  328. .avatar-preview {
  329. width: 80rpx;
  330. height: 80rpx;
  331. border-radius: 80rpx;
  332. margin-right: 16rpx;
  333. }
  334. /* 昵称修改弹窗 */
  335. .nickname-overlay {
  336. position: fixed;
  337. top: 0;
  338. left: 0;
  339. right: 0;
  340. bottom: 0;
  341. background-color: rgba(0, 0, 0, 0.4);
  342. justify-content: center;
  343. align-items: center;
  344. }
  345. .nickname-dialog {
  346. width: 600rpx;
  347. background-color: #FFFFFF;
  348. border-radius: 28rpx;
  349. align-items: center;
  350. overflow: hidden;
  351. }
  352. .dialog-title {
  353. font-size: 34rpx;
  354. color: #333333;
  355. font-weight: bold;
  356. margin-top: 48rpx;
  357. margin-bottom: 40rpx;
  358. }
  359. .input-box {
  360. width: 520rpx;
  361. height: 88rpx;
  362. background-color: #F5F6F8;
  363. border-radius: 16rpx;
  364. justify-content: center;
  365. padding-left: 24rpx;
  366. padding-right: 24rpx;
  367. margin-bottom: 48rpx;
  368. }
  369. .nickname-input {
  370. font-size: 30rpx;
  371. color: #333333;
  372. }
  373. .dialog-btns {
  374. flex-direction: row;
  375. align-self: stretch;
  376. border-top-width: 1px;
  377. border-top-color: #F0F0F0;
  378. border-top-style: solid;
  379. height: 96rpx;
  380. align-items: center;
  381. }
  382. .dialog-btn {
  383. flex: 1;
  384. height: 96rpx;
  385. justify-content: center;
  386. align-items: center;
  387. }
  388. .dialog-cancel {
  389. font-size: 32rpx;
  390. color: #999999;
  391. }
  392. .dialog-divider {
  393. width: 1px;
  394. height: 96rpx;
  395. background-color: #F0F0F0;
  396. }
  397. .dialog-confirm {
  398. font-size: 32rpx;
  399. color: #389588;
  400. font-weight: bold;
  401. }
  402. </style>