Newer
Older
urbanLifeline_YanAn / src / store / modules / imouPlayerToken.js
@zhangzhihui zhangzhihui 5 days ago 2 KB 本地视频接入
import CryptoJS from 'crypto-js';
import { getAccessToken, getKitToken } from '@/api/imouPlayerApi'; // 部门
import modal from '@/plugins/modal';

let imouUser = {
  appId: 'lc9878202a35844ac8',
  appSecret: 'd87fe493037a47669c0ea5ffdaeae0',
};

let accessParam = {
  system: {
    ver: '1.0',
    sign: '',
    appId: '',
    time: '',
    nonce: '',
  },
  params: {},
  id: '',
};

const useImouPlayerStore = defineStore('imouPlayer', {
  state: () => {
    return {
      kitToken: '',
      access_token: '',
      msg: '',
    };
  },
  actions: {
    getAccess() {
      let paramsVal = changeParams(accessParam, imouUser.appId, imouUser.appSecret);
      getAccessToken(paramsVal).then(res => {
        // console.log('access_token', res.result.data.accessToken);
        this.access_token = res.result.data.accessToken;
      });
    },
    getKit(val) {
      let paramsVal = changeParams(accessParam, imouUser.appId, imouUser.appSecret);
      paramsVal.params = val;
      getKitToken(paramsVal).then(res => {
        console.log('kitToken', res.result);
        if (res.result.code == '0') {
          this.kitToken = res.result.data.kitToken;
          this.msg = res.result.msg;
        } else if (res.result.code == 'OP1009') {
          modal.msgWarning('设备未被当前用户绑定');
          this.msg = res.result.msg;
        }
        // this.kitToken = res.result.data.kitToken;
      });
    },

    clear_kit() {
      this.kitToken = '';
    },
  },
  persist: {
    enabled: true,
    // 自定义持久化参数
    strategies: [
      {
        storage: sessionStorage,
        paths: ['kitToken', 'access_token', 'msg'],
      },
    ],
  },
});
export default useImouPlayerStore;

function changeParams(params, appId, appSecret) {
  params.system.appId = appId;
  var timestamp = getTime();
  //   pm.environment.set('time', timestamp);
  params.system.time = timestamp;
  var nonce = Math.random().toString(36).substr(2);
  //   pm.environment.set('nonce', nonce);
  params.system.nonce = nonce;
  //   var appSecret = pm.environment.get('appSecret');
  var sign = calcSign(timestamp, nonce, appSecret);
  //   pm.environment.set('sign', sign);
  params.system.sign = sign;
  var id = Math.floor(Math.random() * (50 - 1 + 1) + 1);
  //   pm.environment.set('id', id);
  params.id = id;
  return params;
}

function getTime() {
  var timestamp = Math.round(new Date().getTime() / 1000);
  return timestamp;
}

function calcSign(timestamp, nonce, appSecret) {
  var str = 'time:' + timestamp + ',nonce:' + nonce + ',appSecret:' + appSecret;
  var sign = CryptoJS.MD5(str).toString();
  return sign;
}