register.vue 5.0 KB
<template>
	<view class="content">
		<view class="register">
			<!-- url="./highRegister" -->
			<view class="type">
				<image class="photo" src="../../static/gaozhongsheng_img@2x.png"></image>
				<view class="text">我是高中生</view>
				<button class="btn_group" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo"
				 :data-type="1"></button>
			</view>
			<!-- url="./college" -->
			<view class="type">
				<image class="photo" src="../../static/daxeusheng_img@2x.png"></image>
				<view class="text">我是大学生</view>
				<button class="btn_group" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo"
				 :data-type="2"></button>
			</view>
		</view>

	</view>
</template>

<script>
	import app from "../../App.vue";
	export default {
		data() {
			return {
				iv: '',
				encrypted_data: '',
				session_key: '',
				openid: '',
				type:'',
				istoken:''
			}
		},
		onLoad(options) {
			if(options.istoken!=undefined){
				this.istoken=options.istoken
			}
			if(uni.getStorageSync('token')!=''&&this.istoken==''){
				this.getusermsg()
			}else if (uni.getStorageSync('token')==''&&this.istoken==''){
				uni.navigateTo({
					url:'../school/school'
				})
			}
			
			this.get_code()
		},
		methods: {
			
			getusermsg(){
				
				var url = "student/getMyInfo"
				var params={
						token:uni.getStorageSync('token')
				}
				app.post(url,params,"get").then((res)=>{
					if(res.level==1){
						wx.redirectTo({
							url:'../school/school'
						})
					}else if (res.level==2){
						wx.redirectTo({
							url:'../schoolmate/schoolmate?type='+2
						})
					}
				}).catch((err)=>{
					console.log(err)
				})
			},

			wxGetUserInfo(e) {
				let that = this;
				// type:1  高中生  2  大学生
				let type = e.currentTarget.dataset.type;
				console.log(type)
				that.type = type
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						console.log(infoRes)
						that.login()
						that.iv = infoRes.iv;
						that.encrypted_data = infoRes.encryptedData
						that.login()
					},
					fail(res) {

					}
				});


			},
			// 获取sessionkey
			get_code() {
				let that = this;
				uni.login({
					provider: "weixin",
					success(r) {
						console.log(r)
						let url = "common/getSessionKey";
						app.post(url, {
							code: r.code
						}, "post").then(r => {
							console.log(r)
							that.session_key = r.session_key;
							that.openid = r.openid;
							
							console.log(that.session_key);
							console.log(that.openid)
							

							// uni.setStorageSync('openid', r.openid);
							// uni.setStorageSync('session_key', r.session_key);
						}).catch(err => {})

					}
				})
			},

			// 获取token  登录
			login() {
				let that = this;
				// uni.showLoading({ title: '加载中' })
				console.log(343438)
				console.log(that.session_key);
				console.log()
				let url = "common/login";
				let param = {
					sessionKey: that.session_key,
					encryptedData: that.encrypted_data,
					iv: that.iv
				}
				app.post(url, param, "get").then(r => {
					uni.setStorageSync('token', r.userInfo.token);

					this.isuserlogin()

				}).catch(err => {
					console.log(err)
					
					
				})
			},
			
			isuserlogin(){
					var that = this
				//   console.log(6666)
				  var url ='register/isRegister';
				  var params = {
					  token:uni.getStorageSync('token')
				  }
				  app.post(url,params,"get").then((res)=>{
					  console.log(res)
					  
						var url = "student/getMyInfo"
						var params={
							token:uni.getStorageSync('token')
						}
						app.post(url,params,"get").then((res)=>{
							console.log(res)
							if(res.level==1){
								wx.redirectTo({
									url:'../school/school'
								})
							}else if (res.level==2){
								wx.redirectTo({
									url:'../schoolmate/schoolmate?type='+2
								})
							}
						}).catch((err)=>{
							console.log(err)
						})
				  }).catch((err) => {
					  console.log(err)
					  if(err.data==null){
						if (that.type == 1) {
								// console.log(9999)
								uni.navigateTo({
									url: "./highRegister"
								})
							} else {
								uni.navigateTo({
								url: "./college"
							})
						}
					  }
				})
			  }
		}

	}
</script>

<style lang="less">
	.content {
		background-color: #f9f9f9;
		height: 100vh;
		position: relative;
		width: 750rpx;

		.register {
			display: flex;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			// margin: 0 auto;
		}

		.type {
			width: 266rpx;
			height: 352rpx;
			background-color: #fff;
			text-align: center;
			margin: 0 22rpx;
			position: relative;

			.photo {
				width: 160rpx;
				height: 228rpx;
				margin-top: 36rpx;
			}

			.text {
				color: rgba(6, 18, 30, 1);
				font-size: 28rpx;
				margin-top: 20rpx;
			}

			.btn_group {
				width: 226rpx;
				height: 352rpx;
				position: absolute;
				top: 0;
				left: 0;
				background: transparent;
				border: none;
				outline: none
			}
		}
	}
</style>