iep-ui
Version:
An enterprise-class UI design language and Vue-based implementation
57 lines (54 loc) • 1.73 kB
JavaScript
import axios from 'axios';
import JsSha from 'jssha';
/**
* interface Options {
* uid: string // 用户账号
* channelName: string // 频道名称
* appkey: string // 你的 appkey
* appSecret: string // 你的 appsecret
* }
*/
export var getToken = function getToken(_ref) {
var uid = _ref.uid,
channelName = _ref.channelName,
appkey = _ref.appkey,
appSecret = _ref.appSecret;
var getTokenUrl = 'https://api.netease.im/nimserver/user/getToken.action';
var Nonce = Math.ceil(Math.random() * 1e9);
var CurTime = Math.ceil(Date.now() / 1000);
var aaa = '' + appSecret + Nonce + CurTime;
var sha1 = new JsSha('SHA-1', 'TEXT', { encoding: 'UTF8' });
sha1.update(aaa);
var CheckSum = sha1.getHash('HEX');
return new Promise(function (resolve, reject) {
axios.post(getTokenUrl, 'uid=' + uid + '&channelName=' + channelName, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
AppKey: appkey,
Nonce: Nonce,
CurTime: CurTime,
CheckSum: CheckSum
}
}).then(function (data) {
var d = data.data;
if (d.code !== 200) {
reject(new Error('getChecksum code: ' + d.code));
return;
}
resolve(d.token);
})['catch'](function (error) {
reject(new Error('getChecksum error: ', error));
});
});
};
export var checkBrowser = function checkBrowser(type) {
var ua = navigator.userAgent.toLowerCase();
var info = {
ie: /msie/.test(ua) && !/opera/.test(ua),
opera: /opera/.test(ua),
safari: /version.*safari/.test(ua),
chrome: /chrome/.test(ua),
firefox: /gecko/.test(ua) && !/webkit/.test(ua)
};
return info[type] || false;
};