Просмотр исходного кода

新增发送短信和通话记录

zhoufan лет назад: 2
Родитель
Сommit
d97bc0c00d

+ 55 - 0
ZhuMaDian12345.Common/CommonHelper.cs

@@ -1,6 +1,8 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3
+using System.Configuration;
3 4
 using System.Linq;
5
+using System.Net.Http;
4 6
 using System.Text;
5 7
 using System.Threading.Tasks;
6 8
 using System.Web;
@@ -45,5 +47,58 @@ namespace ZhuMaDian12345.Common
45 47
                 return "127.0.0.1";
46 48
             }
47 49
         }
50
+
51
+        /// <summary>
52
+        /// 发送短信
53
+        /// </summary>
54
+        /// <param name="telephone">手机号</param>
55
+        /// <param name="content">内容</param>
56
+        /// <returns></returns>
57
+        public static bool SendSms(string telephone, string content)
58
+        {
59
+            bool result = false;
60
+
61
+            string url = ConfigurationManager.AppSettings["sms_url"].ToString();
62
+            string username = ConfigurationManager.AppSettings["sms_username"].ToString();
63
+            string password = ConfigurationManager.AppSettings["sms_password"].ToString();
64
+
65
+            //dc integer(1) 消息编码,用 15 即可
66
+            //sm string 短信内容,必填,需转 URLEncoder -UTF8 编码(一定要加签名,示例:【测试】)
67
+            //da string 手机号码,必填(单一内容群发时用; 隔开即可)
68
+            //sa string 扩展码,可不填,手机收到短信码号中的几位
69
+            //ex integer(8) 外部编码,可以不填,接收上行与状态报告是用于区分
70
+            //rd integer(1) 是否需要状态报告(设为 1 为需要,0 为不需要),设为 1 就行
71
+            //un string 用户名,必填
72
+            //pw string 密码,必填
73
+            //rf integer(1) 返回格式固定为 2,返回 JSON 格式
74
+            //ts string 时间标记,用于验证,格式 yyyyMMddHHmmss,一般不填,加密模式下使用
75
+            //tf integer(1) 短信内容传输编码,设为 3 即可,3 为 URLEncoder -UTF8 编码
76
+            var encodecontent = new FormUrlEncodedContent(new[] {
77
+                    new KeyValuePair<string, string>("dc", "15"),
78
+                    new KeyValuePair<string, string>("tf", "3"),
79
+                    new KeyValuePair<string, string>("un", username),
80
+                    new KeyValuePair<string, string>("pw", password),
81
+                    new KeyValuePair<string, string>("da", telephone),
82
+                    new KeyValuePair<string, string>("sm", content),
83
+                    new KeyValuePair<string, string>("rd", "1"),
84
+                    new KeyValuePair<string, string>("rf", "2") });
85
+            try
86
+            {
87
+                HttpClient client = new HttpClient();
88
+                var postresult = client.PostAsync(url, encodecontent).Result;
89
+                string resultContent = postresult.Content.ReadAsStringAsync().Result;
90
+
91
+                LogHelper.Warn(resultContent);
92
+
93
+                var jsonresult = resultContent.ToJObject();
94
+                result = (bool)jsonresult["success"];
95
+            }
96
+            catch (Exception ex)
97
+            {
98
+                LogHelper.Error("发送短信失败,号码:" + telephone + ",内容:" + content, ex);
99
+            }
100
+
101
+            return result;
102
+        }
48 103
     }
49 104
 }

+ 61 - 29
ZhuMaDian12345.Common/LogHelper.cs

@@ -1,6 +1,7 @@
1 1
 using log4net;
2 2
 using System;
3 3
 using System.Collections.Generic;
4
+using System.Diagnostics;
4 5
 using System.IO;
5 6
 using System.Linq;
6 7
 using System.Text;
@@ -16,42 +17,73 @@ namespace ZhuMaDian12345.Common
16 17
             FileInfo configFile = new FileInfo(HttpContext.Current.Server.MapPath("/Configs/log4net.config"));
17 18
             log4net.Config.XmlConfigurator.Configure(configFile);
18 19
         }
19
-        public static Log GetLogger(Type type)
20
+        /// <summary>
21
+        /// Error
22
+        /// </summary>
23
+        /// <param name="message"></param>
24
+        /// <param name="ex"></param>
25
+        public static void Error(string message, Exception ex = null)
20 26
         {
21
-            return new Log(LogManager.GetLogger(type));
27
+            ILog _log = LogManager.GetLogger(new StackTrace().GetFrame(1).GetMethod().DeclaringType.FullName);
28
+            if (ex != null)
29
+            {
30
+                _log.Error(message, ex);
31
+            }
32
+            else
33
+            {
34
+                _log.Error(message);
35
+            }
22 36
         }
23
-        public static Log GetLogger(string str)
37
+        /// <summary>
38
+        /// Info
39
+        /// </summary>
40
+        /// <param name="message"></param>
41
+        /// <param name="ex"></param>
42
+        public static void Info(string message, Exception ex = null)
24 43
         {
25
-            return new Log(LogManager.GetLogger(str));
44
+            ILog _log = LogManager.GetLogger(new StackTrace().GetFrame(1).GetMethod().DeclaringType.FullName);
45
+            if (ex != null)
46
+            {
47
+                _log.Info(message, ex);
48
+            }
49
+            else
50
+            {
51
+                _log.Info(message);
52
+            }
26 53
         }
27
-    }
28
-
29
-    public class Log
30
-    {
31
-        private ILog logger;
32
-        public Log(ILog log)
33
-        {
34
-            this.logger = log;
35
-        }
36
-        public void Debug(object message)
37
-        {
38
-            this.logger.Debug(CommonHelper.GetIP() + "  " + message);
39
-        }
40
-        public void Error(object message)
41
-        {
42
-            this.logger.Error(CommonHelper.GetIP() + "  " + message);
43
-        }
44
-        public void Error(object message,Exception ex)
45
-        {
46
-            this.logger.Error(CommonHelper.GetIP() + "  " + message,ex);
47
-        }
48
-        public void Info(object message)
54
+        /// <summary>
55
+        /// Debug
56
+        /// </summary>
57
+        /// <param name="message"></param>
58
+        /// <param name="ex"></param>
59
+        public static void Debug(string message, Exception ex = null)
49 60
         {
50
-            this.logger.Info(CommonHelper.GetIP() + "  " + message);
61
+            ILog _log = LogManager.GetLogger(new StackTrace().GetFrame(1).GetMethod().DeclaringType.FullName);
62
+            if (ex != null)
63
+            {
64
+                _log.Debug(message, ex);
65
+            }
66
+            else
67
+            {
68
+                _log.Debug(message);
69
+            }
51 70
         }
52
-        public void Warn(object message)
71
+        /// <summary>
72
+        /// Warn
73
+        /// </summary>
74
+        /// <param name="message"></param>
75
+        /// <param name="ex"></param>
76
+        public static void Warn(string message, Exception ex = null)
53 77
         {
54
-            this.logger.Warn(CommonHelper.GetIP() + "  " + message);
78
+            ILog _log = LogManager.GetLogger(new StackTrace().GetFrame(1).GetMethod().DeclaringType.FullName);
79
+            if (ex != null)
80
+            {
81
+                _log.Warn(message, ex);
82
+            }
83
+            else
84
+            {
85
+                _log.Warn(message);
86
+            }
55 87
         }
56 88
     }
57 89
 }

Разница между файлами не показана из-за своего большого размера
+ 144 - 1223
ZhuMaDian12345.DB/DbHelperSQL.cs


+ 20 - 10
ZhuMaDian12345/App_Start/ErrorAttribute.cs

@@ -13,19 +13,29 @@ namespace ZhuMaDian12345
13 13
         public override void OnException(ExceptionContext context)
