public.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const {static_prefix} = require('../config/sys_test')
  2. const Router = require('koa-router');
  3. // const qiniu = require('qiniu')
  4. const path = require('path');
  5. const router = new Router();
  6. const permission = require('../intercept/permission');
  7. const fileLocal = path.join(__dirname, '../static/images');
  8. const fileRemote = static_prefix + '/images/';
  9. const uploadFile = require('../util/upload')(fileLocal)
  10. const upqiniu = require('../util/upqiniu')
  11. // const accessKey = 'dlPPwgZky_F-iP8CbSbJpiAtAcqw3BYwb9rdHMrS'
  12. // const secretKey = 'YEtkLKDsImXB-8m1CT1zV_YwCwwGvrUvo2ktj9KZ'
  13. // const scope = 'cgaii'
  14. // const expires = 2 * 60 * 60 * 1000
  15. // const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
  16. // const putPolicy = new qiniu.rs.PutPolicy({ scope, expires});
  17. // let token
  18. // (function updateToken () {
  19. // token = putPolicy.uploadToken(mac);
  20. // console.log(token)
  21. // setTimeout(updateToken, expires - 60 * 1000)
  22. // })()
  23. /**
  24. * 文件上传
  25. */
  26. router.post('/upload', permission, async ctx => {
  27. let {file} = await uploadFile(ctx);
  28. let url = await upqiniu(path.join(fileLocal, file), file)
  29. ctx.body = {
  30. msg: '文件上传成功',
  31. content: '//' + url
  32. }
  33. });
  34. router.post('/uploads', permission, async ctx => {
  35. let { file } = await uploadFile(ctx);
  36. let url = await upqiniu(path.join(fileLocal, file), file)
  37. ctx.body = {
  38. errno: 0,
  39. data: ['//' + url]
  40. }
  41. });
  42. // router.get('/qiniuToken', permission, async ctx => {
  43. // ctx.body = {
  44. // msg: '获取token成功',
  45. // content: token
  46. // }
  47. // })
  48. module.exports = exports = { path: '/system', router };