userinfo.vue 6.9 KB
Newer Older
1 2 3
<template>
	<view>
		<uni-list>
4
			<uni-list-item class="item">
DCloud_JSON's avatar
DCloud_JSON 已提交
5
				<view slot="body" class="item">
DCloud_JSON's avatar
DCloud_JSON 已提交
6
					<text>头像</text>
7 8 9
					<uni-file-picker @click.native="uploadAvatarImg" @delete="removeAvatar" v-model="avatar_file"
						fileMediatype="image" return-type="object" :image-styles="listStyles"
						disabled />
10 11
				</view>
			</uni-list-item>
12 13 14 15
			<uni-list-item class="item" @click="setNickname('')" title="昵称" :rightText="userInfo.nickname||'未设置'" link>
			</uni-list-item>
			<uni-list-item class="item" @click="bindMobile" title="手机号" :rightText="userInfo.mobile||'未绑定'" link>
			</uni-list-item>
16 17
		</uni-list>
		<uni-popup ref="dialog" type="dialog">
18 19
			<uni-popup-dialog mode="input" :value="userInfo.nickname" @confirm="setNickname" title="设置昵称"
				placeholder="请输入要设置的昵称">
20 21 22 23 24 25 26 27 28 29 30 31 32 33
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>
<script>
	import {
		mapMutations,
		mapGetters
	} from 'vuex';
	const db = uniCloud.database();
	const usersTable = db.collection('uni-id-users')
	export default {
		data() {
			return {
34 35 36 37 38 39 40
				avatar_file: {
					"extname": "jpg",
					"fileType": "image",
					"name": "707756af-e9a9-4d08-8db9-5d1f34b84ea6.jpg",
					"size": 98513,
					"url": "cloud://tcb-lseqkmkcq0w1wzwcb18f3-be0d77.7463-tcb-lseqkmkcq0w1wzwcb18f3-be0d77-1304530278/1624523618672_0.jpg"
				},
41 42 43 44 45 46 47
				univerifyStyle: {
					authButton: {
						"title": "本机号码一键绑定", // 授权按钮文案
					},
					otherLoginButton: {
						"title": "其他号码绑定",
					}
48 49 50 51 52 53 54 55 56 57
				},
				listStyles: {
					"height": 80, // 边框高度
					"width": 80, // 边框宽度
					"border": { // 如果为 Boolean 值,可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "10px" // 边框圆角,支持百分比
					}
58 59 60 61 62 63 64 65
				}
			}
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				login: 'user/hasLogin'
			})
66 67 68 69
		},
		onLoad() {
			this.avatar_file = this.userInfo.avatar_file
			console.log(this.avatar_file);
70 71 72 73 74 75 76 77 78 79
		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			bindMobile() {
				// #ifdef APP-PLUS
				uni.preLogin({
					provider: 'univerify',
					success: this.univerify(), //预登录成功
80
					fail: (res) => { // 预登录失败
81
						// 不显示一键登录选项(或置灰)
82
						console.log(res)
83
						this.bindMobileBySmsCode()
84 85 86
					}
				})
				// #endif
87
				// #ifndef APP-PLUS
88 89
				this.bindMobileBySmsCode()
				//...去用验证码绑定
90 91 92 93 94 95 96 97
				// #endif
			},
			univerify() {
				uni.login({
					"provider": 'univerify',
					"univerifyStyle": this.univerifyStyle,
					success: async e => {
						console.log(e.authResult);
98 99 100 101 102 103 104 105 106
						uniCloud.callFunction({
							name: 'uni-id-cf',
							data: {
								action: 'bind_mobile_by_univerify',
								params: e.authResult,
							},
							success: ({
								result
							}) => {
107
								console.log(result);
108 109 110 111
								if (result.code === 0) {
									this.setUserInfo({
										"mobile": result.mobile
									})
112
									uni.closeAuthView()
113
								} else {
114
									uni.showModal({
115
										content: result.msg,
116 117 118 119 120
										showCancel: false,
										complete() {
											uni.closeAuthView()
										}
									});
121 122
								}
							}
123
						})
124
					},
125 126
					fail: (err) => {
						console.log(err);
127
						if (err.code == '30002' || err.code == '30001') {
128 129
							this.bindMobileBySmsCode()
						}
130 131 132 133
					}
				})
			},
			bindMobileBySmsCode() {
134
				uni.navigateTo({
135
					url: '/pages/ucenter/userinfo/bind-mobile/bind-mobile'
136 137 138
				})
			},
			setNickname(nickname) {
139
				console.log(9527, nickname);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
				if (nickname) {
					usersTable.where('_id==$env.uid').update({
						nickname
					}).then(e => {
						console.log(e);
						if (e.result.updated) {
							uni.showToast({
								title: '更新成功',
								icon: 'none'
							});
							this.setUserInfo({
								nickname
							});
						} else {
							uni.showToast({
								title: '没有变化',
								icon: 'none'
							});
						}
					})
					this.$refs.dialog.close()
				} else {
					this.$refs.dialog.open()
				}
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			},
			removeAvatar(){
				this.setAvatarFile(null)
			},
			setAvatarFile(avatar_file){
				uni.showLoading({
					title: '设置中',
					mask: true
				});
				// 使用 clientDB 提交数据
				usersTable.where('_id==$env.uid').update({
					avatar_file
				}).then((res) => {
					console.log(res);
					if(avatar_file){
						uni.showToast({
							icon: 'none',
							title: '设置成功'
						})
					}else{
						uni.showToast({
							icon: 'none',
							title: '删除成功'
						})
					}
					this.avatar_file = avatar_file
					this.setUserInfo({
						avatar_file
					});
				}).catch((err) => {
					uni.showModal({
						content: err.message ||
							'请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
202
			},
203 204 205 206 207 208 209
			uploadAvatarImg(res) {
				const crop = {
					quality: 100,
					width: 600,
					height: 600,
					resize: true
				};
210
				uni.chooseImage({
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
					count: 1,crop,
					success: async (res) => {
						console.log(res);
						let tempFile = res.tempFiles[0],
						avatar_file = {
							// #ifndef APP-PLUS
							extname:tempFile.name.split('.')[tempFile.name.split('.').length-1],
							// #endif
							// #ifdef APP-PLUS
							extname:tempFile.path.split('.')[tempFile.path.split('.').length-1]
							// #endif
						},
						filePath = res.tempFilePaths[0]
						// #ifndef APP-PLUS
						//非app端用前端组件剪裁头像,app端用内置的原生裁剪
						filePath = await new Promise((callback) => {
							uni.navigateTo({
								url: '/pages/ucenter/userinfo/cropImage?path=' + filePath +
									`&options=${JSON.stringify(crop)}`,
								animationType: "fade-in",
								events: {
									success: url=> {
										callback(url)
									}
								}
							});
						})
						// #endif
						console.log(this.userInfo);
						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"
						});
						// console.log(result)
						avatar_file.url = fileID
						console.log({avatar_file});
						uni.hideLoading()
						
						this.setAvatarFile(avatar_file)
256 257
					}
				})
258
			}
259 260 261 262 263 264 265 266
		}
	}
</script>
<style>
	.item {
		width: 750rpx;
		flex-direction: row;
		justify-content: space-between;
267
		align-items: center;
268
	}
269

270 271
	.avatarUrl {
		width: 50px;
272
		height: 50px;
273
		border-radius: 6px;
274
	}
275 276 277 278 279 280 281 282 283

	.chooseAvatar {
		border: solid 1px #ddd;
		border-radius: 10px;
		width: 130rpx;
		height: 130rpx;
		line-height: 130rpx;
	}
</style>