login.vue 11.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
<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>
				<text class='checkUrl' @tap="switchConnect">切换服务器地址</text>
				<button class="loginBtn" @tap="login">登录</button>
        	</view>
        </view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				username: '',
				password:'',
				navheight:0,
				atoken:'',
				id:'',
				list:[]
			}
		},
		onLoad() {
			uni.getStorage({
				key:'user',
				success: (res) => {
					this.username=res.data;
				}
			})
			uni.getStorage({
				key:'pwd',
				success: (res) => {
					this.password=res.data;
				}
			})
			var isLoging=false;
			uni.getStorage({
				key:'isLoging',
				success:(res)=>{
					isLoging=res.data;
				}
			})
			if(isLoging){
				window.webviewPub = this.login;
				try{
					model.webAutoLogin()
				}catch(e){
					//TODO handle the exception
				}
			}
		},
		onReady(){
			uni.getStorage({
				key:'statusBarH',
				success:(res)=>{
					this.navheight=res.data;
				}
			})
		},
		methods: {
				getDate(){
					var dayData=[];
					var obj={};
					var lastyears=new Date().getFullYear()+4;
					var firtyears=new Date().getFullYear()-15;
					for(var i=firtyears;i<lastyears;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=[];
					var lastyears=new Date().getFullYear()+4;
					var firtyears=new Date().getFullYear()-15;
					for(var i=firtyears;i<lastyears;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-firtyears;
					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);
					  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)
					  })
				},
				constructorData(datas){
					datas.forEach(item=>{
						if(item.children){
							this.constructorData(item.children)
						}
						if(item.name=='home'){
							if(item.children[0]){
								uni.setStorage({
									key:'homes',
									data:item.children[0].name
								})
								uni.setStorage({
									key:'type',
									data:item.children[0].name
								})
							}
						}
						this.list.push({name:item.name})
					})
				},
				judgeClient() {
					let client = '';
					if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {  //判断iPhone|iPad|iPod|iOS
					  client = 'ios';
					} else if (/(Android)/i.test(navigator.userAgent)) {  //判断Android
					  client = 'android';
					} else {
					  client = 'pc';
					}
					return client;
				  },
				switchConnect() {
					let that = this;
					that.$message.confirm('<div class="dialog-content" style="color: #333; font-size: 17px;text-align: center;">确认切换该服务器地址?</div>', function(res, body) {
					}, [{
						className: 'dialog-btn',
						label: '取消',
						callback: function(res, $body) {
							return true;
						}
					}, {
						className: 'dialog-btn',
						label: '确定',
						callback: function(res, $body) {
							if(that.judgeClient()=='ios'){
								that.swiftFun();
							}else if(that.judgeClient()=='android'){
								model.switchConnectType() 
							}
						}
					}], '提示');
				},
				swiftFun(){
					window.webkit.messageHandlers.switchConnectType.postMessage("");
				},
			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){
							console.log('aaaa')
							Object.keys(window.localStorage).forEach(item => {
								if(item!='user' && item!='pwd' && item != 'statusBarH'&&item!='url'){
									uni.removeStorage({
										key: item,
										success: function (res) {
										}
									});
								}
							})
							this.getDate()
							this.dateArr()
							uni.setStorage({
								key:'isLoging',
								data:'true'
							})
							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 + '/auth/api/v1/auth/apps',
								data:{
									_t:Date.parse(new Date())/1000
								},
								header: {
									'Authorization': this.atoken //自定义请求头信息
								},
								method:'GET',
								success:(res) =>{
									var unid=''
									res.data.list_data.forEach(item=>{
										if(item.name==window.pageType){
											unid=item.unid;
										}
									})
									uni.request({
										url: window.url + '/auth/api/v1/auth/apps/'+unid+'/menus',
										data:{
											_t:Date.parse(new Date())/1000
										},
										header: {
											'Authorization': this.atoken //自定义请求头信息
										},
										method:'GET',
										success:(res) =>{
											this.constructorData(res.data.menu_tree)
											localStorage.setItem('menus',JSON.stringify(this.list));
											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.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.setStorage({
								key:'atoken',
								data:data.data.atoken
							})
							uni.setStorage({
								key:'accountId',
								data:data.data.user.accountId
							})
						}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{
		position: relative;
		display: inline-block;
		font-size: 30.79upx;
		top: 9.05upx;
		left:54.34upx;
	}
	.pwdInput{
		position: relative;
		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;
	}
	.checkUrl{
		position: absolute;
		    bottom: 18.12upx;
			right: 21.74upx;
			font-size: 21.74upx;
			color: #777;
		}
</style>