<template>

	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<!-- <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix"></image>
			<text style="font-weight: bold;" class="title">宜宾消防公共平台移动端</text> -->
			<image :src="login_top" mode="widthFix" class="width100" style="margin-top: -80upx;"></image>
			<image :src="description" mode="widthFix" class="img_top"
				style="position: absolute;right: 0;top: 120upx; z-index: 999;width: 346upx;height: 330upx;"></image>
		</view>

		<!-- 账号密码框 -->
		<view class="login-form-content">
			<text style="color: #98AEAF;">账号</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<text style="color: #98AEAF;">密码</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<view class="">
				<u-checkbox-group @change="checkboxChange" activeColor="#4cb2b6">
					<u-checkbox type="checkbox" :checked='isHistory' :key="1" :name="1" label="记住密码"></u-checkbox>
				</u-checkbox-group>
			</view>
			<Verify @success="pwdLogin" :mode="'pop'" :captchaType="'blockPuzzle'"
				:imgSize="{ width: '330px', height: '155px' }" ref="verify"></Verify>

			<!-- 登录按钮 -->
			<view class="login-form-content2">
				<button @click="handleLogin" class="login-btn cu-btn block lg">登录</button>

				<!-- 最新版登录方法  current -->	
				<!-- #ifdef MP-WEIXIN -->
				<view class="or">OR</view>
				<view v-on:tap="getUserProfiles">
					<img class="wx_login" :src="wx_login" alt="微信登录">
				</view>
				<!-- #endif -->
				<!-- 老版本登录方法 -->
				<!-- <button class="wx_login" v-else open-type="getUserInfo" lang="zh_CN" @getuserinfo="bindGetUserInfo">微信登录</button> -->
			</view>
			<!-- #ifdef MP-WEIXIN -->
			<u-checkbox-group v-model="radiovalue1" @change="handleClick" activeColor="#4cb2b6">
				<u-checkbox :customStyle="{fontSize:'10px'}">
				</u-checkbox>
				登录即代表同意<span style="margin-left: 10rpx;color: #53B7BB;">获取您的用户信息</span>
			</u-checkbox-group>
			<!-- #endif -->
		</view>
		<!-- <view class="xieyi text-center">
			<text class="text-grey1">登录即代表同意</text>
			<text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
			<text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
		</view> -->
	</view>
