setting.vue 12 KB

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