231 lines
7.2 KiB
C#
231 lines
7.2 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace AutoConfig
|
|
{
|
|
public class WPExcelDownloader
|
|
{
|
|
|
|
private Dictionary<string, string> _headers = new Dictionary<string, string>() {
|
|
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" },
|
|
{"Content-Type", "application/x-www-form-urlencoded" },
|
|
//{"Cookie","wedoc_openid=wozbKqDgAAYt6fSGwDXda25jv2p5OP0w; wedoc_sid=1kBsM4ycejYuLERYAMRFUQAA; wedoc_sids=13102701888889220&1oQwT4xhZXMur2NhAINNSgAA|13102702970838080&1kBsM4ycejYuLERYAMRFUQAA; wedoc_skey=13102701888889220&ac00476fed4403712cae26060dfb39b6||13102702970838080&b89214c23c46059d0221af76cb528adb; wedoc_ticket=13102701888889220&CAESIG807y5KZMJpoC13g7JnkNY7pdaXE4n_sycBDYT9KEnH|13102702970838080&CAESIAhdu0oATYX8_OokDtb82-QP6zCNIe-ecFzDi53s5For;" }
|
|
};
|
|
|
|
public string cookies;
|
|
public string docId;
|
|
public string docName;
|
|
|
|
private string _operationId;
|
|
private int _progress;
|
|
private string _downloadUrl;
|
|
|
|
public bool isDownloaded = false;
|
|
public bool isFinished = false;
|
|
public bool isCanceled = false;
|
|
|
|
public WPExcelDownloader(string docName, string docId, string cookies)
|
|
{
|
|
this.docName = docName;
|
|
this.docId = docId;
|
|
_progress = 0;
|
|
|
|
this.cookies = cookies;
|
|
|
|
_headers["Cookie"] = this.cookies;
|
|
}
|
|
|
|
public void Cancel()
|
|
{
|
|
isCanceled = true;
|
|
isFinished = true;
|
|
}
|
|
|
|
public async UniTask<bool> StartDownload()
|
|
{
|
|
Debug.Log("开始下载:" + docName);
|
|
|
|
var result = await _RequestOperationId();
|
|
if (!result)
|
|
{
|
|
isFinished = true;
|
|
return false;
|
|
}
|
|
if (isCanceled)
|
|
{
|
|
return false;
|
|
}
|
|
while (_progress < 100)
|
|
{
|
|
var r2 = await _RequestDownloadUrl();
|
|
if (!r2)
|
|
{
|
|
isFinished = true;
|
|
return false;
|
|
}
|
|
|
|
if (isCanceled)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
await UniTask.Delay(100);
|
|
|
|
if (isCanceled)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
var r3 = await _StartDownload();
|
|
|
|
if (isCanceled)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
isDownloaded = r3;
|
|
isFinished = true;
|
|
return r3;
|
|
}
|
|
|
|
public async UniTask<bool> OnlyCheck()
|
|
{
|
|
var result = await _RequestOperationId();
|
|
return result;
|
|
}
|
|
|
|
private async UniTask<bool> _StartDownload()
|
|
{
|
|
var url = _downloadUrl;
|
|
var request = UnityWebRequest.Get(url);
|
|
UnityWebRequest result;
|
|
try
|
|
{
|
|
result = await request.SendWebRequest();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
return false;
|
|
}
|
|
if (!string.IsNullOrEmpty(result.error))
|
|
{
|
|
Debug.LogError("下载:" + docName + "发生错误:" + result.error);
|
|
return false;
|
|
}
|
|
var bytesData = result.downloadHandler.data;
|
|
// 写到文件
|
|
var savePath = DownloadConfig.SAVE_PATH + docName;
|
|
// 先删旧的
|
|
try
|
|
{
|
|
if (File.Exists(savePath))
|
|
{
|
|
File.Delete(savePath);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
Debug.LogError($"文件{docName}占用中");
|
|
return false;
|
|
}
|
|
|
|
await File.WriteAllBytesAsync(savePath, bytesData);
|
|
Debug.Log("下载:" + docName + "成功");
|
|
return true;
|
|
}
|
|
|
|
private async UniTask<bool> _RequestDownloadUrl()
|
|
{
|
|
var milliseconds = DateTime.Now.Ticks / 10000;
|
|
var url = $"https://doc.weixin.qq.com/v1/export/query_progress?operationId={_operationId}×tamp={milliseconds}";
|
|
var request = UnityWebRequest.Get(url);
|
|
foreach (var data in _headers)
|
|
{
|
|
request.SetRequestHeader(data.Key, data.Value);
|
|
}
|
|
UnityWebRequest result;
|
|
try
|
|
{
|
|
result = await request.SendWebRequest();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
return false;
|
|
}
|
|
var jsonStr = result.downloadHandler.text;
|
|
var jsonObj = SimpleJSON.JSON.Parse(jsonStr);
|
|
var ret = (int)jsonObj["ret"];
|
|
if (ret == 0)
|
|
{
|
|
_progress = (int)jsonObj["progress"];
|
|
if (_progress >= 100)
|
|
{
|
|
_downloadUrl = jsonObj["file_url"];
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("_RequestDownloadUrl 失败:" + ret);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async UniTask<bool> _RequestOperationId()
|
|
{
|
|
var sid = Random.Range(1000, 10000) + "";
|
|
// 设置忽略ssl
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
var url = $"https://doc.weixin.qq.com/v1/export/export_office?sid={sid}&wedoc_xsrf=1&docId={docId}&version=2";
|
|
var cacheJsonObj = new SimpleJSON.JSONObject();
|
|
cacheJsonObj["docId"] = docId;
|
|
cacheJsonObj["version"] = 2;
|
|
var formJson = cacheJsonObj.ToString();
|
|
var request = UnityWebRequest.PostWwwForm(url, "");
|
|
foreach (var data in _headers)
|
|
{
|
|
request.SetRequestHeader(data.Key, data.Value);
|
|
}
|
|
UnityWebRequest result;
|
|
try
|
|
{
|
|
result = await request.SendWebRequest();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
Debug.LogError("下载:" + docName + "发生错误:" + e.Message);
|
|
Debug.LogError("url:" + url);
|
|
return false;
|
|
}
|
|
var jsonStr = result.downloadHandler.text;
|
|
var jsonObj = SimpleJSON.JSON.Parse(jsonStr);
|
|
var ret = (int)jsonObj["ret"];
|
|
if (ret == 0)
|
|
{
|
|
_operationId = jsonObj["operationId"];
|
|
// 成功
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
// 失败
|
|
Debug.LogError("_RequestOperationId 失败:" + jsonStr);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|