14 14
         {
15 15
             base.OnException(context);
16
-            WriteLog(context);
16
+
17
+            var cnt = context.RequestContext.HttpContext;
18
+            var rt = cnt.Request;
19
+            Dictionary<string, string> Params = new Dictionary<string, string>();
20
+            Params.Add("request_url", rt.Url.ToString());
21
+
22
+            if (rt.HttpMethod.ToUpper() != "GET")
23
+            {
24
+                foreach (var key in rt.Params.AllKeys)
25
+                {
26
+                    if (key == "ALL_HTTP")
27
+                    {
28
+                        break;
29
+                    }
30
+                    Params.Add(key, rt.Params[key]);
31
+                }
32
+            }
33
+
34
+            LogHelper.Error("系统异常:" + Params.ToJson(), context.Exception);
35
+
17 36
             context.ExceptionHandled = true;
18 37
             context.HttpContext.Response.StatusCode = 200;
19 38
             context.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.error.ToString(), message = context.Exception.ToString() }.ToJson() };
20 39
         }
21
-
22
-        private void WriteLog(ExceptionContext context)
23
-        {
24
-            if (context == null)
25
-                return;
26
-            var actionName = context.RouteData.Values["Action"];
27
-            var log = LogHelper.GetLogger(context.Controller.ToString() + "/" + actionName);
28
-            log.Error(context.Exception.ToString());
29
-        }
30 40
     }
31 41
 }

+ 4 - 4
ZhuMaDian12345/Configs/log4net.config

@@ -20,7 +20,7 @@
20 20
     <!-- 错误 Error.log-->
21 21
     <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
22 22
       <!--目录路径,可以是相对路径或绝对路径-->
23
-      <param name="File" value="D:\CallCenter_log"/>
23
+      <param name="File" value="logs"/>
24 24
       <!--文件名,按日期生成文件夹-->
25 25
       <param name="DatePattern" value="/yyyy-MM-dd/&quot;Error.log&quot;"/>
26 26
       <!--追加到文件-->
@@ -46,7 +46,7 @@
46 46
     <!-- 警告 Warn.log-->
47 47
     <appender name="WarnLog" type="log4net.Appender.RollingFileAppender">
48 48
       <!--目录路径,可以是相对路径或绝对路径-->
49
-      <param name="File" value="D:\CallCenter_log"/>
49
+      <param name="File" value="logs"/>
50 50
       <!--文件名,按日期生成文件夹-->
51 51
       <param name="DatePattern" value="/yyyy-MM-dd/&quot;Warn.log&quot;"/>
52 52
       <!--追加到文件-->
@@ -72,7 +72,7 @@
72 72
     <!-- 信息 Info.log-->
73 73
     <appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
74 74
       <!--目录路径,可以是相对路径或绝对路径-->
75
-      <param name="File" value="D:\CallCenter_log"/>
75
+      <param name="File" value="logs"/>
76 76
       <!--文件名,按日期生成文件夹-->
77 77
       <param name="DatePattern" value="/yyyy-MM-dd/&quot;Info.log&quot;"/>
78 78
       <!--追加到文件-->
@@ -98,7 +98,7 @@
98 98
     <!-- 调试 Debug.log-->
99 99
     <appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
100 100
       <!--目录路径,可以是相对路径或绝对路径-->
101
-      <param name="File" value="D:\CallCenter_log"/>
101
+      <param name="File" value="logs"/>
102 102
       <!--文件名,按日期生成文件夹-->
103 103
       <param name="DatePattern" value="/yyyy-MM-dd/&quot;Debug.log&quot;"/>
104 104
       <!--追加到文件-->

+ 111 - 0
ZhuMaDian12345/Controllers/ApiController.cs

@@ -1,8 +1,11 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3
+using System.Configuration;
4
+using System.Data;
3 5
 using System.Linq;
4 6
 using System.Web;
5 7
 using System.Web.Mvc;
