TimeTask.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using Common;
  2. using Common.Model;
  3. using Common.Wechat;
  4. using CoreEntity.Entity;
  5. using JCSoft.WX.Framework.Api;
  6. using JCSoft.WX.Framework.Extensions;
  7. using JCSoft.WX.Framework.Models;
  8. using JCSoft.WX.Framework.Models.ApiRequests;
  9. using JCSoft.WX.Framework.Models.ApiResponses;
  10. using JCSoft.WX.Framework.Models.Exceptions;
  11. using Newtonsoft.Json;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Data.SqlClient;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using ZcPeng.PublicLibrary;
  19. using ZcPeng.weixin.PublicAccount;
  20. namespace CoreEntity.TimeJob
  21. {
  22. public class TimeTask
  23. {
  24. private int _id;
  25. private string _hour;
  26. private string _minute;
  27. private string _second;
  28. public TimeTask()
  29. {
  30. }
  31. public int Id
  32. {
  33. get { return _id; }
  34. set { _id = value; }
  35. }
  36. public string Hour
  37. {
  38. get { return _hour; }
  39. set { _hour = value; }
  40. }
  41. public string Minute
  42. {
  43. get { return _minute; }
  44. set { _minute = value; }
  45. }
  46. public string Second
  47. {
  48. get { return _second; }
  49. set { _second = value; }
  50. }
  51. public static Message sendSupplyMassMesage(IApiClient _client,
  52. Int32 PushAccountId,
  53. string RecordId,//
  54. string GoodsName,
  55. string StoreGapNums,
  56. string TotalAmount,
  57. ////string GoodsSpec,
  58. ////string BrandName,
  59. ////decimal LastPrice,
  60. DateTime EndTime)
  61. {
  62. string accountName = string.Empty;
  63. string accountPassword = string.Empty;
  64. string WxOpenId = "";
  65. //用户账户,密码
  66. #region 获取user
  67. string result = "";
  68. DataTable dt = new DataTable();
  69. string commandTextUser = "Select roleId,accountName,AccountPhone,AccountPassWord,WxOpenId From "
  70. + Config.TablePrefix + "Account Where Id=@PushAccountId";
  71. List<List<Object>> parametersRecord = new List<List<Object>>();
  72. parametersRecord.Add(new List<Object>() { "PushAccountId", PushAccountId });
  73. List<SqlParameter> parameters1 = DataAccess.ToParameters(parametersRecord);
  74. bool result1 = DataAccess.GetValues(commandTextUser, ref dt, parameters1.ToArray(), out result);
  75. List<Account> users = new List<Account>();
  76. if (dt != null && dt.Rows.Count > 0)
  77. {
  78. // 把DataTable转换为IList<UserInfo>
  79. users = (List<Account>)ModelConvertHelper<Account>.ConvertToModel(dt);
  80. accountName = users.ToArray()[0].AccountName;
  81. accountPassword = users.ToArray()[0].AccountPassWord;
  82. WxOpenId = users.ToArray()[0].WxOpenId;
  83. }
  84. #endregion
  85. if(WxOpenId != "" && WxOpenId != null) {
  86. #region 推送信息
  87. //ErrorMessage errorMessage;
  88. //long message = -1;
  89. //string content = "我们有新的采购需求:"+""
  90. // +",请登陆:http:///coop.360lj.com:8000 填写您的可供货数量";
  91. //message = MassMessage.Send(Config.WeChatAppName, true, "group", MassMessageTypeEnum.text,
  92. // content, out errorMessage);
  93. Dictionary<string, TemplateDataProperty> temp = new Dictionary<string, TemplateDataProperty>();
  94. TemplateDataProperty tmpdatap = new TemplateDataProperty();
  95. tmpdatap.Value = "采购需求推送:";
  96. tmpdatap.Color = "#173177";
  97. temp.Add("first", tmpdatap);
  98. //产品品种需求数量
  99. tmpdatap = new TemplateDataProperty();
  100. tmpdatap.Value = GoodsName;
  101. tmpdatap.Color = "#173177";
  102. temp.Add("keyword1", tmpdatap);
  103. //tmpdatap = new TemplateDataProperty();
  104. //tmpdatap.Value = GoodsSpec;
  105. //tmpdatap.Color = "#173177";
  106. //temp.Add("GoodsSpec", tmpdatap);
  107. //tmpdatap = new TemplateDataProperty();
  108. //tmpdatap.Value = StoreGapNum.ToString();
  109. //tmpdatap.Color = "#173177";
  110. //temp.Add("StoreGapNum", tmpdatap);
  111. //tmpdatap = new TemplateDataProperty();
  112. //tmpdatap.Value = LastPrice.ToString();
  113. //tmpdatap.Color = "#173177";
  114. //temp.Add("LastPrice", tmpdatap);
  115. tmpdatap = new TemplateDataProperty();
  116. tmpdatap.Value = StoreGapNums;
  117. tmpdatap.Color = "#173177";
  118. temp.Add("keyword2", tmpdatap);
  119. tmpdatap = new TemplateDataProperty();
  120. tmpdatap.Value = TotalAmount;
  121. tmpdatap.Color = "#173177";
  122. temp.Add("keyword3", tmpdatap);
  123. tmpdatap = new TemplateDataProperty();
  124. tmpdatap.Value = "https://" + Config.Host;
  125. tmpdatap.Color = "#173177";
  126. temp.Add("keyword4", tmpdatap);
  127. tmpdatap = new TemplateDataProperty();
  128. string content = "请登陆我们的系统填写可供货数量:http://" + Config.Host + ":8000" +
  129. " 账户:" + accountName +
  130. " 密码:" + accountPassword +
  131. " 截止时间:" + EndTime.ToString()
  132. ;
  133. tmpdatap.Value = content;
  134. tmpdatap.Color = "#173177";
  135. temp.Add("remark", tmpdatap);
  136. //加入测试公众号
  137. AccountInfo account = new AccountInfo(Config.WeChatAppName,
  138. Config.WeChatAppKey, Config.WeChatAppSecret, null, null);
  139. AccountInfoCollection.SetAccountInfo(account);
  140. //加入正式公众号
  141. //AccountInfoCollection.SetAccountInfo(new AccountInfo("YourId2", "AppId", "AppSecret", "Token", "EncodingAesKey", "非測試"));
  142. ZcPeng.weixin.PublicAccount.AccessToken token = ZcPeng.weixin.PublicAccount.AccessToken.Get(Config.WeChatAppName);
  143. if (token == null)
  144. {
  145. ErrorMessage errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取许可令牌失败。");
  146. Message msg = new Message(Guid.NewGuid(), MessageType.Request, errorMessage.errcode == 0 ? "" : errorMessage.errcode.ToString(),
  147. DateTime.Now, "TemplateSend", errorMessage.errcode + ":" +errorMessage.errmsg);
  148. Message.Insert(msg);
  149. return null;
  150. }
  151. var request = new TemplateSendRequest
  152. {
  153. AccessToken = token.access_token,
  154. ToUser = WxOpenId,
  155. TemplateID = "SnuX3WGoLBBEUs9wnwyqur0ocdypUk2euX45BIj9JKk",
  156. TopColor = "#FF0000",
  157. Url = "https://" + Config.Host + "/web/supplymobile/listData?RecordId=" + RecordId,
  158. Data = temp,
  159. };
  160. Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
  161. log.log("发模板消息前:"+token.ToString());
  162. if (_client != null){
  163. var response = _client.Execute(request);
  164. TemplateSendResponse resp = (TemplateSendResponse)response;
  165. Message msg = new Message(Guid.NewGuid(), MessageType.Request, content,
  166. DateTime.Now, "TemplateSend", resp.ErrorMessage);
  167. Message.Insert(msg);
  168. return msg;
  169. }
  170. else{
  171. string uri = string.Format(request.GetUrlFormat, token.access_token);
  172. var url = $"{new WXOptions().ApiUrl}{uri}";
  173. log.log("发模板消息前 url:" + url);
  174. var json = request.GetPostContentJson();
  175. log.log("发模板消息前 json:" + json);
  176. string responseContent = HttpHelper.HttpPost(url, json, "application/text").Result;
  177. TemplateSendResponse resp = JsonConvert.DeserializeObject<TemplateSendResponse>(responseContent);
  178. Message msg = new Message(Guid.NewGuid(), MessageType.Request, content,
  179. DateTime.Now, "TemplateSend",resp.ErrorCode+":"+ resp.ErrorMessage);
  180. Message.Insert(msg);
  181. return msg;
  182. }
  183. #endregion
  184. }
  185. return null;
  186. }
  187. }
  188. }