nwjs.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. openurl:function(url){
  92. this.runcmd(''+this.getpath()+'/images/start.bat '+url+'');
  93. },
  94. editoffice:function(cstr){
  95. this.runcmd(''+this.getpath()+'/images/rockoffice.exe '+cstr+'');
  96. },
  97. winshow:function(){
  98. if(!this.nw){
  99. window.focus();
  100. return;
  101. }
  102. this.win.show();
  103. this.win.focus();
  104. },
  105. jumpicon:function(oi,bo){
  106. if(!this.tray)return;
  107. clearTimeout(this.jumptime);
  108. var s=this.changeicon(this.wdshu,true);
  109. if(oi==1)s='images/logo_none.png';
  110. this.tray.icon = s;
  111. oi = (oi==1)?0:1;
  112. if(!bo)this.jumptime=setTimeout('nwjs.jumpicon('+oi+')',500);
  113. if(bo)this.changeicon(this.wdshu);
  114. },
  115. jumpclear:function(){
  116. this.jumpicon(0,true);
  117. },
  118. wdshu:0,
  119. changeicon:function(oi,lx){
  120. if(!this.tray)return;
  121. var s='images/logo.png';
  122. if(oi>0){
  123. s='images/logo_new.png';
  124. }
  125. this.wdshu = oi;
  126. if(lx)return s;
  127. if(!lx)this.tray.icon = s;
  128. },
  129. writeFile:function(path, str){
  130. if(!this.nw)return;
  131. if(!this.fs)this.fs = require('fs');
  132. var oatg = this.getpath();
  133. this.fs.writeFile(''+oatg+'/'+path+'', str,function(err){
  134. if(err){
  135. js.msg('msg','error:'+err+'');
  136. };
  137. });
  138. },
  139. getpath:function(){
  140. if(!this.pathobj)this.pathobj = require('path');
  141. var oatg = this.pathobj.dirname(process.execPath);
  142. oatg = oatg.replace(/\\/g, '/');
  143. return oatg;
  144. var peiz= nwjsgui.App.manifest;
  145. if(peiz.localpath)return peiz.localpath;
  146. var url = peiz.main;
  147. var las = url.lastIndexOf('\\');
  148. var oatg = url.substr(0, las);
  149. if(oatg.substr(0,5)=='file:')oatg=oatg.substr(7)
  150. return oatg;
  151. },
  152. addfile:function(){
  153. return;
  154. js.ajaxss('down','file',function(ret){
  155. var fs = require("fs");
  156. fs.writeFile('rock.php', jm.base64decode(ret.filecont), function(err) {
  157. alert(err);
  158. });
  159. });
  160. },
  161. banben:function(o1){
  162. o1.innerHTML='已是最新';
  163. },
  164. getipmac:function(){
  165. var json={ip:'','mac':''};
  166. if(!this.nw)return json;
  167. var os = require('os');
  168. var network = os.networkInterfaces();
  169. for(var a in network){
  170. for(var i = 0; i < network[a].length; i++) {
  171. var json = network[a][i];
  172. if(json.family == 'IPv4') {
  173. json.ip = json.address
  174. break;
  175. }
  176. }
  177. break;
  178. }
  179. return json;
  180. },
  181. closeserver:function(){
  182. if(!this.server)return;
  183. if(this.socketobj)this.socketobj.destroy();
  184. this.server.close();
  185. this.server=false;
  186. },
  187. socketobj:false,
  188. udpserver:function(funarr){
  189. if(!this.nw)return;
  190. var http = require('http');
  191. this.server = http.createServer(function(req, res){
  192. var url = req.url.toString(),bstr='ok';
  193. if(url.indexOf('?')>-1){
  194. try{
  195. var urla= url.split('?'),batr= urla[urla.length-1],i,bas1,bst='',bas={},k,v;
  196. var batra = batr.split('&');
  197. for(i=0;i<batra.length;i++){
  198. bas1 = batra[i].split('=');
  199. k = bas1[0]; v = bas1[1]; if(!v)v='';
  200. if(v.indexOf('base64')==0)v=jm.base64decode(v.substr(6));
  201. bas[k]= v;
  202. }
  203. var barr = nwjs.serverdata(bas);
  204. if(typeof(barr)=='object')bas = js.apply(bas, barr);
  205. for(k in bas)bst+=',"'+k+'":"'+bas[k]+'"';
  206. if(bst!='')bst=bst.substr(1);
  207. bstr= '{'+bst+'}';
  208. if(typeof(barr)=='string')bstr = barr;
  209. if(bas.callback)bstr=''+bas.callback+'({'+bst+'})';
  210. }catch(e){}
  211. }
  212. res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});res.write(bstr);res.end();
  213. nwjs.socketobj.destroy();
  214. nwjs.socketobj=false;
  215. });
  216. this.server.on('connection',function(socket){
  217. nwjs.socketobj = socket;
  218. });
  219. this.server.listen(2829,'127.0.0.1',function(){});
  220. },
  221. downfile:function(params){
  222. var cans = js.apply({url:'',savefile:'',onsuccess:function(){},onjindu:function(){},onerror:function(){}},params);
  223. var http = require('http');
  224. http.get(cans.url, function(res) {
  225. if(res.statusCode != 200){
  226. cans.onerror('not found');
  227. return;
  228. }
  229. var filesize = res.headers['content-length'];
  230. if(!filesize)filesize = res.headers['accept-length'];
  231. filesize = parseFloat(filesize);
  232. res.setEncoding('binary');
  233. var str = '';
  234. res.on('data',function(s){
  235. str+=s;
  236. var jd = Math.round(100*str.length/filesize);
  237. cans.onjindu(jd, filesize*jd*0.01);
  238. }).on('end', function(){
  239. nwjs.fs.writeFile(cans.savefile, str, 'binary', function(err){
  240. cans.onsuccess();
  241. });
  242. });
  243. }).on('error', function(e) {
  244. cans.onerror('error');
  245. });
  246. },
  247. createdir:function(path){
  248. var a1 = path.split('/'),spth='';
  249. for(var i=0;i<a1.length-1;i++){
  250. spth+=''+a1[i]+'/';
  251. if(!this.fs.existsSync(spth))this.fs.mkdirSync(spth);
  252. }
  253. },
  254. filetobase64:function(path){
  255. var data = this.fs.readFileSync(path);
  256. data = new Buffer(data).toString('base64');
  257. //this.fs.writeFileSync(path, data);
  258. return data;
  259. }
  260. };