userinfo.vue 7.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<!-- 用户资料页 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3 4 5 6 7 8 9
<template>
	<view class="uni-content">
		<view class="avatar">
			<uni-id-pages-avatar width="260rpx" height="260rpx"></uni-id-pages-avatar>
		</view>
		<uni-list>
			<uni-list-item class="item" @click="setNickname('')" title="昵称" :rightText="userInfo.nickname||'未设置'" link>
			</uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
10 11
			<uni-list-item class="item" @click="bindMobile" title="手机号" :rightText="userInfo.mobile||'未绑定'" link>
			</uni-list-item>
12 13
			<uni-list-item v-if="userInfo.email" class="item" title="电子邮箱" :rightText="userInfo.email">
			</uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
14
			<uni-list-item v-if="hasPwd" class="item" @click="changePassword" title="修改密码" link>
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16
			</uni-list-item>
		</uni-list>
17 18 19 20 21 22 23 24 25 26
		<uni-list class="mt10">
			<uni-list-item @click="bindThirdAccount('Weixin')" title="微信" link
				:rightText="userInfo.wx_openid ? '已绑定': '未绑定' "></uni-list-item>
			<uni-list-item @click="bindThirdAccount('QQ')" title="QQ" link
				:rightText="userInfo.qq_openid ? '已绑定': '未绑定' "></uni-list-item>
			<uni-list-item @click="bindThirdAccount('Alipay')" title="支付宝" link
				:rightText="userInfo.ali_openid ? '已绑定': '未绑定' "></uni-list-item>
			<uni-list-item @click="bindThirdAccount('Apple')" title="Apple" link
				:rightText="userInfo.apple_openid ? '已绑定': '未绑定' "></uni-list-item>
		</uni-list>
DCloud_JSON's avatar
DCloud_JSON 已提交
27 28 29 30 31 32 33 34 35
		<uni-list class="mt10">
			<uni-list-item @click="deactivate" title="注销账号" link="navigateTo"></uni-list-item>
		</uni-list>
		<uni-popup ref="dialog" type="dialog">
			<uni-popup-dialog mode="input" :value="userInfo.nickname" @confirm="setNickname" title="设置昵称"
				placeholder="请输入要设置的昵称">
			</uni-popup-dialog>
		</uni-popup>
		<uni-id-pages-bind-mobile ref="bind-mobile-by-sms" @success="getUserInfo"></uni-id-pages-bind-mobile>
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
36 37 38 39
		<template v-if="showLoginManage">
			<button v-if="hasLogin" @click="logout">退出登录</button>
			<button v-else @click="login">去登录</button>
		</template>
DCloud_JSON's avatar
DCloud_JSON 已提交
40 41 42
	</view>