8
+using ZhuMaDian12345.Common;
6 9
 using ZhuMaDian12345.DB;
7 10
 
8 11
 namespace ZhuMaDian12345.Controllers
@@ -63,5 +66,113 @@ namespace ZhuMaDian12345.Controllers
63 66
                 return Error("获取失败");
64 67
             }
65 68
         }
69
+
70
+        /// <summary>
71
+        /// 获取通话记录列表
72
+        /// </summary>
73
+        /// <returns></returns>
74
+        [OutActionFilter]
75
+        public ActionResult GetCallrecordList(string phone, string usercode, int calltype = -1, DateTime? starttime = null, DateTime? endtime = null, int page = 1, int pagesize = 10)
76
+        {
77
+            string wheresql = string.Empty;
78
+            Dictionary<string, string> wherepra = new Dictionary<string, string>();
79
+            if (!string.IsNullOrEmpty(phone))
80
+            {
81
+                wheresql += " and CallNumber like @CallNumber";
82
+                wherepra.Add("@CallNumber", "%" + phone + "%");
83
+            }
84
+            if (!string.IsNullOrEmpty(usercode))
85
+            {
86
+                wheresql += " and UserCode = @UserCode";
87
+                wherepra.Add("@UserCode", usercode);
88
+            }
89
+            if (calltype > -1)
90
+            {
91
+                wheresql += " and CallType ='" + calltype + "' ";
92
+            }
93
+            if (starttime != null)
94
+            {
95
+                wheresql += " and BeginTime>='" + starttime.Value.ToString("yyyy-MM-dd HH:mm:ss") + "' ";
96
+            }
97
+            if (endtime != null)
98
+            {
99
+                wheresql += " and BeginTime<='" + endtime.Value.ToString("yyyy-MM-dd HH:mm:ss") + "' ";
100
+            }
101
+
102
+            int total = 0;
103
+            var dt = DbHelperSQL.RunPaginationBySqlParameter(
104
+                "T_Call_CallRecords with(nolock)",
105
+                "CallId,CallNumber,CallType,CallState,BeginTime,TalkStartTime,TalkEndTime,EndTime,UserCode,ExtNumber,FilePath",
106
+                wheresql,
107
+                wherepra,
108
+                " order by CallRecordsId desc",
109
+                pagesize,
110
+                page,
111
+                out total
112
+                );
113
+
114
+            string filepath = ConfigurationManager.AppSettings["FilePath"].ToString();
115
+            foreach (DataRow dr in dt.Rows)
116
+            {
117
+                string path = dr["FilePath"] != null ? dr["FilePath"].ToString() : "";
118
+                if (path != "")
119
+                {
120
+                    string lujing = path.Replace("/home/ubuntu/videofile", filepath);
121
+
122
+                    dr["FilePath"] = lujing;
123
+                }
124
+            }
125
+
126
+            var obj = new
127
+            {
128
+                state = "success",
129
+                message = "成功",
130
+                rows = dt,
131
+                total = total
132
+            };
133
+
134
+            return Content(obj.ToJson());
135
+        }
136
+
137
+        /// <summary>
138
+        /// 根据分机号获取手机号发短信
139
+        /// </summary>
140
+        /// <param name="ext"></param>
141
+        /// <returns></returns>
142
+        [InActionFilter]
143
+        public ActionResult SendSMS(string ext)
144
+        {
145
+            string sql = "select top 1 Telephone from Tel_DeptTelExt where Extension=@Extension order by CreateTime desc";
146
+            Dictionary<string, string> paras = new Dictionary<string, string>();
147
+            paras.Add("@Extension", ext);
148
+
149
+            string tel = DbHelperSQL.GetSingle(sql, paras)?.ToString() ?? "";
150
+            if (!string.IsNullOrEmpty(tel))
151
+            {
152
+                string content = ConfigurationManager.AppSettings["sms_content"].ToString();
153
+                bool issuccess = CommonHelper.SendSms(tel, content);
154
+
155
+                string sql1 = "insert into Tel_SendSms(Extension,Telephone,[Content],IsSuccess,CreateTime) values(@Extension,@Telephone,@Content,@IsSuccess,getdate())";
156
+                Dictionary<string, string> paras1 = new Dictionary<string, string>();
157
+                paras1.Add("@Extension", ext);
158
+                paras1.Add("@Telephone", tel);
159
+                paras1.Add("@Content", content);
160
+                paras1.Add("@IsSuccess", (issuccess ? 1 : 0).ToString());
161
+                DbHelperSQL.ExecuteSql(sql1, paras1);
162
+
163
+                if (issuccess)
164
+                {
165
+                    return Success("发送成功");
166
+                }
167
+                else
168
+                {
169
+                    return Error("发送失败");
170
+                }
171
+            }
172
+            else
173
+            {
174
+                return Error("找不到手机号");
175
+            }
176
+        }
66 177
     }
