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> data = new Dictionary<string, string>();
- string pname = Application.productName.Replace(" ", "");
- data.Add("dataId", Application.version);
- data.Add("group", pname);
- Debug.Log("login==>" + pname);
- Debug.Log("login==>" + Application.version);
- data.Add("accessToken", jdata["accessToken"].ToString());
- StartCoroutine(SendHttp("nacos/v1/cs/configs", data, (string json) =>
- {
- JsonData backData = JsonMapper.ToObject(json);
- HttpSDKAction.Instance.jsonData = json;
- string pt = "pro";
- #if UNITY_EDITOR
- pt = "dev";
- #endif
- HttpSDKAction.Instance.loginUrl = backData["unity"]["url"][pt]["login"].ToString();
- HttpSDKAction.Instance.managerUrl = backData["unity"]["url"][pt]["manager"].ToString();
- HttpSDKAction.Instance.officeSocket = backData["unity"]["url"][pt]["officeSocket"].ToString();
- HttpSDKAction.Instance.pointCloudUrl = backData["unity"]["url"][pt]["pointCloud"].ToString();
- HttpSDKAction.Instance.roomSocket = 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.result == UnityWebRequest.Result.ConnectionError)
- {
- Debug.LogError(webRequest.error);
- }
- else
- {
- string result = webRequest.downloadHandler.text;
- CallBack(result);
- }
- }
- }
|