<template>
	<mobile-frame>
		<view class="main">
			<view class="two">
				<uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
					<uni-forms-item label="账号" name="phone">
						<uni-easyinput type="text" v-model="form.phone" placeholder="请输入账号" />
					</uni-forms-item>
					<uni-forms-item label="密码" name="password">
						<uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
					</uni-forms-item>
					<view class="btn">
						<button type="default" size="mini" @click="toSubmit('form')">登录</button>
						<button type="default" size="mini" @click="wxLogin()">微信信任登录</button>
					</view>
				</uni-forms>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				logoUrl: this.$config.logoUrl,
				form: {},
				rules: {
					phone: {
						rules: [{
								required: true,
								errorMessage: '请输入账号',
							},
							{
								minLength: 11,
								maxLength: 11,
								errorMessage: '账号长度在{maxLength}个字符',
							}
						]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码',
						}, ]
					},
				}
			};
		},
		onShow: function() {
			const that = this;
			that.search();

		},
		methods: {
			search() {
				const that = this;
			},
			// 提交登录
			toSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					const res = await that.$api(`/user/login`, 'POST', {
						...params
					})
					if (res.errcode == '0') {
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						});
						uni.setStorage({
							key: 'token',
							data: res.data,
							success: function() {
								uni.navigateBack({
									delta: 1
								})
							}
						});
					} else {
						if (res.errcode == '-5') {
							// 账号无,注册账号
							that.createUser(params);
						} else {
							uni.showToast({
								title: res.errmsg,
								icon: 'none'
							});
						}
					}
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			// 注册账号,去登录
			async createUser(params) {
				const that = this;
				const res = await that.$api(`/user`, 'POST', {
					...params
				})
				if (res.errcode == '0') {
					that.userLogin(params, '1')
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 注册微信账号,去登录
			async createwxUser(params) {
				const that = this;
				const res = await that.$api(`/user`, 'POST', {
					...params
				})
				if (res.errcode == '0') {
					that.userLogin(params, '2')
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 账号,微信登录
			async userLogin(params, type) {
				const that = this;
				let res;
				if (type == '1') {
					res = await that.$api(`/user/login`, 'POST', {
						...params
					})
				} else if (type == '2') {
					res = await that.$api(`/user/wxLogin`, 'POST', {
						openid: params.openid
					})
				}
				if (res.errcode == '0') {
					uni.showToast({
						title: '登录成功',
						icon: 'success'
					});
					uni.setStorage({
						key: 'token',
						data: res.data,
						success: function() {
							uni.navigateBack({
								delta: 1
							})
						}
					});
				} else {
					if (res.errcode == '-5') {
						// 账号无,注册账号
						that.createwxUser(params);
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						});
					}
				}
			},
			// 微信登录
			wxLogin() {
				const that = this;
				uni.login({
					provider: 'weixin',
					success: function(res) {
						// 获取平台信息
						uni.getSystemInfo({
							success: async function(arr) {
								let platForm = arr.uniPlatform;
								if (platForm == 'app') { //app平台
									uni.showToast({
										title: 'app端暂未开通微信登录',
										icon: 'none'
									});
								} else if (platForm == 'mp-weixin') { //微信平台
									const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
										js_code: res.code,
										config: 'pointApp'
									})
									if (aee.errcode == '0') {
										// 微信登录
										that.userLogin({
											openid: aee.data.openid
										}, '2');
									} else {
										uni.showToast({
											title: res.errmsg,
										});
									}
								}
							}
						});
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.main {


		.two {
			padding: 2vw;

			.btn {
				text-align: center;
				margin: 0 0 2vw 0;

				button {
					margin: 0 2vw;
					background-color: var(--f35BColor);
					color: var(--fffColor);
				}
			}
		}
	}
</style>
