KeChengJiaoFu/App.vue

102 lines
2.7 KiB
Vue
Raw Normal View History

2023-05-11 02:51:56 +00:00
<script>
2024-10-21 07:22:55 +00:00
import {
postLoginIndex,
getInfo
} from "@/api/index";
2023-05-11 02:51:56 +00:00
export default {
globalData:{
projectname:'', // 项目名称
lat:'', // 公司地址维度
lng:'' ,// 公司地址经度
2024-10-21 07:22:55 +00:00
hostapi:'https://course-buy.dev.scdxtc.cn',// 域名配置
webapi:'https://course.dev.scdxtc.cn'
2023-05-11 02:51:56 +00:00
},
// 优先于show方法
onLaunch() {
2024-10-21 07:22:55 +00:00
console.log(uni.$u.config.v,'uview版本号');
2023-05-11 02:51:56 +00:00
let that = this;
2024-10-21 07:22:55 +00:00
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
2023-05-11 02:51:56 +00:00
uni.login({
provider: 'weixin',
success: (result)=> {
2024-10-21 07:22:55 +00:00
// 获取基本信息
getInfo().then(res => {
if (res.code == 0) {
uni.setStorageSync('baseInfo',res.data)//缓存用户基本信息
}
});
that.getLogin(result.code);
2023-05-11 02:51:56 +00:00
},
});
2024-10-21 07:22:55 +00:00
// #endif
2023-05-11 02:51:56 +00:00
},
2024-10-21 07:22:55 +00:00
onShow() {
},
onHide() {
2023-05-11 02:51:56 +00:00
},
2024-10-21 07:22:55 +00:00
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();
}
})
}
2023-05-11 02:51:56 +00:00
}
};
</script>
2024-10-21 07:22:55 +00:00
<style lang="scss">
// 公用css
@import "@/components/uview-ui/index.scss";
@import "@/commons/common.scss";
@import "@/commons/style.scss";
2023-05-11 02:51:56 +00:00
</style>