From a3a17ffa0abe50484dcc1e2bef76fd4dddde33c4 Mon Sep 17 00:00:00 2001 From: Kaviilee Date: Sun, 7 Jan 2024 20:36:34 +0800 Subject: [PATCH] perf: update js file --- js/api_misc.js | 11 ++++++++-- js/axios.js | 58 +++++++++++++++++++++++++------------------------- js/https.js | 15 ++++++------- js/https_v2.js | 28 +++++++++++------------- js/idb.js | 28 ++++++++++-------------- js/reporter.js | 49 ++++++++++++++++-------------------------- js/request.js | 15 +++---------- js/server.js | 2 +- js/stat.js | 11 +++------- js/user.js | 28 +++++++++--------------- package.json | 2 +- 11 files changed, 104 insertions(+), 143 deletions(-) diff --git a/js/api_misc.js b/js/api_misc.js index 65b9fe7..a126561 100644 --- a/js/api_misc.js +++ b/js/api_misc.js @@ -1,4 +1,6 @@ -import { $cms } from './https' +import { + $cms +} from './https' // 面包屑 function getBreadcrumb(key) { @@ -34,4 +36,9 @@ function getMenus(params) { } -export { getBreadcrumb, getMenu, getArticle, getMenus }; +export { + getBreadcrumb, + getMenu, + getArticle, + getMenus +}; \ No newline at end of file diff --git a/js/axios.js b/js/axios.js index 87a5cf0..2a4db46 100644 --- a/js/axios.js +++ b/js/axios.js @@ -1,12 +1,12 @@ import axios from "axios"; import Vue from "vue"; -import { Message, Notification,MessageBox } from "element-ui"; +import { Message, Notification, MessageBox } from "element-ui"; Vue.prototype.$notify = Notification; Vue.prototype.$message = Message; Vue.prototype.$alert = MessageBox.alert; const POP = new Vue(); -function loadPop(msg,popType='message'){ - switch (popType){ +function loadPop(msg, popType = 'message') { + switch (popType) { case 'message': POP.$message.error(msg) break; @@ -19,96 +19,96 @@ function loadPop(msg,popType='message'){ POP.$notify.error({ title: '错误', message: msg - }); + }); break; - default : + default: break; } } -function PopNextworkError(err){ +function PopNextworkError(err) { console.log(err.response); return Promise.reject(err); } -function installInterceptors(target,options) { +function installInterceptors(target, options) { let popType = options && options.popType || 'message' target["interceptors"]["response"].use( function (response) { return response; }, function (err) { - if(!options || !options.mute){ + if (!options || !options.mute) { if (err.response && err.response.data) { - err.response.data.msg && loadPop(err.response.data.msg,popType); + err.response.data.msg && loadPop(err.response.data.msg, popType); } else { - loadPop('网络请求异常',popType); + loadPop('网络请求异常', popType); } } return PopNextworkError(err) } ); } -function installNextInterceptors(target,options) { +function installNextInterceptors(target, options) { let popType = options && options.popType || 'message' target["interceptors"]["response"].use( function (response) { if (response.data.code) { - if(!options || !options.mute){ - response.data.msg && loadPop(`[${response.data.code}]${response.data.msg}`,popType); + if (!options || !options.mute) { + response.data.msg && loadPop(`[${response.data.code}]${response.data.msg}`, popType); } return Promise.reject(response); } return response; }, function (err) { - if(!options || !options.mute){ - if(err.response && err.response.data && err.response.data.msg){ - loadPop(err.response.data.msg,popType); - }else{ - loadPop('接口异常',popType); + if (!options || !options.mute) { + if (err.response && err.response.data && err.response.data.msg) { + loadPop(err.response.data.msg, popType); + } else { + loadPop('接口异常', popType); } } return PopNextworkError(err) } ); } -function installCmsInterceptors(target,options) { +function installCmsInterceptors(target, options) { let popType = options && options.popType || 'message' target["interceptors"]["response"].use( function (response) { if (response.data.code) { - if(!options || !options.mute){ - response.data.msg && loadPop(`[${response.data.code}]${response.data.msg}`,popType); + if (!options || !options.mute) { + response.data.msg && loadPop(`[${response.data.code}]${response.data.msg}`, popType); } return Promise.reject(response); } return response; }, function (err) { - if(!options || !options.mute){ - loadPop(`[${err.response.data.statusCode}]${err.response.data.message}`,popType); + if (!options || !options.mute) { + loadPop(`[${err.response.data.statusCode}]${err.response.data.message}`, popType); } return PopNextworkError(err) } ); } -function installHelperInterceptors(target,options) { +function installHelperInterceptors(target, options) { let popType = options && options.popType || 'message' target["interceptors"]["response"].use( function (response) { if (response.data.code == 200 || !response.data.code) { return response; - }else{ - if(!options || !options.mute){ - loadPop(`[${response.data.code}]${response.data.message}`,popType); + } else { + if (!options || !options.mute) { + loadPop(`[${response.data.code}]${response.data.message}`, popType); } return Promise.reject(response); } }, function (err) { - if(!options || !options.mute){ - loadPop('网络请求异常',popType); + if (!options || !options.mute) { + loadPop('网络请求异常', popType); } return PopNextworkError(err) } diff --git a/js/https.js b/js/https.js index bc7a6d9..da156b1 100644 --- a/js/https.js +++ b/js/https.js @@ -14,7 +14,7 @@ const server_map = { // cms通用请求接口 function $cms(options) { - let domain = options && options.domain || __cms; + let domain = (options && options.domain) || __cms; let config = { // 同时发送cookie和basic auth withCredentials: true, @@ -38,7 +38,7 @@ function $cms(options) { // helper通用请求接口 import { jx3ClientType } from "./utils"; function $helper(options) { - let domain = options && options.domain || __helperUrl; + let domain = (options && options.domain) || __helperUrl; let config = { // 同时发送cookie和basic auth withCredentials: true, @@ -74,7 +74,7 @@ function $helper(options) { // next通用请求接口 function $next(options) { - let domain = options && options.domain || __next; + let domain = (options && options.domain) || __next; let config = { // 同时发送cookie和basic auth withCredentials: true, @@ -96,23 +96,23 @@ function $next(options) { } function $team(options) { - let _options = options && Object.assign(options, { domain: __team }) || { domain: __team }; + let _options = (options && Object.assign(options, { domain: __team })) || { domain: __team }; return $next(_options); } function $pay(options) { - let _options = options && Object.assign(options, { domain: __pay }) || { domain: __pay }; + let _options = (options && Object.assign(options, { domain: __pay })) || { domain: __pay }; return $next(_options); } function $lua(options) { - let _options = options && Object.assign(options, { domain: __lua }) || { domain: __lua }; + let _options = (options && Object.assign(options, { domain: __lua })) || { domain: __lua }; return $next(_options); } // node function $node(options) { - let domain = options && options.domain || __node; + let domain = (options && options.domain) || __node; let config = { // 同时发送cookie和basic auth withCredentials: true, @@ -137,7 +137,6 @@ function $node(options) { return ins; } - import { tokenExpires } from "../data/conf.json"; function isLogin() { let created_at = !localStorage.getItem("created_at") ? -Infinity : localStorage.getItem("created_at"); diff --git a/js/https_v2.js b/js/https_v2.js index fa1cee1..75bc106 100644 --- a/js/https_v2.js +++ b/js/https_v2.js @@ -43,8 +43,7 @@ function $helper(options) { baseURL: domain, headers: { Accept: "application/prs.helper.v2+json", - "JX3-Client-Type": - (options && options.client_id) || jx3ClientType(), + "JX3-Client-Type": (options && options.client_id) || jx3ClientType(), }, }; @@ -127,10 +126,7 @@ function $node(options) { // 是否需要开启本地代理作为测试 if (options && options.proxy) { - config.baseURL = - process.env.NODE_ENV === "production" - ? domain - : `http://localhost:${options.port}`; + config.baseURL = process.env.NODE_ENV === "production" ? domain : `http://localhost:${options.port}`; } // 创建实例 @@ -193,7 +189,7 @@ function throwError(err) { return Promise.reject(err); } -function loadDefaultRequestErrorPop(err, popType = 'message'){ +function loadDefaultRequestErrorPop(err, popType = "message") { loadPop(`[${err.response.status}]${err.response.statusText}`, popType); } @@ -204,7 +200,7 @@ function loadDefaultRequestErrorPop(err, popType = 'message'){ * @param {*} options */ function installInterceptors(target, options) { - let popType = options && options.popType || 'message' + let popType = (options && options.popType) || "message"; target["interceptors"]["response"].use( function (response) { return response; @@ -214,7 +210,7 @@ function installInterceptors(target, options) { if (err.response && err.response.data) { err.response.data.msg && loadPop(err.response.data.msg, popType); } else { - loadDefaultRequestErrorPop(err) + loadDefaultRequestErrorPop(err); } } return throwError(err); @@ -229,7 +225,7 @@ function installInterceptors(target, options) { * @param {*} options */ function installStandardInterceptors(target, options) { - let popType = options && options.popType || 'message' + let popType = (options && options.popType) || "message"; target["interceptors"]["response"].use( function (response) { if (response.data.code) { @@ -242,14 +238,14 @@ function installStandardInterceptors(target, options) { }, function (err) { if (!options || !options.mute) { - console.log(err.response) + console.log(err.response); if (err.response && err.response.data && err.response.data.msg) { loadPop(err.response.data.msg, popType); } else { - loadDefaultRequestErrorPop(err) + loadDefaultRequestErrorPop(err); } } - return throwError(err) + return throwError(err); } ); } @@ -261,7 +257,7 @@ function installStandardInterceptors(target, options) { * @param {*} options */ function installHelperInterceptors(target, options) { - let popType = options && options.popType || 'message' + let popType = (options && options.popType) || "message"; target["interceptors"]["response"].use( function (response) { if (response.data.code == 200 || !response.data.code) { @@ -275,9 +271,9 @@ function installHelperInterceptors(target, options) { }, function (err) { if (!options || !options.mute) { - loadDefaultRequestErrorPop(err) + loadDefaultRequestErrorPop(err); } - return throwError(err) + return throwError(err); } ); } diff --git a/js/idb.js b/js/idb.js index 3a97b62..611f207 100644 --- a/js/idb.js +++ b/js/idb.js @@ -1,33 +1,27 @@ const localforage = require("localforage"); class IDB { - // 初始化数据库 - constructor(dbName,storeName) { - - if(!window.indexedDB){ - console.log('不支持IndexedDB') - return + constructor(dbName, storeName) { + if (!window.indexedDB) { + console.log("不支持IndexedDB"); + return; } this.dbName = dbName || "jx3box"; - this.storeName = storeName || "posts" - console.log( - '%c[IDB] Use Database - ', - 'color:#ff3399;', - `${this.dbName} > ${this.storeName}`, - ) + this.storeName = storeName || "posts"; + console.log("%c[IDB] Use Database - ", "color:#ff3399;", `${this.dbName} > ${this.storeName}`); this.db = localforage.createInstance({ name: this.dbName, - storeName : this.storeName, - driver : localforage.INDEXEDDB + storeName: this.storeName, + driver: localforage.INDEXEDDB, }); - return this.db + return this.db; } } -export default IDB +export default IDB; // const DB = new IDB(); // DB.ready().then(() => { // DB.setItem('key','val') -// }) \ No newline at end of file +// }) diff --git a/js/reporter.js b/js/reporter.js index fe4f7d8..bc7f33b 100644 --- a/js/reporter.js +++ b/js/reporter.js @@ -1,15 +1,13 @@ -import { - Reporter -} from "@jx3box/reporter" -import { __Domain } from "../data/jx3box.json" +import { Reporter } from "@jx3box/reporter"; +import { __Domain } from "../data/jx3box.json"; import { v4 as uuidv4 } from "uuid"; -import User from "./user" +import User from "./user"; /** - * 16进制转int - * @param {string} str - * @returns - */ + * 16进制转int + * @param {string} str + * @returns + */ function int162hex(str) { let hex = str.toString(16); return hex.length == 1 ? "0" + hex : hex; @@ -70,7 +68,7 @@ export function reportNow(binding) { userId: User.getInfo().uid, use_query: use_query, }); - R.p({ uuid: getUUID(), cookies: getCookies(), ...data }) + R.p({ uuid: getUUID(), cookies: getCookies(), ...data }); } /** @@ -86,7 +84,6 @@ export function reportNow(binding) { * data: 上报数据 */ const reporter = { - /** * 上报指令 vue2 * @param {boolean} use_query 是否上报当前页面中url中的参数 默认false @@ -96,19 +93,15 @@ const reporter = { install(Vue) { Vue.directive("reporter", { bind: function (el, binding) { - const { - use_query = false, - caller, - data - } = binding.value; + const { use_query = false, caller, data } = binding.value; const R = new Reporter({ caller, use_query, // 上报当前页面中url中的参数 默认false - userId: User.getInfo().uid // 当前登录用户id + userId: User.getInfo().uid, // 当前登录用户id }); el.clickHandler = function () { - R.p({ uuid: getUUID(), cookies: getCookies(), ...data }) + R.p({ uuid: getUUID(), cookies: getCookies(), ...data }); }; el.addEventListener("click", el.clickHandler); @@ -116,7 +109,7 @@ const reporter = { unbind: function (el) { el.removeEventListener("click", el.clickHandler); el.clickHandler = null; - } + }, }); }, @@ -130,21 +123,15 @@ const reporter = { installVue3(app) { app.directive("reporter", { mounted(el, binding) { - const { - user_id, - use_query = false, - caller, - data - } = binding.value; + const { user_id, use_query = false, caller, data } = binding.value; const R = new Reporter({ caller, use_query, // 上报当前页面中url中的参数 默认false - userId: user_id // 当前登录用户id + userId: user_id, // 当前登录用户id }); el.clickHandler = function () { - - R.p({ uuid: getUUID(), ...data }) + R.p({ uuid: getUUID(), ...data }); }; el.addEventListener("click", el.clickHandler); @@ -152,9 +139,9 @@ const reporter = { unmounted(el) { el.removeEventListener("click", el.clickHandler); el.clickHandler = null; - } + }, }); - } -} + }, +}; export default reporter; diff --git a/js/request.js b/js/request.js index b019799..66315d2 100644 --- a/js/request.js +++ b/js/request.js @@ -2,12 +2,7 @@ import axios from "axios"; // 拦截器 -import { - installInterceptors, - installNextInterceptors, - installHelperInterceptors, - installCmsInterceptors, -} from "./interceptors.js"; +import { installInterceptors, installNextInterceptors, installHelperInterceptors, installCmsInterceptors } from "./interceptors.js"; // 域名映射 import domains from "../data/jx3box.json"; @@ -50,8 +45,7 @@ function $helper(options) { baseURL: domain, headers: { Accept: "application/prs.helper.v2+json", - "JX3-Client-Type": - (options && options.client_id) || jx3ClientType(), + "JX3-Client-Type": (options && options.client_id) || jx3ClientType(), }, }; @@ -134,10 +128,7 @@ function $node(options) { // 是否需要开启本地代理作为测试 if (options && options.proxy) { - config.baseURL = - process.env.NODE_ENV === "production" - ? domain - : `http://localhost:${options.port}`; + config.baseURL = process.env.NODE_ENV === "production" ? domain : `http://localhost:${options.port}`; } // 创建实例 diff --git a/js/server.js b/js/server.js index 27508e7..a15b815 100644 --- a/js/server.js +++ b/js/server.js @@ -17,7 +17,7 @@ function getConfig(key) { } else { return _config; } - }) + }); } function getArticle(id) { diff --git a/js/stat.js b/js/stat.js index b8195c4..4d17d15 100644 --- a/js/stat.js +++ b/js/stat.js @@ -1,7 +1,7 @@ import axios from "axios"; import JX3BOX from "../data/jx3box.json"; -const { __postType,__next } = JX3BOX; +const { __postType, __next } = JX3BOX; const cms_types = Object.keys(__postType); const $next = axios.create({ baseURL: process.env.NODE_ENV === "production" ? __next : "/", @@ -9,9 +9,7 @@ const $next = axios.create({ // 发送统计:同一个ip的同一篇文章的同一个动作10分钟内不重复统计 function postStat(type, id, action = "views") { - let api = cms_types.includes(type) - ? "/api/summary-any/" + id - : "/api/summary-any/" + type + "-" + id; + let api = cms_types.includes(type) ? "/api/summary-any/" + id : "/api/summary-any/" + type + "-" + id; return $next.get(api, { params: { type: type, @@ -22,13 +20,10 @@ function postStat(type, id, action = "views") { // 30秒缓存 function getStat(type, id) { - let api = cms_types.includes(type) - ? "/api/summary-any/" + id + "/stat" - : "/api/summary-any/" + type + "-" + id + "/stat"; + let api = cms_types.includes(type) ? "/api/summary-any/" + id + "/stat" : "/api/summary-any/" + type + "-" + id + "/stat"; return $next.get(api); } - // 获取统计 function getStatRank(type, action = "views", limit = 10, sort = "7days") { return $next.get("/api/summary/visit/rank", { diff --git a/js/user.js b/js/user.js index bd72eec..06a630e 100644 --- a/js/user.js +++ b/js/user.js @@ -32,17 +32,12 @@ class User { check() { if (this.isLogin()) { this.profile.uid = localStorage && localStorage.getItem("uid"); - this.profile.group = - (localStorage && localStorage.getItem("group")) || 1; + this.profile.group = (localStorage && localStorage.getItem("group")) || 1; this.profile.token = localStorage && localStorage.getItem("token"); this.profile.name = localStorage && localStorage.getItem("name"); - this.profile.status = - localStorage && localStorage.getItem("status"); - this.profile.bind_wx = - localStorage && localStorage.getItem("bind_wx"); - this.profile.avatar_origin = - (localStorage && localStorage.getItem("avatar")) || - default_avatar; + this.profile.status = localStorage && localStorage.getItem("status"); + this.profile.bind_wx = localStorage && localStorage.getItem("bind_wx"); + this.profile.avatar_origin = (localStorage && localStorage.getItem("avatar")) || default_avatar; this.profile.avatar = showAvatar(this.profile.avatar_origin, "s"); } else { this.profile = this.anonymous; @@ -57,11 +52,8 @@ class User { // 判断是否已登录 isLogin() { - this.created_at = !localStorage.getItem("created_at") - ? -Infinity - : localStorage.getItem("created_at"); - this.logged_in = - localStorage.getItem("logged_in") == "true" ? true : false; + this.created_at = !localStorage.getItem("created_at") ? -Infinity : localStorage.getItem("created_at"); + this.logged_in = localStorage.getItem("logged_in") == "true" ? true : false; return this.logged_in && Date.now() - this.created_at < this.expires; } @@ -167,7 +159,7 @@ class User { if (this.isLogin()) { return $cms() .get("/api/cms/user/is_super_author/" + this.getInfo().uid) - .then(res => { + .then((res) => { return res.data.data; }); } else { @@ -210,7 +202,7 @@ class User { resolve(this._isPRO(this.asset) || this._isVIP(this.asset)); }); } else { - return this.getAsset().then(asset => { + return this.getAsset().then((asset) => { return this._isPRO(asset) || this._isVIP(asset); }); } @@ -223,7 +215,7 @@ class User { resolve(this._isPRO(this.asset)); }); } else { - return this.getAsset().then(asset => { + return this.getAsset().then((asset) => { return this._isPRO(asset); }); } @@ -253,7 +245,7 @@ class User { } else { return $pay() .get("/api/vip/i") - .then(res => { + .then((res) => { let asset = res.data.data; this.asset = asset; return asset; diff --git a/package.json b/package.json index ee189f4..9f03419 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jx3box/j3box-common", - "version": "8.1.7", + "version": "8.1.8", "description": "JX3BOX公共基础模块", "main": "main.js", "repository": {