login.vue 8.42 KB
<template>
	<view>
		<div :style="{height:navheight+'px',background:'#008fff'}"></div>
        <view class="loginBg">
        	<view class="loginBox">
        		<text class="loginTitle">智慧商业客流6.0</text>
				<view class="inputBox userBox">
					<image src="../../static/login/user.png" mode=""></image>
					<input class="uni-input userInput" focus placeholder="请输入用户名" v-model="username" />
				</view>
				<view class="inputBox pwdBox">
					<image src="../../static/login/pwd.png" mode=""></image>
					<input class="uni-input pwdInput" placeholder="请输入密码" password v-model="password"/>
				</view>
				<button class="loginBtn" @tap="login">登录</button>
        	</view>
        </view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				username: '',
				password:'',
				navheight:0,
				atoken:'',
				id:''
			}
		},
		onLoad() {
			console.log('aaa')
			uni.getStorage({
				key:'user',
				success: (res) => {
					console.log(res)
					this.username=res.data;
				}
			})
			uni.getStorage({
				key:'pwd',
				success: (res) => {
					this.password=res.data;
				}
			})
			this.getDate()
		},
		onReady(){
			uni.getStorage({
				key:'statusBarH',
				success:(res)=>{
					this.navheight=res.data;
				}
			})
		},
		created() {
			window.webviewPub = this.login;
			try{
				model.webAutoLogin()
			}catch(e){
				//TODO handle the exception
			}
		},
		methods: {
				getDate(){
					var dayData=[];
					var obj={};
					for(var i=1999;i<2020;i++){
						obj={
							'value':i+'年',
							'children':[]
						}
						for(var j=0;j<12;j++){
							var  val=j+1;
							val= val > 9 ? val : '0' + val;
							var obj2={
								'value':val+'月',
								'children':[]
							}
							obj.children.push(obj2)
							 var  day = new Date(i,j+1,0);
							var daycount = day.getDate();
							for(var k=0;k<daycount;k++){
								var  val=k+1;
								val= val > 9 ? val : '0' + val;
								var obj3={
									'value':val+'日'
								}
								obj.children[j].children.push(obj3)
							}
						}
						dayData.push(obj)
					}
					uni.setStorage({
						key:'dayData',
						data:JSON.stringify(dayData)
					})
					
				},
				dateArr(){
					var yearArr=[];
					var yearData=[];
					var currentYear=[];
					var monthData=[];
					var currentMonth=[];
					var weekData=[];
					var currentWeek=[];
					var currentDay=[];
					for(var i=1999;i<2020;i++){
						yearArr.push(i+'年')
					}
					yearData.push(yearArr)
					var monthArr=[];
					for(var i=1;i<12;i++){
						var month=i
						if(i<10){
							month='0'+i;
						}
						monthArr.push(month+'月')
					}
					monthData.push(yearArr);
					monthData.push(monthArr);
					var weekArr=['第一周','第二周','第三周','第四周','第五周'];
					weekData.push(yearArr)
					weekData.push(monthArr)
					weekData.push(weekArr);
					
					const date = new Date();
					var year = date.getFullYear();
					var yearIndex=year-1999
					currentYear.push(yearIndex);
					var monthIndex = date.getMonth();
				
					var month = monthIndex+1 > 9 ? monthIndex+1 : '0' + (monthIndex+1);
					currentMonth.push(yearIndex);
					currentMonth.push(monthIndex);
					  const w = date.getDay()
					  const d = date.getDate();
					   var day= d > 9 ? d : '0' + d;
					  var weekIndex=Math.ceil((d + 6 - w) / 7)-1;
					  currentWeek.push(yearIndex);
					  currentWeek.push(monthIndex);
					  currentWeek.push(weekIndex);
					  var dayIndex=d-1;
					   currentDay.push(yearIndex);
					  currentDay.push(monthIndex);
					  currentDay.push(dayIndex);
					  console.log('login set current day:', currentDay)
					  uni.setStorage({
					  	key:'yearData',
					  	data:JSON.stringify(yearData)
					  })
					  uni.setStorage({
					  	key:'yearData',
					  	data:JSON.stringify(yearData)
					  })
					  uni.setStorage({
					  	key:'currentYear',
					  	data:JSON.stringify(currentYear)
					  })
					  uni.setStorage({
					  	key:'monthData',
					  	data:JSON.stringify(monthData)
					  })
					  uni.setStorage({
					  	key:'currentMonth',
					  	data:JSON.stringify(currentMonth)
					  })
					  uni.setStorage({
					  	key:'weekData',
					  	data:JSON.stringify(weekData)
					  })
					  uni.setStorage({
					  	key:'currentWeek',
					  	data:JSON.stringify(currentWeek)
					  })
					  uni.setStorage({
					  	key:'currentDay',
					  	data:JSON.stringify(currentDay)
					  })
				},
			login(){
				if(this.username.length<=0){
					uni.showToast({
						icon:'none',
						title:'请输入用户名'
					});
					return;
				}
				if(this.password.length<=0){
					uni.showToast({
						icon:'none',
						title:'请输入密码'
					});
					return;
				}
				uni.showLoading({
					title: '加载中'
				});
				uni.request({
					url: window.url + '/users/login',
					data:{
						loginName:this.username,
						password:this.password
					},
					method:'POST',
					success:(res)=> {
						uni.hideLoading();
						var data=res.data;
						if(data.code==200){
							Object.keys(window.localStorage).forEach(item => {
								if(item!='user' && item!='pwd' && item != 'statusBarH'){
									uni.removeStorage({
										key: item,
										success: function (res) {
										}
									});
								}
							})
							this.dateArr()
							uni.setStorage({
								key:'user',
								data:this.username
							})
							uni.setStorage({
								key:'pwd',
								data:this.password
							})
							this.atoken=data.data.atoken;
							this.id=data.data.user.accountId
							uni.request({
								url: window.url + '/malls',
								data:{
									accountId:this.id,
									status:1,
									_t:Date.parse(new Date())/1000
								},
								header: {
									'Authorization': this.atoken //自定义请求头信息
								},
								method:'GET',
								success:(res) =>{
									uni.setStorage({
										key:'accountList',
										data:JSON.stringify(res.data.data)
									})
								}
							})
							uni.request({
								url: window.url + "/accounts",
								data:{
									id:this.id,
									_t:Date.parse(new Date())/1000
								},
								header: {
									'Authorization': this.atoken //自定义请求头信息
								},
								method:'GET',
								success(res){
									if(res.data.code==200){
										var datas=res.data.data;
										uni.setStorage({
											key:'orgName',
											data:datas[0].name
										})
										uni.setStorage({
											key:'accountName',
											data:datas[0].name
										})
									}
									uni.switchTab({
										url:'../index/index',
									})
								}
							})
							
							uni.setStorage({
								key:'atoken',
								data:data.data.atoken
							})
							uni.setStorage({
								key:'accountId',
								data:data.data.user.accountId
							})
							uni.setStorage({
								key:'orgIds',
								data:data.data.user.accountId
							})
							uni.setStorage({
								key:'type',
								data:'account'
							})
						}else{
							uni.showToast({
								icon:'none',
								title:data.msg
							});
						}
					}
				})
			},
		}
	}
