js调用本地程序exe (2018-01-25 17:14:50)转载▼
    分类: javascript/css
    onPrintAction: function () {
    var me = this;
    this.ajaxPost(“PayMoney/printPayMoneyList.do”, {“moneyno”:(this.form.findField(“moneyno”)?this.form.findField(“moneyno”).getValue():this.form.findField(“payno”).getValue())},
    function (r) {
    me.printweb(r);
    }, function (e) {
    me.msgError(e);
    });
    return;
    }
    printweb:function(r){
    var me = this;
    $.ajax({
    type:”get”,
    url:”http://127.0.0.1:"+window.printport+"?url="+r.url+"&modelfilename="+r.modelfilename+"&datafilename="+r.datafilename+
    “&FCanPreview=”+r.canpreview+”&FCanPrint=”+r.canprint+”&FCanExport=”+r.canexport+”&FReportName=”+encodeURI(r.reportname)+
    ‘&r=’+(new Date()),
    dataType:”jsonp”,
    jsonpCallback:”jsonpCallback”,
    success:function(r){
    },
    error:function(e){
    me.msgConfirm(“打印出现问题,可能原因如下:
    “+
    “1.没有安装打印组件。点击“确定”后下载打印组件并安装;如果已下载,直接安装
    “+
    “2.打印组件没有启动。
    “+
    “3.打印组件和服务器端口设置相同,并且没有被占用。
    “,
    function(c){
    if (c == “yes”) {
    window.open(“toyorReport/PrintServiceSetUp.zip”)
    }
    }) ;
    }
    });
    },
    delphi部分:
    procedure TfmPrintServiceTykj.actionPortExecute(Sender: TObject);
    var
    fmport: TfmPort;
    begin
    with fmport do
    try
    fmport := TfmPort.Create(Application);
    fmport.sedtPort.Text := IntToStr(Fport);
    with fmport do
    if ShowModal = mrOk then
    begin
    StartPrintService(StrToInt(sedtPort.Text));
    Fport := StrToInt(fmport.sedtPort.Text);
    lblPort.Caption := IntToStr(FPort);
    end;
    finally
    FreeAndNil(fmport);
    end;
    end;
    procedure TfmPrintServiceTykj.idhtpsrvr1CommandGet(AThread: TIdPeerThread; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    var
    sRequestInfo: string;
    slParam: TStrings;
    url, modelfilename, datafilename: string;
    begin
    sRequestInfo := ARequestInfo.Document;
    slParam := ARequestInfo.Params;
    if (slParam.Text = ‘’) then
    begin
    AResponseInfo.ContentText := ‘jsonpCallback(“打印参数不正确”)’;
    Exit;
    end;
    url := slParam.Values[‘url’];
    if (Trim(url) = ‘’) then
    begin
    AResponseInfo.ContentText := ‘jsonpCallback(“打印参数url不正确”)’;
    Exit;
    end;
    modelfilename := slParam.Values[‘modelfilename’];
    if (Trim(modelfilename) = ‘’) then
    begin
    AResponseInfo.ContentText := ‘jsonpCallback(“打印参数modelfilename不正确”)’;
    Exit;
    end;
    datafilename := slParam.Values[‘datafilename’];
    if (Trim(datafilename) = ‘’) then
    begin
    AResponseInfo.ContentText := ‘jsonpCallback(“打印参数datafilename不正确”)’;
    Exit;
    end;
    FReportName := Utf8ToAnsi(slParam.Values[‘FReportName’]);
    FCanExport := slParam.Values[‘FCanExport’];
    FCanPrint := slParam.Values[‘FCanPrint’];
    FCanPreview := slParam.Values[‘FCanPreview’];
    AResponseInfo.ContentType := ‘text/html;Charset=UTF-8’;
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.ContentText := ‘jsonpCallback(“Ok:’ + FormatDateTime(‘yyyy-MM-dd HH:mm:ss’, Now) + ‘“)’;
    FBaseUrl := url;
    //测试时使用同一个文件,就不删除了
    if (modelfilename <> FModelFileName) and (FDataFileName <> datafilename) then
    DelServerReportTempFile;
    FModelFileName := modelfilename;
    FDataFileName := datafilename;
    {Application.Restore;
    SetWindowPos(handle,HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE);//更改窗口zorder 使之最上 SetWindowPos(x,HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE);//还原
    SetForegroundWindow(handle);
    }
    CoolTrayIcon1.ShowMainForm;
    timDownDelay.Enabled := true;
    end;
    procedure TfmPrintServiceTykj.ApplicationEvents1Exception(Sender: TObject; E: Exception);
    var
    msg: string;
    begin
    msg := LowerCase(E.Message);
    if (Pos(‘socket error’, msg) > 0) then
    begin
    Application.Messagebox(‘端口被占用!’, ‘提示’, MB_OK + MB_ICONWARNING);
    Exit;
    end;
    if (Pos(‘address and port are already in use’, msg) > 0) then
    begin
    Application.Messagebox(‘端口已经打开!’, ‘提示’, MB_OK + MB_ICONWARNING);
    Exit;
    end;
    end;
    procedure TfmPrintServiceTykj.ReadIniFile;
    var
    MyINIFile: TIniFile;
    MyFileName: string;
    begin
    MyFileName := ExtractFilePath(ParamStr(0)) + ChangeFileExt(ExtractFileName(Application.ExeName), ‘’) + ‘.ini’;
    MyINIFile := TIniFile.Create(MyFileName);
    try
    Fport := MyINIFile.ReadInteger(‘端口设置’, ‘port’, 8090);
    lblPort.Caption := intToStr(Fport);
    finally
    FreeAndNil(MyINIFile);
    end;
    end;
    procedure TfmPrintServiceTykj.FormCreate(Sender: TObject);
    begin
    btnPreview.Enabled := False;
    btnPrint.Enabled := False;
    btnExport.Enabled := False;
    ReadIniFile;
    SetAutoRun(True);
    StartPrintService(FPort);
    end;
    procedure TfmPrintServiceTykj.StartPrintService(port: integer);
    begin
    {idhtpsrvr1.DefaultPort := StrToInt(edtPort.Text);
    idhtpsrvr1.Active:=True;}
    idhtpsrvr1.Active := False;
    //idhtpsrvr1.ReuseSocket := true;
    idhtpsrvr1.Bindings.Clear;
    idhtpsrvr1.Bindings.Add.Port := port;
    //Binding.IP := getLocalIp;
    idhtpsrvr1.Active := true;
    end;
    procedure TfmPrintServiceTykj.ShowWindow1Click(Sender: TObject);
    begin
    CoolTrayIcon1.ShowMainForm;
    end;
    procedure TfmPrintServiceTykj.HideWindow1Click(Sender: TObject);
    begin
    CoolTrayIcon1.HideMainForm;
    end;
    procedure TfmPrintServiceTykj.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
    btnPreview.Enabled := False;
    btnPrint.Enabled := False;
    btnExport.Enabled := False;
    lblLoadModelInfo.Caption := ‘没有数据信息’;
    lblLoadDataInfo.Caption := ‘没有数据信息’;
    lblReportTitle.Caption := ‘未命名报表’;
    Refresh;
    CanClose := false;
    if not CanClose then
    begin
    CoolTrayIcon1.HideMainForm;
    CoolTrayIcon1.IconVisible := True;
    end;
    DelServerReportTempFile();
    end;
    procedure TfmPrintServiceTykj.actionCloseExecute(Sender: TObject);
    begin
    Application.Terminate;
    end;
    procedure TfmPrintServiceTykj.N2Click(Sender: TObject);
    begin
    Application.MessageBox(Pchar(‘报表打印组件客户端 ‘ + #10#13#10#13 + ‘最后更新:2018-01-20’), ‘关于’, MB_OK + MB_ICONINFORMATION);
    end;
    procedure TfmPrintServiceTykj.DelServerReportTempFile;
    begin
    if (FDataFileName = ‘’) or (FModelFileName = ‘’) then
    Exit;
    try
    IdHTTP1.HandleRedirects := False; //允许头转向
    IdHTTP1.ReadTimeout := 5000; //请求超时设置
    IdHTTP1.HandleRedirects := True; //支持重定向
    IdHTTP1.Request.CacheControl := ‘no-cache’; //不缓存
    IdHTTP1.Request.AcceptEncoding := ‘’; // ‘gzip, deflatee’; 压缩格式,如果不需要,就置空
    IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol]; //设置协议
    IdHTTP1.ProtocolVersion := pv1_1;
    IdHTTP1.Request.Connection := ‘Keep-Alive’;
    //IdHTTP1.Request.ContentType := ‘application/json’; //设置内容类型为json
    IdHTTP1.Get(FBaseUrl + FBaseVDirName + ‘BaseList/delTempReportFile.do?datafilename=’ + FDataFileName + ‘&modelfilename=’ + FModelFileName); //用MEMO控件接收POST后的数据返回
    except
    end;
    end;
    procedure TfmPrintServiceTykj.SetAutoRun(isAutoStart: boolean);
    var
    Reg: TRegistry; //首先定义一个TRegistry类型的变量Reg
    begin
    Reg := TRegistry.Create;
    try //创建一个新键
    try
    Reg.RootKey := HKEY_CURRENT_USER; //将根键设置为HKEY_LOCAL_MACHINE
    Reg.OpenKey(‘Software\Microsoft\Windows\CurrentVersion\Run’, true); //打开一个键
    if isAutoStart then
    begin
    Reg.WriteString(‘printservicetykj’, ExpandFileName(ParamStr(0))); //在Reg这个键中写入数据名称和数据数值
    // ShowMessage(‘注册成功!’);
    end
    else
    begin
    Reg.DeleteValue(‘printservicetykj’);
    //ShowMessage(‘删除成功!’)
    end;
    Reg.CloseKey; //关闭键
    except
    end;
    finally
    Reg.Free;
    end;
    end;
    分享: