学员端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

163 lines
4.0 KiB

<template>
<view class="pageBgImg">
<topNavbar title="上传头像"></topNavbar>
<view class="pad">
<view class="card">
<view class="h2">请上传或拍摄一张五官清晰的照片</view>
<view class="phone" @click="uploadTap" v-if="!avatar">
<view class="phoneIcon">
<image src="@/static/images/index/btn_tupian.png" mode=""></image>
</view>
<view class="lab">添加图片</view>
</view>
<view class="phone" v-else @click="uploadTap">
<image :src="avatar" mode=""></image>
</view>
<view class="btnBg" @click="$goPage('/pages/indexEntry/enroll/registInfo/registInfo')">上传</view>
</view>
</view>
</view>
</template>
<script>
import { APP_API, APP_HOST } from '@/site.config.js';
const _url = APP_HOST + APP_API + '/util/manage/uploadFile.do';
export default {
data() {
return {
avatar: ''
}
},
onLoad() {
uni.$on('uAvatarCropper',(url)=>{
// console.log('裁剪后的图片路径')
this.avatar = url
console.log(url)
})
},
methods: {
addImage() {
var that = this;
// 从本地相册选择图片或使用相机拍照。
uni.chooseImage({
count: 1, //最多可以选择的图片张数,默认9
//album 从相册选图,camera 使用相机,默认二者都有。
sourceType: ['album'],
success: function(res) {
//获取图片信息。
uni.getImageInfo({
src: res.tempFilePaths[0],
success: function(image) {
that.src = (res.tempFilePaths[0]);
console.log('这都能成功')
console.log(that.src); //打印出图片信息,在浏览器上打开就是你上传的图片
//将本地资源上传到开发者服务器,客户端发起一个 POST 请求,其中 content-type 为 multipart/form-data
}
})
}
})
},
//选择图片
chooseImages(type) {
uni.chooseImage({
count: 1, //允许选择的数量
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], //从相册选择
success: res => {
uni.showLoading({
title: '图片上传中...'
});
res.tempFilePaths.forEach( (item,index)=>{
this.uploadImgApi(item)
})
}
})
},
uploadTap() {
const _this = this;
uni.chooseImage({
count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: (res) => {
let url = res.tempFilePaths[0];
// 获取裁剪图片资源后,给data添加src属性及其值
uni.navigateTo({
url: '/pages/indexEntry/enroll/uploadAvatar/uAvatarCropper/uAvatarCropper?url=' + encodeURIComponent(url)
})
}
});
},
uploadImgApi(filePath) {
console.log(filePath)
let _this = this
// 上传图片到服务器
uni.uploadFile({
url: _url,//接口
filePath: filePath,//要上传的图片的本地路径
name: 'file',
formData: {
fileType: 1,
fileSuffix: "png"
},
header: {
token: uni.getStorageSync("Authorization") || '',
},
success(res) {
console.log('上传成功')
let res2 = JSON.parse(res.data)
_this.imgArr.push(res2.data)
console.log(res2)
uni.hideLoading();
},
complete: ()=> {}
})
},
}
}
</script>
<style lang="scss" scoped>
.card {
display: flex;
align-items: center;
flex-direction: column;
.h2 {
padding: 126rpx 0 84rpx 0;
font-size: 36rpx;
font-weight: 600;
}
}
.phone {
border: 2rpx dashed #CDCED0;
width: 290rpx;
height: 290rpx;
border-radius: 16rpx;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
.phoneIcon {
width: 60rpx;
height: 60rpx;
}
.lab {
font-size: 20rpx;
color: #686B73;
margin-top: 8rpx;
}
}
.btnBg {
width: 396rpx;
margin: 130rpx 0;
}
</style>