setting.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <view class="page">
  3. <!-- 表单 -->
  4. <view class="form">
  5. <u-form label-position="left" :model="form" ref="form" label-width="180rpx">
  6. <!-- 用户ID -->
  7. <u-form-item label="用户ID">
  8. <view class="value id-box">
  9. <text>{{ userInfo.userId }}</text>
  10. <u-button size="mini" type="success" plain class="copy-btn" @click="copyUserId">复制</u-button>
  11. </view>
  12. </u-form-item>
  13. <!-- 头像 -->
  14. <u-form-item label="头像">
  15. <view class="value avatarbox">
  16. <button open-type="chooseAvatar" class="avatarBtn" @chooseavatar="onChooseavatar">
  17. <u-avatar style="height: 72rpx;" size="72" :src="userInfo.imgPath"></u-avatar>
  18. </button>
  19. </view>
  20. </u-form-item>
  21. <!-- 昵称 -->
  22. <u-form-item label="昵称">
  23. <view class="value nickname-input">
  24. <input type="nickname" v-model="userInfo.nickName" placeholder="请输入昵称"
  25. placeholder-style="color: #696969" @input="onNicknameInput" />
  26. </view>
  27. </u-form-item>
  28. <!-- 手机号 -->
  29. <u-form-item label="手机号">
  30. <view class="value">
  31. <!-- 未绑定手机号,显示获取手机号按钮 -->
  32. <button class="get-phone-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  33. <text class="common-text-2" v-if="userInfo.mobile">{{ userInfo.mobile }}</text>
  34. <text class="common-text" v-else>未绑定</text>
  35. <u-icon :color="arrowColor" style="margin-left: 20rpx;" size="28"
  36. name="arrow-right"></u-icon>
  37. </button>
  38. </view>
  39. </u-form-item>
  40. </u-form>
  41. </view>
  42. <!-- 提交按钮 -->
  43. <view class="btn" style="padding: 60rpx 0;">
  44. <u-button type="primary" shape="circle" :loading="submitting" @click="submitChanges">提交</u-button>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. data() {
  51. return {
  52. arrowColor: this.$appTheme.appThemeTextGrayColor,
  53. themeColor: this.$appTheme.appThemeColor,
  54. userInfo: {
  55. userId: '125885222658442698874444',
  56. nickName: '',
  57. imgPath: '',
  58. mobile: ''
  59. },
  60. originalInfo: {}, // 保存原始信息,用于对比是否有修改
  61. submitting: false, // 提交状态
  62. uploading: false, // 添加上传状态
  63. }
  64. },
  65. onLoad() {
  66. this.getUserInfo()
  67. },
  68. methods: {
  69. // 获取用户信息
  70. getUserInfo() {
  71. uni.$u.http.get('/token/user/detail').then(res => {
  72. if (res.code == 200) {
  73. this.userInfo = res.data
  74. }
  75. })
  76. },
  77. // 复制用户ID
  78. copyUserId() {
  79. uni.setClipboardData({
  80. data: this.userInfo.userId,
  81. success: () => {
  82. uni.$u.toast('复制成功')
  83. }
  84. })
  85. },
  86. // 昵称输入
  87. onNicknameInput(e) {
  88. this.userInfo.nickName = e.detail.value
  89. },
  90. // 选择并上传头像
  91. async onChooseavatar(e) {
  92. const tempFilePath = e.detail.avatarUrl
  93. if (!tempFilePath) {
  94. return uni.$u.toast('获取头像失败')
  95. }
  96. try {
  97. this.uploading = true
  98. uni.showLoading({
  99. title: '上传中...',
  100. mask: true
  101. })
  102. // 上传头像
  103. const uploadRes = await this.uploadFile(tempFilePath)
  104. if (uploadRes.code === 200) {
  105. this.userInfo.imgPath = uploadRes.data
  106. // uni.$u.toast('头像上传成功')
  107. } else {
  108. this.uploading = false
  109. uni.hideLoading()
  110. throw new Error(uploadRes.msg || '上传失败')
  111. }
  112. } finally {
  113. this.uploading = false
  114. uni.hideLoading()
  115. }
  116. },
  117. // 文件上传方法
  118. uploadFile(filePath) {
  119. return new Promise((resolve, reject) => {
  120. uni.uploadFile({
  121. url: this.$u.http.config.baseUrl + '/token/user/avatarUpload',
  122. filePath: filePath,
  123. name: 'file',
  124. header: {
  125. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  126. },
  127. success: (uploadFileRes) => {
  128. try {
  129. // 解析响应数据
  130. const res = JSON.parse(uploadFileRes.data)
  131. resolve(res)
  132. } catch (e) {
  133. reject(new Error('解析响应失败'))
  134. }
  135. },
  136. fail: (error) => {
  137. reject(error)
  138. }
  139. })
  140. })
  141. },
  142. // 获取手机号
  143. async getPhoneNumber(e) {
  144. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  145. return uni.$u.toast('获取手机号失败')
  146. }
  147. try {
  148. uni.showLoading({
  149. title: '手机号获取中'
  150. })
  151. // 调用后端接口解密手机号
  152. uni.$u.http.post('/token/user/wxMobileUpdate', {
  153. code: e.detail.code
  154. }).then(res => {
  155. if (res.code === 200) {
  156. // 更新本地用户信息
  157. this.userInfo.mobile = res.data.mobile
  158. uni.showToast({
  159. title: '绑定成功',
  160. icon: 'success'
  161. })
  162. } else {
  163. uni.$u.toast('手机号绑定失败')
  164. }
  165. })
  166. } catch (error) {
  167. uni.$u.toast('手机号绑定失败')
  168. } finally {
  169. uni.hideLoading()
  170. }
  171. },
  172. //更新用户信息
  173. submitChanges() {
  174. this.submitting = true
  175. uni.$u.http.post('/token/user/wxBasicUpdate', {
  176. nickName: this.userInfo.nickName,
  177. imgPath: this.userInfo.imgPath
  178. }).then(res => {
  179. if (res.code == 200) {
  180. this.$u.toast('更新成功')
  181. uni.navigateBack({
  182. delta: 1
  183. })
  184. }
  185. }).finally(() => {
  186. this.submitting = false
  187. })
  188. },
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .page {
  194. padding: 24rpx 30rpx;
  195. background-color: $app-theme-bg-color;
  196. min-height: 100vh;
  197. display: flex;
  198. flex-direction: column;
  199. .form {
  200. flex: 1;
  201. background: #FFFFFF;
  202. border-radius: 12rpx;
  203. padding: 20rpx 0;
  204. .value {
  205. display: flex;
  206. align-items: center;
  207. justify-content: flex-end;
  208. font-size: 28rpx;
  209. color: #333333;
  210. &.nickname-input {
  211. input {
  212. text-align: right;
  213. width: 400rpx;
  214. font-size: 28rpx;
  215. }
  216. .wechat-tag {
  217. font-size: 24rpx;
  218. color: #999;
  219. margin-left: 12rpx;
  220. }
  221. }
  222. &.id-box {
  223. .copy-btn {
  224. margin-left: 20rpx;
  225. padding: 0 20rpx;
  226. height: 48rpx !important;
  227. line-height: 44rpx !important;
  228. }
  229. }
  230. .phone-box {
  231. display: flex;
  232. align-items: center;
  233. }
  234. .get-phone-btn {
  235. background: none;
  236. border: none;
  237. padding: 0;
  238. margin: 0;
  239. display: flex;
  240. align-items: center;
  241. font-size: 28rpx;
  242. &::after {
  243. border: none;
  244. }
  245. }
  246. }
  247. }
  248. .avatarBtn {
  249. border: none;
  250. background-color: transparent;
  251. display: inline-flex;
  252. align-items: center;
  253. padding: 0;
  254. margin: 0;
  255. width: 75rpx;
  256. height: 75rpx;
  257. border-radius: 50%;
  258. }
  259. }
  260. </style>