Commit 76072b19 authored by 任国军's avatar 任国军

add uploadImage

parent 319a7d4e
Pipeline #21801 passed with stage
in 13 seconds
......@@ -7,5 +7,24 @@ class CommonController extends Controller {
const ret = await service.common.oss.get_business_signature();
ctx.success(ret);
}
async uploadOss() {
const { ctx, service } = this;
const input_params = ctx.request.body;
if (typeof (input_params.image) === 'undefined') {
ctx.failed('image error');
}
if (typeof (input_params.height) === 'undefined') {
ctx.failed('height error');
}
if (typeof (input_params.width) === 'undefined') {
ctx.failed('width error');
}
const result = await service.common.oss.uploadOss(input_params);
const ret = { ret: result };
ctx.success(ret);
}
}
module.exports = CommonController;
......@@ -9,7 +9,7 @@ module.exports = {
const { ctx } = this;
const bearerToken = ctx.request.header.authorization;
if (!bearerToken) {
this.throw(422, 'error auth');
ctx.failed('error auth');
}
return bearerToken && bearerToken.replace('Bearer ', '');
},
......@@ -20,7 +20,7 @@ module.exports = {
const decode_res = await ctx.service.jwt.decode_token(token);
const token_black = await this.app.memcache.get('auth_token_' + decode_res.data.user_uuid);
if (token_black == token) {
this.throw(422, 'jwt校验失败');
ctx.failed('jwt校验失败');
}
ctx.setUserUuid(decode_res.data.user_uuid);
ctx.setOpenId(decode_res.data.openid);
......
......@@ -4,5 +4,7 @@ module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/common');
router.get('third_oss_signature', '/oss/signature', 'common.oss.signature');// 业务的 oss 签名
router.post('third', '/oss/image', app.middleware.verifyAuth(), 'common.oss.uploadOss');// 上传图片
router.get('third_test', '/test', 'common.test.test');
};
......@@ -6,56 +6,57 @@ class CommonService extends Service {
async get_business_signature() {
const { ctx } = this;
const region='oss-cn-hangzhou';
const bucket='51shequ';
const region = 'oss-cn-hangzhou';
const bucket = '51shequ';
const id= 'LTAIbdayv82O7OYM';
const key= 'plCSzfiZCpMoh39omrzdaHHEltv4Un';
const id = 'LTAIbdayv82O7OYM';
const key = 'plCSzfiZCpMoh39omrzdaHHEltv4Un';
const host = '//' + bucket + '.' + region + '.aliyuncs.com';
const url="//r.51gjj.com/";
let now = Math.round(new Date().getTime()/1000).toString();
const url = '//r.51gjj.com/';
const now = Math.round(new Date().getTime() / 1000).toString();
const expire = 120;
let end = now + expire;
const end = now + expire;
let expiration = new Date().toISOString().replace(/\..+/,'') + 'Z';
const expiration = new Date().toISOString().replace(/\..+/, '') + 'Z';
let dir = 'webpublic/resume/';
const dir = 'webpublic/resume/';
//最大文件大小.用户可以自己设置
let condition = new Array(3);
// 最大文件大小.用户可以自己设置
const condition = new Array(3);
condition[0] = 'content-length-range';
condition[1] = 0;
condition[2] = 1048576000;
let start = new Array('starts-with', '$key', dir);
const start = new Array('starts-with', '$key', dir);
let conditions = new Array(condition, start);
const conditions = new Array(condition, start);
let policy = {expiration: expiration, conditions: conditions};
const policy = { expiration, conditions };
let base64_policy = new Buffer(JSON.stringify(policy)).toString('base64');
const base64_policy = new Buffer(JSON.stringify(policy)).toString('base64');
let string_to_sign = base64_policy;
const string_to_sign = base64_policy;
const crypto = require('crypto');
let signature = crypto.createHmac('sha1', key).update(string_to_sign).digest().toString('base64');;
let config = {
const signature = crypto.createHmac('sha1', key).update(string_to_sign).digest()
.toString('base64');
const config = {
region,
accessKeyId: id,
accessKeySecret: key,
bucket: bucket,
bucket,
}
let response = {
};
const response = {
accessid: id,
static_host: url + dir,
host: host,
host,
policy: base64_policy,
signature: signature,
signature,
expire: end,
config: config,
dir
config,
dir,
};
return response;
......@@ -63,6 +64,23 @@ class CommonService extends Service {
}
async uploadOss(input) {
const { ctx } = this;
const image = input.image.replace(/^data:image\/\w+;base64,/, '');
const imgBuffer = new Buffer(image, 'base64');
const uuidv4 = require('uuid/v4');
const uuid = uuidv4();
const now = new Date();
// 上传oss
const name = 'image/catalog/' + input.directory + '/' + uuid + now.getTime() + '.png';
const result = await ctx.oss.put(name, imgBuffer);
if (result && typeof (result.url) !== 'undefined') {
const url = result.url + '?height=' + input.height + '&width=' + input.width;
return url;
}
ctx.failed('uploadOss failed');
}
}
module.exports = CommonService;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment