edit.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="edit">
  3. <!-- <view class="head-box">
  4. <image @click="chooseHead" class="head" :src="headImg||defaultImg" mode=""></image>
  5. <image class="icon" src="../../static/take_photo.png" mode=""></image>
  6. </view> -->
  7. <view class="content">
  8. <u-cell size="large" title="头像" :border="false" isLink>
  9. <image @click="chooseHead" slot="value" class="head" :src="headImg||defaultImg" mode=""></image>
  10. </u-cell>
  11. <u-cell size="large" title="修改昵称" :border="false" :isLink="true" url="/my/edit/editName"></u-cell>
  12. <u-cell size="large" title="退订移动套餐" :border="false" :isLink="true" url="/my/edit/unSubscribe"></u-cell>
  13. </view>
  14. <button class="login-out" @click="loginOut">退出当前账号</button>
  15. </view>
  16. </template>
  17. <script>
  18. import {
  19. uploadImg,finishUploadImg
  20. } from '@/api/common.js';
  21. import crypto from 'crypto-js';
  22. import { Base64 } from 'js-base64';
  23. export default {
  24. data() {
  25. return {
  26. headImg: '',
  27. defaultImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fci.xiaohongshu.com%2Fc34b7b74-ba38-0456-982a-43c0f97522fe%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fci.xiaohongshu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693532127&t=a2e186c12aecaab7723611cb52a6778f',
  28. userId: '',
  29. userInfo: null
  30. }
  31. },
  32. methods: {
  33. chooseHead() {
  34. let that = this
  35. uni.chooseImage({
  36. count: 1,
  37. success(r) {
  38. console.log(r);
  39. uploadImg({
  40. "fineName": r.tempFilePaths[0].substr(r.tempFilePaths[0].lastIndexOf('/')+1)
  41. }).then(res => {
  42. if (res.state == 'Success') {
  43. // that.headImg = r.tempFilePaths[0]
  44. // uni.showToast({
  45. // title: '上传成功',
  46. // });
  47. // that.userInfo.imgPath = r.tempFilePaths[0]
  48. // uni.setStorageSync('userInfo',JSON.stringify(this.userInfo))
  49. const date = new Date();
  50. date.setHours(date.getHours() + 1);
  51. const policyText = {
  52. expiration: date.toISOString(), // 设置policy过期时间。
  53. conditions: [
  54. // 限制上传大小。
  55. ["content-length-range", 0, 1024 * 1024 * 1024],
  56. ],
  57. };
  58. let fileName = r.tempFilePaths[0].substr(r.tempFilePaths[0].lastIndexOf('/')+1);
  59. const host = 'https://' + res.content.bucket + '.'+ res.content.endPoint;
  60. const policy = Base64.encode(JSON.stringify(policyText));
  61. const signature = crypto.enc.Base64.stringify(crypto.HmacSHA1(policy, res.content.token.accessKeySecret));;
  62. const filePath = r.tempFilePaths[0]; // 待上传文件的文件路径。
  63. let paths = res.content.paths[0]
  64. // paths.pop()
  65. uni.uploadFile({
  66. url: host, // 开发者服务器的URL。
  67. filePath: filePath,
  68. name: 'file', // 必须填file。
  69. formData: {
  70. key:paths.join('/'),
  71. policy,
  72. success_action_status: '200', //让服务端返回200,不然,默认会返回204
  73. OSSAccessKeyId: res.content.token.accessKeyId,
  74. signature,
  75. 'x-oss-security-token': res.content.token.securityToken // 使用STS签名时必传。
  76. },
  77. success: (res1) => {
  78. if (res1.statusCode === 200) {
  79. console.log('上传成功');
  80. // 告知服务器上传成功地址
  81. finishUploadImg(
  82. [{
  83. path:res.content.paths[0]
  84. }]).then(msg=>{
  85. that.headImg = host + '/' + paths.join('/')
  86. let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  87. userInfo.imgPath = that.headImg
  88. uni.getStorageSync('userInfo',JSON.stringify(userInfo))
  89. })
  90. }
  91. },
  92. fail: err => {
  93. console.log(err);
  94. }
  95. });
  96. }
  97. })
  98. }
  99. })
  100. },
  101. loginOut() {
  102. // 保存部分信息
  103. let location = uni.getStorageSync('location')
  104. let inviteCode = uni.getStorageSync('inviteCode')
  105. uni.clearStorageSync()
  106. uni.setStorageSync('location',location)
  107. uni.setStorageSync('inviteCode',inviteCode)
  108. uni.switchTab({
  109. url: '/pages/index/index'
  110. })
  111. }
  112. },
  113. created() {
  114. this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  115. this.headImg = this.userInfo.imgPath || ''
  116. this.userId = this.userInfo.userId || ''
  117. }
  118. }
  119. </script>
  120. <style lang="scss" scoped>
  121. .edit {
  122. padding: 20rpx 24rpx ;
  123. background: #F9F9F9;
  124. min-height: 100vh;
  125. .content{
  126. background: #fff;
  127. border-radius: 12rpx 12rpx 12rpx 12rpx;
  128. }
  129. .head{
  130. width: 60rpx;
  131. height: 60rpx;
  132. border-radius: 50%;
  133. }
  134. .head-box {
  135. margin: 80rpx auto 50rpx;
  136. width: 150rpx;
  137. height: 150rpx;
  138. position: relative;
  139. .head {
  140. width: 150rpx;
  141. height: 150rpx;
  142. border-radius: 50%;
  143. }
  144. .icon {
  145. position: absolute;
  146. width: 44rpx;
  147. height: 44rpx;
  148. bottom: 0%;
  149. right: 0;
  150. }
  151. }
  152. .login-out {
  153. position: fixed;
  154. bottom: 150rpx;
  155. left: 50%;
  156. width: 400rpx;
  157. line-height: 110rpx;
  158. text-align: center;
  159. color: #fff;
  160. background: rgb(164, 173, 179);
  161. border-radius: 55rpx;
  162. transform: translateX(-50%);
  163. font-size: 30rpx;
  164. }
  165. }
  166. </style>