setting.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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">
  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. this.updateUserInfo()
  90. },
  91. // 选择并上传头像
  92. async onChooseavatar(e) {
  93. const tempFilePath = e.detail.avatarUrl
  94. if (!tempFilePath) {
  95. return uni.$u.toast('获取头像失败')
  96. }
  97. try {
  98. this.uploading = true
  99. uni.showLoading({
  100. title: '上传中...',
  101. mask: true
  102. })
  103. // 上传头像
  104. const uploadRes = await this.uploadFile(tempFilePath)
  105. if (uploadRes.code === 200) {
  106. this.userInfo.imgPath = uploadRes.data
  107. uni.$u.toast('头像上传成功')
  108. } else {
  109. throw new Error(uploadRes.msg || '上传失败')
  110. }
  111. } catch (error) {
  112. uni.$u.toast('头像上传失败,请重试')
  113. // 还原头像
  114. this.userInfo.imgPath = this.originalInfo.imgPath
  115. } finally {
  116. this.uploading = false
  117. uni.hideLoading()
  118. }
  119. },
  120. // 文件上传方法
  121. uploadFile(filePath) {
  122. return new Promise((resolve, reject) => {
  123. uni.uploadFile({
  124. url: this.$u.http.config.baseUrl + '/token/user/avatarUpload',
  125. filePath: filePath,
  126. name: 'file',
  127. header: {
  128. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  129. },
  130. success: (uploadFileRes) => {
  131. try {
  132. // 解析响应数据
  133. const res = JSON.parse(uploadFileRes.data)
  134. resolve(res)
  135. } catch (e) {
  136. reject(new Error('解析响应失败'))
  137. }
  138. },
  139. fail: (error) => {
  140. reject(error)
  141. }
  142. })
  143. })
  144. },
  145. // 跳转换绑手机号页面
  146. goToChangeMobile() {
  147. uni.navigateTo({
  148. url: '/pages-mine/pages/change-mobile/index'
  149. })
  150. },
  151. // 获取手机号
  152. async getPhoneNumber(e) {
  153. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  154. return uni.$u.toast('获取手机号失败')
  155. }
  156. try {
  157. uni.showLoading({
  158. title: '手机号获取中'
  159. })
  160. // 调用后端接口解密手机号
  161. uni.$u.http.post('/token/user/wxMobileUpdate', { code: e.detail.code }).then(res => {
  162. if (res.code === 200) {
  163. // 更新本地用户信息
  164. this.userInfo.mobile = res.data.mobile
  165. uni.showToast({
  166. title: '绑定成功',
  167. icon: 'success'
  168. })
  169. } else {
  170. uni.$u.toast('手机号绑定失败')
  171. }
  172. })
  173. } catch (error) {
  174. uni.$u.toast('手机号绑定失败')
  175. } finally {
  176. uni.hideLoading()
  177. }
  178. },
  179. //更新用户信息
  180. updateUserInfo() {
  181. uni.$u.http.post('/token/user/wxBasicUpdate', {
  182. nickName: this.userInfo.nickName,
  183. imgPath: this.userInfo.imgPath
  184. }).then(res => {
  185. console.log(res)
  186. })
  187. },
  188. // 提交修改
  189. async submitChanges() {
  190. if (this.uploading) {
  191. return uni.$u.toast('头像上传中,请稍候...')
  192. }
  193. if (!this.hasChanges) return
  194. if (!this.userInfo.nickName.trim()) {
  195. return uni.$u.toast('请输入昵称')
  196. }
  197. this.submitting = true
  198. try {
  199. const { code, data } = await this.$u.api.setUserBaseInfoAjax({
  200. nickName: this.userInfo.nickName,
  201. imgPath: this.userInfo.imgPath
  202. })
  203. if (code === 1) {
  204. uni.$u.toast('保存成功')
  205. this.originalInfo = { ...this.userInfo }
  206. uni.$emit("getUserInfo")
  207. setTimeout(() => {
  208. uni.navigateBack()
  209. }, 1500)
  210. }
  211. } catch (error) {
  212. uni.$u.toast('保存失败,请重试')
  213. } finally {
  214. this.submitting = false
  215. }
  216. },
  217. // 更新计算属性中的判断逻辑
  218. computed: {
  219. hasChanges() {
  220. return !this.uploading && (
  221. this.userInfo.nickName !== this.originalInfo.nickName ||
  222. this.userInfo.imgPath !== this.originalInfo.imgPath
  223. )
  224. }
  225. }
  226. }
  227. }
  228. </script>
  229. <style lang="scss" scoped>
  230. .page {
  231. padding: 24rpx 30rpx;
  232. background-color: $app-theme-bg-color;
  233. min-height: 100vh;
  234. display: flex;
  235. flex-direction: column;
  236. .form {
  237. flex: 1;
  238. background: #FFFFFF;
  239. border-radius: 12rpx;
  240. padding: 20rpx 0;
  241. .value {
  242. display: flex;
  243. align-items: center;
  244. justify-content: flex-end;
  245. font-size: 28rpx;
  246. color: $app-theme-card-gray-deep-color;
  247. &.nickname-input {
  248. input {
  249. text-align: right;
  250. width: 400rpx;
  251. font-size: 28rpx;
  252. }
  253. .wechat-tag {
  254. font-size: 24rpx;
  255. color: #999;
  256. margin-left: 12rpx;
  257. }
  258. }
  259. &.id-box {
  260. .copy-btn {
  261. margin-left: 20rpx;
  262. padding: 0 20rpx;
  263. height: 48rpx !important;
  264. line-height: 44rpx !important;
  265. }
  266. }
  267. .phone-box {
  268. display: flex;
  269. align-items: center;
  270. }
  271. .get-phone-btn {
  272. background: none;
  273. border: none;
  274. padding: 0;
  275. margin: 0;
  276. display: flex;
  277. align-items: center;
  278. font-size: 28rpx;
  279. &::after {
  280. border: none;
  281. }
  282. }
  283. }
  284. }
  285. .avatarBtn {
  286. border: none;
  287. background-color: transparent;
  288. display: inline-flex;
  289. align-items: center;
  290. padding: 0;
  291. margin: 0;
  292. width: 75rpx;
  293. height: 75rpx;
  294. border-radius: 50%;
  295. }
  296. }
  297. </style>