321 lines
11 KiB
C#
321 lines
11 KiB
C#
using System.Net;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Http;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using Debug = System.Diagnostics.Debug;
|
|
|
|
namespace BF
|
|
{
|
|
public class BFParseClientManager : ManagerBase
|
|
{
|
|
public Action<string> luaCheckPurchaseOrderCallback;
|
|
public Action<string> luaCheckPurchaseConsumeCallback;
|
|
public Action<string> luaFirstRequestCallback;
|
|
public Action<string> luaCheckPurchaseIOSOrderCallback;
|
|
public Action<string> luaCheckPurchaseIOSConsumeCallback;
|
|
|
|
static BFParseClientManager instance;
|
|
public static BFParseClientManager Create()
|
|
{
|
|
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
|
|
instance = new BFParseClientManager();
|
|
return instance;
|
|
}
|
|
BFParseClientManager() { }
|
|
|
|
public override void Init()
|
|
{
|
|
base.Init();
|
|
#if UNITY_EDITOR
|
|
HttpManager.Instance.SetServerUrl("http://game.juzugame.com:1337/parse/");
|
|
HttpManager.Instance.SetParseRevocableSession("1");
|
|
HttpManager.Instance.SetParseApplicationId("1");
|
|
HttpManager.Instance.SetParseClientKey("1kYT4UrExyQ0gaFh");
|
|
#else
|
|
// HttpManager.Instance.SetServerUrl("http://34.233.204.253:1337/parse/");
|
|
// HttpManager.Instance.SetParseRevocableSession("1");
|
|
// HttpManager.Instance.SetParseApplicationId("1");
|
|
// HttpManager.Instance.SetParseClientKey("1kYT4UrExyQ0gaFh");
|
|
HttpManager.Instance.SetServerUrl("https://b2.bigfoot-studio.link/parse/");
|
|
HttpManager.Instance.SetParseRevocableSession("1");
|
|
HttpManager.Instance.SetParseApplicationId("101");
|
|
HttpManager.Instance.SetParseClientKey("Xxl3j5TBIn4ArY1m");
|
|
#endif
|
|
}
|
|
|
|
public void SetServerUrl(string url, string psarseRevocableSession = "1", string parseApplicationId = "1", string parseClientKey = "1kYT4UrExyQ0gaFh")
|
|
{
|
|
HttpManager.Instance.SetServerUrl(url);
|
|
HttpManager.Instance.SetParseRevocableSession(psarseRevocableSession);
|
|
HttpManager.Instance.SetParseApplicationId(parseApplicationId);
|
|
HttpManager.Instance.SetParseClientKey(parseClientKey);
|
|
}
|
|
|
|
public void SetLuaCheckPurchaseOrderCallback(Action<string> callback)
|
|
{
|
|
luaCheckPurchaseOrderCallback = callback;
|
|
}
|
|
|
|
public void SetLuaCheckPurchaseConsumeCallback(Action<string> callback)
|
|
{
|
|
luaCheckPurchaseConsumeCallback = callback;
|
|
}
|
|
public void SetLuaCheckPurchaseIOSOrderCallback(Action<string> callback)
|
|
{
|
|
luaCheckPurchaseIOSOrderCallback = callback;
|
|
}
|
|
|
|
public void SetLuaCheckPurchaseIOSConsumeCallback(Action<string> callback)
|
|
{
|
|
luaCheckPurchaseIOSConsumeCallback = callback;
|
|
}
|
|
public void SetLuaFirstRequestCallback(Action<string> callback)
|
|
{
|
|
luaFirstRequestCallback = callback;
|
|
}
|
|
|
|
public void UserLogin(string loginiInfo, Action<string> complete)
|
|
{
|
|
UsersRequest.Instance.Users(loginiInfo, s =>
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
//错误
|
|
return;
|
|
}
|
|
|
|
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(s);
|
|
Debug.Assert(data != null, nameof(data) + " != null");
|
|
if (!data.TryGetValue("sessionToken", out var sessionToken)) return;
|
|
var token = sessionToken as string;
|
|
// ttToken.text = $"{token}";
|
|
HttpManager.Instance.SetToken(token);
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
public void UserTokenLogin(string token, string loginiInfo, Action<string> complete)
|
|
{
|
|
UsersRequest.Instance.UsersTokenLogin(token, loginiInfo, s =>
|
|
{
|
|
if (!string.IsNullOrEmpty(s))
|
|
{
|
|
var data = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(s);
|
|
Debug.Assert(data != null, nameof(data) + " != null");
|
|
if (!data.TryGetValue("result", out Dictionary<string, object> result)) return;
|
|
|
|
if (!result.TryGetValue("sessionToken", out var sessionToken)) return;
|
|
|
|
var token = sessionToken as string;
|
|
// ttToken.text = $"{token}";
|
|
HttpManager.Instance.SetToken(token);
|
|
}
|
|
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
public void UsersPost(string key, string body, Action<string> complete)
|
|
{
|
|
UsersRequest.Instance.UsersPost(key, body, s =>
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
//错误
|
|
return;
|
|
}
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
public void UsersGet(string key, string body, Action<string> complete)
|
|
{
|
|
UsersRequest.Instance.UsersGet(key, body, s =>
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
//错误
|
|
return;
|
|
}
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
public void UsersPut(string key, string body, Action<string> complete)
|
|
{
|
|
UsersRequest.Instance.UsersPut(key, body, s =>
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
//错误
|
|
return;
|
|
}
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
public void UsersDel(string key, string body, Action<string> complete)
|
|
{
|
|
UsersRequest.Instance.UsersDel(key, body, s =>
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
//错误
|
|
return;
|
|
}
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
public void CheckPurchaseGPOrder(string purchase)
|
|
{
|
|
FunctionRequest.Instance.Invoke("iapValidateGoogle", purchase, s =>
|
|
{
|
|
// var data = JsonConvert.DeserializeObject<Dictionary<string>>(s);
|
|
BFLog.Log("CheckPurchaseGPOrder");
|
|
OnCheckPurchaseGPOrder(s);
|
|
});
|
|
}
|
|
|
|
public void CheckPurchaseGPConsume(string purchase)
|
|
{
|
|
FunctionRequest.Instance.Invoke("iapRecordGoogle", purchase, s =>
|
|
{
|
|
// var data = JsonConvert.DeserializeObject<Dictionary<string>>(s);
|
|
BFLog.Log("CheckPurchaseGPConsume");
|
|
OnCheckPurchaseGPConsume(s);
|
|
});
|
|
}
|
|
|
|
public void FirstRequestAction(string info)
|
|
{
|
|
FunctionRequest.Instance.Invoke("version", info, s =>
|
|
{
|
|
// var data = JsonConvert.DeserializeObject<Dictionary<string>>(s);
|
|
BFLog.Log("FirstRequestAction");
|
|
OnFirstRequestAction(s);
|
|
});
|
|
}
|
|
|
|
public void CheckPurchaseIOSOrder(string purchase)
|
|
{
|
|
FunctionRequest.Instance.Invoke("iapValidateApple", purchase, s =>
|
|
{
|
|
// var data = JsonConvert.DeserializeObject<Dictionary<string>>(s);
|
|
BFLog.Log("CheckPurchaseIOSOrder");
|
|
OnCheckPurchaseGPOrder(s);
|
|
});
|
|
}
|
|
|
|
public void CheckPurchaseIOSConsume(string purchase)
|
|
{
|
|
FunctionRequest.Instance.Invoke("iapRecordApple", purchase, s =>
|
|
{
|
|
// var data = JsonConvert.DeserializeObject<Dictionary<string>>(s);
|
|
BFLog.Log("CheckPurchaseIOSConsume");
|
|
OnCheckPurchaseGPConsume(s);
|
|
});
|
|
}
|
|
|
|
public void OnCheckPurchaseGPOrder(string msg)
|
|
{
|
|
if (luaCheckPurchaseOrderCallback != null)
|
|
{
|
|
luaCheckPurchaseOrderCallback(msg);
|
|
luaCheckPurchaseOrderCallback = null;
|
|
}
|
|
}
|
|
public void OnCheckPurchaseGPConsume(string msg)
|
|
{
|
|
if (luaCheckPurchaseConsumeCallback != null)
|
|
{
|
|
luaCheckPurchaseConsumeCallback(msg);
|
|
luaCheckPurchaseConsumeCallback = null;
|
|
}
|
|
}
|
|
public void OnFirstRequestAction(string msg)
|
|
{
|
|
if (luaFirstRequestCallback != null)
|
|
{
|
|
luaFirstRequestCallback(msg);
|
|
luaFirstRequestCallback = null;
|
|
}
|
|
}
|
|
public void OnCheckPurchaseIOSOrder(string msg)
|
|
{
|
|
if (luaCheckPurchaseIOSOrderCallback != null)
|
|
{
|
|
luaCheckPurchaseIOSOrderCallback(msg);
|
|
luaCheckPurchaseIOSOrderCallback = null;
|
|
}
|
|
}
|
|
public void OnCheckPurchaseIOSConsume(string msg)
|
|
{
|
|
if (luaCheckPurchaseIOSConsumeCallback != null)
|
|
{
|
|
luaCheckPurchaseIOSConsumeCallback(msg);
|
|
luaCheckPurchaseIOSConsumeCallback = null;
|
|
}
|
|
}
|
|
|
|
// Function
|
|
public void FunctionPost(string url, string body, Action<string> complete)
|
|
{
|
|
FunctionRequest.Instance.Invoke(url, body, s =>
|
|
{
|
|
complete(s);
|
|
});
|
|
}
|
|
|
|
// Storage
|
|
public void StorageWrite(string key, string body, Action<string> complete)
|
|
{
|
|
StorageRequest.Instance.Write(key, body, complete);
|
|
}
|
|
|
|
public void StorageRead(string key, string body, Action<string> complete)
|
|
{
|
|
StorageRequest.Instance.Read(key, body, complete);
|
|
}
|
|
|
|
public void StorageUpdate(string key, string body, Action<string> complete)
|
|
{
|
|
StorageRequest.Instance.Update(key, body, complete);
|
|
}
|
|
|
|
public void StorageDelete(string key, string deleteKeys, Action<string> complete)
|
|
{
|
|
StorageRequest.Instance.Delete(key, deleteKeys, complete);
|
|
}
|
|
|
|
public void Put(string url, string body, Action<int, string> complete)
|
|
{
|
|
var http = HttpManager.Instance.Put(url, info => OnResult(info, complete));
|
|
http.AddToken();
|
|
http.AddParameter(body);
|
|
http.Send();
|
|
}
|
|
|
|
private void OnResult(ResultInfo info, Action<int, string> complete)
|
|
{
|
|
if (info.State == RequestState.Success)
|
|
{
|
|
complete?.Invoke(0, info.Response.DataAsText);
|
|
return;
|
|
}
|
|
else if (info.Response != null)
|
|
{
|
|
if (info.Response.StatusCode >= 200 || info.Response.StatusCode < 500)
|
|
{
|
|
complete?.Invoke(0, info.Response.DataAsText);
|
|
return;
|
|
}
|
|
}
|
|
complete?.Invoke(1, string.Empty);
|
|
}
|
|
}
|
|
}
|