</template>
<script>
	// #ifdef H5
	import Verify from '@/pagesA/components/verifition/Verify';
	// #endif
	import {
		setToken,
		setUserId,
	} from '@/utils/auth'
	import config from '@/config'
	const baseUrlImg = config.baseUrlImg
	export default {
		name: 'Login',
		components: {
			// #ifdef H5
			Verify
			// #endif
		},
		data() {
			return {
				description: `${baseUrlImg}/login/description.png`, //登录页面顶部装饰图
				login_top: `${baseUrlImg}/login/login_top.png`, //登录页面顶部
				wx_login: `${baseUrlImg}/login/wx_login.png`, //登录按钮
				captchaEnabled: true, // 验证码开关 TODO 芋艿：需要抽到配置里
				canIUseGetUserProfile: false, // 用来判断用哪个登录
				globalConfig: getApp().globalData.config,
				userInfo: '',
				loginForm: {
					// admin123
					username: uni.getStorageSync("username")?uni.getStorageSync("username"):'',
					password: uni.getStorageSync("password")?uni.getStorageSync("password"):'',
					captchaVerification: '',
					socialCode: '',
					socialType: '',
					socialState: '',
				},
				radiovalue1: [],
				isHistory: false,
			};
		},
		onLoad() {
			//如果手机支持最新版登录就用新方法
			if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true;
			}
		
		},
		onShow(){
			this.isHistory=uni.getStorageSync("username")?true:false
			if(!this.isHistory){
				uni.removeStorageSync("username");
				uni.removeStorageSync("password");
			}
		},
		methods: {
			checkboxChange(e) {
				this.isHistory = e.length > 0 ? true : false
				if(e.length<1 ){
					uni.removeStorageSync("username");
					uni.removeStorageSync("password");
				}
			},
			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0];
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`);
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1];
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`);
			},
			// 微信登录模块
			//老版登录接口(不再弹出登录弹框)
			bindGetUserInfo(e) {
				if (e.detail.userInfo) {
					//业务逻辑
				}
			},
			signUser() {
				uni.navigateTo({
					url: '/pages/sign/sign'
				})
			},
			handleClick(n) {

			},
			// 弹出登录弹框(新版)

			getUserProfiles() {
				// #ifdef H5
				const redirect_uri = 'http://api-security.byants.com/admin-api/system/auth/social-login';
				// const state = Math.floor(Math.random() * (10)) + 1
				const state = new Date().getTime()
				const href_uri =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7f523d8c12d25542&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`
				location.href = href_uri

				// #endif
				// #ifdef MP-WEIXIN
				if (this.radiovalue1[0] != 1) {
					this.$modal.msg('请阅读并勾选用户协议')
					return
				}
				const that = this
				//推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息                                              均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				// uni.getUserProfile({
				// 	desc: '用于完善您的个人信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				// 	success: res => {
				// 		//业务逻辑
				// 		this.userInfo = res.userInfo;
				// 		uni.setStorageSync('userInfo', this.userInfo);
				// // 		console.log(res, '成功获取个人信息');
				// 发起微信登录请求
				uni.login({
					provider: 'weixin',
					onlyAuthorize: true, // 微信登录仅请求授权认证
					success: function(res) {
						const {
							code
						} = res;
						//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
						uni.request({
							// url: 'http://api-security.byants.com/admin-api/system/user/register',
							url: 'http://api-security.byants.com/admin-api/system/auth/social-login',
							method: 'POST',
							// header: {
							// 		'content-type': 'application/x-www-form-urlencoded'
							// 	},
							data: {
								type: 34,
								code: res.code,
								state: '9b2ffbc1-7425-4155-9894-9d5c08541d62'
								// socialType: 34,
								// socialState: '9b2ffbc1-7425-4155-9894-9d5c08541d62'
							},
							success: reponse => {
								if (reponse.data.code == 1002000005) {
									that.$modal.msg(reponse.data.msg)
									setTimeout(() => {
										uni.navigateTo({
											url: `/pages/bind/bind?socialCode=${res.code}`
										})
									}, 1000)

								} else {
									setToken(reponse.data.data)
									setUserId(reponse.data.data)
									that.loginSuccess();
								}


								//获得token完成登录
								// uni.setStorageSync('token', res.token);
							},
							fail: response => {
							}
						});


					},
					fail: function(err) {
						// 登录授权失败
						// err.code是错误码
					}
				});
				// },
				// 	fail: res => {
				// 		uni.showToast({
				// 			title: '你已取消授权',
				// 			icon: 'none',
				// 			duration: 2000
				// 		});
				// 	}
				// });
				// #endif
			},
			// 登录方法
			async handleLogin(params) {
				if (this.loginForm.username === '') {
					this.$modal.msgError('请输入您的账号');
				} else if (this.loginForm.password === '') {
					this.$modal.msgError('请输入您的密码');
				} else {
					// 显示验证码
					// if (this.captchaEnabled) {
					// 	this.$refs.verify.show()
					// } else { // 直接登录
					await this.pwdLogin({});
					// }
				}
			},
			// 密码登录
			async pwdLogin(captchaParams) {
				this.$modal.loading('登录中，请耐心等待...');
				// 执行登录
				this.loginForm.captchaVerification = captchaParams.captchaVerification;
				this.$store.dispatch('Login', this.loginForm).then(() => {
					// uni.setStorageSync('token', res.data.data.token);
					this.$modal.closeLoading();
					this.loginSuccess();
				});
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				if (this.isHistory) {
					uni.setStorageSync("username", this.loginForm.username);
					uni.setStorageSync("password", this.loginForm.password);
				}
				this.$store.dispatch('loadDictDatas');
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index');
					// this.$tab.reLaunch('/pages/work/index');
				});
			}
		}
	};
</script>
<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: left;
			margin: 20px auto;
			margin-top: 10%;
			width: 80%;

			.input-item {
				margin: 15px auto;
				background-color: #f5f9f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}
			}

			.login-btn {
				margin-top: 30px;
				height: 90upx;
				border-radius: 356upx;
				background-color: #4cb2b6;
				color: #ffffff;
				box-shadow: 0px 2upx 6upx 0px rgba(123, 123, 123, 0.3);
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}
		}

		.easyinput {
			width: 100%;
		}
	}

	// 登录按钮
	.login-form-content2 {
		text-align: center;
		margin: 20px auto;
		margin-top: 0%;
		width: 75%;

		.wx_login {
			height: 100upx;
			width: 450upx;

			// font-size: 16px;
			// padding-top: 2px;
			// background-color: #f5f7f9;
			// color: #728f90;
			// border-radius: 356upx;
			// box-shadow: 0px 2upx 6upx 0px rgba(123, 123, 123, 0.3);
		}

		.or {
			font-size: 20upx;
			margin: 20upx;
			color: #98aeaf;
		}
	}

	.login-code-img {
		height: 45px;
	}

	.wx_icon {
		position: relative;
		margin-top: 27%;
		margin-left: 42%;
	}

	.u-checkbox-group--row {
		// justify-content: center;
		align-items: center
	}
</style>
© 2023 Gogs 当前版本: 0.11.91.0811 页面: 33ms 模板: 0ms 简体中文 官方网站 Go1.12.7
