tools.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var COS = require('./lib/cos-wx-sdk-v5');
  2. var config = require('./config');
  3. const Beacon = require('./lib/beacon_mp.min');
  4. const ClsClient = require('./lib/cls.min');
  5. const clsClient = new ClsClient({
  6. topicId: 'xxxxxx-xxxx-xxxx-xxxx-xxxxxx', // 日志主题 id
  7. region: 'ap-guangzhou', // 日志主题所在地域,比如 ap-guangzhou,需在小程序平台设置域名白名单:https://ap-guangzhou.cls.tencentcs.com
  8. maxRetainDuration: 30, // 默认 30s
  9. maxRetainSize: 20, // 默认20条
  10. });
  11. // 签名回调
  12. var getAuthorization = function (options, callback) {
  13. // 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
  14. // 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
  15. // 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
  16. wx.request({
  17. method: 'GET',
  18. url: config.stsUrl, // 服务端签名,参考 server 目录下的两个签名例子
  19. dataType: 'json',
  20. success: function (result) {
  21. var data = result.data;
  22. var credentials = data && data.credentials;
  23. if (!data || !credentials) return console.error('credentials invalid');
  24. callback({
  25. TmpSecretId: credentials.tmpSecretId,
  26. TmpSecretKey: credentials.tmpSecretKey,
  27. SecurityToken: credentials.sessionToken,
  28. StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  29. ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
  30. });
  31. },
  32. });
  33. // // 格式二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重复使用临时密钥,后端可以通过 Scope 细粒度控制权限
  34. // // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master/scope.md
  35. // wx.request({
  36. // method: 'POST',
  37. // url: 'http://127.0.0.1:3000/sts-scope',
  38. // data: options.Scope,
  39. // dataType: 'json',
  40. // success: function(result) {
  41. // var data = result.data;
  42. // var credentials = data && data.credentials;
  43. // if (!data || !credentials) return console.error('credentials invalid');
  44. // callback({
  45. // TmpSecretId: credentials.tmpSecretId,
  46. // TmpSecretKey: credentials.tmpSecretKey,
  47. // XCosSecurityToken: credentials.sessionToken,
  48. // StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  49. // ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
  50. // ScopeLimit: true, // 细粒度控制权限需要设为 true,会限制密钥只在相同请求时重复使用
  51. // });
  52. // }
  53. // });
  54. // // 格式三、(不推荐,分片上传权限不好控制)前端每次请求前都需要通过 getAuthorization 获取签名,后端使用固定密钥或临时密钥计算签名返回给前端
  55. // // 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
  56. // // 注意:这种有安全风险,后端需要通过 method、pathname 严格控制好权限,比如不允许 put / 等
  57. // wx.request({
  58. // method: 'POST',
  59. // url: 'https://example.com/sts-auth.php, // 服务端签名,参考 server 目录下的两个签名例子
  60. // data: {
  61. // method: options.Method,
  62. // pathname: options.Pathname,
  63. // query: options.Query,
  64. // headers: options.Headers,
  65. // },
  66. // dataType: 'json',
  67. // success: function(result) {
  68. // var data = result.data;
  69. // if (!data || !data.authorization) return console.error('authorization invalid');
  70. // callback({
  71. // Authorization: data.authorization,
  72. // // XCosSecurityToken: data.sessionToken, // 如果使用临时密钥,需要传 sessionToken
  73. // });
  74. // }
  75. // });
  76. // // 格式四、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名
  77. // var authorization = COS.getAuthorization({
  78. // SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  79. // SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  80. // Method: options.Method,
  81. // Pathname: options.Pathname,
  82. // Query: options.Query,
  83. // Headers: options.Headers,
  84. // Expires: 60,
  85. // });
  86. // callback({
  87. // Authorization: authorization,
  88. // // XCosSecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 XCosSecurityToken
  89. // });
  90. };
  91. var cos = new COS({
  92. getAuthorization: getAuthorization,
  93. // 是否使用全球加速域名。开启该配置后仅以下接口支持操作:putObject、getObject、headObject、optionsObject、multipartInit、multipartListPart、multipartUpload、multipartAbort、multipartComplete、multipartList、sliceUploadFile、uploadFiles
  94. // UseAccelerate: true,
  95. // BeaconReporter: Beacon, // 开启灯塔上报
  96. // ClsReporter: clsClient, // 开启 cls 上报
  97. });
  98. // 回调统一处理函数
  99. var requestCallback = function (err, data) {
  100. console.log(err || data);
  101. if (err && err.error) {
  102. wx.showModal({
  103. title: '返回错误',
  104. content: '请求失败:' + (err.error.Message || err.error) + ';状态码:' + err.statusCode,
  105. showCancel: false,
  106. });
  107. } else if (err) {
  108. wx.showModal({
  109. title: '请求出错',
  110. content: '请求出错:' + err + ';状态码:' + err.statusCode,
  111. showCancel: false,
  112. });
  113. } else {
  114. wx.showToast({
  115. title: '请求成功',
  116. icon: 'success',
  117. duration: 3000,
  118. });
  119. }
  120. };
  121. module.exports = {
  122. cos,
  123. requestCallback,
  124. };