alioss.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const OSS = require('ali-oss')
  2. // 创建oss实例用于向阿里云发送请求
  3. // export const client = new OSS({
  4. // region: 'oss-cn-beijing',
  5. // accessKeyId: 'LTAI4GJ5dbnpeCoHUn3Lvu8Y',
  6. // accessKeySecret: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq',
  7. // bucket: 'narcosis'
  8. // })
  9. export function client() {
  10. var client = new OSS({
  11. region: 'oss-cn-beijing',
  12. accessKeyId: 'LTAI4GJ5dbnpeCoHUn3Lvu8Y',
  13. accessKeySecret: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq',
  14. bucket: 'narcosis'
  15. })
  16. return client
  17. }
  18. // 存储目录
  19. export const catelog = 'after-sale-img/'
  20. // 生成uuid 用于生成图片名
  21. export function uuid() {
  22. var s = []
  23. var hexDigits = '0123456789abcdef'
  24. for (var i = 0; i < 36; i++) {
  25. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  26. }
  27. s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  28. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  29. s[8] = s[13] = s[18] = s[23] = '-'
  30. var uuid = s.join('')
  31. return uuid
  32. }
  33. export function uploadoss() {
  34. uni.chooseImage({
  35. count: 1, //默认9
  36. sizeType: ['compressed'],
  37. sourceType: ['album'],
  38. success: res => {
  39. let tiemr = new Date();
  40. let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr.getDate();
  41. address = 'zskp/userHead/' + address + '/';
  42. var imageSrc = res.tempFilePaths[0];
  43. let str = res.tempFilePaths[0].substr(res.tempFilePaths[0].lastIndexOf('.'));
  44. let nameStr = address + tiemr.getTime() + str;
  45. let ossUrl = 'http://oss-cn-beijing.aliyuncs.com';
  46. uni.uploadFile({
  47. url: ossUrl, //输入你的bucketname.endpoint
  48. filePath: imageSrc,
  49. fileType: 'image',
  50. name: 'file',
  51. formData: {
  52. name: nameStr,
  53. key: nameStr,
  54. // policy: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq', // 输入你获取的的policy
  55. OSSAccessKeyId: 'LTAI4GJ5dbnpeCoHUn3Lvu8Y', // 输入你的AccessKeyId
  56. success_action_status: '200', // 让服务端返回200,不然,默认会返回204
  57. // signature: 'MuBbYB4GgpSSb8T3lhS4at9OqLMKLq' // 输入你获取的的signature
  58. },
  59. success: res => {
  60. if (res.statusCode == '200') {
  61. consonle.log(ossUrl + nameStr);
  62. alert(ossUrl + nameStr)
  63. } else {
  64. consonle.log(res);
  65. alert(res)
  66. }
  67. }
  68. });
  69. }
  70. });
  71. }