2022-02-16 18:22:27 +08:00
|
|
|
|
using Assets.Scripts;
|
|
|
|
|
|
using Assets.Scripts.Apis.Models;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
2022-02-11 17:26:36 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
2022-02-16 18:22:27 +08:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
using ZenFulcrum.EmbeddedBrowser;
|
|
|
|
|
|
#endif
|
2022-02-11 17:26:36 +08:00
|
|
|
|
public class ActivityController : PFUIPanel
|
|
|
|
|
|
{
|
|
|
|
|
|
Transform browser;
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
Browser webView;
|
|
|
|
|
|
#else
|
2022-02-16 18:22:27 +08:00
|
|
|
|
UniWebView webView;
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#endif
|
2022-02-11 17:26:36 +08:00
|
|
|
|
protected override void Awake()
|
|
|
|
|
|
{
|
2022-02-22 18:11:26 +08:00
|
|
|
|
this.url = null;
|
2022-02-11 17:26:36 +08:00
|
|
|
|
browser = transform.Find("Browser");
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
webView = browser.GetComponent<Browser>();
|
|
|
|
|
|
webView.onLoad -= RegisterFunc;
|
|
|
|
|
|
webView.onLoad += RegisterFunc;
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
webView.ShowDevTools(true);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#else
|
2022-02-16 18:22:27 +08:00
|
|
|
|
webView = browser.GetComponent<UniWebView>();
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#endif
|
2022-02-16 18:22:27 +08:00
|
|
|
|
//webView.Show();
|
2022-02-11 17:26:36 +08:00
|
|
|
|
}
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
private void RegisterFunc(JSONNode obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
RunJavaScript($"navigator.pfLang = '{App.GetLocalLanguage()}';");
|
|
|
|
|
|
webView.RegisterFunction("Close", args =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("执行返回");
|
|
|
|
|
|
CloseFunc();
|
|
|
|
|
|
});
|
|
|
|
|
|
webView.RegisterFunction("StartRide", args =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Count == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartRide((int)args[0], args[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
webView.RegisterFunction("OpenUrl", args =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Count == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
OpenUrl(args[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
webView.RegisterFunction("Share", args =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Count == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
Share((int)args[0], (int)args[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
2022-02-11 17:26:36 +08:00
|
|
|
|
public override void Show()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Show();
|
2022-02-16 18:22:27 +08:00
|
|
|
|
//Debug.Log(20);
|
|
|
|
|
|
//Invoke("StartPageFunc",0.5f);
|
2022-02-11 17:26:36 +08:00
|
|
|
|
}
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
private void StartPageFunc(Action action = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (action == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
webView.LoadURL(url, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
action.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
2022-02-18 18:12:31 +08:00
|
|
|
|
private void StartPageFunc(Action action = null)
|
2022-02-11 17:26:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("开始");
|
|
|
|
|
|
// Load a URL.
|
2022-02-17 16:05:40 +08:00
|
|
|
|
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
|
|
|
|
|
|
webView.ReferenceRectTransform = browser.GetComponent<RectTransform>();
|
|
|
|
|
|
webView.SetUserAgent($"UniWebView {Application.platform} {Application.version}");
|
|
|
|
|
|
webView.BackgroundColor = Utils.HexToColorHtml("#23232d");
|
2022-02-18 18:12:31 +08:00
|
|
|
|
if (action == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
webView.Load(url);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
action.Invoke();
|
|
|
|
|
|
}
|
2022-02-11 17:26:36 +08:00
|
|
|
|
// Show it.
|
|
|
|
|
|
webView.Show();
|
|
|
|
|
|
webView.OnMessageReceived -= FromJs;
|
|
|
|
|
|
webView.OnMessageReceived += FromJs;
|
|
|
|
|
|
}
|
|
|
|
|
|
private void FromJs(UniWebView webView, UniWebViewMessage message)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (message.Path)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "Close": CloseFunc(); break;
|
2022-02-22 18:11:26 +08:00
|
|
|
|
case "StartRide": StartRide(int.Parse(message.Args["id"]),message.Args.ContainsKey("routeResult")?message.Args["routeResult"]:null); break;
|
|
|
|
|
|
case "Share": Share(int.Parse(message.Args["type"]),int.Parse(message.Args["id"])); break;
|
|
|
|
|
|
case "OpenUrl": OpenUrl(message.Args["url"]); break;
|
2022-02-11 17:26:36 +08:00
|
|
|
|
default:break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#endif
|
2022-02-16 18:22:27 +08:00
|
|
|
|
|
2022-02-22 18:11:26 +08:00
|
|
|
|
private void OpenUrl(string url)
|
2022-02-17 16:05:40 +08:00
|
|
|
|
{
|
2022-02-22 18:11:26 +08:00
|
|
|
|
Application.OpenURL(System.Net.WebUtility.UrlDecode(url));
|
2022-02-17 16:05:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-22 18:11:26 +08:00
|
|
|
|
private void Share(int type,int id)
|
2022-02-16 18:22:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
string url = $"http://192.168.0.101:3081/Activity/LightUpChina/Share?userId={App.CurrentUser.Id}";
|
|
|
|
|
|
if (type < 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (App.weChatController.IsWeChatAppInstalled())
|
|
|
|
|
|
{
|
|
|
|
|
|
App.weChatController.ShareWebpageToWX(type, url, "点亮中国活动", "by " + App.CurrentUser.Nickname, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-02-22 18:11:26 +08:00
|
|
|
|
else if (type == 2)
|
2022-02-16 18:22:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
UnityEngine.GUIUtility.systemCopyBuffer = url;
|
2022-02-18 18:12:31 +08:00
|
|
|
|
RunJavaScript("window.postMessage(\"webview;复制成功\")");
|
2022-02-16 18:22:27 +08:00
|
|
|
|
}
|
2022-02-22 18:11:26 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Facebook.Unity.FB.ShareLink(contentURL: new Uri(url),
|
|
|
|
|
|
contentTitle: "点亮中国活动",
|
|
|
|
|
|
contentDescription: "By " + App.CurrentUser.Nickname,
|
|
|
|
|
|
photoURL: new Uri(App.CurrentUser.WxHeadImg));
|
|
|
|
|
|
}
|
2022-02-16 18:22:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-22 18:11:26 +08:00
|
|
|
|
private void StartRide(int id,string routeResult)
|
2022-02-16 18:22:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
App.RouteIdParam = id;
|
2022-02-22 18:11:26 +08:00
|
|
|
|
if (routeResult!=null)
|
2022-02-16 18:22:27 +08:00
|
|
|
|
{
|
2022-02-22 18:11:26 +08:00
|
|
|
|
App.routeResult = JsonConvert.DeserializeObject<RouteResult>(routeResult);
|
2022-02-16 18:22:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
SceneManager.LoadScene("Ride");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-11 17:26:36 +08:00
|
|
|
|
private void CloseFunc()
|
|
|
|
|
|
{
|
|
|
|
|
|
CancelInvoke("StartPageFunc");
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
2022-02-18 18:12:31 +08:00
|
|
|
|
|
|
|
|
|
|
private void RunJavaScript(string js)
|
|
|
|
|
|
{
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
webView.EvalJS(js);
|
|
|
|
|
|
#else
|
2022-02-18 18:12:31 +08:00
|
|
|
|
browser.GetComponent<UniWebView>().EvaluateJavaScript(js);
|
2022-02-22 18:11:26 +08:00
|
|
|
|
#endif
|
2022-02-18 18:12:31 +08:00
|
|
|
|
}
|
2022-02-11 17:26:36 +08:00
|
|
|
|
// Update is called once per frame
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-02-16 18:22:27 +08:00
|
|
|
|
string url { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public void Initial(string url)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (this.url != url)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.url = url;
|
|
|
|
|
|
StartPageFunc();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//执行浏览器内刷新或者其他操作
|
2022-02-18 18:12:31 +08:00
|
|
|
|
StartPageFunc(()=>
|
|
|
|
|
|
{
|
|
|
|
|
|
RunJavaScript("window.postMessage(\"webview;Refresh\")");
|
|
|
|
|
|
});
|
2022-02-16 18:22:27 +08:00
|
|
|
|
}
|
2022-02-22 18:11:26 +08:00
|
|
|
|
|
2022-02-16 18:22:27 +08:00
|
|
|
|
}
|
2022-02-11 17:26:36 +08:00
|
|
|
|
}
|