1 Star 0 Fork 23

zakailynn / 天天商城

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
App.vue 6.59 KB
一键复制 编辑 原始数据 按行查看 历史
<script>
//#ifdef APP-PLUS
import checkUpdate from "@/uni_modules/uni-upgrade-center-app/utils/check-update";
//#endif
import {
mapState,
mapActions,
mapMutations
} from 'vuex';
import Vue from 'vue';
let inviteCode = "";
export default {
onLaunch: function(options) {
console.log('App Launch', options)
// uni.showLoading()
//邀请者
if (options.query.u) {
inviteCode = options.query.u;
console.log("invite", inviteCode)
this.setInviteCode(options.query.u);
} else {
this.getInviteFromClipboard();
}
//设置默认信息
this.setDefaultInfo({});
//如果存在登录信息,自动登录
this.autoLogin();
//#ifdef MP
this.micLogin();
//#endif
//#ifdef APP-PLUS
//检测更新
checkUpdate().then(res => {
console.log("检查更新结果", res)
});
this.initUniverify();
this.checkPush();
//#endif
this.initSize(uni.getSystemInfoSync());
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
computed: {
//...mapState('user', ['inviteCode']),
},
methods: {
...mapMutations({
setInviteCode(commit, code) {
//更新邀请码
commit('user/SET_INVITECODE', code)
},
setDefaultInfo(commit, info) {
//更新邀请码
commit('user/SET_USER_INFO', info)
},
setPushClient(commit, push) {
//更新push,如果用户未登录,只是将push参数存到store,
//等待用户登录完成之后,再调用action:user/updatePushClient
commit('user/UPDATE_PUSH_CLIENT', push)
},
setUniverifyInit(commit) {
//APP初始化一键登录成功
commit('user/UNIVERIFY_INIT')
},
}),
...mapActions({
initApp: 'app/init',
mpLogin: 'user/mpLogin',
}),
/**
* 从上一次缓存,自动登录,store已经从缓存中读取上一次的token
*/
async autoLogin() {
if (this.$store.getters['user/isTokenValid']) {
console.log("获取APP信息,需要用户详细信息")
}
this.initApp({});
},
async getInviteFromClipboard() {
//#ifdef APP-PLUS
uni.getClipboardData({
success: (res) => {
//检测是不是正确的邀请码
uniCloud.database().collection("uni-id-users").where(
`my_invite_code=="${res.data}"`).field(
"avatar,nickname").limit(1).get().then(({
result
}) => {
if (result.data && result.data.length > 0) {
this.setInviteCode(res.data);
console.log("getInviteFromClipboard", res.data);
} else {
console.log("粘贴板邀请码无效")
}
})
},
fail: (e) => {}
});
//#endif
},
/**
* 小程序自动登录,
*/
async micLogin(state) {
//#ifdef MP
console.log("小程序用户自动登录开始")
uni.getProvider({
service: 'oauth',
success: (res) => {
console.log(res)
let provider = res.provider[0];
uni.login({
provider: provider,
success: (loginRes) => {
console.log('授权code', loginRes);
this.mpLogin({
provider,
inviteCode: inviteCode,
code: loginRes.code
});
console.log("小程序用户自动登录结束")
}
});
}
});
//#endif
},
/**
* 初始化uni一键登录
*/
async initUniverify(index) {
let _this = this;
// #ifdef APP-PLUS
uni.preLogin({
provider: 'univerify',
success() { //预登录成功
// 显示一键登录选项
console.log("一键登录初始化成功")
_this.setUniverifyInit();
},
fail(res) { // 预登录失败
// 不显示一键登录选项(或置灰)
// 根据错误信息判断失败原因,如有需要可将错误提交给统计服务器
console.log(res.errCode)
console.log(res.errMsg)
//去掉多次检测
/* if (index < 30) {
//重试2次
setTimeout(() => {
_this.initUniverify(index + 1)
}, index * 1000 + 3000)
} */
}
})
// #endif
},
checkPush() {
// #ifdef APP-PLUS
//监听push推送通知
plus.push.addEventListener('receive', ({
type,
title,
content,
payload
}) => { //console.log(type,title,content,payload);
console.log(type, 'type');
if (type == 'receive' || uni.getSystemInfoSync().platform !=
"ios") { //如果type!='receive'是自己本地插件的push消息栏,“拦截”避免死循环',安卓系统没有这个问题
if (typeof payload != 'object') {
payload = JSON.parse(payload)
} //判断是否为object,不是的话手动转一下。hbuilderx 3.0以上版本已经修复此问题可省略
plus.push.createMessage(content, JSON.stringify(payload), {
title: payload.title,
subtitle: payload.content
});
}
});
//监听点击通知栏
plus.push.addEventListener('click', function({
payload
}) {
if (typeof payload != 'object') {
payload = JSON.parse(payload)
}
let pages = getCurrentPages();
let currentWebview = pages[pages.length - 1].$getAppWebview();
if (currentWebview.__uniapp_route != 'pages/msg-center/msg-center') {
uni.navigateTo({
url: '/pages/msg-center/msg-center'
})
}
uni.$emit('readMsg', payload)
});
//获取push参数
plus.push.getClientInfoAsync(
info => {
console.log('push.getClientInfoAsync', info);
this.setPushClient(info)
},
err => {
console.log('push.getClientInfoAsync', err);
}
);
// #endif
},
/**
* 存储设备信息 参考colorUI
* @param {Object}
*/
initSize(e) {
const systemInfo = e;
let navigationBarHeight;
let custom = {};
// #ifndef MP
custom = {
height: 36,
width: 88
};
navigationBarHeight = 44;
// #endif
// #ifdef MP-WEIXIN
custom = wx.getMenuButtonBoundingClientRect();
navigationBarHeight = custom.bottom + custom.top - e.statusBarHeight * 2;
// #endif
// #ifdef MP-TOUTIAO
custom = tt.getMenuButtonBoundingClientRect();
navigationBarHeight = custom.bottom + custom.top - e.statusBarHeight * 2;
// #endif
systemInfo.custom = custom;
systemInfo.navigationBarHeight = navigationBarHeight;
systemInfo.development = process.env.NODE_ENV == "development";
console.log(systemInfo);
Vue.prototype.systemInfo = systemInfo;
},
}
}
</script>
<style lang="scss">
/*每个页面公共css */
/* #ifndef APP-NVUE */
@import "common/css/font_2479288_9cqg4e7ml9.css";
@import "common/css/theme.scss";
@import "common/css/style.scss";
/* #endif */
</style>
1
https://gitee.com/zakailynn/tiantian-mall.git
git@gitee.com:zakailynn/tiantian-mall.git
zakailynn
tiantian-mall
天天商城
master

搜索帮助