using CefSharp; using CefSharp.WinForms; using efunbox_xyyf_windows.baseMvp; using efunbox_xyyf_windows.bean; using efunbox_xyyf_windows.contract; using efunbox_xyyf_windows.cusview; using efunbox_xyyf_windows.model; using efunbox_xyyf_windows.util; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static efunbox_xyyf_windows.model.MainModel; namespace efunbox_xyyf_windows.presenter { public class MainPresenter : BasePresenter, MainContract.IPresenter { private MainModel mModel; private string mLoadNowUrl; public MainPresenter() { mModel = new MainModel(); } /* * 初始化webview * */ public void initWebView(ChromiumWebBrowser webview) { var setting = new CefSettings(); setting.Locale = "zh-CN"; setting.AcceptLanguageList = "zh-CN,zh;q=0.8"; setting.CefCommandLineArgs.Add("enable-system-flash", "1"); //启用flash setting.CefCommandLineArgs.Add("enable-media-stream", "1"); //启用媒体流 setting.CefCommandLineArgs.Add("--disable-web-security", ""); setting.IgnoreCertificateErrors = true; setting.LogSeverity = LogSeverity.Verbose; setting.MultiThreadedMessageLoop = true; if (Cef.IsInitialized == false) { Cef.Initialize(setting, performDependencyCheck: false, browserProcessHandler: null); } webview.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true; webview.JavascriptObjectRepository.Settings.JavascriptBindingApiEnabled = true; webview.LoadError += webviewLoadError; webview.LoadingStateChanged += webviewStateChange; webview.AddressChanged += new EventHandler(webviewAddressChange); webview.JavascriptObjectRepository.ResolveObject += (s, eve) => { var repo = eve.ObjectRepository; //eve.ObjectName对应html里面绑定的bindName if (eve.ObjectName == "efunboxJS") { repo.Register("efunboxJS", new JSInterfaces(mView), isAsync: true, options: BindingOptions.DefaultBinder); } // else if (eve.ObjectName == "videoJS") // { // repo.Register("videoJS", new VideoJs(), isAsync: true, options: BindingOptions.DefaultBinder); // } }; //学有义方地址 String url = "http://m-xyyf-web.ai160.com/stage/index/index.html?"; //自己的测试地址 //String url = "F:\\Work_Space\\VisualStudio_Space\\efunbox_xyyf_windows\\efunbox-xyyf-windwos-master\\efunbox_xyyf_windows\\HtmlTestPage\\test.html?"; //video测试地址 //String url = "https://www.runoob.com/html/html5-video.html?"; //是否支持h264测试地址 //String url = "http://html5test.com?"; StringBuilder paramss = new StringBuilder(); paramss.Append("uuid=" + ComputerUniqueHelper.GetComputerUUID()); paramss.Append("&"); paramss.Append("appCode=2006"); paramss.Append("&"); paramss.Append("lenovoId=" + LenvSDK.mLenvUserInfo.data.lenovoId); url = url + paramss.ToString(); Console.WriteLine("加载地址:" + url); webview.LoadUrl(url); webview.MenuHandler = new MenuHandler(); webview.DragHandler = new DragHandler(); webview.Width = 1280; webview.Height = 800; webview.BackColor = Color.Gray; webview.Dock = DockStyle.Fill; } /* * 获取webview地址 * **/ public string getWebUrl() { return mLoadNowUrl; } /* * * 根据token获取联想ID * **/ public void getLenvIdByToken() { if (!TextUtils.isEmpty(LenvSDK.LevnToken)) { //获取token成功了,换取lenvId string timestamp = DateUtil.getNowTimeStamp(); string runNonce = TextUtils.GetRandomCharacters(); Dictionary requestData = new Dictionary(); requestData.Add("appId", Consts.DEFAULT.getLenvAppId()); requestData.Add("mchId", Consts.DEFAULT.getLenvMchId()); requestData.Add("nonce", runNonce); requestData.Add("timestamp", timestamp); requestData.Add("version", "2.0"); requestData.Add("signType", "RSA2"); string paixu = LenvSDK.getParamSrc(requestData); string signString = SHA256WithRSAHelper.Sign(paixu, Consts.DEFAULT.getLenvPrivKey()); requestData.Add("sign", signString); //转成json string json = JsonConvert.SerializeObject(requestData); WebHeaderCollection headrs = new WebHeaderCollection(); headrs.Add("token", LenvSDK.LevnToken); headrs.Add("realm", "pcapp.lenovomm.com"); HttpItem httpItem = new HttpItem(LenvSDK.getIdByTokenUrl, requestData: json) { Header = headrs, Method = RequestMethod.POST, ContentType = HttpContentType.APPLICATION_JSON }; HttpResult httpResult = mModel.getLenvIdByToken(httpItem); String result = ""; if (httpResult.Status) { result = httpResult.HttpStringData; LenvSDK.mLenvUserInfo = JsonConvert.DeserializeObject(result); mView.getLenvIdSuccess(); } else { result = "error:" + httpResult.Msg; MessageBox.Show(result); getLenvIdByToken(); } } else { MessageBox.Show("联想token为空"); } } /* * * 自动登录联想逻辑 * **/ public void LenvAutoLogin() { Dictionary dict = new Dictionary(); dict.Add("appid", Consts.DEFAULT.getLenvAppId()); dict.Add("mchId", Consts.DEFAULT.getLenvMchId()); dict.Add("key", Consts.DEFAULT.getLenvPrivKey()); string Contentjson = JsonConvert.SerializeObject(dict); IntPtr loginResult = LenvSDK.LenvLogin(Contentjson, false); Console.WriteLine("loginResult:" + loginResult); if (loginResult == IntPtr.Zero) { if (LenvSDK.LenvLoginStatus() == 1) { mView.getLoadingBar().Value = 30; //登录成功,获取token int token = LenvSDK.LenvGetToekn(); while (true) { token = LenvSDK.LenvGetToekn(); if (token == 0) { mView.getLoadingBar().Value = 70; //获取token成功,然后根据token获取lenvId getLenvIdByToken(); break; } else { MessageBox.Show("获取联想token失败:" + token); } } } else { LenvAutoLogin(); } } else { MessageBox.Show("登陆失败"); LenvAutoLogin(); } } //webview加载错误 private void webviewLoadError(object sender, CefSharp.LoadErrorEventArgs e) { MessageBox.Show("load-error"); } /* * webview状态改变 * **/ private void webviewStateChange(object sender, CefSharp.LoadingStateChangedEventArgs e) { } /* * webview当前加载地址 * **/ private void webviewAddressChange(object sender, AddressChangedEventArgs e) { //e.Address就是网址 mLoadNowUrl = e.Address; } } }