123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- const jwt = require('jsonwebtoken');
- const sdk = require('@baiducloud/sdk');
- const config = require('../config');
- const { Keyv } = require('keyv');
- const keyv = new Keyv();
- const request = require('./request');
- module.exports = {
-
- success(ctx, data = '', code = 0) {
- ctx.body = {
- code,
- data,
- message: '操作成功',
- };
- },
-
- fail(ctx, message = '', code = -1, data = '') {
- ctx.body = {
- code,
- data,
- message,
- };
- },
-
- isNotEmpty(val) {
- if (val === undefined || val == null || val === '') {
- return false;
- } else if (typeof val === 'string') {
- if (val.trim() === '') {
- return false;
- } else {
- return true;
- }
- } else {
- return true;
- }
- },
-
- isNumber(val) {
- const isTrue = this.isNotEmpty(val);
- return isTrue && (typeof val === 'number' || isNaN(val) === false);
- },
-
- checkEnv(env) {
- if (['stg', 'pre', 'prd'].includes(env)) {
- return true;
- }
- return false;
- },
-
- getCookie(ctx, name) {
- return ctx.cookies.get(name);
- },
-
- createToken(payload) {
- return jwt.sign(payload, config.JWT_SECRET, {
- expiresIn: config.JWT_EXPIRES_IN,
- });
- },
-
- decodeToken(ctx) {
- try {
- let [, authorization] = (ctx.request.headers?.authorization || '').split(' ');
- if (!authorization) {
- return ctx.throw(400, '账号信息异常,请重新登录');
- }
- return jwt.verify(authorization, config.JWT_SECRET);
- } catch (error) {
- return ctx.throw(401, '账号信息异常,请重新登录');
- }
- },
-
- decodeResetToken(token) {
- return jwt.verify(token, config.JWT_SECRET);
- },
-
- async uploadString(userId, fileName, content) {
- let client = null;
- if (config.OSS_TYPE === 'minio') {
-
- client = new Minio.Client({
- endPoint: config.OSS_ENDPOINT,
- port: config.OSS_PORT,
- useSSL: config.OSS_USESSL,
- accessKey: config.OSS_ACCESSKEY,
- secretKey: config.OSS_ACCESSKEYSECRET,
- });
- } else if (config.OSS_TYPE === 'baidu') {
- const ossConfig = {
- endpoint: config.OSS_ENDPOINT,
- credentials: {
- ak: config.OSS_ACCESSKEY,
- sk: config.OSS_ACCESSKEYSECRET,
- },
- };
- client = new sdk.BosClient(ossConfig);
- } else {
- client = new OSS({
-
- region: config.OSS_REGION,
-
- accessKeyId: config.OSS_ACCESSKEY,
- accessKeySecret: config.OSS_ACCESSKEYSECRET,
- authorizationV4: true,
-
- bucket: config.OSS_BUCKET,
- });
- }
- let key = `libs/${userId}/${fileName}`;
- let ContentType = '';
- if (fileName.endsWith('.js')) {
- ContentType = 'application/javascript; charset=utf-8';
- } else if (fileName.endsWith('.html')) {
- ContentType = 'text/html; charset=utf-8';
- } else if (fileName.endsWith('.json')) {
- ContentType = 'application/json; charset=utf-8';
- } else if (fileName.endsWith('.css')) {
- ContentType = 'text/css; charset=utf-8';
- }
-
- if (config.OSS_TYPE === 'aliyun') {
- await client.put(key, Buffer.from(content, 'utf8'));
- } else {
- await client.putObject(config.OSS_BUCKET, key, Buffer.from(content, 'utf8'), {
- 'Content-Type': ContentType,
- 'Cache-Control': 'public, max-age=31536000',
- 'x-bce-acl': 'public-read',
- });
- }
- },
-
- getClientIp(ctx) {
- let ip = ctx.request.headers['x-forwarded-for'] || ctx.request.headers['x-real-ip'] || ctx.request.headers['x-cluster-client-ip'];
-
- if (!ip) {
- ip = ctx.request.ip;
- }
-
- if (ip && ip.includes(',')) {
- ip = ip.split(',')[0];
- }
- return ip;
- },
- };
|