Commit 21c4acc86af10f97e4f41bbba5dd54f194ac9674

Authored by fengtao
1 parent 84e7df9f

feat:app端 上传个人头像新增相机相册权限

Showing 1 changed file with 149 additions and 3 deletions
... ... @@ -2,6 +2,7 @@
2 2 <view class="set-page">
3 3 <!-- 公共组件-每个页面必须引入 -->
4 4 <public-module></public-module>
  5 + <!-- #ifdef MP-WEIXIN -->
5 6 <view @click="upAvatar" class="u-flex set-main">
6 7 <view class="main-image">
7 8 <image class="image" :src="avatar || '../../static/logo.png'"></image>
... ... @@ -10,6 +11,17 @@
10 11 <image class="image" src="../../static/arrow-right.png"></image>
11 12 </view>
12 13 </view>
  14 + <!-- #endif -->
  15 + <!-- #ifdef APP-PLUS -->
  16 + <view @click="upAppAvatar" class="u-flex set-main">
  17 + <view class="main-image">
  18 + <image class="image" :src="avatar || '../../static/logo.png'"></image>
  19 + </view>
  20 + <view class="main-right-image">
  21 + <image class="image" src="../../static/arrow-right.png"></image>
  22 + </view>
  23 + </view>
  24 + <!-- #endif -->
13 25 <view class="u-m-t-20 basic-text"><text class="text">基本资料</text></view>
14 26 <view class="basic-main">
15 27 <u--form labelPosition="left" :model="myInfoModel" ref="myForm">
... ... @@ -63,6 +75,8 @@
63 75 </u-popup>
64 76 </view>
65 77 <!-- 解绑账号 -->
  78 + <u-picker :show="showSelectType" :columns="avgColumns" keyName="label" closeOnClickOverlay
  79 + @confirm="confirmAvgGap" @cancel="showSelectType=false" @close="showSelectType=false"></u-picker>
66 80 </view>
67 81 </template>
68 82
... ... @@ -75,10 +89,21 @@
75 89 mapState
76 90 } from 'vuex';
77 91 import api from '@/api/index.js'
  92 + import permission from '@/js_sdk/wa-permission/permission.js'
