231 lines
6.6 KiB
C#
231 lines
6.6 KiB
C#
using System;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine;
|
||
using UnityEditor;
|
||
using System.Collections.Generic;
|
||
using Cysharp.Threading.Tasks;
|
||
using UnityEngine.Networking;
|
||
using System.Text;
|
||
|
||
public class GMPartSendMail : GMPart
|
||
{
|
||
CommonResult result;
|
||
private GUIStyle errorStyle;
|
||
|
||
List<AddInfo> addList;
|
||
|
||
List<cfg.ActorCfg.DataItem> cfgList;
|
||
|
||
string [] Names;
|
||
|
||
string title;
|
||
string body;
|
||
string actorID;
|
||
string mailID;
|
||
|
||
public override string GetName()
|
||
{
|
||
return "发送邮件";
|
||
}
|
||
|
||
public override void Init()
|
||
{
|
||
cfgList = Framework.EditorTableManager.instance.tables.Item.DataList;
|
||
|
||
addList = new List<AddInfo>();
|
||
AddInfo info = new AddInfo();
|
||
info.id = (uint)cfgList[0].ID;
|
||
info.count = 0;
|
||
info.select = 0;
|
||
addList.Add(info);
|
||
|
||
errorStyle = new GUIStyle(EditorStyles.label);
|
||
errorStyle.normal.textColor = Color.red;
|
||
errorStyle.fontStyle = FontStyle.Bold;
|
||
|
||
List<string> itemNames = new List<string>();
|
||
for(int i = 0; i < cfgList.Count; i ++)
|
||
{
|
||
itemNames.Add("物品" + cfgList[i].ID);
|
||
}
|
||
|
||
Names = itemNames.ToArray();
|
||
}
|
||
|
||
public override void OnTabGUI()
|
||
{
|
||
actorID = EditorGUILayout.TextField("收件人角色ID:", actorID);
|
||
mailID = EditorGUILayout.TextField("邮件配置ID:", mailID);
|
||
title = EditorGUILayout.TextField("自定义邮件标题:", title);
|
||
body = EditorGUILayout.TextField("自定义邮件正文:", body);
|
||
|
||
for (int i = 0; i < addList.Count; )
|
||
{
|
||
GUILayout.BeginHorizontal();
|
||
|
||
EditorGUILayout.LabelField("物品:", GUILayout.Width(100f));
|
||
var select = EditorGUILayout.Popup(addList[i].select, Names);
|
||
if(select != addList[i].select)
|
||
{
|
||
addList[i].select = select;
|
||
addList[i].id = (uint)cfgList[select].ID;
|
||
}
|
||
|
||
EditorGUILayout.Space(20);
|
||
|
||
string countStr = EditorGUILayout.TextField("物品数量:", addList[i].count.ToString());
|
||
try
|
||
{
|
||
uint count = Convert.ToUInt32(countStr);
|
||
if (count != addList[i].count)
|
||
addList[i].count = count;
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
|
||
if (GUILayout.Button("删除"))
|
||
{
|
||
addList.RemoveAt(i);
|
||
}
|
||
else
|
||
{
|
||
i++;
|
||
}
|
||
|
||
GUILayout.EndHorizontal();
|
||
}
|
||
|
||
if (GUILayout.Button("增加"))
|
||
{
|
||
AddInfo info = new AddInfo();
|
||
info.id = (uint)cfgList[0].ID;
|
||
info.count = 0;
|
||
info.select = 0;
|
||
addList.Add(info);
|
||
}
|
||
}
|
||
|
||
public override void OnOKGUI()
|
||
{
|
||
EditorGUILayout.LabelField(result.message);
|
||
}
|
||
|
||
public override void Execute(string token, string ip, string actor)
|
||
{
|
||
if (nStatus == 100)
|
||
{
|
||
EditorUtility.DisplayDialog(GetName(), "正在查询中,请稍后....", "确定");
|
||
return;
|
||
}
|
||
|
||
string url = GetURL(token, ip, actor);
|
||
if (string.IsNullOrEmpty(url))
|
||
{
|
||
EditorUtility.DisplayDialog(GetName(), "参数存在错误,请检查", "确定");
|
||
return;
|
||
}
|
||
|
||
nStatus = 100;
|
||
|
||
// HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, OnLocalFinished);
|
||
//request.SetHeader("Content-Type", "application/json");
|
||
|
||
|
||
string postData = "{\"actor_id\": " + actorID
|
||
+ ", \"mail_id\": " + mailID
|
||
+ ", \"title\" : \""+ title
|
||
+"\", \"body\" : \""+ body
|
||
+"\", \"items\" : [" + GetItemStr() + "], \"msgs\" : [] }";
|
||
|
||
// var data = System.Text.Encoding.UTF8.GetBytes(postData);
|
||
// request.Send();
|
||
SendRequest(url, postData);
|
||
|
||
}
|
||
|
||
async UniTask SendRequest(string url, string data)
|
||
{
|
||
var request = new UnityWebRequest(url, "POST");
|
||
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(data));
|
||
request.SetRequestHeader("Content-Type", "application/json");
|
||
await request.SendWebRequest();
|
||
switch (request.result)
|
||
{
|
||
// The request finished without any problem.
|
||
case UnityWebRequest.Result.Success:
|
||
//Debug.Log("Request Finished! Text received: " + response.DataAsText);
|
||
OnRequestOK(request.downloadHandler.text);
|
||
break;
|
||
|
||
default:
|
||
string text = "Request Finished with Error! " + request.result + "\n" + request.error;
|
||
OnRequestError(text);
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
// void OnLocalFinished(HTTPRequest request, HTTPResponse response)
|
||
// {
|
||
// switch (request.State)
|
||
// {
|
||
// // The request finished without any problem.
|
||
// case HTTPRequestStates.Finished:
|
||
// //Debug.Log("Request Finished! Text received: " + response.DataAsText);
|
||
// OnRequestOK(response.DataAsText);
|
||
// break;
|
||
//
|
||
// default:
|
||
// string text = "Request Finished with Error! " + (request.Exception != null ? (request.Exception.Message + "\n" + request.Exception.StackTrace) : "No Exception");
|
||
// OnRequestError(text);
|
||
// break;
|
||
// }
|
||
// }
|
||
|
||
string GetItemStr()
|
||
{
|
||
if (addList.Count <= 0)
|
||
return "";
|
||
|
||
string param = "";
|
||
for (int i = 0; i < addList.Count; i++)
|
||
{
|
||
if (addList[i].id <= 0 || addList[i].count <= 0)
|
||
continue;
|
||
|
||
if (string.IsNullOrEmpty(param))
|
||
param = "{ \"id\": " + addList[i].id + ", \"count\": " + addList[i].count + "}";
|
||
else
|
||
param += "," + "{ \"id\": " + addList[i].id + ", \"count\": " + addList[i].count + "}";
|
||
}
|
||
|
||
return param;
|
||
}
|
||
|
||
public override string GetURL(string token, string ip, string actor)
|
||
{
|
||
return $"http://{ip}/send_mail?gm_token={token}";
|
||
}
|
||
|
||
public override uint DoJsonDeserialize(string json, out string message)
|
||
{
|
||
uint code = 0;
|
||
try
|
||
{
|
||
result = JsonConvert.DeserializeObject<CommonResult>(json);
|
||
message = result.message;
|
||
code = result.code;
|
||
}
|
||
catch
|
||
{
|
||
Debug.LogError("json error, json=" + json);
|
||
message = "json error, json=" + json;
|
||
code = 1000;
|
||
}
|
||
|
||
return code;
|
||
}
|
||
}
|