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 { ...@@ -7,5 +7,24 @@ class CommonController extends Controller {
const ret = await service.common.oss.get_business_signature(); const ret = await service.common.oss.get_business_signature();
ctx.success(ret); 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; module.exports = CommonController;
...@@ -9,7 +9,7 @@ module.exports = { ...@@ -9,7 +9,7 @@ module.exports = {
const { ctx } = this; const { ctx } = this;
const bearerToken = ctx.request.header.authorization; const bearerToken = ctx.request.header.authorization;
if (!bearerToken) { if (!bearerToken) {
this.throw(422, 'error auth'); ctx.failed('error auth');
} }
return bearerToken && bearerToken.replace('Bearer ', ''); return bearerToken && bearerToken.replace('Bearer ', '');
}, },
...@@ -20,7 +20,7 @@ module.exports = { ...@@ -20,7 +20,7 @@ module.exports = {
const decode_res = await ctx.service.jwt.decode_token(token); 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); const token_black = await this.app.memcache.get('auth_token_' + decode_res.data.user_uuid);
if (token_black == token) { if (token_black == token) {
this.throw(422, 'jwt校验失败'); ctx.failed('jwt校验失败');
} }
ctx.setUserUuid(decode_res.data.user_uuid); ctx.setUserUuid(decode_res.data.user_uuid);
ctx.setOpenId(decode_res.data.openid); ctx.setOpenId(decode_res.data.openid);
......
...@@ -4,5 +4,7 @@ module.exports = app => { ...@@ -4,5 +4,7 @@ module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/common'); const router = app.router.namespace(app.config.projectRootPath + '/common');
router.get('third_oss_signature', '/oss/signature', 'common.oss.signature');// 业务的 oss 签名 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'); router.get('third_test', '/test', 'common.test.test');
}; };
...@@ -6,56 +6,57 @@ class CommonService extends Service { ...@@ -6,56 +6,57 @@ class CommonService extends Service {
async get_business_signature() { async get_business_signature() {
const { ctx } = this; const { ctx } = this;
const region='oss-cn-hangzhou'; const region = 'oss-cn-hangzhou';
const bucket='51shequ'; const bucket = '51shequ';
const id= 'LTAIbdayv82O7OYM'; const id = 'LTAIbdayv82O7OYM';
const key= 'plCSzfiZCpMoh39omrzdaHHEltv4Un'; const key = 'plCSzfiZCpMoh39omrzdaHHEltv4Un';
const host = '//' + bucket + '.' + region + '.aliyuncs.com'; const host = '//' + bucket + '.' + region + '.aliyuncs.com';
const url="//r.51gjj.com/"; const url = '//r.51gjj.com/';
let now = Math.round(new Date().getTime()/1000).toString(); const now = Math.round(new Date().getTime() / 1000).toString();
const expire = 120; 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[0] = 'content-length-range';
condition[1] = 0; condition[1] = 0;
condition[2] = 1048576000; 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};
let base64_policy = new Buffer(JSON.stringify(policy)).toString('base64'); const policy = { expiration, conditions };
let string_to_sign = base64_policy;
const base64_policy = new Buffer(JSON.stringify(policy)).toString('base64');
const string_to_sign = base64_policy;
const crypto = require('crypto'); const crypto = require('crypto');
let signature = crypto.createHmac('sha1', key).update(string_to_sign).digest().toString('base64');; const signature = crypto.createHmac('sha1', key).update(string_to_sign).digest()
let config = { .toString('base64');
const config = {
region, region,
accessKeyId: id, accessKeyId: id,
accessKeySecret: key, accessKeySecret: key,
bucket: bucket, bucket,
} };
let response = { const response = {
accessid: id, accessid: id,
static_host: url + dir, static_host: url + dir,
host: host, host,
policy: base64_policy, policy: base64_policy,
signature: signature, signature,
expire: end, expire: end,
config: config, config,
dir dir,
}; };
return response; return response;
...@@ -63,6 +64,23 @@ class CommonService extends Service { ...@@ -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; 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