nwjs.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * createname:雨中磐石
  3. * homeurl:http://www.rockoa.com/
  4. * Copyright (c) 2016 rainrock (xh829.com)
  5. * Date:2016-01-01
  6. */
  7. var nwjs={
  8. init:function(){
  9. this.nw = nwjsgui;
  10. if(!this.nw)return;
  11. this.fs = require('fs');
  12. this.win = nwjsgui.Window.get();
  13. },
  14. serverdata:function(str){
  15. },
  16. createtray:function(tls, lx){
  17. if(!this.nw)return;
  18. var icon = 'images/logo.png';
  19. if(lx==0)icon='images/logo_hui.png';
  20. var tray = new nwjsgui.Tray({ title:tls, icon: icon});
  21. tray.tooltip = tls;
  22. var menu = new nwjsgui.Menu();
  23. menu.append(new nwjsgui.MenuItem({label: '打开窗口',click:function(){
  24. nwjs.winshow();
  25. }}));
  26. this.closebool = false;
  27. menu.append(new nwjsgui.MenuItem({label: '退出',click:function(){
  28. nwjs.closebool = true;
  29. try{bodyunload();js.onunload();}catch(e){}
  30. nw.App.quit();
  31. }}));
  32. tray.menu = menu;
  33. tray.on('click',function(){
  34. nwjs.winshow();
  35. });
  36. this.tray = tray;
  37. this.win.removeAllListeners('close');
  38. this.win.on('close',function(){
  39. if(nwjs.closebool){
  40. try{bodyunload();js.onunload();}catch(e){}
  41. nw.App.quit();
  42. //nw.Window.get().close(true);
  43. }else{
  44. nwjs.win.hide();
  45. }
  46. });
  47. if(lx==0)return;
  48. var kjj=js.getoption('kuaijj','Q');
  49. this.addShortcut(kjj);
  50. this.addfile();
  51. var llq = navigator.userAgent.toLowerCase();
  52. try{if(llq.indexOf('windows nt 5')<0)this.udpserver();}catch(e){}
  53. },
  54. addShortcut:function(v){
  55. var option = {
  56. key : 'Ctrl+Alt+'+v+'',
  57. active : function() {
  58. nwjs.changewinhide();
  59. }
  60. };
  61. this.shortcut = new nwjsgui.Shortcut(option);
  62. nwjsgui.App.unregisterGlobalHotKey(this.shortcut);
  63. nwjsgui.App.registerGlobalHotKey(this.shortcut);
  64. },
  65. changekuai:function(o1){
  66. var val=o1.value;
  67. this.addShortcut(val);
  68. js.setoption('kuaijj',val);
  69. },
  70. removetray:function(){
  71. if(!this.nw)return;
  72. if(this.tray)this.tray.remove();
  73. this.win.removeAllListeners('close');
  74. if(this.shortcut)nwjsgui.App.unregisterGlobalHotKey(this.shortcut);
  75. this.closeserver();
  76. this.tray = false;
  77. this.shortcut = false;
  78. },
  79. changewinhide:function(){
  80. if(windowfocus){
  81. this.win.hide();
  82. }else{
  83. this.winshow();
  84. }
  85. },
  86. runcmd:function(cmd){
  87. if(!this.nw)return;
  88. if(!this.execcmd)this.execcmd= require('child_process').exec;
  89. this.execcmd(cmd);
  90. },
  91. openfile:function(path){
  92. var ss = this.getpath()+'/'+path;
  93. this.runcmd(ss);
  94. },
  95. openurl:function(url){
  96. this.runcmd(''+this.getpath()+'/images/start.bat '+url+'');
  97. },
  98. editoffice:function(cstr){
  99. this.runcmd(''+this.getpath()+'/images/rockoffice.exe '+cstr+'');
  100. },
  101. winshow:function(){
  102. if(!this.nw){
  103. window.focus();
  104. return;
  105. }
  106. this.win.show();
  107. this.win.focus();
  108. },
  109. jumpicon:function(oi,bo){
  110. if(!this.tray)return;
  111. clearTimeout(this.jumptime);
  112. var s=this.changeicon(this.wdshu,true);
  113. if(oi==1)s='images/logo_none.png';
  114. this.tray.icon = s;
  115. oi = (oi==1)?0:1;
  116. if(!bo)this.jumptime=setTimeout('nwjs.jumpicon('+oi+')',500);
  117. if(bo)this.changeicon(this.wdshu);
  118. },
  119. reload:function(){
  120. this.win.reloadIgnoringCache();
  121. },
  122. jumpclear:function(){
  123. this.jumpicon(0,true);
  124. },
  125. wdshu:0,
  126. changeicon:function(oi,lx){
  127. if(!this.tray)return;
  128. var s='images/logo.png';
  129. if(oi>0){
  130. s='images/logo_new.png';
  131. }
  132. this.wdshu = oi;
  133. if(lx)return s;
  134. if(!lx)this.tray.icon = s;
  135. },
  136. writeFile:function(path, str){
  137. if(!this.nw)return;
  138. if(!this.fs)this.fs = require('fs');
  139. var oatg = this.getpath();
  140. this.fs.writeFile(''+oatg+'/'+path+'', str,function(err){
  141. if(err){
  142. js.msg('msg','error:'+err+'');
  143. };
  144. });
  145. },
  146. getpath:function(){
  147. if(!this.pathobj)this.pathobj = require('path');
  148. var oatg = this.pathobj.dirname(process.execPath);
  149. oatg = oatg.replace(/\\/g, '/');
  150. return oatg;
  151. var peiz= nwjsgui.App.manifest;
  152. if(peiz.localpath)return peiz.localpath;
  153. var url = peiz.main;
  154. var las = url.lastIndexOf('\\');
  155. var oatg = url.substr(0, las);
  156. if(oatg.substr(0,5)=='file:')oatg=oatg.substr(7)
  157. return oatg;
  158. },
  159. addfile:function(){
  160. return;
  161. js.ajaxss('down','file',function(ret){
  162. var fs = require("fs");
  163. fs.writeFile('rock.php', jm.base64decode(ret.filecont), function(err) {
  164. alert(err);
  165. });
  166. });
  167. },
  168. banben:function(o1){
  169. o1.innerHTML='已是最新';
  170. },
  171. getipmac:function(){
  172. var json={ip:'','mac':''};
  173. if(!this.nw)return json;
  174. var os = require('os');
  175. var network = os.networkInterfaces();
  176. for(var a in network){
  177. for(var i = 0; i < network[a].length; i++) {
  178. var json = network[a][i];
  179. if(json.family == 'IPv4') {
  180. json.ip = json.address
  181. break;
  182. }
  183. }
  184. break;
  185. }
  186. return json;
  187. },
  188. closeserver:function(){
  189. if(!this.server)return;
  190. if(this.socketobj)this.socketobj.destroy();
  191. this.server.close();
  192. this.server=false;
  193. },
  194. socketobj:false,
  195. udpserver:function(funarr){
  196. if(!this.nw)return;
  197. var http = require('http');
  198. this.server = http.createServer(function(req, res){
  199. var url = req.url.toString(),bstr='ok';
  200. if(url.indexOf('?')>-1){
  201. try{
  202. var urla= url.split('?'),batr= urla[urla.length-1],i,bas1,bst='',bas={},k,v;
  203. var batra = batr.split('&');
  204. for(i=0;i<batra.length;i++){
  205. bas1 = batra[i].split('=');
  206. k = bas1[0]; v = bas1[1]; if(!v)v='';
  207. if(v.indexOf('base64')==0)v=jm.base64decode(v.substr(6));
  208. bas[k]= v;
  209. }
  210. var barr = nwjs.serverdata(bas);
  211. if(typeof(barr)=='object')bas = js.apply(bas, barr);
  212. for(k in bas)bst+=',"'+k+'":"'+bas[k]+'"';
  213. if(bst!='')bst=bst.substr(1);
  214. bstr= '{'+bst+'}';
  215. if(typeof(barr)=='string')bstr = barr;
  216. if(bas.callback)bstr=''+bas.callback+'({'+bst+'})';
  217. }catch(e){}
  218. }
  219. res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});res.write(bstr);res.end();
  220. nwjs.socketobj.destroy();
  221. nwjs.socketobj=false;
  222. });
  223. this.server.on('connection',function(socket){
  224. nwjs.socketobj = socket;
  225. });
  226. this.server.listen(2829,'127.0.0.1',function(){});
  227. },
  228. downfile:function(params){
  229. var cans = js.apply({url:'',savefile:'',onsuccess:function(){},onjindu:function(){},onerror:function(){}},params);
  230. var http = require('http');
  231. http.get(cans.url, function(res) {
  232. if(res.statusCode != 200){
  233. cans.onerror('not found');
  234. return;
  235. }
  236. var filesize = res.headers['content-length'];
  237. if(!filesize)filesize = res.headers['accept-length'];
  238. filesize = parseFloat(filesize);
  239. res.setEncoding('binary');
  240. var str = '';
  241. res.on('data',function(s){
  242. str+=s;
  243. var jd = Math.round(100*str.length/filesize);
  244. cans.onjindu(jd, filesize*jd*0.01);
  245. }).on('end', function(){
  246. nwjs.fs.writeFile(cans.savefile, str, 'binary', function(err){
  247. cans.onsuccess();
  248. });
  249. });
  250. }).on('error', function(e) {
  251. cans.onerror('error');
  252. });
  253. },
  254. createdir:function(path){
  255. var a1 = path.split('/'),spth='';
  256. for(var i=0;i<a1.length-1;i++){
  257. spth+=''+a1[i]+'/';
  258. if(!this.fs.existsSync(spth))this.fs.mkdirSync(spth);
  259. }
  260. },
  261. filetobase64:function(path){
  262. var data = this.fs.readFileSync(path);
  263. data = new Buffer(data).toString('base64');
  264. //this.fs.writeFileSync(path, data);
  265. return data;
  266. }
  267. };
粤ICP备19079148号