<template>
	<ax-body blank="0" hideIndicatorArea>
		<image src="@/static/img/page-bg01.png" class="page-background"></image>
		<view class="body">
			<view class="title">
				<view>
					<view class="h1">您好!</view>
					<view class="h2">欢迎使用中数电动</view>
				</view>
				<image src="@/static/img/logo.svg" class="logo" mode="widthFix"></image>
			</view>
			<view class="logo-wrap">
				<image src="@/static/img/login-bg.svg" class="bg" mode="widthFix"></image>
			</view>
			<view class="footer" style="padding-left: 0;padding-right: 0;">
				<button @getphonenumber="onGetPhoneNumber" open-type="getPhoneNumber" class="submit"
					:disabled="!agreement" style="margin-left: 35px;width: calc(100% - 70px);">
					<!-- <text class="ax ax-iconblock i-weixin"></text> -->
					<text>手机号一键登录</text>
				</button>
				<view class="cancel" @click="cancelLogin">取消登录</view>
				<view @click="agreement = !agreement" class="agreement" style="font-size: 17px;">
					<view class="mask" :class="{ active: agreement }"><text class="ax ax-iconline i-select"></text>
					</view>
					<text>我已阅读并理解和同意</text>
					<text @click.stop="checkPrivacy()" class="em">《隐私政策协议》</text>
				</view>
			</view>
		</view>
	</ax-body>
</template>

<script>
import $config from '@/static/js/config.js';
export default {
	onLoad() {
		uni.getPrivacySetting({
			success: res => {
				this.privacy.name = res.privacyContractName;
				this.privacy.need = res.needAuthorization;
			}
		});
	},
	data() {
		return {
			agreement: false,
			privacy: {
				need: true,
				name: ''
			},
		}
	},
	methods: {
		// 查看隐私
		checkPrivacy() {
			uni.openPrivacyContract();
		},
		onGetPhoneNumber(e) {
			console.log(e);
			// let channelUrl='http://192.168.110.241:9120/zs/channel/admin/'
			let channelUrl = 'https://channel-api.zonelife.cn/zs/channel/admin/';
			uni.login({
				provider: 'weixin',
				success: (res) => {
					if (res.code) {
						if (e.detail.code) {
							this.$api.base("post", '/api/v1/auth/wx/miniapp/phone-code-login', {
								code: res.code,
								phoneCode: e.detail.code
							}).then(async res => {
								uni.setStorageSync($config.keyname.accessToken, res?.data?.accessToken);
								uni.setStorageSync($config.keyname.refreshToken, res?.data?.refreshToken);
								// 识别用户是否通过分销码进入小程序
								if (uni.getStorageSync('ADMIN_USERID')) {
									uni.request({
										url: channelUrl + 'ums/umsAdminUser/distributionBindUser',
										method: 'POST',
										header: {
											'content-type': 'application/json'
										},
										data: {
											userId: res.userInfo.id,
											adminUserId: uni.getStorageSync('ADMIN_USERID'),
										},
										success: (res) => {
											console.info(res.data, '----扫码接口调用');
											setTimeout(() => {
												this.$app.storage.remove('ADMIN_USERID')
											}, 500)
										},
										fail(err) {
											console.log(err, '----错误信息');
										}
									});
								}
								if (this.$app.storage.get('FRIM_ID')) {
									this.$api.base("post", "/userApi/add-firm-user?firmId=" + parseInt(this.$app.storage.get('FRIM_ID')), {}, { error: false }).then(res => {
										this.$app.popup.alert(res.msg);
										setTimeout(() => {
											this.$app.storage.remove('FRIM_ID')
										}, 500)
										console.log(res, '----企业用户通过扫码进入');
									}).catch(err => {
										setTimeout(() => {
											this.$app.storage.remove('FRIM_ID')
										}, 500)
									})
								}
								this.$app.url.back()
							});
						}
					}
				},
			})
		},
		cancelLogin() {
			this.$app.url.back()
		}
	}
}
</script>

<style scoped>
@import url("login.css");
</style>