setting.vue 12 KB

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