123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /**
- * 信令认证模块,负责对连接信令的用户进行认证和状态记录
- * 终端---->信令(加载本模块)---->业务系统
- * Created by WJ on 2018-12-06
- * @云视睿博 www.ruiboyun.com
- */
-
- const http = require('http');
- const qs = require('querystring');
-
- class NtvAuthModule {
-
- constructor(config) {
- this.config = config;
- }
-
- errorMsg(error){
- return {code:5,err_desc:error};
- }
-
- /**
- * 解析接口返回内容
- * 并回调,回调传输人的第二个参数是一个对象,格式同接口返回格式。
- * @param {*} data
- * @param {*} callback 参数为json对象,结构同ntv g3接口规范
- */
- parseData(data,callback) {
- if(typeof(callback)!="function"){
- console.log("auth module needs callback function");
- return;
- }
- var obj = null;
- try {
- obj = JSON.parse(data);
- if(!"code" in obj){
- obj.code = 5;
- obj.err_desc = "接口返回数据内容不可理解!";
- }
- } catch(error) {
- obj = this.errorMsg("解析接口返回数据失败!")
- }
-
- callback(obj);
- }
-
- /**
- * 请求远程接口
- * @param {接口的action} action
- * @param {参数} data 参数键值对 对象或字符串
- * @param {回调} callback
- */
- postCall(host,port,path,data,callback) {
- var returnData = '';
- var content = '';
- if(typeof(data)=="object"){
- content = qs.stringify(data);
- }else{
- content = data;
- }
-
- var options = {
- hostname: host,
- port: port,
- path: path,
- method: 'POST',
- timeout: 2000,
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- }
- };
- var that = this;
- var req = http.request(options, function(res) {
- res.setEncoding('utf8');
- res.on('data', function(chunk) {
- returnData = returnData + chunk;
- });
- res.on('end', () => {
- that.parseData(returnData,callback);
- });
- });
-
- req.on('error', function(e) {
- if(typeof(callback)=="function"){
- that.callback(that.errorMsg(5,'无法连接业务服务器!'));
- }
- });
-
- req.write(content);
- req.end();
- }
-
- auth(usrId,callback){
- var config = this.config;
- var path = "/api/sipMgr/";
- var paras= "action=sip_auth&id=" + usrId;
- var host = config.host;
- var port = config.port;
- this.postCall(host,port,path,paras,callback);
- }
-
- /**
- *
- * @param {*} usrId
- * @param {*} status 1 = online
- * @param {*} callback
- */
- status(usrId,status,callback){
- var config = this.config;
- var path = "/api/sipMgr/";
- var paras= "action=sip_status&id=" + usrId +"&status=" + status;
- var host = config.host;
- var port = config.port;
- this.postCall(host,port,path,paras,callback);
- }
-
- /**
- *
- * @param {*} usrId
- * @param {*} status 1 = online
- * @param {*} callback
- */
- play_status(usrId,status,callback){
- var config = this.config;
- var path = "/api/sipMgr/";
- var paras= "action=play_status&id=" + usrId +"&status=" + status;
- var host = config.host;
- var port = config.port;
- this.postCall(host,port,path,paras,callback);
- }
- }
-
- module.exports = NtvAuthModule;
|