| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- import Vue from 'vue'
- import App from './App'
- import directive from './common/directive.js'
- import utils from './common/utils.js'
- import config from './config.js'
- import onfire from './common/onfire.js'
- import {
- gotopage
- } from '@/common/gotopage.js'
- /* Vuex */
- import store from "./store/index.js"
- Vue.prototype.$store = store;
- // 公共组件
- import headerBar from './components/header.vue'
- Vue.component('header-bar', headerBar)
- // import Vconsole from 'vconsole'
- // const vConsole = new Vconsole()
- // Vue.use(vConsole)
- Vue.prototype.$fire = new onfire()
- Vue.config.productionTip = false
- App.mpType = 'app'
- Vue.prototype.config = config
- const app = new Vue({
- ...App
- })
- app.$mount()
- Vue.prototype.websiteUrl = config.app_url;
- Vue.prototype.app_id = config.app_id;
- //h5发布路径
- Vue.prototype.h5_addr = config.h5_addr;
- /*页面跳转*/
- Vue.prototype.gotoPage = gotopage;
- //#ifdef H5
- app.$router.afterEach((to, from) => {
- const u = navigator.userAgent.toLowerCase()
- if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') return
- if (to.path !== global.location.pathname) {
- location.assign(config.h5_addr + to.fullPath);
- }
- })
- //#endif
- //是否是ios
- Vue.prototype.ios = function() {
- const u = navigator.userAgent.toLowerCase();
- if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') {
- return false;
- }
- return true;
- };
- //get请求
- Vue.prototype._get = function(path, data, success, fail, complete) {
- data = data || {};
- data.token = uni.getStorageSync('token') || '';
- data.app_id = this.getAppId();
- uni.request({
- url: this.websiteUrl + '/index.php/api/' + path,
- data: data,
- dataType: 'json',
- method: 'GET',
- success: (res) => {
- if (res.statusCode !== 200 || typeof res.data !== 'object') {
- return false;
- }
- if (res.data.code === -2) {
- console.log('用户已删除');
- console.log(res.data.msg)
- this.showError(res.data.msg, function() {
- uni.removeStorageSync('token');
- this.gotoPage('/pages/index/index', 'reLaunch');
- })
- } else if (res.data.code === -1) {
- // 登录态失效, 重新登录
- console.log('登录态失效, 重新登录');
- this.doLogin();
- } else if (res.data.code === 0) {
- this.showError(res.data.msg, function() {
- fail && fail(res);
- });
- return false;
- } else {
- success && success(res.data);
- }
- },
- fail: (res) => {
- fail && fail(res);
- },
- complete: (res) => {
- uni.hideLoading();
- complete && complete(res);
- },
- });
- };
- //get请求
- Vue.prototype._post = function(path, data, success, fail, complete) {
- data = data || {};
- data.token = uni.getStorageSync('token') || '';
- data.app_id = this.getAppId();
- uni.request({
- url: this.websiteUrl + '/index.php/api/' + path,
- data: data,
- dataType: 'json',
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- },
- success: (res) => {
- if (res.statusCode !== 200 || typeof res.data !== 'object') {
- return false;
- }
- if (res.data.code === -1) {
- // 登录态失效, 重新登录
- console.log('登录态失效, 重新登录');
- this.doLogin();
- } else if (res.data.code === 0) {
- this.showError(res.data.msg, function() {
- fail && fail(res);
- });
- return false;
- } else {
- success && success(res.data);
- }
- },
- fail: (res) => {
- fail && fail(res);
- },
- complete: (res) => {
- uni.hideLoading();
- complete && complete(res);
- },
- });
- };
- Vue.prototype.doLogin = function() {
- let pages = getCurrentPages();
- if (pages.length) {
- let currentPage = pages[pages.length - 1];
- if ("pages/login/login" != currentPage.route &&
- "pages/login/weblogin" != currentPage.route &&
- "pages/login/openlogin" != currentPage.route) {
- uni.setStorageSync("currentPage", currentPage.route);
- uni.setStorageSync("currentPageOptions", currentPage.options);
- }
- }
- //公众号
- // #ifdef H5
- if (this.isWeixin()) {
- let invitation_id = uni.getStorageSync('invitation_id') ? uni.getStorageSync('invitation_id') : 0;
- window.location.href = this.websiteUrl + '/index.php/api/user.usermp/login?app_id=' + this.getAppId() +
- '&referee_id=' + uni.getStorageSync('referee_id') + '&invitation_id=' + invitation_id;
- } else {
- this.gotoPage("/pages/login/weblogin");
- }
- // #endif
- // #ifdef APP-PLUS
- this.gotoPage("/pages/login/openlogin");
- return;
- // #endif
- // 非公众号,跳转授权页面
- // #ifndef H5
- this.gotoPage("/pages/login/login");
- // #endif
- };
- /**
- * 显示失败提示框
- */
- Vue.prototype.showError = function(msg, callback) {
- uni.showModal({
- title: '友情提示',
- content: msg,
- showCancel: false,
- success: function(res) {
- callback && callback();
- }
- });
- };
- /**
- * 显示失败提示框
- */
- Vue.prototype.showSuccess = function(msg, callback) {
- uni.showModal({
- title: '友情提示',
- content: msg,
- showCancel: false,
- success: function(res) {
- callback && callback();
- }
- });
- };
- /**
- * 获取应用ID
- */
- Vue.prototype.getAppId = function() {
- return this.app_id || 10001;
- };
- Vue.prototype.compareVersion = function(v1, v2) {
- v1 = v1.split('.')
- v2 = v2.split('.')
- const len = Math.max(v1.length, v2.length)
- while (v1.length < len) {
- v1.push('0')
- }
- while (v2.length < len) {
- v2.push('0')
- }
- for (let i = 0; i < len; i++) {
- const num1 = parseInt(v1[i])
- const num2 = parseInt(v2[i])
- if (num1 > num2) {
- return 1
- } else if (num1 < num2) {
- return -1
- }
- }
- return 0
- };
- /**
- * 生成转发的url参数
- */
- Vue.prototype.getShareUrlParams = function(params) {
- let self = this;
- return utils.urlEncode(Object.assign({
- referee_id: self.getUserId(),
- app_id: self.getAppId()
- }, params));
- };
- /**
- * 当前用户id
- */
- Vue.prototype.getUserId = function() {
- return uni.getStorageSync('user_id');
- };
- //#ifdef H5
- var jweixin = require('jweixin-module');
- Vue.prototype.configWx = function(signPackage, shareParams, params) {
- if (signPackage == '') {
- return;
- }
- let self = this;
- jweixin.config(JSON.parse(signPackage));
- let url_params = self.getShareUrlParams(params);
- jweixin.ready(function(res) {
- jweixin.updateAppMessageShareData({
- title: shareParams.title,
- desc: shareParams.desc,
- link: self.websiteUrl + self.h5_addr + shareParams.link + '?' + url_params,
- imgUrl: shareParams.imgUrl,
- success: function() {
- }
- });
- jweixin.updateTimelineShareData({
- title: shareParams.title,
- desc: shareParams.desc,
- link: self.websiteUrl + self.h5_addr + shareParams.link + '?' + url_params,
- imgUrl: shareParams.imgUrl,
- success: function() {
- }
- });
- });
- };
- Vue.prototype.configWxScan = function(signPackage) {
- if (signPackage == '') {
- return;
- }
- jweixin.config(JSON.parse(signPackage));
- return jweixin;
- };
- //#endif
- /**
- * 获取当前平台
- */
- Vue.prototype.getPlatform = function(params) {
- let platform = 'wx';
- // #ifdef APP-PLUS
- platform = 'app';
- // #endif
- // #ifdef H5
- if (this.isWeixin()) {
- platform = 'mp';
- } else {
- platform = 'h5';
- }
- // #endif
- return platform;
- };
- /**
- * 订阅通知,目前仅小程序
- */
- Vue.prototype.subMessage = function(temlIds, callback) {
- let self = this;
- // #ifdef MP-WEIXIN
- //小程序订阅消息
- const version = wx.getSystemInfoSync().SDKVersion;
- if (temlIds && temlIds.length != 0 && self.compareVersion(version, '2.8.2') >= 0) {
- wx.requestSubscribeMessage({
- tmplIds: temlIds,
- success(res) {},
- fail(res) {},
- complete(res) {
- callback();
- },
- });
- } else {
- callback();
- }
- // #endif
- // #ifndef MP-WEIXIN
- callback();
- // #endif
- };
- Vue.prototype.isWeixin = function() {
- var ua = navigator.userAgent.toLowerCase();
- if (ua.match(/MicroMessenger/i) == "micromessenger") {
- return true;
- } else {
- return false;
- }
- };
- Vue.prototype.topBarTop = function() {
- // #ifdef MP-WEIXIN
- return uni.getMenuButtonBoundingClientRect().top;
- // #endif
- // #ifndef MP-WEIXIN
- const SystemInfo = uni.getSystemInfoSync();
- return SystemInfo.statusBarHeight;
- // #endif
- };
- Vue.prototype.topBarHeight = function() {
- // #ifdef MP-WEIXIN
- return uni.getMenuButtonBoundingClientRect().height;
- // #endif
- // #ifndef MP-WEIXIN
- return 0
- // #endif
- };
- Vue.prototype.yulan = function(e, i) {
- let image_path_arr = [];
- if (!Array.isArray(e)) {
- image_path_arr = [e];
- } else {
- if (e[0].file_path) {
- e.forEach((item, index) => {
- image_path_arr.push(item.file_path)
- })
- } else {
- image_path_arr = e
- }
- }
- let picnum = i * 1;
- uni.previewImage({
- urls: image_path_arr,
- current: picnum,
- });
- }
- Vue.prototype.theme = function() {
- return this.$store.state.theme
- }
|