</template>
<script>
43 44 45 46 47 48 49 50 51 52
const db = uniCloud.database();
const usersTable = db.collection('uni-id-users')
const uniIdCo = uniCloud.importObject("uni-id-co")
import common from '@/uni_modules/uni-id-pages/common/common.js';
export default {
	data() {
		return {
			univerifyStyle: {
				authButton: {
					"title": "本机号码一键绑定", // 授权按钮文案
DCloud_JSON's avatar
DCloud_JSON 已提交
53
				},
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
				otherLoginButton: {
					"title": "其他号码绑定",
				}
			},
			userInfo: {
				mobile: '',
				nickname: ''
			},
			hasLogin: false,
			hasPwd: false,
			showLoginManage: false//通过页面传参隐藏登录&退出登录按钮
		}
	},
	async onShow() {
		this.univerifyStyle.authButton.title = "本机号码一键绑定"
		this.univerifyStyle.otherLoginButton.title = "其他号码绑定"
	},
	async onLoad(e) {
		if (e.showLoginManage) {
			this.showLoginManage = true//通过页面传参隐藏登录&退出登录按钮
		}
		this.getUserInfo()
		//判断当前用户是否有密码,否则就不显示密码修改功能
		let res = await uniIdCo.getAccountInfo()
		this.hasPwd = res.isPasswordSet
	},
	methods: {
		login() {
			uni.navigateTo({
				url: '/uni_modules/uni-id-pages/pages/login/login-withoutpwd',
				complete: (e) => {
					console.log(e);
				}
			})
DCloud_JSON's avatar
DCloud_JSON 已提交
88
		},
89 90 91 92 93 94 95 96
		logout: common.logout,
		changePassword() {
			uni.navigateTo({
				url: '/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd',
				complete: (e) => {
					console.log(e);
				}
			})
DCloud_JSON's avatar
DCloud_JSON 已提交
97
		},
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		getUserInfo(e) {
			uni.showLoading({
				mask: true
			});
			usersTable.where("'_id' == $cloudEnv_uid").field('mobile,nickname,email,wx_openid,wx_unionid,ali_openid,apple_open,qq_openid,qq_unionid').get().then(res => {
				console.log({ res });
				this.userInfo = res.result.data[0]
				console.log('this.userInfo', this.userInfo);
				this.hasLogin = true
			}).catch(e => {
				this.userInfo = {}
				this.hasLogin = false
				console.log(e.message, e.errCode);
			}).finally(e => {
				// console.log(e);
				uni.hideLoading()
			})
		},
		bindMobile() {
			// #ifdef APP-PLUS
			uni.preLogin({
				provider: 'univerify',
				success: this.univerify(), //预登录成功
				fail: (res) => { // 预登录失败
					// 不显示一键登录选项(或置灰)
					console.log(res)
					this.bindMobileBySmsCode()
				}
			})
			// #endif

			// #ifdef MP-WEIXIN
			this.$refs['bind-mobile-by-sms'].open()
			// #endif

			// #ifdef H5
			//...去用验证码绑定
			this.bindMobileBySmsCode()
			// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
137
		},
138 139 140 141 142 143 144 145 146 147
		univerify() {
			uni.login({
				"provider": 'univerify',
				"univerifyStyle": this.univerifyStyle,
				success: async e => {
					console.log(e.authResult);
					uniIdCo.bindMobileByUniverify(e.authResult).then(res => {
						console.log(res);
						this.getUserInfo()
					}).catch(e => {
DCloud_JSON's avatar
DCloud_JSON 已提交
148
						console.log(e);
149
					}).finally(e => {
DCloud_JSON's avatar
DCloud_JSON 已提交
150
						console.log(e);
151 152 153 154 155 156
						uni.closeAuthView()
					})
				},
				fail: (err) => {
					console.log(err);
					if (err.code == '30002' || err.code == '30001') {
DCloud_JSON's avatar
DCloud_JSON 已提交
157 158
						this.bindMobileBySmsCode()
					}
159 160 161 162 163 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
				}
			})
		},
		bindMobileBySmsCode() {
			uni.navigateTo({
				url: './bind-mobile/bind-mobile',
				events: {
					getUserInfo: () => {
						this.getUserInfo()
					}
				},
				complete(e) {
					console.log(e);
				}
			})
		},
		setNickname(nickname) {
			console.log(nickname);
			if (nickname) {
				usersTable.where('_id==$env.uid').update({
					nickname
				}).then(e => {
					console.log(e);
					if (e.result.updated) {
						uni.showToast({
							title: "更新成功",
							icon: 'none'
						});
						this.userInfo.nickname = nickname
					} else {
						uni.showToast({
							title: "没有改变",
							icon: 'none'
						});
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
194
				})
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
				this.$refs.dialog.close()
			} else {
				this.$refs.dialog.open()
			}
		},
		deactivate() {
			uni.navigateTo({
				url: "/uni_modules/uni-id-pages/pages/userinfo/deactivate/deactivate"
			})
		},
		async bindThirdAccount(provider) {
			const uniIdCo = uniCloud.importObject("uni-id-co")
      const bindField = {
        weixin: 'wx_openid',
        alipay: 'ali_openid',
        apple: 'apple_openid',
        qq: 'qq_openid'
      }[provider.toLowerCase()]
DCloud_JSON's avatar
DCloud_JSON 已提交
213

214 215 216 217
			if (this.userInfo[bindField]) {
				await uniIdCo['unbind' + provider]()
				await this.getUserInfo()
			} else {
DCloud_JSON's avatar
DCloud_JSON 已提交
218
				uni.login({
219 220
					provider: provider.toLowerCase(),
					onlyAuthorize: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
221
					success: async e => {
222 223
						const res = await uniIdCo['bind' + provider]({
							code: e.code
DCloud_JSON's avatar
DCloud_JSON 已提交
224
						})
225 226 227 228
						if (res.errCode) {
							uni.showToast({
								title: res.errMsg || '绑定失败'
							})
DCloud_JSON's avatar
DCloud_JSON 已提交
229
						}
230
						await this.getUserInfo()
DCloud_JSON's avatar
DCloud_JSON 已提交
231
					},
232 233 234
					fail: async (err) => {
						console.log(err);
						uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
235 236 237
					}
				})
			}
238

DCloud_JSON's avatar
DCloud_JSON 已提交
239 240
		}
	}
241
}
DCloud_JSON's avatar
DCloud_JSON 已提交
242 243
</script>
<style lang="scss" scoped>
244 245 246 247 248
@import url("/uni_modules/uni-id-pages/common/login-page.scss");

.uni-content {
	padding: 0;
}
DCloud_JSON's avatar
DCloud_JSON 已提交
249

250 251 252 253 254 255 256 257
/* #ifndef APP-NVUE */
view {
	display: flex;
	box-sizing: border-box;
	flex-direction: column;
}

@media screen and (min-width: 690px) {
DCloud_JSON's avatar
DCloud_JSON 已提交
258 259
	.uni-content {
		padding: 0;
260 261 262 263 264 265
		max-width: 690px;
		margin-left: calc(50% - 345px);
		border: none;
		max-height: none;
		border-radius: 0;
		box-shadow: none;
DCloud_JSON's avatar
DCloud_JSON 已提交
266
	}
267
}
DCloud_JSON's avatar
DCloud_JSON 已提交
268

269 270 271 272 273 274 275
/* #endif */
.avatar {
	align-items: center;
	justify-content: center;
	margin: 22px 0;
	width: 100%;
}
DCloud_JSON's avatar
DCloud_JSON 已提交
276

277 278 279 280 281 282
.item {
	flex: 1;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
}
DCloud_JSON's avatar
DCloud_JSON 已提交
283

284 285 286 287 288 289 290
button {
	margin: 10%;
	margin-top: 40px;
	border-radius: 0;
	background-color: #FFFFFF;
	width: 80%;
}
291

292 293 294
.mt10 {
	margin-top: 10px;
}
DCloud_JSON's avatar
DCloud_JSON 已提交
295
</style>