setting.vue 9.4 KB

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