ali.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import http from '@/utils/http'
  2. import config from './config'
  3. import crypto from 'crypto-js';
  4. import { Base64 } from 'js-base64';
  5. export default {
  6. getCredential() {
  7. return http.post('mini/ali/mini_credential')
  8. },
  9. // 图片校验
  10. checkImg(data) {
  11. return http.post('mini/mini_wechat_app_info/media_check_async',data)
  12. },
  13. // 图片上传
  14. uploadImg(data) {
  15. return http.post('mini/lz_tree_image/insert',data)
  16. },
  17. // 计算签名
  18. computeSignature(accessKeySecret, canonicalString) {
  19. return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
  20. },
  21. getFormDataParams(){
  22. return new Promise((resolve)=>{
  23. this.getCredential().then(({code, data})=>{
  24. let { accessKeyId, accessKeySecret, securityToken } = data;
  25. const policy = Base64.encode(JSON.stringify(this.getPolicyText()))
  26. const signature = this.computeSignature(accessKeySecret, policy)
  27. const formData = {
  28. OSSAccessKeyId: accessKeyId,
  29. signature,
  30. policy,
  31. "x-oss-security-token":securityToken
  32. }
  33. resolve(formData)
  34. })
  35. })
  36. },
  37. getPolicyText(){
  38. const date = new Date();
  39. date.setHours(date.getHours() + 1);
  40. const policyText = {
  41. expiration: date.toISOString(), // 设置policy过期时间。
  42. conditions: [
  43. // 限制上传大小。
  44. ["content-length-range", 0, 1024 * 1024 * 1024],
  45. ],
  46. };
  47. return policyText
  48. },
  49. upload(formData,key,src){
  50. let host = "https://birdseye.oss-cn-guangzhou.aliyuncs.com/"
  51. return new Promise((resolve)=>{
  52. uni.uploadFile({
  53. url: host,
  54. filePath: src,
  55. name: 'file', // 必须填file。
  56. formData: {
  57. key,
  58. ...formData // 使用STS签名时必传。
  59. },
  60. success: (res) => {
  61. resolve(res)
  62. },
  63. fail: err => {
  64. console.log(err);
  65. }
  66. });
  67. })
  68. },
  69. }