using Furion.DependencyInjection;
using Nirvana.Common;
using Senparc.Weixin.Open.ComponentAPIs;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Core;
using YBDevice.Entity;
namespace YBDevice.WXApplication.WXInfo
{
///
/// 微信相关管理
///
public class WXService : IWXService, ITransient
{
private readonly ISqlSugarRepository repository;
private readonly SqlSugarClient dbClient;
private readonly ILoggerService _loggerService;
public WXService(ISqlSugarRepository sqlSugarRepository, ILoggerService loggerService)
{
repository = sqlSugarRepository;
dbClient = repository.Context;
_loggerService = loggerService;
}
public Task CheckIsRZFAsync(string appid)
{
throw new NotImplementedException();
}
///
/// 获取openticket
///
///
///
public async Task GetOpenTicketAsync(string componentAppId)
{
var component = await dbClient.Queryable().Where(x => x.ComponentAppId == componentAppId).FirstAsync();
return component !=null?component.OpenTicket:"";
}
///
/// 获取公众号的刷新token
///
/// 公众号appid
/// 开放平台appid
///
public async Task GetRefreshToken(string authorizerId, string componentAppId)
{
var data = await dbClient.Queryable().FirstAsync(x => x.authorizer_appid == authorizerId && x.componentappid == componentAppId);
if (data != null)
{
return data.authorizer_refresh_token;
}
return "";
}
///
/// 插入或者更新公众号信息
///
///
///
///
public async Task InsertOrUpdateAsync(YB_OfficlaAccount model, string bid)
{
if (await dbClient.Queryable().AnyAsync(x => x.authorizer_appid == model.authorizer_appid && x.componentappid == model.componentappid))
{
await dbClient.Updateable().SetColumns(x => new YB_OfficlaAccount
{
authorizer_access_token = model.authorizer_access_token,
authorizer_refresh_token = model.authorizer_refresh_token,
head_img = model.head_img,
nick_name = model.nick_name,
user_name = model.user_name,
alias = model.alias,
service_type_info = model.service_type_info,
verify_type_info = model.verify_type_info,
lastmodifytime = DateTime.Now,
func_info = model.func_info,
qrcode_url = model.qrcode_url,
isauthorize = 1,
type = model.type
})
.Where(x => x.authorizer_appid == model.authorizer_appid && x.componentappid == model.componentappid).ExecuteCommandAsync();
}
else
{
var data = new YB_OfficlaAccount
{
alias = model.alias.ToStr(),
service_type_info = model.service_type_info.ToStr(),
authorizer_access_token = model.authorizer_access_token.ToStr(),
authorizer_appid = model.authorizer_appid.ToStr(),
authorizer_refresh_token = model.authorizer_refresh_token.ToStr(),
componentappid = model.componentappid.ToStr(),
createtime = DateTime.Now,
func_info = model.func_info.ToStr(),
head_img = model.head_img.ToStr(),
isauthorize = 1,
lastmodifytime = DateTime.Now,
nick_name = model.nick_name.ToStr(),
qrcode_url = model.qrcode_url.ToStr(),
user_name = model.user_name.ToStr(),
verify_type_info = model.verify_type_info.ToStr(),
type = model.type,
authorizeationcode = model.authorizeationcode.ToStr()
};
await dbClient.Insertable(data).ExecuteCommandAsync();
}
if (!string.IsNullOrEmpty(bid))
{
var businessid = bid.ToInt();
if (businessid > 0 && !await dbClient.Queryable().AnyAsync(x => x.BusinessId == businessid && x.AppId == model.authorizer_appid))
{
await dbClient.Insertable(new YB_BusinessOffice
{
AppId = model.authorizer_appid,
BusinessId = businessid,
CreateTime = DateTime.Now
}).ExecuteCommandAsync();
}
}
}
///
/// 增加或者更新openticket
///
///
public async Task InsertOrUpdateAsync(YB_OpenWXConfig model)
{
if (await dbClient.Queryable().AnyAsync(x => x.ComponentAppId == model.ComponentAppId))
{
await dbClient.Updateable().SetColumns(x => new YB_OpenWXConfig
{
OpenTicket = model.OpenTicket
}).Where(x => x.ComponentAppId == model.ComponentAppId).ExecuteCommandAsync();
}
else
{
await dbClient.Insertable(model).ExecuteCommandAsync();
}
}
///
/// 更新或者插入授权token
///
///
/// 公众号appid
/// 开放平台id
public async Task InsertOrUpdateAuthorizerTokenAsync(RefreshAuthorizerTokenResult result, string authorizerId, string componentAppId)
{
if (await dbClient.Queryable().AnyAsync(x => x.authorizer_appid == authorizerId && x.componentappid == componentAppId))
{
await dbClient.Updateable().SetColumns(x => new YB_OfficlaAccount
{
authorizer_access_token = result.authorizer_access_token,
authorizer_refresh_token = result.authorizer_refresh_token
}).Where(x => x.authorizer_appid == authorizerId && x.componentappid == componentAppId).ExecuteCommandAsync();
}
}
///
/// 更新公众号授权信息
///
///
///
public async Task NoticeUpdateAsync(YB_OfficlaAccount model)
{
if (await dbClient.Queryable().AnyAsync(x => x.authorizer_appid == model.authorizer_appid && x.componentappid == model.componentappid))
{
//成功授权
if (model.isauthorize == 1)
{
await dbClient.Updateable().SetColumns(x => new YB_OfficlaAccount
{
authorizeationcode = model.authorizeationcode,
authorizationcodeexpiredtime = model.authorizationcodeexpiredtime,
isauthorize = model.isauthorize
}).Where(x => x.authorizer_appid == model.authorizer_appid && x.componentappid == model.componentappid).ExecuteCommandAsync();
}
}
//取消授权
else
{
//更新公众号
await dbClient.Updateable().SetColumns(x => new YB_OfficlaAccount
{
isauthorize = 0
}).Where(x => x.componentappid == model.componentappid && x.authorizer_appid == model.authorizer_appid).ExecuteCommandAsync();
}
}
///
/// 更新提交的审核状态
///
/// 微信原始id
/// 审核状态,-1-代码已上传,0-审核成功,1-审核被拒绝,2-审核中,3-已撤回,4-审核延后
/// 状态备注
/// 不通过时的截图,mediaid形式
/// 时间戳
///
public async Task UpdateMiniInfoAsync(string appid, int status, string remark, string screenshot, string time)
{
_loggerService.AddLogger($"小程序审核状态:appid:{appid},status={status},remark={remark},screenshot={screenshot},time={time}",2);
var office = await dbClient.Queryable().FirstAsync(x => x.user_name == appid);
if (office != null)
{
var data = await dbClient.Queryable().Where(x => x.appid == office.authorizer_appid).OrderBy(x => x.CreateTime, OrderByType.Desc).FirstAsync();
DateTime lasttime = time.ToDatetimeFromTimeStamp();
remark = remark.ToStr();
screenshot = screenshot.ToStr();
if (data != null)
{
await dbClient.Updateable().SetColumns(x => new YB_MiniProgramHistory
{
Status = status,
StatusRemark = remark,
ScreenShot = screenshot,
LastTime = lasttime
}).Where(x => x.Id == data.Id).ExecuteCommandAsync();
}
}
}
}
}