uni-id-pages-user-profile.vue 3.7 KB
Newer Older
雪洛's avatar
雪洛 已提交
1 2 3 4 5 6 7
<template>
	<uni-popup ref="popup" type="bottom">
		<view class="box">
			<text class="headBox">绑定资料</text>
			<text class="tip">获取你的微信头像和昵称,完善你的个人资料</text>
			<view class="btnBox">
				<text @click="closeMe" class="close">关闭</text>
8
				<button class="agree uni-btn" type="primary" @click="getUserProfile">确定</button>
雪洛's avatar
雪洛 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
			</view>
		</view>
	</uni-popup>
</template>

<script>
	const db = uniCloud.database();
	const usersTable = db.collection('uni-id-users')
	let userId = ''
	export default {
		emits:['next'],
		data() {
			return {}
		},
		methods: {
			async open(uid){
				userId = uid
				this.$refs.popup.open()
			},
			async getUserProfile(){
				uni.showLoading();
				let res = await new Promise((callBack) => {
					uni.getUserProfile({
						desc: "用于设置账户昵称和头像",
						complete: (e) => {
34
							console.log("getUserProfile:", e);
雪洛's avatar
雪洛 已提交
35 36 37 38 39 40 41 42
							callBack(e)
						}
					})
				})
				// console.log("userInfo", res.userInfo);
				if(res.errMsg != "getUserProfile:ok"){
					return this.closeMe()
				}
43
				let {avatarUrl,nickName} = res.userInfo;
雪洛's avatar
雪洛 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
				
				let tempFilePath = await new Promise((callBack)=>{
					uni.downloadFile({
					    url: avatarUrl,
					    success: (res) => {
					        if (res.statusCode === 200) {
					            // console.log('下载成功');
								callBack(res.tempFilePath)
					        }
							callBack()
					    },
						fail: (err) => {
							console.error(err)
						},
						complete: (e) => {
							// console.log("downloadFile",e);
						}
					});
				})
63 64
				const extName = tempFilePath.split('.').pop() || 'jpg'
				const cloudPath = 'user/avatar/'+ userId+'/'+Date.now()+'-avatar.'+extName;
雪洛's avatar
雪洛 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
				// console.log(tempFilePath);
				const result = await uniCloud.uploadFile({
					filePath: tempFilePath,
					cloudPath,
					fileType:'image'
				});
				// console.log("上传成功",{result});
				let userInfo = {
					"nickname":nickName,
					"avatar_file":{
						name:cloudPath,
						extname:"jpg",
						url:result.fileID
					}
				}
				this.doUpdate(userInfo,()=>{
					this.$refs.popup.close()
				})
			},
			closeMe(e){
				uni.showLoading();
86
				this.doUpdate({nickname:"匿名微信用户"},()=>{
雪洛's avatar
雪洛 已提交
87 88 89 90 91 92 93 94 95 96 97
					uni.hideLoading()
					this.$refs.popup.close()
				})
			},
			doUpdate(data,callback){
				// console.log('dododo',data);
				// 使用 clientDB 提交数据
				usersTable.where('_id==$env.uid').update(data).then((res) => {
					callback(res)
				}).catch((err) => {
					uni.showModal({
98
						content: err.message || '请求服务失败',
雪洛's avatar
雪洛 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
						showCancel: false
					})
					callback(err)
				}).finally(() => {
					this.$emit('next')
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
112
@import "@/uni_modules/uni-id-pages/common/login-page.scss";
雪洛's avatar
雪洛 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
view{
	display: flex;
}
.box{
	background-color: #FFFFFF;
	height:200px;
	width: 750rpx;
	flex-direction: column;
	border-top-left-radius: 15px;
	border-top-right-radius: 15px;
}
.headBox{
	padding:20rpx;
	height:80rpx;
	line-height:80rpx;
	text-align: left;
129
	font-size:16px;
雪洛's avatar
雪洛 已提交
130 131 132 133 134 135 136 137
	color:#333333;
	margin-left: 15rpx;
}
.tip{
	color:#666666;
	text-align: left;
	justify-content: center;
	margin:10rpx 30rpx;
138
	font-size:18px;
雪洛's avatar
雪洛 已提交
139 140 141 142 143 144 145 146 147 148 149
}
.btnBox{
	margin-top:45rpx;
	justify-content: center;
	flex-direction: row;
}
.close,.agree{
	text-align: center;
	width:200rpx;
	height:80upx;
	line-height:80upx;
150
	border-radius:5px;
雪洛's avatar
雪洛 已提交
151
	margin:0 20rpx;
152
	font-size:14px;
雪洛's avatar
雪洛 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}
.close{
	color:#999999;
	border-color: #EEEEEE;
	border-style: solid;
	border-width: 1px;
	background-color:#FFFFFF;
}
.close:active{
	color:#989898;
	background-color:#E2E2E2;
}
.agree{
	color:#FFFFFF;
}
/* #ifdef MP */
.agree::after {
   border: none;
}
/* #endif */
.agree:active{
	background-color:#F5F5F6;
}
</style>