Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NtvAuthModule.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * 信令认证模块,负责对连接信令的用户进行认证和状态记录
  3. * 终端---->信令(加载本模块)---->业务系统
  4. * Created by WJ on 2018-12-06
  5. * @云视睿博 www.ruiboyun.com
  6. */
  7. const http = require('http');
  8. const qs = require('querystring');
  9. class NtvAuthModule {
  10. constructor(config) {
  11. this.config = config;
  12. }
  13. errorMsg(error){
  14. return {code:5,err_desc:error};
  15. }
  16. /**
  17. * 解析接口返回内容
  18. * 并回调,回调传输人的第二个参数是一个对象,格式同接口返回格式。
  19. * @param {*} data
  20. * @param {*} callback 参数为json对象,结构同ntv g3接口规范
  21. */
  22. parseData(data,callback) {
  23. if(typeof(callback)!="function"){
  24. console.log("auth module needs callback function");
  25. return;
  26. }
  27. var obj = null;
  28. try {
  29. obj = JSON.parse(data);
  30. if(!"code" in obj){
  31. obj.code = 5;
  32. obj.err_desc = "接口返回数据内容不可理解!";
  33. }
  34. } catch(error) {
  35. obj = this.errorMsg("解析接口返回数据失败!")
  36. }
  37. callback(obj);
  38. }
  39. /**
  40. * 请求远程接口
  41. * @param {接口的action} action
  42. * @param {参数} data 参数键值对 对象或字符串
  43. * @param {回调} callback
  44. */
  45. postCall(host,port,path,data,callback) {
  46. var returnData = '';
  47. var content = '';
  48. if(typeof(data)=="object"){
  49. content = qs.stringify(data);
  50. }else{
  51. content = data;
  52. }
  53. var options = {
  54. hostname: host,
  55. port: port,
  56. path: path,
  57. method: 'POST',
  58. timeout: 2000,
  59. headers: {
  60. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  61. }
  62. };
  63. var that = this;
  64. var req = http.request(options, function(res) {
  65. res.setEncoding('utf8');
  66. res.on('data', function(chunk) {
  67. returnData = returnData + chunk;
  68. });
  69. res.on('end', () => {
  70. that.parseData(returnData,callback);
  71. });
  72. });
  73. req.on('error', function(e) {
  74. if(typeof(callback)=="function"){
  75. that.callback(that.errorMsg(5,'无法连接业务服务器!'));
  76. }
  77. });
  78. req.write(content);
  79. req.end();
  80. }
  81. auth(usrId,callback){
  82. var config = this.config;
  83. var path = "/api/sipMgr/";
  84. var paras= "action=sip_auth&id=" + usrId;
  85. var host = config.host;
  86. var port = config.port;
  87. this.postCall(host,port,path,paras,callback);
  88. }
  89. /**
  90. *
  91. * @param {*} usrId
  92. * @param {*} status 1 = online
  93. * @param {*} callback
  94. */
  95. status(usrId,status,callback){
  96. var config = this.config;
  97. var path = "/api/sipMgr/";
  98. var paras= "action=sip_status&id=" + usrId +"&status=" + status;
  99. var host = config.host;
  100. var port = config.port;
  101. this.postCall(host,port,path,paras,callback);
  102. }
  103. /**
  104. *
  105. * @param {*} usrId
  106. * @param {*} status 1 = online
  107. * @param {*} callback
  108. */
  109. play_status(usrId,status,callback){
  110. var config = this.config;
  111. var path = "/api/sipMgr/";
  112. var paras= "action=play_status&id=" + usrId +"&status=" + status;
  113. var host = config.host;
  114. var port = config.port;
  115. this.postCall(host,port,path,paras,callback);
  116. }
  117. }
  118. module.exports = NtvAuthModule;