headImg.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <view class="ezy-ghtx-page">
  3. <view class="icon-title-navBar-box">
  4. <view @click="handleBack" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">更换头像</text>
  6. </view>
  7. <img class="ghtx-img" :src="userImg" alt="">
  8. <view @click="chooseImage" class="xc-btn">相册</view>
  9. <view @click="handleBack" class="qx-btn">取消</view>
  10. </view>
  11. </template>
  12. <script setup>
  13. import agreeContentDialog from '@/pages/login/agreeContentDialog.vue';
  14. import {
  15. toast,
  16. getUserIdentity
  17. } from "@/utils/common";
  18. import cacheManager from '@/utils/cacheManager.js';
  19. import {
  20. getFilePolicy,
  21. updataHead,myInfo
  22. } from '@/api/my.js'
  23. import {
  24. onLoad
  25. } from '@dcloudio/uni-app';
  26. import axios from 'axios';
  27. import {
  28. reactive,
  29. ref
  30. } from "vue";
  31. import {
  32. onShow
  33. } from '@dcloudio/uni-app';
  34. let userImg = ref(null);
  35. let currentPlatform = ref(null);
  36. const tempPath = ref('')
  37. const checkAlbumPermission = async () => {
  38. const status = await uni.getSetting()
  39. if (!status.authSetting['scope.album']) {
  40. await uni.authorize({
  41. scope: 'scope.album'
  42. })
  43. }
  44. }
  45. const chooseImage = async () => {
  46. try {
  47. await checkAlbumPermission()
  48. uni.chooseImage({
  49. count: 1,
  50. sizeType: ['compressed'],
  51. sourceType: ['album'], // 仅限相册选择‌:ml-citation{ref="1" data="citationList"}
  52. success: (res) => {
  53. uploadFile(res.tempFilePaths[0])
  54. },
  55. fail: (err) => {
  56. console.error('选择失败:', err)
  57. }
  58. })
  59. } catch (err) {
  60. uni.showModal({
  61. title: '权限申请失败',
  62. content: '请前往设置开启相册权限'
  63. })
  64. }
  65. }
  66. function updateIcon(data){
  67. updataHead({icon:data}).then(res=>{
  68. if(res.code ==0){
  69. uni.showToast({
  70. title: '上传成功',
  71. icon: 'success'
  72. });
  73. }else{
  74. uni.showToast({
  75. title: '上传失败',
  76. });
  77. }
  78. })
  79. }
  80. function uploadFile(filePath) {
  81. const suffix = filePath.split('.').pop();
  82. let req = {
  83. prefix: 'resource/',
  84. suffix: suffix
  85. }
  86. getFilePolicy(req).then(res => {
  87. if (res.code === 0) {
  88. const policyData = res.data;
  89. console.log('policyData', policyData);
  90. uni.uploadFile({
  91. url: policyData.uploadUrl,
  92. filePath: filePath,
  93. name: 'file',
  94. formData: {
  95. key: policyData.key,
  96. policy: policyData.policy,
  97. OSSAccessKeyId: policyData.accessid,
  98. signature: policyData.signature,
  99. success_action_status: '200'
  100. },
  101. header: {
  102. 'Content-Type': 'multipart/form-data'
  103. },
  104. success(uploadRes) {
  105. console.log('uploadRes', uploadRes);
  106. if (uploadRes.statusCode === 200) {
  107. userImg.value = `${policyData.downloadUrl}/${policyData.key}`;
  108. updateIcon(userImg.value)
  109. } else {
  110. uni.showToast({
  111. title: '阿里云上传错误,请重试!',
  112. });
  113. return false
  114. }
  115. },
  116. fail(err) {
  117. console.log('err', err);
  118. }
  119. });
  120. }
  121. })
  122. }
  123. function handleBack() {
  124. uni.redirectTo({
  125. url: '/pages/my/yingyongshezhi'
  126. })
  127. }
  128. function openXiangce2() {
  129. }
  130. function openXiangce() {
  131. }
  132. function isIOSorAndroid() {
  133. const systemInfo = uni.getSystemInfoSync();
  134. console.log('systemInfo', systemInfo);
  135. if (systemInfo.platform == 'ios') {
  136. return currentPlatform.value = 'ios'
  137. } else {
  138. return currentPlatform.value = 'android'
  139. }
  140. }
  141. function getMyInfo() {
  142. myInfo({}).then(res => {
  143. if (res.code == 0) {
  144. userImg.value = res.data.icon;
  145. } else {
  146. toast('获取头像失败')
  147. return false
  148. }
  149. })
  150. }
  151. onLoad((options) => {
  152. isIOSorAndroid();
  153. getMyInfo();
  154. })
  155. </script>