xscan.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. function ajax(options) {
  2. var options = options || {};
  3. options.type = (options.type || 'GET').toUpperCase();
  4. options.dataType = options.dataType || 'json';
  5. params = formatParams(options.data);
  6. //创建-第一步
  7. var xhr;
  8. //非IE6
  9. if (window.XMLHttpRequest) {
  10. xhr = new XMLHttpRequest();
  11. } else {
  12. //ie6及其以下版本浏览器
  13. xhr = ActiveXObject('Microsoft.XMLHTTP');
  14. }
  15. //接收-第三步
  16. xhr.onreadystatechange = function () {
  17. if (xhr.readyState == 4) {
  18. var status = xhr.status;
  19. if (status >= 200 && status < 300) {
  20. options.success && options.success(xhr.responseText, xhr.responseXML);
  21. } else {
  22. options.error && options.error(status);
  23. }
  24. }
  25. }
  26. //连接和发送-第二步
  27. if (options.type == 'GET') {
  28. xhr.open('GET', options.url + '?' + params, true);
  29. xhr.send(null);
  30. } else if (options.type == 'POST') {
  31. xhr.open('POST', options.url, true);
  32. //设置表单提交时的内容类型
  33. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  34. xhr.send(params);
  35. }else if (options.type == 'HEAD') {
  36. xhr.open('Head', options.url + '?' + params, true);
  37. xhr.send(null);
  38. }
  39. }
  40. //格式化参数
  41. function formatParams(data) {
  42. var arr = [];
  43. for (var name in data) {
  44. arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
  45. }
  46. // arr.push(('v='+Math.random()).replace('.',''));
  47. return arr.join('&');
  48. }
  49. function OpenDwSource() {
  50. var host = "http://localhost:19882"
  51. var xsource = '<div style="width:300px;position: absolute;border:1px solid #dddddd;background: #eeeeee;left:0; right:0;top:89px;margin:auto;box-shadow:0 1px 5px rgb(0 0 0 / 5%);border-radius:6px;z-index:10000000;">';
  52. xsource += '<div id="xcanner-content" style="padding: 15px 20px 0px 20px;">';
  53. xsource += '<h4 style="margin:0px;">请选择扫描仪:</h4>';
  54. xsource += '<div id="xscanner-sourcelist">';
  55. xsource += '<ul style="background: #fff;list-style: none;margin:0px;padding:0px;border-left:2px solid #777777;border-top:2px solid #777777;">';
  56. xsource += '</ul></div>';
  57. xsource += '<div><button id="xscanner-confirm-btn" style="margin:10px 10px 20px 0px;min-width: 80px;">确定</button><button id="xscanner-cancel-btn" style="margin:10px 10px 20px 0px;min-width: 80px;">取消</button></div>';
  58. var sourcediv = document.createElement("div")
  59. sourcediv.id = "xscanner-model"
  60. sourcediv.innerHTML = xsource;
  61. document.getElementsByTagName("body")[0].appendChild(sourcediv)
  62. //
  63. ajax({
  64. url: host + '/xscanSourceList.aspx',
  65. type: 'get',
  66. dataType: 'json',
  67. data: {},
  68. success: function (response, xml) {
  69. var listr = ""
  70. JSON.parse(response).forEach((item, index) => {
  71. if (index == 0) {
  72. listr += "<li style='background:rgb(199, 222, 252);'>" + item + "</li>";
  73. } else {
  74. listr += "<li>" + item + "</li>";
  75. }
  76. })
  77. document.getElementById("xscanner-sourcelist").children[0].innerHTML = listr;
  78. },
  79. error: function (status) {
  80. //失败后执行的代码
  81. }
  82. });
  83. // 确定
  84. document.getElementById("xscanner-confirm-btn").addEventListener("click", function () {
  85. ajax({
  86. url: host + '/xscanAction.aspx',
  87. type: 'GET',
  88. dataType: 'json',
  89. data:{tid:123},
  90. success: function (response, xml) {
  91. if (response) {
  92. }
  93. },
  94. error: function (status) {
  95. //失败后执行的代码
  96. }
  97. });
  98. var last = null;
  99. var counter = "15982"
  100. var i = 0;
  101. var timer = window.setInterval(function () {
  102. i += 1
  103. var xscannercontainer = document.getElementById("XScannerContainer")
  104. xscannercontainer.style = "overflow:scroll;padding:10px 20px 20px 20px;"
  105. last = xscannercontainer.firstChild;
  106. ajax({
  107. url: host + '/xscanImgUri.aspx',
  108. type: 'GET',
  109. dataType: 'json',
  110. data: {t:new Date().getTime()},
  111. success: function (response, xml) {
  112. console.log(response)
  113. if (response) {
  114. response.split(",").forEach((item,index)=>{
  115. var img = document.createElement("img")
  116. img.src = host + "/xscanImgReview.aspx?imgid="+item
  117. img.style = "width:100%;margin-bottom:10px;"
  118. if (last) {
  119. xscannercontainer.insertBefore(img, last)
  120. } else {
  121. xscannercontainer.append(img)
  122. }
  123. })
  124. }
  125. },
  126. error: function (status) {
  127. //失败后执行的代码
  128. }
  129. });
  130. }, 1000)
  131. document.getElementById("xscanner-model").remove();
  132. })
  133. // 取消
  134. document.getElementById("xscanner-cancel-btn").addEventListener("click", function () {
  135. document.getElementById("xscanner-model").remove();
  136. })
  137. }