</script>

<style>
	.loginBg{
		background: url(../../static/login/loginBg.png)  no-repeat;
		background-size: 100% 100%;
		height: 731.88upx;
	}
	.loginBox{
		background: #ffffff;
		width: 670.28upx;
		height: 663.04upx;
		position: relative;
		top: 311.59upx;
		margin: 0 auto;
		box-shadow:0px 0px 12.68upx 0px rgba(0,0,0,0.14);
		border-radius:7.24upx;
		padding-top:85.14upx;
	}
	.loginTitle{
		font-size:43.47upx;
		color:rgba(74,74,74,1);
		letter-spacing:1.81upx;
		text-align: center;
		display: block;
		position: relative;
		margin-bottom: 119.56upx;
	}
	.inputBox{
		width: 528.98upx;
		border-bottom: 1.81upx solid #9B9B9B;
		margin: 0 auto;
		padding-left: 12.68upx;
		position: relative;
	}
	.userBox{
		margin-bottom: 43.47upx;
	}
	.userBox image{
		height:32.6upx ;
		width: 25.5upx;
		display: inline-block;
		position: absolute;
		top: 16.3upx;
	}
	.pwdBox image{
		height:32.6upx ;
		width: 32.6upx;
		display: inline-block;
		position: absolute;
		top: 16.3upx;
	}
	.userInput{
		display: inline-block;
		font-size: 30.79upx;
		top: 9.05upx;
		left:54.34upx;
	}
	.pwdInput{
		display: inline-block;
		font-size: 30.79upx;
		top: 9.05upx;
		left: 54.34upx;
	}
	.pwdBox{
		margin-bottom: 94.2upx;
	}
	.loginBtn{
		width:516.36upx;
		height:76.08upx;
		background:rgba(0,105,255,1);
		border-radius:21px;
		font-size:36.23upx;
		color:rgba(255,255,255,1);
		line-height:76.08upx;
		letter-spacing: 25.36upx;
		padding: 0;
		padding-left: 25.36upx;
	}
</style>