123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- public class GatewayHttp : MonoBehaviour
- {
- string baseUrl = "https://api-cs.ghz-tech.com/";
- string username = "unity";
- string password = "unitylisten";
- Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- // Start is called before the first frame update
- void Start()
- {
- Debug.Log("Start Queue SendHttp Start" + Application.version);
- Dictionary<string, string> data = new Dictionary<string, string>();
- data.Add("username", username);
- data.Add("password", password);
- StartCoroutine(SendHttp("nacos/v1/auth/users/login", data, (string json) => {
- Debug.Log(json);
- JsonData jdata = JsonMapper.ToObject(json);
- Dictionary<string, string> data2 = new Dictionary<string, string>();
- string pname = Application.productName.Replace(" ", "");
- data2.Add("dataId", Application.version);
- data2.Add("group", pname);
- Debug.Log("login==>" + pname);
- Debug.Log("login==>" + Application.version);
- data2.Add("accessToken", jdata["accessToken"].ToString());
- StartCoroutine(SendHttp("nacos/v1/cs/configs", data, (string json2) =>
- {
- JsonData backData = JsonMapper.ToObject(json2);
- // HttpSDKAction.Instance.jsonData = json2;
- string pt = "pro";
- #if UNITY_EDITOR
- pt = "dev";
- #endif
- NetWorkHeadersConfig.Remote_Net_url = backData["unity"]["url"][pt]["login"].ToString();
- NetWorkHeadersConfig.Remote_Net_url = backData["unity"]["url"][pt]["manager"].ToString();
- NetWorkHeadersConfig.Remote_SocketHall_url = backData["unity"]["url"][pt]["officeSocket"].ToString();
- // NetWorkHeadersConfig.pointCloudUrl = backData["unity"]["url"][pt]["pointCloud"].ToString();
- NetWorkHeadersConfig.Remote_SocketRtc_url = backData["unity"]["url"][pt]["roomSocket"].ToString();
- }, false));
- }));
- }
- void test()
- {
- JsonData data = new JsonData();
- data["unity"] = new JsonData();
- data["unity"]["url"] = new JsonData();
- data["unity"]["devuser"] = new JsonData();
- data["unity"]["url"]["dev"] = new JsonData();
- data["unity"]["url"]["fat"] = new JsonData();
- data["unity"]["url"]["pro"] = new JsonData();
- data["unity"]["url"]["dev"]["login"] = "";
- data["unity"]["url"]["dev"]["manager"] = "";
- data["unity"]["url"]["dev"]["pointCloud"] = "";
- data["unity"]["url"]["dev"]["roomSocket"] = "";
- data["unity"]["url"]["dev"]["officeSocket"] = "";
- data["unity"]["url"]["fat"]["login"] = "";
- data["unity"]["url"]["fat"]["manager"] = "";
- data["unity"]["url"]["fat"]["pointCloud"] = "";
- data["unity"]["url"]["fat"]["roomSocket"] = "";
- data["unity"]["url"]["fat"]["officeSocket"] = "";
- data["unity"]["url"]["pro"]["login"] = "";
- data["unity"]["url"]["pro"]["manager"] = "";
- data["unity"]["url"]["pro"]["pointCloud"] = "";
- data["unity"]["url"]["pro"]["roomSocket"] = "";
- data["unity"]["url"]["pro"]["officeSocket"] = "";
- data["unity"]["user"]["111@111.com"] = "dev";
- Debug.Log(data.ToJson());
- }
- public void initHead()
- {
- requestHeader.Clear();
- requestHeader.Add("Content-Type", "application/x-www-form-urlencoded");
- }
- public IEnumerator SendHttp(string methodName, Dictionary<string, string> fromDic, Action<string> CallBack, bool isPost = true)
- {
- Debug.Log("Start Queue SendHttp " + methodName);
- string url = baseUrl + methodName;
- UnityWebRequest webRequest;
- if (!isPost)
- {
- url += "?";
- foreach (var item in fromDic)
- {
- url += item.Key + "=" + item.Value + "&";
- }
- webRequest = UnityWebRequest.Get(url);
- }
- else
- {
- WWWForm form = new WWWForm();
- foreach (var item in fromDic)
- {
- form.AddField(item.Key, item.Value);
- }
- webRequest = UnityWebRequest.Post(url, form);
- webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.error !="")
- {
- Debug.LogError(webRequest.error);
- }
- else
- {
- string result = webRequest.downloadHandler.text;
- CallBack(result);
- }
- }
- }
|