MainPresenter.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using CefSharp;
  2. using CefSharp.WinForms;
  3. using efunbox_xyyf_windows.baseMvp;
  4. using efunbox_xyyf_windows.bean;
  5. using efunbox_xyyf_windows.contract;
  6. using efunbox_xyyf_windows.cusview;
  7. using efunbox_xyyf_windows.model;
  8. using efunbox_xyyf_windows.util;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using static efunbox_xyyf_windows.model.MainModel;
  19. namespace efunbox_xyyf_windows.presenter
  20. {
  21. public class MainPresenter : BasePresenter<MainContract.IView>, MainContract.IPresenter
  22. {
  23. private MainModel mModel;
  24. private string mLoadNowUrl;
  25. public MainPresenter()
  26. {
  27. mModel = new MainModel();
  28. }
  29. /*
  30. * 初始化webview
  31. *
  32. */
  33. public void initWebView(ChromiumWebBrowser webview)
  34. {
  35. var setting = new CefSettings();
  36. setting.Locale = "zh-CN";
  37. setting.AcceptLanguageList = "zh-CN,zh;q=0.8";
  38. setting.CefCommandLineArgs.Add("enable-system-flash", "1"); //启用flash
  39. setting.CefCommandLineArgs.Add("enable-media-stream", "1"); //启用媒体流
  40. setting.CefCommandLineArgs.Add("--disable-web-security", "");
  41. setting.IgnoreCertificateErrors = true;
  42. setting.LogSeverity = LogSeverity.Verbose;
  43. setting.MultiThreadedMessageLoop = true;
  44. if (Cef.IsInitialized == false)
  45. {
  46. Cef.Initialize(setting, performDependencyCheck: false, browserProcessHandler: null);
  47. }
  48. webview.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
  49. webview.JavascriptObjectRepository.Settings.JavascriptBindingApiEnabled = true;
  50. webview.LoadError += webviewLoadError;
  51. webview.LoadingStateChanged += webviewStateChange;
  52. webview.AddressChanged += new EventHandler<AddressChangedEventArgs>(webviewAddressChange);
  53. webview.JavascriptObjectRepository.ResolveObject += (s, eve) =>
  54. {
  55. var repo = eve.ObjectRepository;
  56. //eve.ObjectName对应html里面绑定的bindName
  57. if (eve.ObjectName == "efunboxJS")
  58. {
  59. repo.Register("efunboxJS", new JSInterfaces(mView), isAsync: true, options: BindingOptions.DefaultBinder);
  60. }
  61. // else if (eve.ObjectName == "videoJS")
  62. // {
  63. // repo.Register("videoJS", new VideoJs(), isAsync: true, options: BindingOptions.DefaultBinder);
  64. // }
  65. };
  66. //学有义方地址
  67. String url = "http://m-xyyf-web.ai160.com/stage/index/index.html?";
  68. //自己的测试地址
  69. //String url = "F:\\Work_Space\\VisualStudio_Space\\efunbox_xyyf_windows\\efunbox-xyyf-windwos-master\\efunbox_xyyf_windows\\HtmlTestPage\\test.html?";
  70. //video测试地址
  71. //String url = "https://www.runoob.com/html/html5-video.html?";
  72. //是否支持h264测试地址
  73. //String url = "http://html5test.com?";
  74. StringBuilder paramss = new StringBuilder();
  75. paramss.Append("uuid=" + ComputerUniqueHelper.GetComputerUUID());
  76. paramss.Append("&");
  77. paramss.Append("appCode=2006");
  78. paramss.Append("&");
  79. paramss.Append("lenovoId=" + LenvSDK.mLenvUserInfo.data.lenovoId);
  80. url = url + paramss.ToString();
  81. Console.WriteLine("加载地址:" + url);
  82. webview.LoadUrl(url);
  83. webview.MenuHandler = new MenuHandler();
  84. webview.DragHandler = new DragHandler();
  85. webview.Width = 1280;
  86. webview.Height = 800;
  87. webview.BackColor = Color.Gray;
  88. webview.Dock = DockStyle.Fill;
  89. }
  90. /*
  91. * 获取webview地址
  92. * **/
  93. public string getWebUrl()
  94. {
  95. return mLoadNowUrl;
  96. }
  97. /*
  98. *
  99. * 根据token获取联想ID
  100. * **/
  101. public void getLenvIdByToken()
  102. {
  103. if (!TextUtils.isEmpty(LenvSDK.LevnToken))
  104. {
  105. //获取token成功了,换取lenvId
  106. string timestamp = DateUtil.getNowTimeStamp();
  107. string runNonce = TextUtils.GetRandomCharacters();
  108. Dictionary<String, String> requestData = new Dictionary<String, String>();
  109. requestData.Add("appId", Consts.DEFAULT.getLenvAppId());
  110. requestData.Add("mchId", Consts.DEFAULT.getLenvMchId());
  111. requestData.Add("nonce", runNonce);
  112. requestData.Add("timestamp", timestamp);
  113. requestData.Add("version", "2.0");
  114. requestData.Add("signType", "RSA2");
  115. string paixu = LenvSDK.getParamSrc(requestData);
  116. string signString = SHA256WithRSAHelper.Sign(paixu, Consts.DEFAULT.getLenvPrivKey());
  117. requestData.Add("sign", signString);
  118. //转成json
  119. string json = JsonConvert.SerializeObject(requestData);
  120. WebHeaderCollection headrs = new WebHeaderCollection();
  121. headrs.Add("token", LenvSDK.LevnToken);
  122. headrs.Add("realm", "pcapp.lenovomm.com");
  123. HttpItem httpItem = new HttpItem(LenvSDK.getIdByTokenUrl, requestData: json)
  124. {
  125. Header = headrs,
  126. Method = RequestMethod.POST,
  127. ContentType = HttpContentType.APPLICATION_JSON
  128. };
  129. HttpResult httpResult = mModel.getLenvIdByToken(httpItem);
  130. String result = "";
  131. if (httpResult.Status)
  132. {
  133. result = httpResult.HttpStringData;
  134. LenvSDK.mLenvUserInfo = JsonConvert.DeserializeObject<LenvUserInfo>(result);
  135. mView.getLenvIdSuccess();
  136. }
  137. else
  138. {
  139. result = "error:" + httpResult.Msg;
  140. MessageBox.Show(result);
  141. getLenvIdByToken();
  142. }
  143. }
  144. else
  145. {
  146. MessageBox.Show("联想token为空");
  147. }
  148. }
  149. /*
  150. *
  151. * 自动登录联想逻辑
  152. * **/
  153. public void LenvAutoLogin()
  154. {
  155. Dictionary<String, String> dict = new Dictionary<String, String>();
  156. dict.Add("appid", Consts.DEFAULT.getLenvAppId());
  157. dict.Add("mchId", Consts.DEFAULT.getLenvMchId());
  158. dict.Add("key", Consts.DEFAULT.getLenvPrivKey());
  159. string Contentjson = JsonConvert.SerializeObject(dict);
  160. IntPtr loginResult = LenvSDK.LenvLogin(Contentjson, false);
  161. Console.WriteLine("loginResult:" + loginResult);
  162. if (loginResult == IntPtr.Zero)
  163. {
  164. if (LenvSDK.LenvLoginStatus() == 1)
  165. {
  166. mView.getLoadingBar().Value = 30;
  167. //登录成功,获取token
  168. int token = LenvSDK.LenvGetToekn();
  169. while (true)
  170. {
  171. token = LenvSDK.LenvGetToekn();
  172. if (token == 0)
  173. {
  174. mView.getLoadingBar().Value = 70;
  175. //获取token成功,然后根据token获取lenvId
  176. getLenvIdByToken();
  177. break;
  178. }
  179. else
  180. {
  181. MessageBox.Show("获取联想token失败:" + token);
  182. }
  183. }
  184. }
  185. else
  186. {
  187. LenvAutoLogin();
  188. }
  189. }
  190. else
  191. {
  192. MessageBox.Show("登陆失败");
  193. LenvAutoLogin();
  194. }
  195. }
  196. //webview加载错误
  197. private void webviewLoadError(object sender, CefSharp.LoadErrorEventArgs e)
  198. {
  199. MessageBox.Show("load-error");
  200. }
  201. /*
  202. * webview状态改变
  203. * **/
  204. private void webviewStateChange(object sender, CefSharp.LoadingStateChangedEventArgs e)
  205. {
  206. }
  207. /*
  208. * webview当前加载地址
  209. * **/
  210. private void webviewAddressChange(object sender, AddressChangedEventArgs e)
  211. {
  212. //e.Address就是网址
  213. mLoadNowUrl = e.Address;
  214. }
  215. }
  216. }