<%@ WebHandler Language="C#" Class="ExternalHandler" %>
|
|
using System;
|
using System.Web;
|
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json;
|
using Model;
|
using Common;
|
using Dao;
|
|
public class ExternalHandler : IHttpHandler {
|
|
public void ProcessRequest(HttpContext context)
|
{
|
JsonPageResult result = null;
|
|
PageContext<SysUser> pc = new PageContext<SysUser>(context);
|
|
try
|
{
|
result = ProcessRequestInternal(pc);
|
}
|
catch (Exception e)
|
{
|
result = new JsonPageResult(false, e.Message);
|
}
|
|
IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter();
|
|
datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
context.Response.ContentType = "application/json";
|
var jsonText = JsonConvert.SerializeObject(result, Formatting.Indented, datetimeConverter);
|
context.Response.Write(jsonText);
|
context.Response.End();
|
}
|
|
private JsonPageResult ProcessRequestInternal(PageContext<SysUser> context)
|
{
|
string action = context.GetString("action");
|
|
switch (action)
|
{
|
case "charge":
|
return Charge(context);
|
|
default:
|
throw new Exception("Invalid Action=" + action);
|
}
|
}
|
|
|
/// <summary>
|
/// API充值接口说明
|
/// 请求地址:http://127.0.0.1:8011/external.ashx
|
/// account:系统账号
|
/// spid:要充值的短信账号
|
/// amount:充值金额,人民币,单位厘
|
/// price:单价,必填,人民币,单位厘(不更新账户单价,则price=0)
|
/// remark:充值备注
|
/// sign:数据签名
|
///1.参数中含有中文地方,统一用UTF8方式URLEncode
|
///2.sign=MD5(account+spid+amount+price+remark+password)
|
///3.password为account账号对应密码
|
///4.MD5 生成为32位,不区分大小写,校验可以到 http://www.cmd5.com/ 测试
|
///例子:http://smgw.131421.com/external.ashx?action=charge&account=admin&spid=922001&amount=1000&price=0&remark=%e5%93%88%e5%93%88&sign=1ed99fd468914232d2297ca15007934a
|
///例子中 account对应密码为 111111,即 md5("admin92200110000哈哈111111") = 1ed99fd468914232d2297ca15007934a
|
/// </summary>
|
/// <param name="context"></param>
|
/// <returns></returns>
|
|
private JsonPageResult Charge(PageContext<SysUser> context)
|
{
|
string account = context.GetString("account");
|
string spid = context.GetString("spid");
|
long amount = context.GetInt64("amount");
|
long price = context.GetInt64("price");
|
string remark = context.GetString("remark");
|
string sign = context.GetString("sign");
|
|
CheckAuth(account, spid, amount,price, remark,sign);
|
|
GwSp sp = null;
|
|
using (GwSpDao dao = new GwSpDao())
|
{
|
sp = dao.Get(spid);
|
}
|
|
if (sp == null)
|
{
|
throw new ArgumentException("账户信息不存在!");
|
}
|
|
GwClient client;
|
|
using (GwClientDao dao = new GwClientDao())
|
{
|
client = dao.Get(sp.ClientID);
|
}
|
|
if (client == null)
|
{
|
throw new ArgumentException("客户信息不存在!");
|
}
|
|
using (GwChargeLogDao dao = new GwChargeLogDao())
|
{
|
dao.Add(new GwChargeLog() { Amount = amount, Remark = "程序API充值=" + remark, OperatorID = account, SpID = spid, ClientID = sp.ClientID, ClientName = client.ClientName, Flag = 0, OccurTime = DateTime.Now });
|
|
}
|
|
if (price > 0)
|
{
|
using (GwSpDao dao = new GwSpDao())
|
{
|
dao.UpdatePrice(spid, price);
|
}
|
}
|
|
return new JsonPageResult(true, "账户" + spid + "充值请求提交成功!");
|
}
|
|
private static void CheckAuth(string account, string spid, long amount, long price, string remark, string sign)
|
{
|
using (UserDao dao = new UserDao())
|
{
|
string password = dao.GetUserPassword(account);
|
|
if (!string.Equals(DataHelper.MD5Hex(account + spid + amount + price + remark + password), sign, StringComparison.OrdinalIgnoreCase))
|
{
|
throw new ArgumentException("数据签名校验失败!");
|
}
|
}
|
}
|
|
public bool IsReusable
|
{
|
get
|
{
|
return false;
|
}
|
}
|
|
}
|