67 178
 }

+ 18 - 8
ZhuMaDian12345/Controllers/BaseController.cs

@@ -9,13 +9,6 @@ namespace ZhuMaDian12345.Controllers
9 9
 {
10 10
     public class BaseController : Controller
11 11
     {
12
-        /// <summary>
13
-        /// Log4net 日志
14
-        /// </summary>
15
-        public Log FileLog
16
-        {
17
-            get { return LogHelper.GetLogger(this.GetType().ToString() + "/" + this.ControllerContext.RouteData.Values["Action"].ToString()); }
18
-        }
19 12
 
20 13
         /// <summary>
21 14
         /// 成功的消息格式化
@@ -48,7 +41,24 @@ namespace ZhuMaDian12345.Controllers
48 41
         protected virtual ActionResult Error(string message)
49 42
         {
50 43
             var jsonMsg = new AjaxResult { state = ResultTypes.error.ToString(), message = message }.ToJson();
51
-            FileLog.Error(jsonMsg);
44
+
45
+            var cnt = Request.RequestContext.HttpContext;
46
+            var rt = cnt.Request;
47
+            Dictionary<string, string> Params = new Dictionary<string, string>();
48
+            Params.Add("request_url", rt.Url.ToString());
49
+
50
+            if (rt.HttpMethod.ToUpper() != "GET")
51
+            {
52
+                foreach (var key in rt.Params.AllKeys)
53
+                {
54
+                    if (key == "ALL_HTTP")
55
+                    {
56
+                        break;
57
+                    }
58
+                    Params.Add(key, rt.Params[key]);
59
+                }
60
+            }
61
+            LogHelper.Error("请求信息:" + Params.ToJson(), new Exception(message));
52 62
             return Content(jsonMsg);
53 63
         }
54 64
     }

+ 8 - 1
ZhuMaDian12345/Web.config

@@ -10,9 +10,16 @@
10 10
     <add key="ClientValidationEnabled" value="true" />
11 11
     <add key="UnobtrusiveJavaScriptEnabled" value="true" />
12 12
     <add key="OutSignCode" value="#ZhuMaDian+12345$" />
13
+    <add key="FilePath" value="https://12345sp1.jbdu.cn:9000" />
14
+
15
+    <!--短信配置-->
16
+    <add key="sms_url" value="" />
17
+    <add key="sms_username" value="" />
18
+    <add key="sms_password" value="" />
19
+    <add key="sms_content" value="" />
13 20
   </appSettings>
14 21
   <connectionStrings>
15
-    <add name="ConnectionString" connectionString="Data Source=192.168.8.3;User ID=sa;pwd=800100;Initial Catalog=SQ12345;" />
22
+    <add name="ConnectionString" connectionString="Data Source=192.168.8.3;User ID=sa;pwd=800100;Initial Catalog=ZMD12345;" />
16 23
   </connectionStrings>
17 24
   <system.web>
18 25
     <compilation debug="true" targetFramework="4.5" />