uni-id-pages-avatar.vue 4.9 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
v1.1.1  
DCloud_JSON 已提交
2
	<button open-type="chooseAvatar" @chooseavatar="bindchooseavatar" @click="uploadAvatarImg" class="box" :class="{'showBorder':border}"  :style="{width,height,lineHeight:height}">
DCloud_JSON's avatar
DCloud_JSON 已提交
3 4 5
		<cloud-image v-if="avatar_file" :src="avatar_file.url" :width="width" :height="height"></cloud-image>
		<uni-icons v-else :style="{width,height,lineHeight:height}" class="chooseAvatar" type="plusempty" size="30"
			color="#dddddd"></uni-icons>
DCloud_JSON's avatar
v1.1.1  
DCloud_JSON 已提交
6
	</button>
DCloud_JSON's avatar
DCloud_JSON 已提交
7 8 9
</template>

<script>
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
10 11 12 13
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
DCloud_JSON's avatar
DCloud_JSON 已提交
14 15 16 17 18 19
	/**
	* uni-id-pages-avatar 
	* @description 用户头像组件
	* @property {String} width	图片的宽,默认为:50px
	* @property {String} height	图片的高,默认为:50px
	*/
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
20
	export default {
DCloud_JSON's avatar
DCloud_JSON 已提交
21 22
		data() {
			return {
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
23
				isPC: false
DCloud_JSON's avatar
DCloud_JSON 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
			}
		},
		props: {
			//头像图片宽
			width: {
				type: String,
				default () {
					return "50px"
				}
			},
			//头像图片高
			height: {
				type: String,
				default () {
					return "50px"
				}
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
40 41 42 43 44 45
			},
			border:{
				type: Boolean,
				default () {
					return false
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
46 47 48 49 50 51 52 53
			}
		},
		async mounted() {
			// #ifdef H5
			this.isPC = !['ios', 'android'].includes(uni.getSystemInfoSync().platform);
			// #endif
		},
		computed: {
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
54 55 56 57 58 59 60 61
			hasLogin() {
				return store.hasLogin
			},
			userInfo() {
				return store.userInfo
			},
			avatar_file() {
				return store.userInfo.avatar_file
DCloud_JSON's avatar
DCloud_JSON 已提交
62 63 64 65
			}
		},
		methods: {
			setAvatarFile(avatar_file) {
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
66 67
				// 使用 clientDB 提交数据
				mutations.updateUserInfo({avatar_file})
DCloud_JSON's avatar
v1.1.1  
DCloud_JSON 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
			},
			async bindchooseavatar(res){
				let avatarUrl = res.detail.avatarUrl
				let avatar_file = {
					extname: avatarUrl.split('.')[avatarUrl.split('.').length - 1],
					name:'',
					url:avatarUrl
				}
				
				// 裁剪
				let filePath = await new Promise((callback) => {
					wx.cropImage({
						src:avatarUrl,
						cropScale:"1:1",
						success: res => {
							callback(res.tempFilePath)
						},
						fail(e){
							console.error(e)
							uni.showModal({
								content: 'wx.cropImage ' + e.errMsg,
								showCancel: false,
								confirmText:"跳过裁剪",
								complete() {
									callback(avatarUrl)
								}
							});
						}
					})
				})
				
				//上传到服务器
				let cloudPath = this.userInfo._id + '' + Date.now()
				avatar_file.name = cloudPath
				try{
					uni.showLoading({
						title: "更新中",
						mask: true
					});
					let {
						fileID
					} = await uniCloud.uploadFile({
						filePath,
						cloudPath,
						fileType: "image"
					});
					avatar_file.url = fileID
					uni.hideLoading()
				}catch(e){
					console.error(e);
				}
				this.setAvatarFile(avatar_file)
DCloud_JSON's avatar
DCloud_JSON 已提交
120
			},
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
121
			uploadAvatarImg(res) {
DCloud_JSON's avatar
v1.1.1  
DCloud_JSON 已提交
122 123 124 125 126
				
				// #ifdef MP-WEIXIN
				return false // 微信小程序走 bindchooseavatar方法
				// #endif
				
DCloud_JSON's avatar
DCloud_JSON 已提交
127 128 129 130
				if(!this.hasLogin){
					return uni.navigateTo({
						url:'/uni_modules/uni-id-pages/pages/login/login-withoutpwd'
					})
DCloud_JSON's avatar
v1.1.1  
DCloud_JSON 已提交
131 132
				}
				
DCloud_JSON's avatar
DCloud_JSON 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
				const crop = {
					quality: 100,
					width: 600,
					height: 600,
					resize: true
				};
				uni.chooseImage({
					count: 1,
					crop,
					success: async (res) => {
						let tempFile = res.tempFiles[0],
							avatar_file = {
								// #ifdef H5
								extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
								// #endif
								// #ifndef H5
								extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
								// #endif
							},
							filePath = res.tempFilePaths[0]
DCloud_JSON's avatar
1.1.0  
DCloud_JSON 已提交
153 154
							
						//非app端剪裁头像,app端用内置的原生裁剪
DCloud_JSON's avatar
DCloud_JSON 已提交
155
						// #ifndef APP-PLUS
DCloud_JSON's avatar
1.1.0  
DCloud_JSON 已提交
156 157 158
						filePath = await new Promise((callback) => {
							// #ifdef H5
							if (!this.isPC) {
DCloud_JSON's avatar
DCloud_JSON 已提交
159 160 161 162 163 164 165 166 167 168
								uni.navigateTo({
									url: '/uni_modules/uni-id-pages/pages/userinfo/cropImage/cropImage?path=' +
										filePath + `&options=${JSON.stringify(crop)}`,
									animationType: "fade-in",
									events: {
										success: url => {
											callback(url)
										}
									},
									complete(e) {
study夏羽's avatar
1.0.37  
study夏羽 已提交
169
										// console.log(e);
DCloud_JSON's avatar
DCloud_JSON 已提交
170 171
									}
								});
DCloud_JSON's avatar
1.1.0  
DCloud_JSON 已提交
172 173 174
							}
							// #endif
						})
DCloud_JSON's avatar
DCloud_JSON 已提交
175
						// #endif
DCloud_JSON's avatar
1.1.0  
DCloud_JSON 已提交
176
						
DCloud_JSON's avatar
DCloud_JSON 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
						let cloudPath = this.userInfo._id + '' + Date.now()
						avatar_file.name = cloudPath
						uni.showLoading({
							title: "更新中",
							mask: true
						});
						let {
							fileID
						} = await uniCloud.uploadFile({
							filePath,
							cloudPath,
							fileType: "image"
						});
						avatar_file.url = fileID
						uni.hideLoading()
						this.setAvatarFile(avatar_file)
					}
				})
			}
		}
	}
</script>

DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
200 201 202 203 204 205
<style>
	/* #ifndef APP-NVUE */
	.box{
		overflow: hidden;
	}
	/* #endif */
DCloud_JSON's avatar
v1.1.1  
DCloud_JSON 已提交
206 207 208
	.box{
		padding: 0;
	}
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
209
	
DCloud_JSON's avatar
DCloud_JSON 已提交
210 211
	.chooseAvatar {
		/* #ifndef APP-NVUE */
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
212 213
		display: inline-block;
		box-sizing: border-box;
DCloud_JSON's avatar
DCloud_JSON 已提交
214 215
		/* #endif */
		border-radius: 10px;
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
216 217 218 219 220
		text-align: center;
		padding: 1px;
	}
	.showBorder{
		border: solid 1px #ddd;
DCloud_JSON's avatar
DCloud_JSON 已提交
221 222
	}
</style>