createParam.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const md5 = require("./md5.js");
  2. const base64 = require("./base64.js");
  3. const Crypto = require("./crypto.js");
  4. module.exports = {
  5. create: function (param, method){
  6. var version = "1.0";
  7. var time = Math.ceil(new Date().getTime() / 1000);
  8. let {token} = wx.getStorageSync("@login");
  9. if( !token ) token = "";
  10. var APPKEY = "5B634B742568BE1";
  11. var SECRET = "17c10c42e05027f50585731623abdd20";
  12. var DEFAULTKEY = "demoþúï"; //"\\xfe\\xfa\\xef";
  13. var JSON_RPC = "2.0";
  14. var requestParam = { id: time, method: method };
  15. /* param 参数部分 */
  16. requestParam.param = param;
  17. /* md5加密 参数部分 */
  18. var format = this.orderFormat(param);
  19. var rwaStr = token + APPKEY + time + format + SECRET + DEFAULTKEY;
  20. var signMd5 = md5.md5(rwaStr).toLowerCase();
  21. /* system 参数部分 */
  22. requestParam.system = { version: version, jsonrpc: JSON_RPC, key: APPKEY, time: time, token: token, sign: signMd5 };
  23. console.warn("requestParam: ", requestParam)
  24. return requestParam;
  25. },
  26. orderFormat:function (param){
  27. let newkey = Object.keys(param).sort();
  28. let paramstr = "";
  29. for (let i = 0; i < newkey.length; i++) {
  30. let value = param[newkey[i]]
  31. if (typeof (value) == "number") {
  32. paramstr += parseInt(value)
  33. } else if( typeof (value) == "string") {
  34. paramstr += value
  35. }
  36. }
  37. return paramstr;
  38. },
  39. //OSS上传文件的参数
  40. getUploadParam: function (filename, accessid, accesskey){
  41. var suffix = this.get_suffix(filename);
  42. var key = this.random_string() + suffix;
  43. var policyText = {
  44. "expiration": "2050-01-01T12:00:00.000Z", //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
  45. "conditions": [
  46. ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
  47. ]
  48. };
  49. var policyBase64 = base64.encode(JSON.stringify(policyText))
  50. var message = policyBase64
  51. var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true });
  52. var signature = Crypto.util.bytesToBase64(bytes);
  53. return {
  54. 'key': key,
  55. 'policy': policyBase64,
  56. 'OSSAccessKeyId': accessid,
  57. 'success_action_status': '200', //让服务端返回200,不然,默认会返回204
  58. 'signature': signature
  59. };
  60. },
  61. random_string:function() {
  62. return new Date().getTime() +"_"+Math.floor(Math.random() * 10000)
  63. },
  64. get_suffix:function(filename) {
  65. var pos = filename.lastIndexOf('.')
  66. var suffix = ''
  67. if (pos != -1) {
  68. suffix = filename.substring(pos)
  69. }
  70. return suffix;
  71. }
  72. }