const md5 = require("./md5.js");
const base64 = require("./base64.js");
const Crypto = require("./crypto.js");

module.exports = {
  create: function (param, method){

    var version = "1.0";
    var time = Math.ceil(new Date().getTime() / 1000);
    let {token} = wx.getStorageSync("@login");
    if( !token ) token = "";
  

    var APPKEY = "5B634B742568BE1";
    var SECRET = "17c10c42e05027f50585731623abdd20";
    var DEFAULTKEY = "demoþúï"; //"\\xfe\\xfa\\xef";
    var JSON_RPC = "2.0";
  

    var requestParam = { id: time, method: method };


    /* param 参数部分 */
   
    requestParam.param = param;


    /* md5加密 参数部分 */
    var format = this.orderFormat(param);
    var rwaStr = token + APPKEY + time + format + SECRET + DEFAULTKEY;
    var signMd5 = md5.md5(rwaStr).toLowerCase();

    /* system 参数部分 */
    requestParam.system = { version: version, jsonrpc: JSON_RPC, key: APPKEY, time: time, token: token, sign: signMd5 };

    console.warn("requestParam: ", requestParam)
  
    return requestParam;
  },

  orderFormat:function (param){
    let newkey = Object.keys(param).sort();
    let paramstr = "";
    for (let i = 0; i < newkey.length; i++) {
      let value = param[newkey[i]]
      if (typeof (value) == "number") {
        paramstr += parseInt(value)
      } else if( typeof (value) == "string") {
        paramstr += value
      }
    }
    return paramstr;
  },

  //OSS上传文件的参数
  getUploadParam: function (filename, accessid, accesskey){
    var suffix = this.get_suffix(filename);
    var key = this.random_string() + suffix;
    var policyText = {
      "expiration": "2050-01-01T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
      "conditions": [
        ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
      ]
    };
    var policyBase64 = base64.encode(JSON.stringify(policyText))
    var message = policyBase64
    var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true });
    var signature = Crypto.util.bytesToBase64(bytes);

    return {
      'key': key,
      'policy': policyBase64,
      'OSSAccessKeyId': accessid,
      'success_action_status': '200', //让服务端返回200,不然，默认会返回204
      'signature': signature
    };
  },

  
  random_string:function() {
    return new Date().getTime() +"_"+Math.floor(Math.random() * 10000)
  },

  get_suffix:function(filename) {
    var pos = filename.lastIndexOf('.')
    var suffix = ''
    if (pos != -1) {
      suffix = filename.substring(pos)
    }
    return suffix;
  }
  
} 