123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <script>
- import { postJSON as $postJSON, get as $get } from "./config/util";
- import { api as $api } from "./config/api";
- import appInfo from "./config/appInfo";
-
- const router = require("./utils/router");
-
- export default {
- globalData: {
- isIOS: false, // 判断是否是ios
- isIphoneX: false, // 是否为 iphoneX 及以上机型
- statusBarHeight: 44, // 状态栏高度
- navBarHeight: 44, // 导航栏高度
- pageContentTop: 88, // 页面默认内边距
- $router: router,
- $appInfo: appInfo, // 小程序静态配置信息
- testResult: '', // 测试结果
- },
- onLaunch: function (options) {
- console.log("App onLaunch options => ", options);
- // 判断当前设备是否为 iphoneX 及以上机型
- this.handleGetIsIphoneX();
- // 判断当前小程序是否为最新版本
- this.handleVersionUpdate();
- // 登录
- // this.handleLogin();
- },
- onShow: function (options) {
- console.log("App onShow options => ", options);
- },
- onHide: function (options) {
- console.log("App onHide options => ", options);
- },
- methods: {
- // 判断当前设备是否为 iphoneX 及以上机型
- handleGetIsIphoneX() {
- const systemInfo = uni.getSystemInfoSync();
- console.log("systemInfo => ", systemInfo);
- const _app = this.$scope || getApp(); // 兼容方式
- const menuButton = uni.getMenuButtonBoundingClientRect();
- // 导航栏高度
- _app.globalData.navBarHeight =
- menuButton.height + (menuButton.top - systemInfo.statusBarHeight) * 2;
- // 状态栏高度
- _app.globalData.statusBarHeight = systemInfo.statusBarHeight;
- // 页面内容内边距
- _app.globalData.pageContentTop =
- _app.globalData.navBarHeight + _app.globalData.statusBarHeight;
-
- if (systemInfo.system.toUpperCase().includes("IOS")) {
- _app.globalData.isIOS = true; // 判断是否是ios
- if (systemInfo.safeArea.top > 20) {
- _app.globalData.isIphoneX = true;
- }
- }
- },
- // 判断当前小程序是否为最新版本
- handleVersionUpdate() {
- const that = this;
- // 判断应用的 getUpdateManager 是否在当前版本可用
- if (uni.canIUse("getUpdateManager")) {
- const updateManager = uni.getUpdateManager();
- // 向小程序后台请求完新版本信息
- updateManager.onCheckForUpdate(function (res) {
- if (res.hasUpdate) {
- // 小程序有新版本,静默下载新版本,新版本下载完成
- updateManager.onUpdateReady(function () {
- uni.showModal({
- title: "更新提示",
- content: "小程序已发布新版本,是否重启应用?",
- success: function (res) {
- if (res.confirm) {
- updateManager.applyUpdate();
- } else if (res.cancel) {
- // 强制用户更新,弹出第二次弹窗
- uni.showModal({
- title: "提示",
- content: "小程序已发布新版本,是否重启应用",
- showCancel: false, // 隐藏取消按钮
- success: function (res) {
- // 第二次提示后,强制更新
- if (res.confirm) {
- // 当新版本下载完成,调用该方法会强制当前小程序应用上新版本并重启
- updateManager.applyUpdate();
- } else if (res.cancel) {
- // 重新回到版本更新提示
- that.handleVersionUpdate();
- }
- },
- });
- }
- },
- });
- });
- // 当新版本下载失败
- updateManager.onUpdateFailed(function () {
- uni.showModal({
- title: "提示",
- content: "小程序版本更新失败,请您删除当前小程序并重新打开",
- });
- });
- }
- });
- } else {
- // 提示用户在最新版本的客户端上体验
- uni.showModal({
- title: "温馨提示",
- content:
- "当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。",
- });
- }
- },
- // 显示获取用户信息确认弹框
- handleShowModal(content) {
- return new Promise((resolve) => {
- uni.showModal({
- title: "提示",
- content: content || "确定执行当前操作吗?",
- success: async (res) => {
- if (res.confirm) {
- resolve("确认");
- }
- },
- });
- });
- },
- // 设置导航栏标题
- handleSetNavTitle(title) {
- uni.setNavigationBarTitle({
- title: title || appInfo.appName,
- });
- },
- // 执行登录
- handleLogin() {
- return new Promise(async (resolve, reject) => {
- try {
- const res = await this.handleGetLoginInfo();
- // 将当前用户登录信息存储至本地(供后续手机号登录及业务使用)
- uni.setStorageSync("openid", res.openid || "");
- // uni.setStorageSync("user_id", res.user_id);
- // uni.setStorageSync("nickname", res.nickname);
- resolve(res);
- } catch (error) {
- reject(error);
- }
- });
- },
- // 获取用户微信登录信息
- handleGetLoginInfo() {
- return new Promise((resolve, reject) => {
- uni.login({
- success: async (res) => {
- const code = res.code;
- try {
- const { data: res = {} } = await $postJSON(
- $api.login_loginCode,
- {
- code,
- },
- {},
- false
- );
- console.log("res => ", res);
- if (res && res.errno == 0) {
- resolve(res.rst);
- } else {
- uni.showToast({ title: res.err, icon: "none" });
- reject(res.err);
- }
- } catch (error) {
- reject(error);
- console.log("error => ", error);
- }
- },
- });
- });
- },
- // 清空用户登录信息缓存
- handleClearUserInfo() {
- const USER_INFO_KEYS = [
- "token",
- "user_id",
- "userInfo",
- "nickname",
- "sex",
- "default_invite_code",
- "openid",
- "session_key",
- "unionid",
- ];
- USER_INFO_KEYS.forEach((key) => {
- uni.removeStorageSync(key);
- });
- },
- },
- };
- </script>
-
- <style lang="scss">
- @import "uview-ui/index.scss";
- @import "./static/style/commom.scss";
- /*每个页面公共css */
-
- // 去除u-navbar自定义导航栏下方分割线/底部下划线
- .u-border-bottom:after {
- border: none !important;
- }
- //统一背景颜色
- page {
- background: #f5f5f5;
- }
- button::after {
- border: none;
- }
- button {
- position: relative;
- display: block;
- margin-left: auto;
- margin-right: auto;
- padding-left: 0px;
- padding-right: 0px;
- box-sizing: border-box;
- text-align: center;
- text-decoration: none;
- -webkit-tap-highlight-color: transparent;
- overflow: hidden;
- color: #000;
- background-color: #fff;
- }
- </style>
|