userinfo.vue 5.3 KB
Newer Older
1 2 3
<template>
	<view>
		<uni-list>
DCloud_JSON's avatar
DCloud_JSON 已提交
4
			<uni-list-item class="item" @click="setAvatar" link>
DCloud_JSON's avatar
DCloud_JSON 已提交
5
				<view slot="body" class="item">
DCloud_JSON's avatar
DCloud_JSON 已提交
6 7
					<text>头像</text>
					<image class="avatarUrl" :src="userInfo.avatar||nullAvatarUrl" mode="widthFix"></image>
8 9
				</view>
			</uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
10 11
			<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>
12 13
		</uni-list>
		<uni-popup ref="dialog" type="dialog">
DCloud_JSON's avatar
DCloud_JSON 已提交
14
			<uni-popup-dialog mode="input" :value="userInfo.nickname" @confirm="setNickname" title="设置昵称" placeholder="请输入要设置的昵称">
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
			</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 {
				nullAvatarUrl: '/static/uni-center/logo.png',
				univerifyStyle: {
					authButton: {
						"title": "本机号码一键绑定", // 授权按钮文案
					},
					otherLoginButton: {
						"title": "其他号码绑定",
					}
				}
			}
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				login: 'user/hasLogin'
			})
		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			bindMobile() {
				// #ifdef APP-PLUS
				uni.preLogin({
					provider: 'univerify',
					success: this.univerify(), //预登录成功
DCloud_JSON's avatar
DCloud_JSON 已提交
55
					fail:(res)=> { // 预登录失败
56
						// 不显示一键登录选项(或置灰)
DCloud_JSON's avatar
DCloud_JSON 已提交
57
						console.log(res)
58
						this.bindMobileBySmsCode()
59 60 61
					}
				})
				// #endif
62
				// #ifndef APP-PLUS
DCloud_JSON's avatar
DCloud_JSON 已提交
63 64
					this.bindMobileBySmsCode()
					//...去用验证码绑定
65 66 67 68 69 70 71 72
				// #endif
			},
			univerify() {
				uni.login({
					"provider": 'univerify',
					"univerifyStyle": this.univerifyStyle,
					success: async e => {
						console.log(e.authResult);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
						// this.-request('uni-id-cf/bind_mobile_by_univerify',
						// 	e.authResult,
						// 	result=>
						// 	{
						// 		console.log(result);
						// 		if(result.code===0){
						// 			this.setUserInfo({"mobile":result.mobile})
						// 			uni.closeAuthView()
						// 		}else{
						// 			uni.showModal({
						// 				content: JSON.stringify(result.msg),
						// 				showCancel: false,
						// 				complete() {
						// 					uni.closeAuthView()
						// 				}
						// 			});
						// 		}
						// 	}
DCloud_JSON's avatar
DCloud_JSON 已提交
91 92 93 94 95 96 97 98
						// )
						uniCloud.callFunction({
							name:'uni-id-cf',
							data:{
								action:'bind_mobile_by_univerify',
								params:e.authResult,
							},
							success: ({result}) => {
99
								console.log(result);
DCloud_JSON's avatar
DCloud_JSON 已提交
100 101
								if(result.code===0){
									this.setUserInfo({"mobile":result.mobile})
102
									uni.closeAuthView()
DCloud_JSON's avatar
DCloud_JSON 已提交
103
								}else{
104 105 106 107 108 109 110
									uni.showModal({
										content: JSON.stringify(result.msg),
										showCancel: false,
										complete() {
											uni.closeAuthView()
										}
									});
DCloud_JSON's avatar
DCloud_JSON 已提交
111 112
								}
							}
113
						})
114
					},
115 116
					fail: (err) => {
						console.log(err);
DCloud_JSON's avatar
DCloud_JSON 已提交
117
						if(err.code=='30002'){
118 119
							this.bindMobileBySmsCode()
						}
120 121 122 123
					}
				})
			},
			bindMobileBySmsCode() {
124
				uni.navigateTo({
DCloud_JSON's avatar
DCloud_JSON 已提交
125
					url:'/pages/ucenter/userinfo/bind-mobile/bind-mobile'
126 127 128
				})
			},
			setNickname(nickname) {
DCloud_JSON's avatar
DCloud_JSON 已提交
129
				console.log(9527,nickname);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
				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()
				}
			},
			setAvatar() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						// 头像剪裁尺寸
						let options = {
							width: 600,
							height: 600
						}
						// 剪裁并上传头像
						uni.navigateTo({
166
							url: '/pages/ucenter/userinfo/uploadCutImageToUnicloud?path=' +
167
								res.tempFilePaths[0] +
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 202 203 204 205 206 207 208 209
								`&options=${JSON.stringify(options)}`,
							animationType: "fade-in",
							events: {
								uploadAvatarAfter: ({
									url
								}) => {
									console.log(url);
									// 使用 clientDB 提交数据
									usersTable.where('_id==$env.uid').update({
										avatar: url
									}).then((res) => {
										console.log(res);
										uni.showToast({
											icon: 'none',
											title: '修改成功'
										})
										this.setUserInfo({
											avatar: url
										});
									}).catch((err) => {
										uni.showModal({
											content: err.message ||
												'请求服务失败',
											showCancel: false
										})
									}).finally(() => {
										uni.hideLoading()
									})
								}
							}
						});
					}
				})
			},
		}
	}
</script>
<style>
	.item {
		width: 750rpx;
		flex-direction: row;
		justify-content: space-between;
210
		align-items: center;
211 212 213
	}
	.avatarUrl {
		width: 50px;
214
		height: 50px;
215
		border-radius: 6px;
216
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
217
</style>