123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- 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.IView>, 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<AddressChangedEventArgs>(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<String, String> requestData = new Dictionary<String, String>();
- 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<LenvUserInfo>(result);
- mView.getLenvIdSuccess();
- }
- else
- {
- result = "error:" + httpResult.Msg;
- MessageBox.Show(result);
- getLenvIdByToken();
- }
- }
- else
- {
- MessageBox.Show("联想token为空");
- }
- }
- /*
- *
- * 自动登录联想逻辑
- * **/
- public void LenvAutoLogin()
- {
- Dictionary<String, String> dict = new Dictionary<String, String>();
- 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;
- }
- }
- }
|