<template>
	<view class="login-page">
		<view class="logo-head">
			<image src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1695262844000_280.png" mode="widthFix"></image>
			<view class="logo-text">旅小友</view>
		</view>
		<view class="login-form">
			<view class="login-title">登录</view>
			<view class="login-item">
				<input type="text" v-model="msg.account" placeholder="请输入你的同业账号" :class="{'focus':currentFocus=='account'}" @blur="changeCurrentFocus('')" @focus="changeCurrentFocus('account')">
			</view>
			<view class="login-item">
				<input type="password" v-model="msg.password" placeholder="请输入你的登录密码" :class="{'focus':currentFocus=='pwd'}" @blur="changeCurrentFocus('')" @focus="changeCurrentFocus('pwd')">
			</view>
			<view class="login-item">
				<view @click="loginByAccount" class="login-btn" :class="{'disable':msg.account==''||msg.password==''}">立即登录</view>
			</view>
			<u-divider contentPosition="center" :margin-top="60" :margin-bottom="60">其他登录方式</u-divider>
			<view style="text-align: center;">
				<button class="wechat-login" @click="loginByOpenId">
					<image src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1695266425000_759.png" mode="widthFix"></image>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				msg:{
					account:'',
					password:'',
					platform:3,
					OpenId:''
				},
				currentFocus:'',
				loginLoading:false
			}
		},
		mounted() {
			this.getUserProfileHandler()
		},
		methods: {
			changeCurrentFocus(val){
				this.currentFocus=val
			},
			loginByAccount(){
				if(this.msg.account!='' && this.msg.password!=''){
					this.loginErpHandler()
				}
			},
			loginByOpenId(){
				if(this.msg.OpenId!=''){
					this.loginErpHandler()
				}else{
					uni.showToast({
						icon:'none',
						title:'获取微信信息失败，请使用账号密码登录'
					})
				}
			},
			loginErpHandler(){
				if(this.loginLoading) return;
				this.loginLoading=true
				uni.showLoading({
					title:'登录中...'
				})
				this.apipost("b2b_post_Login",this.msg,(res) => {
				    if (res.resultCode == 1) {
						uni.setStorageSync('b2b_user',res.data)
						this.getLogin()
					}else{
						this.loginLoading=false
						uni.hideLoading()
					}
					
				},(error)=>{
					this.loginLoading=false
					uni.hideLoading()
				})
			},
			getUserProfileHandler(){
				wx.login({
					success: (res) => {
						this.getCode(res)
					},
					fail() {}
				})
			},
			getCode(obj) {
			  var that = this;
			  that.request2(
			    {
			      url: "/api/mall/GetWeChatOpenId",
			      data: {
			        Code: obj.code,
			      },
			    },
			    (res) => {
				  if(res.resultCode==1){
					  //this.getLogin(res.data)
					  this.msg.OpenId=res.data
				  }
			    }
			  );
			},
			getLogin() {
			  var that = this;
			
			  let pid = uni.getStorageSync("pid") ? uni.getStorageSync("pid").pid : 0;
			  let Up = uni.getStorageSync("Up") ? uni.getStorageSync("Up").Up : 0;
			  let SmallShopId = uni.getStorageSync("SmallShopId")
			    ? uni.getStorageSync("SmallShopId").SmallShopId
			    : 0;
			  let CounponPassword = uni.getStorageSync("CounponPassword")
			    ? uni.getStorageSync("CounponPassword").CounponPassword
			    : 0;
			  let KeyWord = uni.getStorageSync("KeyWord")
			    ? uni.getStorageSync("KeyWord").KeyWord
			    : "";
			  that.request2(
			    {
			      url: "/api/AppletLogin/Login",
			      data: {
			        Source: 1,
			        OpenId:this.msg.OpenId,
			        Name: '微信用户',
			        Photo: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
			        Gender: 1,
			        Moblie: "",
			        SuperiorId: pid,
			        SmallShopId: SmallShopId,
			        UserPageType: Up,
			        CounponPassword: CounponPassword,
			        KeyWord: KeyWord,
			      },
			    },
			    (res) => {
				 uni.hideLoading()
				 this.loginLoading=false
			      if (res.resultCode == 1) {
			        uni.setStorageSync("mall_UserInfo", res.data);
			        uni.removeStorageSync("pid");
			        uni.removeStorageSync("SmallShopId");
			        uni.removeStorageSync("Up");
			        uni.removeStorageSync("CounponPassword");
			        uni.removeStorageSync("KeyWord");
					uni.redirectTo({
						url:'/pages/index/index'
					})
			      }
				 
			    },
			  (err)=>{
				  uni.hideLoading()
				  this.loginLoading=false
			  });
			},
		}
	}
</script>

<style>
	/* 在线链接服务仅供平台体验和调试使用，平台不承诺服务的稳定性，企业客户需下载字体包自行发布使用并做好备份。 */
	
	.login-page{
		padding:20px;
	}
	.login-page .logo-head{
		margin-top: 20px;
		display: flex;
		align-items: center;
	}
	.login-page .logo-head image{
		width:50px;
		height:50px;
		margin-right: 10px;
	}
	.login-page .logo-head .logo-text{
		font-size: 32px;
		font-family: logo-font;
		font-weight: 700;
	}
	.login-page .login-form{
		margin-top:50px;
		padding:20px;
	}
	.login-page .login-form .login-title{
		font-size: 20px;
		font-weight: 700;
		font-family: logo-font;
		margin-bottom: 30px;
	}
	.login-page .login-form  .login-item{
		margin-top: 30px;
	}
	.login-page .login-form  .login-item input{
		background-color: rgba(0,0,0,.05);
		padding:0 12px;
		height: 46px;
		line-height: 46px;
		border-radius: 8px;
		color:#1d1d1d;
		font-family: nav-font;
	}
	.login-page .login-form  .login-item input.focus{
		background: #1d1d1d;
		color:#FFF;
	}
	.login-page .login-form .login-btn {
	  height: 80rpx;
	  background-color: #111111;
	  text-align: center;
	  line-height: 80rpx;
	  color: #fff;
	 /* font-weight: 600; */
	  font-size: 28rpx;
	  border-radius: 16rpx;
	  font-family: logo-font;
	  font-size: 16px;
	}
	
	.login-page .login-form .login-btn.disable {
	  /* background-color: #ecf1f4;
	  color: #111; */
	      opacity: 0.5;
	}
	
	.login-page .login-form .wechat-login {
	  background-color: none;
	  height: 40px;
	  width: 40px;
	  /* border-radius: 40px; */
	  overflow: hidden;
	  /* box-shadow: 0px 0px 20px 0px rgba(76,87,125,0.2)!important; */
	  padding:0;
	}
	.login-page .login-form .wechat-login image{
		width: 38px;
		height: 38px;
		margin: 1px;
	}
	.login-page .login-form .wechat-login::after {
	  border: none !important;
	  height: auto !important;
	}
</style>
