<template>
	<view class="login">
		<view class="contxent">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="form.username" type="text" maxlength="11" placeholder="手机号">
				</wInput>
				<wInput v-model="form.password" type="password" maxlength="8" placeholder="密码" confirm-type="start"
					@confirm="startLogin"></wInput>
			</view>
			<!-- linear-gradient(to bottom right, #73a6a4, #88d0b5) -->
			<wButton bgColor="linear-gradient(to bottom right, rgb(55,186,189), #94e2c4)" class="wbutton" text="登 录"
				:rotate="isRotate" @click="startLogin"></wButton>
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<!-- <view class="login_icon">
				<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view> -->
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">重置密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
			<view class="uni-fixed-bottom">
				<div style="padding: 0 15px;">
					<u-row>
						<u-col span="5">
							<u-text text="技术支持：会山科技" color="#cacaca">
							</u-text>
						</u-col>
						<u-col span="3">
							<div></div>
						</u-col>
						<u-col span="5">
							<u-text style="position: absolute; right: 15px;bottom: 0;" text="hs@willalp.com"
								color="#cacaca">
							</u-text>
						</u-col>
					</u-row>
				</div>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../watch-login/watch-input.vue' //input
	import wButton from '../watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				//logoImage: 'data:image/png;base64,[encoded data, 23364 characters]',
				logoImage: '../../static/img/login/pns_app_icon.png',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true, // 是否聚焦
				form: {
					username: '',
					password: '',
					loginType: 0,
					code: '',
					uuid: ''
				},
				openid: '',
				msgNums: [],
				clearItem1: null,
				clearItem2: null,
			}
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
			this.isLogin();
		},
		created() {
			uni.setStorageSync('refreshCount', 0)
			uni.setStorageSync('refreshStart', 1)
		},
		methods: {
			isLogin() {
				//判断缓存中是否登录过，直接登录
				try {
					const value1 = uni.getStorageSync('userInfo');
					const value2 = uni.getStorageSync('orgInfo');
					if (value1 && value2) {
						uni.reLaunch({
							url: '../../pages/home/index'
						})
					}
				} catch (e) {
					// error
				}
			},
			async startLogin(e) {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (/^1[3|4|5|6|7|8|9][0-9]\d{4,8}$/.test(this.form.username)) {
					this.form.loginType = 1;
				} else
				if (this.form.username.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					_this.isRotate = false;
					return;
				}
				if (this.form.password.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					_this.isRotate = false;
					return;
				}
				_this.isRotate = true
				let that = this
				uni.login({
					provider: "weixin",
					success: function(res) {
						that.form.code = res.code
						that.submitLogin()
					},
					fail() {
						_this.isRotate = false
					}
				})
			},
			async submitLogin() {
				uni.setStorageSync("loginForm", this.form)
				// 发送请求
				const {
					data: res
				} = await _this.$httpRequest({
					url: '/api/login',
					method: 'post',
					data: this.form,
					urlType: 1,
					isNotToken: true
				})
				if (!res) {
					_this.isRotate = false
					this.$showModal('网络异常')
					return
				}
				if (res.code == 200) {
					_this.isRotate = false
					uni.reLaunch({
						url: 'selectOrg'
					})
				} else {
					_this.isRotate = false;
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: '账号或密码错误'
					});
				}
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			async saveOpenId() {
				let data = {
					userNo: uni.getStorageSync("setUserName"),
					pushCid: this.openid
				}
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/push/save/oid',
					method: 'post',
					data: data
				})
				if (res.code == 200) {} else {}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import url("../watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
