KeChengJiaoFu/App.vue

102 lines
2.7 KiB
Vue

<script>
import {
postLoginIndex,
getInfo
} from "@/api/index";
export default {
globalData:{
projectname:'', // 项目名称
lat:'', // 公司地址维度
lng:'' ,// 公司地址经度
hostapi:'https://course-buy.dev.scdxtc.cn',// 域名配置
webapi:'https://course.dev.scdxtc.cn'
},
// 优先于show方法
onLaunch() {
console.log(uni.$u.config.v,'uview版本号');
let that = this;
if (process.env.NODE_ENV === 'development') {
that.globalData.hostapi = 'https://course-buy.dev.scdxtc.cn' //测试
} else {
that.globalData.hostapi = 'https://course-buy.dev.scdxtc.cn' //正式
}
// 微信公众号H5登录
// #ifdef H5
// 获取基本信息
getInfo().then(res => {
if (res.code == 0) {
uni.setStorageSync('baseInfo',res.data)//缓存用户基本信息
}
});
let getcode = uni.getStorageSync('code');
console.log(getcode,'codecode');
let link = window.location.href;
console.log(link, 'link',link.indexOf('code='));
//判断link中有没有code=字符串,
if (link.indexOf('code=') > 0) {
//回调函数已经执行 返回的链接存在code= 地址解析
let temp = link.split("code=")[1],
code = temp.split("&")[0];
console.log(code);
uni.setStorageSync('code',code)//缓存用户基本信息
that.getLogin(code);
}else {
if(!getcode) {
let appid = uni.getStorageSync("baseInfo").official_app_id; //公众号id
//就是用户授权后返回来的地址
let uri = that.globalData.webapi; //重定向地址
window.location.href =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`
}
}
// #endif
// 微信小程序登录
// #ifdef MP-WEIXIN
uni.login({
provider: 'weixin',
success: (result)=> {
// 获取基本信息
getInfo().then(res => {
if (res.code == 0) {
uni.setStorageSync('baseInfo',res.data)//缓存用户基本信息
}
});
that.getLogin(result.code);
},
});
// #endif
},
onShow() {
},
onHide() {
},
methods: {
getLogin(code) {
let that = this;
let params = {
code:code
}
postLoginIndex(params).then(res => {
if(res.code == 0){
uni.setStorageSync('token',res.data.token) //缓存token
uni.setStorageSync('userInfo',res.data.baseInfo)//缓存用户信息
that.$isResolve();
}
})
}
}
};
</script>
<style lang="scss">
// 公用css
@import "@/components/uview-ui/index.scss";
@import "@/commons/common.scss";
@import "@/commons/style.scss";
</style>