utils.js 5.8 KB
import md5 from "./md5.min.js";
var myCommonSalt = function(val) { // 获取盐值
  let len = parseInt(val);
  let $chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
  let maxPos = $chars.length;
  var pwd = "";
  for (var i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  console.log(pwd);
  return pwd;
};
export default {
  myCommonSalt,
  formatDate: function(secs, type = 0) {         //type是可选参数,因为json中时间的格式是year-month-date,不能识别XX年XX月XX日
    var t = new Date(secs);
    var year = t.getFullYear();
    var month = t.getMonth() + 1;
    if (month < 10) {
      month = "0" + month;
    }
    var date = t.getDate();
    if (date < 10) {
      date = "0" + date;
    }
    var hour = t.getHours();
    if (hour < 10) {
      hour = "0" + hour;
    }
    var minute = t.getMinutes();
    if (minute < 10) {
      minute = "0" + minute;
    }
    var second = t.getSeconds();
    if (second < 10) {
      second = "0" + second;
    }
    if (type == 0) {
      return year + "年" + month + "月" + date + "日";
    } else {
      return year + "-" + month + "-" + date;
    }
  },
  timestampToTime: function(timestamp) {
    var d = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var youWant = d.getFullYear() + "-" + this.completedString(d.getMonth() + 1) + "-" + this.completedString(d.getDate());
    return youWant;
  },
  completedString: function(s) {
    return s < 10 ? "0" + s : s;
  },
  dateFormat: function(msd) {  // 时间转换
    var time = msd;
    if (null != time && "" != time) {
      if (time > 60 && time < 60 * 60) {
        time = parseInt(time / 60.0) + "分钟" + parseInt((parseFloat(time / 60.0) -
          parseInt(time / 60.0)) * 60) + "秒";
      }
      else if (time >= 60 * 60 && time < 60 * 60 * 24) {
        time = parseInt(time / 3600.0) + "小时" + parseInt((parseFloat(time / 3600.0) -
          parseInt(time / 3600.0)) * 60) + "分钟" +
          parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
            parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";
      } else if (time >= 60 * 60 * 24) {
        time = parseInt(time / 3600.0 / 24) + "天" + parseInt((parseFloat(time / 3600.0 / 24) -
          parseInt(time / 3600.0 / 24)) * 24) + "小时" + parseInt((parseFloat(time / 3600.0) -
          parseInt(time / 3600.0)) * 60) + "分钟" +
          parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
            parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";
      }
      else {
        time = parseInt(time) + "秒";
      }
    }
    return time;
  },

  clientBrowser: function() { // 判断客户端
    if (/MicroMessenger/.test(window.navigator.userAgent)) {
      console.log("微信客户端");
      return "微信";
    } else if (/AlipayClient/.test(window.navigator.userAgent)) {
      console.log("支付宝客户端");
      return "支付宝";
    } else if (/BankabciPhone/.test(window.navigator.userAgent)) {
      console.log("农行客户端");
      return "立即";
    } else if (/BankabcAndroid/.test(window.navigator.userAgent)) {
      console.log("农行客户端");
      return "立即";
    } else {
      console.log("其他浏览器");
      return "支付宝";
    }
  },
  // 1:支付宝 2:微信 3:银联 10:H5   4微信公众号   34 农行
  clientBrowsePayType: function() { // 判断客户端
    if (/MicroMessenger/.test(window.navigator.userAgent)) {
      console.log("微信客户端");
      return "4";
    } else if (/AlipayClient/.test(window.navigator.userAgent)) {
      console.log("支付宝客户端");
      return "1";
    } else if (/BankabciPhone/.test(window.navigator.userAgent)) {
      console.log("农行客户端");
      return "34";
    } else if (/BankabcAndroid/.test(window.navigator.userAgent)) {
      console.log("农行客户端");
      return "34";
    } else {
      console.log("其他浏览器");
      return "1";
    }
  },
  myVarAppid: "ud8yq5tv0inxupc05xfeau39jywlqoj2",// 公共请求Appid
  myDeviceInfo: "BC0703A4-AFB0-4B51-9089-9B7487C0CC6E", // 公共请求设备信息
  myVxAppId: "wx1489e48e6a547023",
  userToken:localStorage.getItem('userToken'),
  userPhoneNum:localStorage.getItem('userPhoneNum'),
  // openId: "oWw3o5rY_bFsiT_nFd2CEQWGZfhs",

  openId:localStorage.getItem('openId'),
  myOrgId: "10120", // 归属地 赤峰id  10003    黄石 10079


  xuanhuaAppid:'00276515',
  commonTerminalSource:'7',
  // myVxAppId: 'wx1489e48e6a547023',
  signObject: function(jsonObj) { //签名字段
    jsonObj.sign_type = "md5";
    jsonObj.app_id = "lrpo55tmveqe07w3gpovzgx34pdez7eh";
    jsonObj.deviceInfo = "BC0703A4-AFB0-4B51-9089-9B7487C0CC6E";
    jsonObj.salt = myCommonSalt(32);
    jsonObj.token = localStorage.getItem('userToken');
    // jsonObj.token = 'e3b5417a987641219efb8dc1f8b5fbce'  // 为了测试

    let sort = [];
    for (let k in jsonObj) {
      sort.push({
        keyname: k,
        value: jsonObj[k]
      });
    }
    return this.myGetSign(sort);
  },
  myGetSign: function(objb) { // 获取签名
    var compare = function(obj1, obj2) {
      var val1 = obj1.keyname;
      var val2 = obj2.keyname;
      if (val1 < val2) {
        return -1;
      } else if (val1 > val2) {
        return 1;
      } else {
        return 0;
      }
    };
    objb.sort(compare);
    var strmd5 = "gz8wfwd0gn1etamjjhzey1ggcz78pfvd";
    for (var i = 0; i < objb.length; i++) {
      if (objb[i].value != null && objb[i].value != "") {
        strmd5 += objb[i].keyname + objb[i].value;
      }
    }
    strmd5 += "gz8wfwd0gn1etamjjhzey1ggcz78pfvd";
    // console.log('strmd5-------->'+strmd5);
    strmd5 = md5(strmd5);
    strmd5 = strmd5.toUpperCase();
    return strmd5;
  },

};