wzp
2021-09-16 96eac0ce567dc3eef6900ce57dcb4c029fac2082
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<%@ 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;
        }
    }
 
}