<!-- 登陆页面 会员/商家 -->

<template>
	<view class="main_width">
		<view style="text-align: center;overflow: hidden;">
			<view class="title">快来优</view>
			<view style="color: #7b7b83;margin-top: 20upx;">开启全民赚钱时代</view>
			<image style="width: 520upx;height: 520upx;" :src="serverUrlImg"></image>
			<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="main_btn_m get_phone">
				<image style="width: 35upx;height: 30upx;margin-right: 15upx;" src="../../static/wechat.png"></image>
				{{sys_role==1? '商户登录':'会员登录'}}
			</button>
		</view>
		<view class="read">
			<checkbox-group @change="checkboxChange">
				<checkbox color="#48ccae" value="1" />
			</checkbox-group>
			<view style="margin-left: 10upx;">
				<view style="margin-left: 14upx;">登录代表您已仔细阅读并同意</view>
				<view style="display: flex;">
					<navigator url="./web?index=6" style="color: rgb(0, 199, 178);">《用户协议》
					</navigator>与
					<navigator url="./web?index=7" style="color: rgb(0, 199, 178);">《隐私政策》
					</navigator>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import commonData from '../../commonData.js'
	import commonFun from '../../commonFun.js'

	export default {
		data() {
			return {
				serverUrlImg: this.serverUrl + 'login.png',
				is_agree: [], //是否同意用户协议
				sys_role: commonData.sys_role, //用户角色
				baseUserId: '', //登录获取baseUserId
				need_back: 0, //是否需要返回到上页
			}
		},
		onLoad(e) {
			if (e.need_back) {
				this.need_back = e.need_back
			}
			this.get_openid()
		},
		methods: {
			// 获取openid
			get_openid() {
				let that = this
				let url = '&WsAjaxBiz=' + (commonData.sys_role == 0 ? 'Worker' : 'Trader') +
					'&WsAjaxAction=fetchMinaOpenid'

				// 微信获取code
				uni.login({
					success: res => {
						commonFun.requestUrl(url, {
							minaCode: res.code
						}, res => {
							if (res.status) {
								that.baseUserId = res.data.Result.baseUser.id
							}
						})
					},
					fail: res => {
						uni.hideLoading()
						console.log('获取小程序code失败', res)
					}
				})
			},
			// 获取手机号码
			getPhoneNumber(e) {
				let that = this
				console.log(e.detail)
				if (e.detail.errMsg.indexOf('fail') !== -1) {
					uni.showModal({
						content: '获取手机号码失败',
						showCancel: false,
						success: function(res) {}
					});
					return
				}
				if (this.is_agree.length == 0) {
					uni.showModal({
						content: '请勾选隐私政策',
						showCancel: false,
						success: function(res) {}
					});
					return
				}

				uni.showLoading({
					title: '加载中...',
					mask: true
				})

				const params = {
					baseUserId: this.baseUserId,
					...e.detail,
				}

				let url = '&WsAjaxBiz=' + (commonData.sys_role == 0 ? 'Worker' : 'Trader') +
					'&WsAjaxAction=fetchMinaPhone'

				commonFun.requestUrl(
					url,
					params, res => {
						if (res.status) {
							console.log('登录成功返回')
							console.log(res.data)
							uni.hideLoading()
							let result = res.data.Result
							uni.setStorageSync('baseUser', result
								.baseUser)
							uni.setStorageSync('bizWorker', result
								.bizWorker)
							uni.setStorageSync('bizTrader', result
								.bizTrader)
							uni.setStorageSync('FOREND_TOKEN', result
								.forendToken)
							uni.setStorageSync('PHONE', result.baseUser
								.mobile)

							// 取消游客模式
							uni.setStorageSync('IS_TOURIST', '0')
							uni.showModal({
								content: '登录成功',
								showCancel: false,
								success: function(res) {
									uni.showLoading({
										title: '加载中...',
										mask: false
									})
									uni.getUserProfile({
										desc: "获取昵称和头像",
										success: (res) => {
											let params = {
												biz_catalog: 1,
												nickname: res.userInfo.nickName,
												profile_photo: res.userInfo.avatarUrl,
											}
											let user_info = commonData.sys_role == 0 ? uni
												.getStorageSync('bizWorker') : uni
												.getStorageSync('bizTrader')
											user_info.nickname = res.userInfo.nickName
											user_info.profile_photo = res.userInfo
												.avatarUrl

											commonFun.init_go_easy(user_info, commonData
												.sys_role == 0 ? 'user_' : 'mer_')
											commonFun.requestUrl(
												'&WsAjaxBiz=Worker&WsAjaxAction=registerInfo',
												params, res => {
													if (that.need_back == 1) {
														uni.navigateBack({
															delta: 1,
														})
													} else {
														uni.reLaunch({
															url: commonData
																.sys_role == 0 ?
																'../index/index' :
																'../merchant/index'
														});
													}
													uni.hideLoading()
												})
										},
										fail: (err) => {
											uni.hideLoading()
											uni.showToast({
												icon: "none",
												title: '获取昵称和头像失败'
											})
											let user_info = commonData.sys_role == 0 ? uni
												.getStorageSync('bizWorker') : uni
												.getStorageSync('bizTrader')
											commonFun.init_go_easy(user_info, commonData
												.sys_role == 0 ? 'user_' : 'mer_')
											uni.reLaunch({
												url: '../personal/index'
											});
										}
									})
								}
							});
						}
					})
			},
			// 选中同意协议
			checkboxChange(e) {
				this.is_agree = e.detail.value
			},
		}
	}
</script>

<style>
	.get_phone {
		margin-top: 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.title {
		color: #40d0ae;
		font-size: 60upx;
		margin-top: 60upx;
	}

	.read {
		color: #8E97AE;
		margin-top: 100upx;
		font-size: 28upx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}
</style>
