<template>
<view class="applet-box">
<view class="logo">
<image :src="schoolLogo" mode="aspectFill" class="logo-img"></image>
</view>
<!-- #ifdef MP-WEIXIN-->
<button class='btn common' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="login" v-if="isShowWechat==='1'">
微信快速登录
</button>
<!-- #endif -->
<!-- <view class="btn common" @click="login"><text class="login-text login-text-next">微信快速登录</text></view> -->
<view class="btn-next common" @click="goPage('/pages/applet/codeLogin')" v-if="isShowCode==='1'"><text class="login-text">验证码登录</text></view>
<view class="btn-next common" @click="goPage('/pages/applet/pwdLogin')"><text class="login-text">密码登录</text></view>
<view>
<text class="login-text">登录后,可办理业务,查看新闻资讯。还可查看消息通知和校内日程。</text>
</view>
</view>
</template>
<script>
import {
asyncweixin,
asyncode,
asyncLogin,
asyncuserInfo,
asyncFeLogin
} from '@/common/wexin.js'
import sc from '@/common/serviceUrl.js'
export default {
data() {
return {
isShowWechat: JSON.parse(sc.moduleSet).isShowWechat,
isShowCode: JSON.parse(sc.moduleSet).isShowCode,
SessionKey: '',
OpenId: '',
nonce: '',
idToken: '',
type: '1',
schoolLogo:'/static/schoolLogo.png'
}
},
onLoad(){
/ this.idToken = uni.getStorageSync('idToken')
if (this.idToken){
uni.switchTab({
url: '/pages/tab/index/index'
})
} /
},
methods: {
//登录
async login() {
let _this = this;
this.$store.commit('setType', this.type)
let param = {
"appId": "APP_ID",
"deviceId": 'DEVICE_ID',
"osType": 'osType',
"geo": 'GEO',
"state": Math.round(Math.random())
}
let res = await asyncweixin(param)
if (res.data.code == 0) {
let url = res.data.data.callbackUri
this.nonce = res.data.data.nonce
let [error, wxLoginRes] = await uni.login({ // 获取登录用户code
provider: 'weixin',
})
let code = wxLoginRes.code;
let [_, userInfoRes] = await uni.getUserInfo({ //获取用户信息
provider: 'weixin'
})
console.log(userInfoRes.rawData)
var rawData = userInfoRes.rawData
var signature = userInfoRes.signature
var encryptedData = userInfoRes.encryptedData
var iv = userInfoRes.iv
let param = {
"code": code,
"nonce": _this.nonce,
'rawData': rawData,
'signature': signature,
'encryptedData': encryptedData,
'iv': iv,
}
console.log(param, '数据')
var codeRes = await asyncode(param)
if (codeRes.data.code == 0) {
let newnonce = codeRes.data.data.nonce
let noceRes = await asyncuserInfo({ //请求nonce
"nonce": newnonce
})
if (noceRes.data.code == 0) {
let aginnonce = noceRes.data.data.nonce
_this.$store.commit('setNonce', aginnonce);
if (noceRes.data.data.federatedBindStatus) { //已经绑定微信
let arr = noceRes.data.data.accounts
let param = {
"nonce": aginnonce,
"accountId": arr[0].id
}
if (arr.length == 1) { //只绑定一个账号
var loginRes = await asyncFeLogin(param)
if (loginRes.data.code == 0) {
_this.idToken = loginRes.data.data.idToken
try{
uni.setStorageSync('idToken', _this.idToken)
}catch(e){
console.log(e)
}
uni.switchTab({
url: '/pages/tab/index/index'
})
console.log(loginRes)
}
} else if (arr.length > 1) { //绑定多个账号
_this.$store.commit('setAccount', arr);
uni.navigateTo({
url: '/pages/applet/changAccount'
})
}
} else { //没有绑定微信
uni.navigateTo({
url: '/pages/applet/bindAccount'
})
}
}
}
}
},
/* 跳转页面 */
goPage(path) {
uni.navigateTo({
url: path
})
},
}
}
</script>
<style scoped>
.des{
}
.applet-box {
width: 750rpx;
padding: 90rpx 50rpx 0 50rpx;
display: flex;
flex-direction: column;
align-items: center;
box-sizing: border-box;
}
.logo {
width: 170rpx;
height: 170rpx;
/* background-color: rgba(0, 84, 163, 1); */
border-radius: 85rpx;
margin-bottom: 119rpx;
display: flex;
justify-content: center;
align-items: center;
}
.logo-img {
width: 128rpx;
height: 128rpx;
}
.btn-next {
width: 650rpx;
height: 88rpx;
background-color: rgba(255, 255, 255, 1);
box-shadow: 0px 2rpx 8rpx 0px rgba(0, 0, 0, 0.06);
border-radius: 44rpx;
border: 1rpx solid rgba(111, 115, 122, 1);
}
.common {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 50rpx;
color: #FFFFFF;
font-size: 32rpx;
}
.btn {
width: 650rpx;
height: 88rpx;
/* #ifdef MP-WEIXIN */
background-color: rgba(53, 171, 52, 1);
/* #endif */
/* #ifdef MP-ALIPAY */
background-color: #1578FE;
/* #endif */
border-radius: 44rpx;
}
.login-text {
font-size: 32rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
line-height: 32rpx;
color: rgba(58, 60, 64, 1);
}
.login-text-next {
color: rgba(255, 255, 255, 1);
}
</style>