<template>
	<view class="content">
		<view class="title">密码登录</view>
		<view class="form vflex">
			<u-input v-model="userInfo.name" placeholder="用户名/手机号"></u-input>
			<u-input :type="pwd_type" v-model="userInfo.password" placeholder="请输入密码">
				<template slot="suffix">
					<view @click="show_pwd" v-if="pwd_type == 'password'">
						<image src="/static/images/login/biyanjing.png" class="pwd_icon"></image>
					</view>
					<view @click="show_pwd" v-if="pwd_type == 'text'">
						<image src="/static/images/login/yanjing.png" class="pwd_icon"></image>
					</view>
				</template>
			</u-input>
		</view>
		<view class="hflex acenter">
			<u-checkbox-group @change="checkboxChange">
				<u-checkbox v-model="agree" shape="circle"></u-checkbox>
			</u-checkbox-group>
			<view class="text">阅读并同意<span class="read" @click="open(0)">《用户服务协议》</span>、<span class="read" @click="open(1)">《个人信息保护政策》</span></view>
		</view>
		<view class="button hflex acenter jcenter" @click="login">
			<view>登录</view>
		</view>
		<view class="hflex acenter jcenter bottom">
			<view class="text_style1" @click="codeLogin">验证码登录</view>
			<view style="margin: 0 10rpx;">|</view>
			<view class="text_style2" @click="forgotPwd">忘记密码</view>
		</view>
		<!-- 弹出层 -->
		<u-modal :show="showProtocol" :title="protocolTitle" confirmColor="#2988FE" @confirm="isRead" confirmText="我已阅读">
			<view class="slot-content">
				<rich-text :nodes="protocolContent"></rich-text>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				userInfo: {
					name: '',
					password: ''
				},
				pwd_type: 'password',
				agree: false,
				showProtocol: false,
				protocolTitle: '',
				protocolContent: '',
			}
		},
		onLoad() {
			that = this
		},
		methods: {
			// 查看密码
			show_pwd() {
				if(that.pwd_type == 'text') {
					that.pwd_type = 'password'
				} else {
					that.pwd_type = 'text'
				}
			},
			// 阅读并同意
			checkboxChange(n) {
				this.agree = !this.agree
			},
			// 打开弹出层
			open(index) {
				const that = this
				$api.req({
					url: '/data/api.Login/getloginset'
				}, function(res) {
					if (res.code == 1) {
						if(index == 0) {
							uni.setStorageSync('xieyi',res.data.service_agreement)
							$api.jump('/pages/user/agreement?title=用户服务协议')
						} else {
							uni.setStorageSync('xieyi',res.data.protection_policy)
							$api.jump('/pages/user/agreement?title=个人隐私保护政策')
						}
					}
				})
			},
			// isRead() {
			// 	that.showProtocol = false
			// },
			// 登录
			login() {
				if ($api.formCheck(that.userInfo.name,"required") && $api.formCheck(that.userInfo.password, "password")) {
					if (!that.agree) {
						$api.info('请先阅读并同意用户协议和隐私协议')
					} else {
						wx.getSetting({
							success (res){
						        if (res.authSetting['scope.userInfo']) {
						          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
									wx.login({
										success: function(res) {
											console.log("getUserInfo:",res);
											uni.request({
												url: $api.config.baseUrl + '/data/api.Login/getOpenid',
												method: 'POST',
												data: {
													code: res.code
												},
												header: {
													'content-type': 'application/json',
													'api-name': 'wxapp'
												},
												success: (res) => {
													if(res.data.code == 1) {
														$api.req({
															url: '/data/api.Login/in',
															method: 'POST',
															data: {
																phone: that.userInfo.name,
																password: that.userInfo.password,
																openid: res.data.data.openid
															}
														}, function(res) {
															console.log(res)
															if(res.code == 1) {
																wx.setStorageSync("token",res.data.token)
																wx.setStorageSync("id",res.data.id)
																var options = { 
																  user: res.data.huanxinID,
																  pwd: '12345678',
																  appKey: uni.WebIM.config.appkey,
																  success: function (res2) {
																	wx.setStorageSync("myUsername",res.data.huanxinID)
																  },
																  error: function(){
																  }
																};
																uni.WebIM.conn.open(options);
																$api.jump('/pages/tabbar/mine/mine',3)
															} else {
																$api.info(res.info)
															}
														})
													}
												}
												
											})
										  
										}
									})
						        }
							}
						})
						
					}
				}
			},
			// 已阅读
			isRead() {
				that.showProtocol = false
				that.agree = true
			},
			// 去验证码登录
			codeLogin() {
				$api.jump('/pages/login/code_login')
			},
			// 忘记密码
			forgotPwd() {
				$api.jump('/pages/login/forgot_pwd')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		padding: 0 60rpx;
		.title {
			margin: 64rpx 0;
			font-size: 52rpx;
			color: #222;
		}
		.form {
			margin-bottom: 100rpx;
			.u-input {
				width: 630rpx !important;
				height: 104rpx !important;
				background-color: #f4f4f4;
				border-radius: 52rpx;
				font-size: 30rpx !important;
				box-sizing: border-box;
				padding: 30rpx 48rpx !important;
				margin: 26rpx 0;
			}
			.pwd_icon {
				width: 40rpx;
				height: 40rpx;
			}
		}
		.text {
			font-size: 24rpx;
			color: #9c9c9c;
		}
		.read {
			color: #2a63f3;
		}
		.button {
			width: 100%;
			height: 96rpx;
			background-color: #506dff;
			border-radius: 50rpx;
			box-shadow: 0 4rpx 28rpx 0 rgba(132,123,255,0.4);
			font-size: 40rpx;
			color: #fff;
			margin: 40rpx 0 36rpx;
		}
		.bottom {
			width: 100%;
			.text_style1 {
				font-size: 24rpx;
				color: #555;
			}
			.text_style2 {
				font-size: 24rpx;
				color: #506dff;
			}
		}
	}
</style>