1 Star 0 Fork 0

享阅-微月/tt_yundou

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
App.vue 4.54 KB
一键复制 编辑 原始数据 按行查看 历史
feikor 提交于 2023-10-21 09:31 . 第一次提交
<script>
import {
login,
getisOnline,
ttlogin,
getOfficialAppID
} from '@/module/index.js'
import {
config
} from '@/config/index.js'
export default {
globalData: {
iosFlag: true,
LinkType: null
},
onLaunch: function(options) {
let {
official_account_id,
uid,
video_id,
link_id,
video_detail_id,
ip,
ua
} = options.query; //公众号参数
// 保存各种落地页传过来的参数
uni.setStorageSync('login_query', {
query:options.query,
path:options.path,
})
// 如果有token
if (uni.getStorageSync('token')) {
this.$isResolve();
return
}
// h5登录
if (this.isWechat()) {
this.getCode(official_account_id, link_id)
return
}
// 小程序登录
let that = this
tt.login({
success(data) {
let query = {
app_id: tt.getEnvInfoSync().microapp.appId,
code: data.code,
...options.query
}
ttlogin(query).then((res) => {
if (res.code === 200) {
uni.setStorageSync('token', res.data.token)
uni.setStorageSync('login', res.data)
uni.setStorageSync('is_online', (res.data.is_online_a == 1 ? false :
true) ?? false)
uni.setStorageSync('environment', 'applet') // 当前项目运行环境 小程序
} else {
uni.showToast({
title: res.message,
icon: "none"
})
}
that.$isResolve();
}).catch(err => {
console.log(err)
that.$isResolve();
})
}
})
this.globalData.LinkType = uni.getStorageSync('is_online')
var self = this
uni.getSystemInfo({
success: res => {
if (res.osName === 'ios') {
self.globalData.iosFlag = true
} else {
self.globalData.iosFlag = true
}
}
})
},
methods: {
isWechat() {
return String(navigator?.userAgent?.toLowerCase()?.match(/MicroMessenger/i)) === "micromessenger";
},
getCode(id, link_id) {
// 非静默授权,第一次有弹框
let code = '';
let callback_url = config.callback_url; // 获取页面url
let state = this.getUrlCode().state || '';
code = this.getUrlCode().code; // 截取code
if (link_id) {
id = `${id}_${link_id}`
}
if (code == null || code === '') {
// 如果没有code,则去请求
uni.showLoading({
title: '加载中'
});
let sid = id.split('_')
sid = sid[0] || ''
getOfficialAppID({official_account_id:sid}).then((res) => {
if (res.code == 200) {
let appid = res.data.app_id
uni.hideLoading();
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
callback_url
)}&response_type=code&scope=snsapi_userinfo&state=${id}#wechat_redirect`;
} else {
uni.hideLoading();
uni.showToast({
title: res.message,
icon: "none"
})
}
}).catch((err) => {
uni.hideLoading();
uni.showToast({
title: res.message,
icon: "none"
})
})
} else {
// 当code不等于空时,调用后端接口获取用户信息
this.getUserInfo(code, state);
}
},
// 从url中获取code返回
getUrlCode() {
// 截取url中的code方法
var url = location.search;
// this.winUrl = url;
var theRequest = new Object();
if (url.indexOf('?') != -1) {
var str = url.substr(1);
var strs = str.split('&');
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
}
}
return theRequest;
},
getUserInfo(code, uid) {
let data = {
code
}
uid = uid.split('_')
data.official_account_id = uid[0] || ''
if (uid.length > 1) {
data.link_id = uid[1]
}
ttlogin(data).then((res) => {
if (res.code == 200) {
uni.setStorageSync('token', res.data.token)
uni.setStorageSync('login', res.data)
uni.setStorageSync('is_online', true) // h5下默认开启
uni.setStorageSync('environment', 'official_account') // 当前项目运行环境 公众号网页
this.globalData.LinkType = true
} else {
uni.showToast({
title: res.message,
icon: "none"
})
}
this.$isResolve();
}).catch((err) => {
this.$isResolve();
console.log(err)
})
}
}
}
</script>
<style>
@import "/wxcomponents/vant/dist/common/index.wxss";
@import "/iconfont/index.css";
@import "/until/tt-vant-index.css";
/*每个页面公共css */
page {
background-color: #ffffff;
font-family: '微软雅黑 Light';
}
</style>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/microreading/tt_yundou.git
[email protected]:microreading/tt_yundou.git
microreading
tt_yundou
tt_yundou
master

搜索帮助