78 93
79 94 export default {
80 95 data() {
81 96 return {
  97 + avgColumns: [
  98 + [{
  99 + label: '相机',
  100 + value: 1
  101 + }, {
  102 + label: '相册',
  103 + value: 2
  104 + }, ]
  105 + ],
  106 + showSelectType: false,
82 107 showBind: false,
83 108 PrimaryColor: '#377DFF', //主题色
84 109 InfoColor: '#00C9A7', //主题色
... ... @@ -119,14 +144,14 @@
119 144 info: {},
120 145 openIds: '',
121 146 isUpdatePersonOrLoginInfo: false,
122   - isJudgeBindBtn: true
  147 + isJudgeBindBtn: true,
  148 + modify_content: ''
123 149 };
124 150 },
125 151 onReady() {
126 152 this.$refs.myForm.setRules(this.rules);
127 153 },
128 154 onLoad(e) {
129   - console.log(e);
130 155 if (e.data !== null) {
131 156 let params = JSON.parse(e.data);
132 157 this.info = params;
... ... @@ -150,7 +175,103 @@
150 175 ...mapState(['userInfo'])
151 176 },
152 177 methods: {
  178 + modify() {
  179 + uni.showModal({
  180 + title: '需要下列权限才可以正常使用',
  181 + content: this.modify_content,
  182 + confirmText: '前往开启',
  183 + success: function(res) {
  184 + if (res.confirm) {
  185 + permission.gotoAppPermissionSetting(); //动态修改权限
  186 + }
  187 + }
  188 + });
  189 + },
  190 + async requestAndroidPermission(permissionID, e) {
  191 + let resp = await permission.requestAndroidPermission(permissionID, e);
  192 + if (resp == -1) {
  193 + this.modify();
  194 + } else if (resp == 1) {
  195 + if (e == 1) {
  196 + this.chooseImage1()
  197 + } else {
  198 + this.chooseImage2()
  199 + }
  200 + }
  201 + },
153 202 ...mapMutations(['setUserInfo', 'emptyUserInfo']),
  203 + chooseImage1() {
  204 + let token;
  205 + token = this.userInfo.isToken || uni.getStorageSync('userInfo').isToken || undefined;
  206 + // #ifdef H5
  207 + token = window.sessionStorage.getItem('userInfo').isToken;
  208 + // #endif
  209 + if (!token) return uni.$u.toast('请登录后上传图片');
  210 + var cmr = plus.camera.getCamera();
  211 + var res = cmr.supportedImageResolutions[0];
  212 + var fmt = cmr.supportedImageFormats[0];
  213 + cmr.captureImage(
  214 + (path) => {
  215 + this.showSelectType = false
  216 + uni.uploadFile({
  217 + url: `${baseUrl.baseUrl}/yt/oss/upload`,
  218 + filePath: path,
  219 + name: 'file',
  220 + header: {
  221 + 'content-type': 'multipart/form-data',
  222 + Authorization: 'Bearer ' + token
  223 + },
  224 + formData: {},
  225 + success: res => {
  226 + let objImage = JSON.parse(res.data);
  227 + this.avatar = objImage.fileStaticUri;
  228 + uni.$u.toast('头像上传成功');
  229 + }
  230 + });
  231 + },
  232 + function(error) {
  233 + console.log('Capture image failed: ' + error.message);
  234 + }, {
  235 + resolution: res,
  236 + format: fmt
  237 + }
  238 + );
  239 + },
  240 + //选择相册
  241 + chooseImage2() {
  242 + let token;
  243 + token = this.userInfo.isToken || uni.getStorageSync('userInfo').isToken || undefined;
  244 + // #ifdef H5
  245 + token = window.sessionStorage.getItem('userInfo').isToken;
  246 + // #endif
  247 + if (!token) return uni.$u.toast('请登录后上传图片');
  248 + plus.gallery.pick(
  249 + (path) => {
  250 + console.log(path)
  251 + this.showSelectType = false
  252 + uni.uploadFile({
  253 + url: `${baseUrl.baseUrl}/yt/oss/upload`,
  254 + filePath: path,
  255 + name: 'file',
  256 + header: {
  257 + 'content-type': 'multipart/form-data',
  258 + Authorization: 'Bearer ' + token
  259 + },
  260 + formData: {},
  261 + success: res => {
  262 + let objImage = JSON.parse(res.data);
  263 + this.avatar = objImage.fileStaticUri;
  264 + uni.$u.toast('头像上传成功');
  265 + }
  266 + });
  267 + },
  268 + function(e) {
  269 + console.log('取消选择图片');
  270 + }, {
  271 + filter: 'image'
  272 + }
  273 + );
  274 + },
154 275 confrimBind(e) {
155 276 if (e) {
156 277 //解绑
... ... @@ -181,8 +302,32 @@
181 302 clearAccountFunc() {
182 303 this.showBind = true;
183 304 },
  305 + upAppAvatar() {
  306 + this.showSelectType = true
  307 + },
  308 + confirmAvgGap(e) {
  309 + if (e.value[0].value == 1) {
  310 + this.modify_content = '摄像头权限(拍摄头像)';
  311 + //相机
  312 + switch (uni.getSystemInfoSync().platform) {
  313 + case 'android':
  314 + this.requestAndroidPermission('android.permission.CAMERA', 1);
  315 + break;
  316 + }
  317 + } else {
  318 + this.modify_content = '存储权限(读取相册)';
  319 + //相册
  320 + switch (uni.getSystemInfoSync().platform) {
  321 + case 'android':
  322 + this.requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE', 2);
  323 + break;
  324 + }
  325 + }
  326 + this.showSelectAvg = false
  327 + },
184 328 // 修改头像
185 329 async upAvatar() {
  330 + let that = this
186 331 let token;
187 332 token = this.userInfo.isToken || uni.getStorageSync('userInfo').isToken || undefined;
188 333 // #ifdef H5
... ... @@ -191,6 +336,7 @@
191 336 if (!token) return uni.$u.toast('请登录后上传图片');
192 337 uni.chooseImage({
193 338 count: 1,
  339 + sizeType: ['compressed'],
194 340 sourceType: ['camera', 'album'],
195 341 success: res => {
196 342 const tempFilePaths = res.tempFilePaths;
... ... @@ -232,7 +378,7 @@
232 378 uni.$u.toast('头像上传成功');
233 379 }
234 380 });
235   - }
  381 + },
236 382 });
237 383 },
238 384 onSubmitFunc() {
... ...