using CommonUtil;
using CommonUtil.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Operater.DAL;
using Operater.DTO;
using Operater.DTO.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YouZanSDKStandard.Api;
using YouZanSDKStandard.Api.Request;
using CommonUtil.Web;
namespace Api.Operater.Controllers
{
[Route(TopConstants.API_ROUTE)]
public class WxMiniProController : BaseController
{
private IMemoryCache _cache;
public WxMiniProController(IMemoryCache memoryCache)
{
_cache = memoryCache;
}
///
/// 获取有赞小程序urllink
///
///
[HttpPost]
public IActionResult GetYzUrlLink([FromBody] GetYzUrlLinkRequest request)
{
//获取token
string sessionKey = "";
var cache = _cache.Get("yzsessionkey");
if (cache == null)//如果没有该缓存
{
//获取token
var appset = new AppsetDAL().ListGet(t => t.Id != null);
sessionKey = appset[0].YzToken;
_cache.Set("yzsessionkey", sessionKey, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(30)));
}
else
{
sessionKey = cache.ToString();
}
long timespan = ExtendUtil.EncodeUnixMillisecond(DateTime.Now.AddDays(29));
YouZanAppUrlinkGetRequest yzReq = new YouZanAppUrlinkGetRequest()
{
url = request.Url,
url_desc = request.UrlDesc,
is_expire = true,
expired_time = timespan
};
var yzRsp = YouZanClient.Execute(yzReq, sessionKey);
if (yzRsp.IsNull() || !yzRsp.success)
{
LogUtil.Info($"{yzRsp.message}", "微信短链error");
return Error("跳转失败");
}
return Success(yzRsp.data.url_link);
}
///
/// 获取h5跳转短链
///
///
///
[HttpPost]
public IActionResult GetWxShortLink([FromBody] GetWxShortLinkRequest request)
{
Dictionary dic = new Dictionary();
string h5url = "http://h5.ushopvip.com/pages/wxmini/yztransfer";
switch (request.MiniType)
{
case 0://有赞小程序
h5url = "http://h5.ushopvip.com/pages/wxmini/yztransfer";
break;
case 1://羊毛券
h5url = "http://h5.ushopvip.com/pages/wxmini/fxtransfer";
break;
default:
h5url = "http://h5.ushopvip.com/pages/wxmini/yztransfer";
break;
}
dic.Add("Url", $"{h5url}?wxurl={request.Url.Trim()}&wxdesc={request.UrlDesc.Trim()}");
dic.Add("AppName", "wxh5");
dic.Add("Nick", "欧锦希");
string res = new WebUtil().DoPostWithJson("http://vx8.cc/api/Top/Execute", dic, null);
CreateShortLinkResponse response = JSONUtil.JsonToObject(res);
if (!response.error.IsNull())
{
return Error(response.error.message);
}
return Success(response.result.shortUrl);
}
///
/// 分销小程序urllink获取
///
///
///
[HttpPost]
public IActionResult GetFxUrlLink([FromBody] GetFxUrlLinkRequest request)
{
//获取token
string token = "";
var cache = _cache.Get("fxwxtoken");
if (cache == null)//如果没有该缓存
{
//获取token
var appset = new AppsetDAL().ListGet(t => t.Id != null);
token = appset[0].FxWxToken;
_cache.Set("fxwxtoken", token, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
else
{
token = cache.ToString();
}
long timespan = ExtendUtil.EncodeUnixMillisecond(DateTime.Now.AddDays(29));
Dictionary dic = new Dictionary();
string url = $"https://api.weixin.qq.com/wxa/generate_urllink?access_token={token}";
dic.Add("path", request.Url);
dic.Add("is_expire", false);
dic.Add("expire_type", 0);
dic.Add("expire_time", timespan);
dic.Add("query", request.Query);
// dic.Add("page_title", request.UrlDesc);
string resstr = new WebUtil().DoPostWithJson(url, dic, null);
if (!resstr.IsNullOrEmpty())
{
TopWxUrlResponse response = JSONUtil.JsonToObject(resstr);
return Success(response.url_link);
}
return Error("生成失败");
}
}
}