123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <script>
- import {
- postJSON as $postJSON,
- get as $get,
- getUuid as $getUuid,
- } from "./config/util";
- import { api as $api } from "./config/api";
- const router = require("./utils/router");
-
- export default {
- globalData: {
- isIOS: false, // 判断是否是ios
- isIphoneX: false, // 是否为 iphoneX 及以上机型
- statusBarHeight: 44, // 状态栏高度
- navigationBarHeight: 44, // 导航栏高度 (自定义导航-导航烂高度默认44)
- SDKVersion: "", // 当前小程序基础库版本号
- WXVersion: "", // 当前小程序所在微信的版本号
- navbarBackground: {
- // 顶部导航背景图
- backgroundImage:
- "linear-gradient(45deg, rgb(244, 117, 42), rgb(239, 15, 42))",
- },
- newNavbarBackground: {
- background: `url(https://ld.726p.com/tbk/upload/c579f015a49a3f9e066f0ab00ab56f7.png) no-repeat`,
- backgroundSize: "cover",
- },
- $router: router,
- fun_version: "", //过审接口版本号
- },
- onLaunch: function (options) {
- console.log("App onLaunch options => ", options);
- // 判断当前设备是否为 iphoneX 及以上机型
- this.handleGetIsIphoneX();
- // 判断当前小程序是否为最新版本
- this.handleVersionUpdate();
- // 获取当前小程序基础库版本号
- this.handleGetVersion();
- // 小程序审核状态查询
- this.getVerifiedFun();
- // 微信登录
- this.handleWxLogin();
- // 设置用户首次启动小程序标识(用于在首页展示开屏广告)
- this.handleSetUserIsFirst(true);
- },
- onShow: function (options) {
- // 进来options参数处理
- this.onShowEntry(options);
- // 清除微信客服标识
- this.handleRemoveWxServiceFlag();
- },
- onHide: function () {
- const wxServiceDialogFlag = uni.getStorageSync("wxServiceDialogFlag");
- if (!wxServiceDialogFlag) {
- // 获取“微信客服”弹框标识,如果没触发过“微信客服”弹框,再初始化剪贴板(弹出猜你喜欢弹框)
- uni.setStorageSync("clipboard", ""); // 通过clipboard的内容来判断是否已经过弹出剪切板内容,弹出过的clipboard为no,否则clipboard为空
- }
- },
- methods: {
- // 初次进来
- onShowEntry(options) {
- if (options.path != "pages/subPackages/home/shareDetail") {
- // 从好友邀请过来,分享人的邀请码
- if (options.query && options.query.invite_code) {
- uni.setStorageSync("share_invite_code", options.query.invite_code);
- }
- }
- },
- // 判断当前设备是否为 iphoneX 及以上机型
- handleGetIsIphoneX() {
- const systemInfo = uni.getSystemInfoSync();
- const _app = this.$scope || getApp(); // 兼容方式
- _app.globalData.statusBarHeight = systemInfo.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:
- "当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。",
- });
- }
- },
- // 获取当前小程序基础库版本号
- handleGetVersion() {
- const systemInfo = uni.getSystemInfoSync();
- console.log("systemInfo => ", systemInfo);
- const _app = this.$scope || getApp(); // 兼容方式
- _app.globalData.SDKVersion = systemInfo.SDKVersion;
- _app.globalData.WXVersion = systemInfo.version;
- },
- // 显示获取用户信息确认弹框
- handleShowModal(content) {
- return new Promise((resolve) => {
- uni.showModal({
- title: "提示",
- content: content || "确定执行当前操作吗?",
- success: async (res) => {
- if (res.confirm) {
- resolve("确认");
- }
- },
- });
- });
- },
- // 根据token => 获取用户信息 => 更新至本地存储
- handleGetUserInfo(loadingFlag) {
- return new Promise(async (resolve, reject) => {
- try {
- const url = $api.login_userInfo;
- const params = {};
- const headers = {
- token: uni.getStorageSync("token") || "",
- };
- if (!loadingFlag) {
- // 我的页面刷新信息的时候,不需要loading
- uni.showLoading({ title: "加载中...", mask: true });
- }
- const { data: res } = await $postJSON(url, params, headers);
- if (res && res.errno == 0) {
- uni.setStorageSync("userInfo", { ...res.rst });
- resolve({ ...res.rst });
- } else {
- uni.showToast({ title: res.err || "操作失败", icon: "none" });
- reject(res.err);
- }
- } catch (error) {
- console.log("error => ", error);
- reject(error);
- } finally {
- uni.hideLoading();
- }
- });
- },
- // 获取渠道版本信息 - 接口header中使用
- channelGetVersion() {
- return new Promise(async (resolve, reject) => {
- try {
- const url = $api.channel_getVersion;
- const params = {
- channel_name: "猎豆优选小程序",
- };
- const { data: res } = await $get(url, params);
- if (res && res.errno == 0) {
- const { source, version } = res.rst || {};
- uni.setStorageSync("channel_source", source || "");
- uni.setStorageSync("channel_version", version || "");
- resolve(res.rst);
- } else {
- reject(res.err);
- }
- } catch (error) {
- reject(error);
- }
- });
- },
- // 小程序审核状态接口
- async getVerifiedFun() {
- try {
- const url = $api.wx_verified;
- const params = {
- version: "v1.0",
- };
- const { data: res } = await $get(url, params);
- if (res && res.errno == 0) {
- const _app = this.$scope || getApp(); // 兼容方式
- _app.globalData.fun_version = res.rst.version;
- } else {
- _app.globalData.fun_version = "";
- }
- } catch (error) {
- } finally {
- uni.hideLoading();
- }
- },
- // 用户打开详情页行为数据上报categoryNew_countBuy
- async categoryNew_countBuy({ shop_type, type, path, category_id }) {
- let user_label = "";
- // #ifdef H5
- // h5生成随机数 为个统计游客数
- user_label = uni.getStorageSync("uuid_user_label") || "";
- if (!user_label || user_label == "") {
- user_label = $getUuid();
- uni.setStorageSync("uuid_user_label", user_label);
- }
- // #endif
- try {
- const url = $api.categoryNew_countBuy;
- const params = {
- shop_type,
- type,
- path,
- category_id,
- user_label: user_label,
- };
- const { data: res } = await $postJSON(url, params);
- if (res && res.errno == 0) {
- } else {
- }
- } catch (error) {
- } finally {
- }
- },
- // 获取当前用户等级
- handleGetUserLevel() {
- const userInfo = uni.getStorageSync("userInfo") || {};
- return userInfo.user_level || "";
- },
- // 清空用户登录信息缓存
- 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);
- });
- },
- // 执行微信登录
- async handleWxLogin() {
- const isUnionid = this.handleGetUnionid();
- if (!isUnionid) {
- // 没有unionid => 从后台获取
- const res = await this.handleGetWxLoginInfo();
- // 将当前用户微信登录信息存储至本地(供后续手机号登录及业务使用)
- // 新用户(未绑定过微信的用户)
- uni.setStorageSync("openid", res.openid || "");
- uni.setStorageSync("session_key", res.session_key || "");
- uni.setStorageSync("unionid", res.unionid || "");
-
- if (res.token) {
- // 老用户(当前微信绑定过手机号) => 根据token获取用户信息
- uni.setStorageSync("token", res.token);
- uni.setStorageSync("user_id", res.user_id);
- uni.setStorageSync("sex", res.sex);
- uni.setStorageSync("nickname", res.nickname);
- uni.setStorageSync("name", res.name);
- this.handleGetUserInfo(); // 获取用户信息
- }
- }
- },
- // 判断本地是否有微信唯一id unionid
- handleGetUnionid() {
- return uni.getStorageSync("unionid") || "";
- },
- // 获取用户微信登录信息
- handleGetWxLoginInfo() {
- return new Promise((resolve, reject) => {
- uni.login({
- success: async (res) => {
- const code = res.code;
- try {
- const { data: res = {} } = await $get($api.login_getUserInfo, {
- code,
- });
- if (res && res.errno == 0) {
- resolve(res.rst.user_info);
- } else {
- uni.showToast({ title: res.err, icon: "none" });
- reject(res.err);
- }
- } catch (error) {
- reject(error);
- console.log("error => ", error);
- }
- },
- });
- });
- },
-
- // 设置用户首次启动标识(用于在首页展示开屏广告)
- handleSetUserIsFirst(isFirst) {
- uni.setStorageSync("isFirstLaunch", isFirst || false);
- },
- // 获取用户首次启动标识
- handleGetUserIsFirst() {
- return uni.getStorageSync("isFirstLaunch") || false;
- },
- handleRemoveWxServiceFlag() {
- uni.removeStorageSync("wxServiceDialogFlag");
- },
- },
- };
- </script>
-
- <style lang="scss">
- @import "uview-ui/index.scss";
- /*每个页面公共css */
-
- // 去除u-navbar自定义导航栏下方分割线/底部下划线
- .u-border-bottom:after {
- border: none !important;
- }
- //统一背景颜色
- page {
- background: #f5f5f5;
- }
- // 无数据展示
- .no-data-blcok {
- text-align: center;
- height: 100%;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-direction: column;
- flex: 1;
- .no-data-img {
- width: 160rpx;
- height: 160rpx;
- }
- .no-data-hint {
- color: #999999;
- font-size: 26rpx;
- line-height: 50rpx;
- margin-top: 40rpx;
- }
- }
- </style>
|