123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine.Networking;
- using System;
- using LitJson;
- using UnityEngine;
- using Newtonsoft.Json;
- public class WebManager : MonoBehaviour
- {
- public static WebManager GetInstance;
- public void Awake()
- {
- GetInstance = this;
- }
- Action mlogincomplete;
- public void Login(Action logincomplete)
- {
- mlogincomplete = logincomplete;
- var url = PROTOCOLS.URL + PROTOCOLS.Login;
- var code = MainAccountData.code;
- var nickname = MainAccountData.NickName;
- var avatar = MainAccountData.Avatar;
- var gender = MainAccountData.Gender;
- var country = MainAccountData.Country;
- var province = MainAccountData.Province;
- var city = MainAccountData.City;
- var device = MainAccountData.Device;
- var share_id = MainAccountData.ShareId;
- var scene = MainAccountData.Scene;
- var v = MainAccountData.ver;
- var args =
- "code=" + code + "&" +
- "nickname=" + nickname + "&" +
- "avatar=" + avatar + "&" +
- "gender=" + gender + "&" +
- "country=" + country + "&" +
- "province=" + province + "&" +
- "city=" + city + "&" +
- "device=" + device + "&" +
- "share_id=" + share_id + "&" +
- "scene=" + scene + "&" +
- "v=" + v;
- Debug.Log("--->>µÇ½¿ªÊ¼" + code);
- StartCoroutine(PostRequest(PROTOCOLS.Login, args, LoginCallback, null));
- }
- private void LoginCallback(JsonData jsonData)
- {
- //Debug.Log("LoadXJ_Completecb");
- //Debug.Log(_text);
- new MainAccountData(jsonData);
- MainAccountData.Instance.JundgCfg(jsonData["table_version"].ToString(), () =>
- {
- if (mlogincomplete != null) mlogincomplete();
- });
- }
- public void HttpPostRequest_Send(string protocol, Dictionary<string, string> param, System.Action<JsonData> completecb, System.Action<JsonData> errorcb, bool hasToken = true)
- {
- var jsonString = "";
- if (hasToken)
- {
- jsonString = "token=" + MainAccountData.Instance.token + "&";
- }
- if (protocol == PROTOCOLS.game || protocol == PROTOCOLS.end || protocol == PROTOCOLS.fission || protocol == PROTOCOLS.vd || protocol == PROTOCOLS.themefission || protocol == PROTOCOLS.revival || protocol == PROTOCOLS.Sign || protocol == PROTOCOLS.Buy)
- {
- string _newStr = "abcdefghijkimnopqrstuvwxyz0123456789";
- string _code = "{";
- string _mask = "";
- for (int i = 0; i < 4; i++)
- {
- _mask += _newStr[UnityEngine.Random.Range(0, 100) % _newStr.Length];
- }
- int _newIndex = 0;
- if (param != null)
- {
- foreach (var item in param)
- {
- _newIndex++;
- string key = item.Key;
- string value = item.Value;
- _code += "\"" + key + "\":\"" + value + "\"";
- if (_newIndex < param.Count)
- {
- _code += ",";
- }
- else
- {
- _code += "}";
- }
- }
- }
- jsonString += "_r=" + _mask + this.base64Encode(_code);
- }
- else
- {
- if (param != null)
- {
- var _index = 0;
- foreach (var item in param)
- {
- _index++;
- string key = item.Key;
- string value = item.Value;
- jsonString += key + "=" + value;
- if (_index < param.Count)
- {
- jsonString += "&";
- }
- }
- }
- }
- StartCoroutine(this.PostRequest(protocol, jsonString, completecb, errorcb)); ;
- }
- private IEnumerator PostRequest(string protocol, string jsonString, Action<JsonData> completecb, Action<JsonData> errorcb)
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- string url = PROTOCOLS.URL + protocol + "?" + jsonString;
- Debug.Log("PostRequest--->>>url-->" + url);
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(), new UploadHandlerRaw(bodyRaw)))
- {
- webRequest.SetRequestHeader("Content-Type", "application/json");
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
- if (errorcb != null)
- {
- this.onHttpRequestErrorcb(webRequest.downloadHandler.text, errorcb);
- }
- }
- else
- {
- if (completecb != null)
- {
- this.onHttpRequestComplete(webRequest.downloadHandler.text, completecb, errorcb);
- }
- }
- }
- }
- private void onHttpRequestComplete(string _text, Action<JsonData> completecb, Action<JsonData> errorcb)
- {
- //JObject obj = JsonConvert.DeserializeObject(_text);
- JsonData valuePairs = JsonMapper.ToObject(_text);
- if (valuePairs["code"].ToString() != "0")
- {
- Debug.Log("Error! Http request complete error" + valuePairs["message"]);
- if (errorcb != null)
- {
- errorcb(valuePairs);
- }
- return;
- }
- if (completecb != null)
- {
- Debug.Log(_text);
- completecb(valuePairs);
- }
- }
- private void onHttpRequestErrorcb(string _text, Action<JsonData> errorcb)
- {
- JsonData valuePairs = JsonMapper.ToObject(_text);
- Debug.Log("Error! Http request error..." + valuePairs["message"]);
- if (errorcb != null)
- {
- errorcb(valuePairs);
- }
- }
- private string base64Encode(string str)
- {
- Debug.Log("base64Encode-->>" + str);
- int c1, c2, c3;
- string _base64Encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- char[] base64EncodeChars = _base64Encode.ToCharArray();
- char[] strChars = str.ToCharArray();
- int i = 0;
- int len = str.Length;
- string stringStr = "";
- while (i < len)
- {
- c1 = strChars[i++] & 0xff;
- if (i == len)
- {
- stringStr += base64EncodeChars[c1 >> 2];
- stringStr += base64EncodeChars[(c1 & 0x3) << 4];
- stringStr += "==";
- break;
- }
- c2 = str[i++];
- if (i == len)
- {
- stringStr += base64EncodeChars[c1 >> 2];
- stringStr += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
- stringStr += base64EncodeChars[(c2 & 0xF) << 2];
- stringStr += "=";
- break;
- }
- c3 = str[i++];
- stringStr += base64EncodeChars[c1 >> 2];
- stringStr += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)];
- stringStr += base64EncodeChars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)];
- stringStr += base64EncodeChars[c3 & 0x3F];
- }
- Debug.Log(stringStr.Length + " " + stringStr);
- return stringStr;
- }
- void OnHpptError()
- {
- }
- }
|