userinfo.vue 6.7 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 34 35 36 37 38 39 40
			</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 {
				univerifyStyle: {
					authButton: {
						"title": "本机号码一键绑定", // 授权按钮文案
					},
					otherLoginButton: {
						"title": "其他号码绑定",
					}
41 42 43 44 45 46 47 48 49 50
				},
				listStyles: {
					"height": 80, // 边框高度
					"width": 80, // 边框宽度
					"border": { // 如果为 Boolean 值,可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "10px" // 边框圆角,支持百分比
					}
51 52 53 54 55 56 57
				}
			}
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				login: 'user/hasLogin'
58 59 60 61 62 63
			}),
			avatar_file(){
				if(this.userInfo.avatar_file&&this.userInfo.avatar_file.url){
					return this.userInfo.avatar_file
				}
			}
64
		},
65 66 67 68 69 70 71 72 73
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			bindMobile() {
				// #ifdef APP-PLUS
				uni.preLogin({
					provider: 'univerify',
					success: this.univerify(), //预登录成功
74
					fail: (res) => { // 预登录失败
75
						// 不显示一键登录选项(或置灰)
76
						console.log(res)
77
						this.bindMobileBySmsCode()
78 79 80
					}
				})
				// #endif
81
				// #ifndef APP-PLUS
82 83
				this.bindMobileBySmsCode()
				//...去用验证码绑定
84 85 86 87 88 89 90 91
				// #endif
			},
			univerify() {
				uni.login({
					"provider": 'univerify',
					"univerifyStyle": this.univerifyStyle,
					success: async e => {
						console.log(e.authResult);
92 93 94 95 96 97 98 99 100
						uniCloud.callFunction({
							name: 'uni-id-cf',
							data: {
								action: 'bind_mobile_by_univerify',
								params: e.authResult,
							},
							success: ({
								result
							}) => {
101
								console.log(result);
102 103 104 105
								if (result.code === 0) {
									this.setUserInfo({
										"mobile": result.mobile
									})
106
									uni.closeAuthView()
107
								} else {
108
									uni.showModal({
109
										content: result.msg,
110 111 112 113 114
										showCancel: false,
										complete() {
											uni.closeAuthView()
										}
									});
115 116
								}
							}
117
						})
118
					},
119 120
					fail: (err) => {
						console.log(err);
121
						if (err.code == '30002' || err.code == '30001') {
122 123
							this.bindMobileBySmsCode()
						}
124 125 126 127
					}
				})
			},
			bindMobileBySmsCode() {
128
				uni.navigateTo({
129
					url: '/pages/ucenter/userinfo/bind-mobile/bind-mobile'
130 131 132
				})
			},
			setNickname(nickname) {
133
				console.log(9527, nickname);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
				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()
				}
158 159
			},
			removeAvatar(){
160 161 162 163 164 165 166
				this.setAvatarFile({
					"extname": "jpg",
					"fileType": "image",
					"name": "",
					"size": 0,
					"url": ""
				})
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
			},
			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.setUserInfo({
						avatar_file
					});
				}).catch((err) => {
					uni.showModal({
						content: err.message ||
							'请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
201
			},
202 203 204 205 206 207 208
			uploadAvatarImg(res) {
				const crop = {
					quality: 100,
					width: 600,
					height: 600,
					resize: true
				};
209
				uni.chooseImage({
210 211 212 213 214
					count: 1,crop,
					success: async (res) => {
						console.log(res);
						let tempFile = res.tempFiles[0],
						avatar_file = {
215
							// #ifdef H5
216 217
							extname:tempFile.name.split('.')[tempFile.name.split('.').length-1],
							// #endif
218
							// #ifndef H5
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
							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)
255 256
					}
				})
257
			}
258 259 260 261 262 263 264 265
		}
	}
</script>
<style>
	.item {
		width: 750rpx;
		flex-direction: row;
		justify-content: space-between;
266
		align-items: center;
267
	}
268

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

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