BundleController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Options;
  7. using Winsoft.GOV.XF.WebApi.WXCore.Services;
  8. using Senparc.Weixin.Entities;
  9. using Microsoft.Extensions.Logging;
  10. using Winsoft.GOV.XF.WebApi.WXCore.Models;
  11. using Senparc.Weixin.MP.CoreMvcExtension;
  12. using Winsoft.GOV.XF.WebApi.WXCore.Filters;
  13. using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
  14. using System.Net.Http.Headers;
  15. using System.IO;
  16. using Microsoft.AspNetCore.Http;
  17. using Newtonsoft.Json;
  18. using System.Text.RegularExpressions;
  19. using Newtonsoft.Json.Linq;
  20. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  21. namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
  22. {
  23. [Route("api/[controller]")]
  24. public class BundleController : BaseController
  25. {
  26. UsersService _usersService;
  27. BundlesService _bundlesService;
  28. AssetsService _assetsService;
  29. ImagesService _imagesService;
  30. XFApiService _xfApiService;
  31. WXApiService _wxApiService;
  32. public BundleController(IOptions<SenparcWeixinSetting> senparcWeixinSetting,
  33. ILoggerFactory loggerFactory, UsersService usersService,
  34. BundlesService bundlesService, AssetsService assetsService,
  35. ImagesService imagesService, XFApiService xfApiService,
  36. WXApiService wxApiService) : base(senparcWeixinSetting, loggerFactory)
  37. {
  38. _usersService = usersService;
  39. _bundlesService = bundlesService;
  40. _assetsService = assetsService;
  41. _imagesService = imagesService;
  42. _xfApiService = xfApiService;
  43. _wxApiService = wxApiService;
  44. }
  45. // GET: api/values
  46. [HttpGet("{id}")]
  47. [Produces(typeof(Bundle))]
  48. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  49. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  50. public async Task<IActionResult> Get(string id)
  51. {
  52. Bundle b = await _bundlesService.Get(id);
  53. if (b == null)
  54. return BadRequest();
  55. return Ok(b);
  56. }
  57. [HttpGet("detail/{id}")]
  58. [Produces(typeof(Bundle))]
  59. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  60. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  61. public async Task<IActionResult> GetDetail(string id)
  62. {
  63. Bundle b = await _bundlesService.Get(id);
  64. string failsMessage = String.Empty;
  65. if (b == null)
  66. return BadRequest();
  67. if (b.IsAssessed == 0)
  68. {
  69. Bundle r = await _xfApiService.GetBundleByQueryCode(b.SearchCode, e => failsMessage = e);
  70. if (r == null)
  71. return Ok(b);
  72. b.LetterId = r.LetterId;
  73. b.Reply = r.Reply;
  74. b.ResultFromXF = r.ResultFromXF;
  75. b.Status = r.Status;
  76. XFApiIsEvaluateResultData er = await _xfApiService.IsEvaluate(b.LetterId, e => failsMessage = e);
  77. if (er == null)
  78. b.IsEvaluate = 0;
  79. else
  80. {
  81. b.IsEvaluate = er.XFBMPJZT && er.ZRDWPJZT ? 1 : 0;
  82. }
  83. await _bundlesService.UpdateReply(b);
  84. }
  85. return Ok(b);
  86. }
  87. [HttpGet("QueryEvaluate/{id}")]
  88. [Produces(typeof(EvaluateModel))]
  89. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  90. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  91. public async Task<IActionResult> QueryEvaluate(string id)
  92. {
  93. Bundle b = await _bundlesService.Get(id);
  94. if (b == null)
  95. return BadRequest();
  96. string msg;
  97. EvaluateModel e = await _xfApiService.QueryEvaluate(b.SearchCode, m => msg = m);
  98. if (e == null)
  99. return BadRequest();
  100. return Ok(e);
  101. }
  102. //[HttpGet("reply/{id}")]
  103. //[Produces(typeof(Bundle))]
  104. //[WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  105. //[WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  106. //public async Task<IActionResult> GetReply(string id)
  107. //{
  108. // Bundle b = await _bundlesService.GetDetail(id);
  109. // string failsMessage = String.Empty;
  110. // if (b == null)
  111. // return BadRequest();
  112. // if (b.IsAssessed == 0)
  113. // {
  114. // Bundle r = await _xfApiService.GetBundleByQueryCode(b.SearchCode, e => failsMessage = e);
  115. // if (r == null)
  116. // return Ok(b);
  117. // b.LetterId = r.LetterId;
  118. // b.Reply = r.Reply;
  119. // b.ResultFromXF = r.ResultFromXF;
  120. // b.Status = r.Status;
  121. // //b.IsEvaluate = await _xfApiService.IsEvaluate(b.LetterId, e => failsMessage = e) ? 1 : 0;
  122. // }
  123. // return Ok(b);
  124. //}
  125. [HttpGet("bySearchCode/{searchCode}")]
  126. [Produces(typeof(Bundle))]
  127. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  128. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  129. public async Task<IActionResult> GetBySearchCode(string searchCode)
  130. {
  131. Bundle b = await _bundlesService.GetBySearchCode(searchCode);
  132. string failsMessage = String.Empty;
  133. if (b != null)
  134. return Ok(b);
  135. b = await _xfApiService.GetBundleByQueryCode(searchCode, e => failsMessage = e);
  136. if (b == null)
  137. return BadRequest("查无此信息");
  138. //b.IsAssessed = await _xfApiService.IsEvaluate(b.LetterId, e => failsMessage = e) ? 1 : 0;
  139. JObject jo = JsonConvert.DeserializeObject<JObject>(b.ResultFromXF);
  140. b.Describe = jo["letter"]["content"].ToString();
  141. b.Name = jo["letter"]["complainant"].ToString();
  142. b.Title = jo["letter"]["title"].ToString();
  143. b.OpenID = HttpContext.Session.GetString("OpenId");
  144. b.SearchCode = searchCode;
  145. WXUser u = await _usersService.GetUser(b.OpenID);
  146. b.UserId = u.Id;
  147. await _bundlesService.Add(b);
  148. return Ok(b);
  149. }
  150. [HttpPost("evaluate")]
  151. [Produces(typeof(bool))]
  152. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  153. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  154. public async Task<IActionResult> Evaluate([FromBody]EvaluateModel e)
  155. {
  156. string failsMessage = String.Empty;
  157. if (String.IsNullOrEmpty(e.BundleId))
  158. return BadRequest("无效评价目标");
  159. Bundle b = await _bundlesService.GetDetail(e.BundleId);
  160. if (b == null && String.IsNullOrEmpty(b.SearchCode))
  161. return BadRequest("无效评价目标");
  162. //if (!await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m))
  163. // return BadRequest("还不能对此信件的办理结果评价");
  164. e.cxm = b.SearchCode;
  165. bool r = await _xfApiService.SubmitEvaluate(e, m => failsMessage = m);
  166. if (r)
  167. {
  168. b.IsAssessed = 1;
  169. await _bundlesService.UpdateAssessed(b);
  170. }
  171. return Ok(r);
  172. }
  173. //[HttpPost("isevaluate/{id}")]
  174. //[Produces(typeof(XFApiIsEvaluateResultData))]
  175. //[WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  176. //[WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  177. //public async Task<IActionResult> IsEvaluate(string id)
  178. //{
  179. // string failsMessage = String.Empty;
  180. // if (String.IsNullOrEmpty(id))
  181. // return BadRequest("无效评价目标");
  182. // Bundle b = await _bundlesService.Get(id);
  183. // if (b == null && String.IsNullOrEmpty(b.LetterId))
  184. // return BadRequest("无效评价目标");
  185. // //if (!await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m))
  186. // // return BadRequest("还不能对此信件的办理结果评价");
  187. // XFApiIsEvaluateResultData r = await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m);
  188. // return Ok(r);
  189. //}
  190. //[HttpGet("IsEvaluate/{id}")]
  191. //[Produces(typeof(EvaluateModel))]
  192. //[WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  193. //[WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  194. //public async Task<IActionResult> IsEvaluate(string id)
  195. //{
  196. // string failsMessage = String.Empty;
  197. // if (String.IsNullOrEmpty(id))
  198. // return BadRequest("此信访信件不正确");
  199. // Bundle b = await _bundlesService.Get(id);
  200. // if (b == null && String.IsNullOrEmpty(b.SearchCode))
  201. // return BadRequest("此信访信件异常");
  202. // if (!await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m))
  203. // return BadRequest("还不能对此信件的办理结果评价");
  204. // //EvaluateModel r = await _xfApiService.QueryEvaluate(b.SearchCode, m => failsMessage = m);
  205. // return Ok(r);
  206. //}
  207. [HttpGet("all")]
  208. [Produces(typeof(IEnumerable<Bundle>))]
  209. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  210. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  211. public async Task<IActionResult> GetAll()
  212. {
  213. IEnumerable<Bundle> list ;
  214. WXUser u = await _usersService.GetUser(HttpContext.Session.GetString("OpenId"));
  215. if (u != null)
  216. {
  217. list = await _bundlesService.GetByUserId(u.Id);
  218. if (list != null)
  219. return Ok(list);
  220. }
  221. return Ok(new List<Bundle>());
  222. }
  223. [HttpGet("generate/{id}/{ciphertext}")]
  224. [Produces(typeof(string))]
  225. public async Task<IActionResult> GenerateShareURL(string id, string ciphertext)
  226. {
  227. Bundle b = await _bundlesService.Get(id);
  228. if (b == null)
  229. return BadRequest("非法请求");
  230. string data = ciphertext.AESEncrypt(b.Key);
  231. BundleAccessToken a = JsonConvert.DeserializeObject<BundleAccessToken>(data);
  232. if (a == null)
  233. return BadRequest("非法请求");
  234. if (a.Expire != -1)
  235. {
  236. return BadRequest("您无权限分享");
  237. }
  238. string multimediaURL = _bundlesService.GenerateShareURL(b, a, HttpContext.Request.GetBaseURL().Replace("api/Bundle/generate/" + id + "/" + ciphertext, "", StringComparison.CurrentCultureIgnoreCase));
  239. if (!String.IsNullOrEmpty(multimediaURL))
  240. return Ok(multimediaURL);
  241. return BadRequest("无权限发布");
  242. }
  243. [HttpGet("generate/{id}")]
  244. [Produces(typeof(string))]
  245. public async Task<IActionResult> GenerateSampleShareURL(string id)
  246. {
  247. Bundle b = await _bundlesService.Get(id);
  248. if (b == null)
  249. return BadRequest("非法请求");
  250. string multimediaURL = _bundlesService.GenerateSampleShareURL(b, HttpContext.Request.GetBaseURL().Replace("api/Bundle/generate/" + id, "", StringComparison.CurrentCultureIgnoreCase));
  251. if (!String.IsNullOrEmpty(multimediaURL))
  252. return Ok(multimediaURL);
  253. return BadRequest("无权限发布");
  254. }
  255. [HttpGet("getShareInfo/{id}/{ciphertext}")]
  256. [Produces(typeof(Bundle))]
  257. public async Task<IActionResult> GetShareInfo(string id, string ciphertext)
  258. {
  259. Bundle b = await _bundlesService.GetDetail(id);
  260. if (b == null)
  261. return BadRequest("非法请求");
  262. string data = ciphertext.AESDecrypt(b.Key);
  263. BundleAccessToken a = JsonConvert.DeserializeObject<BundleAccessToken>(data);
  264. if (a == null)
  265. return BadRequest("非法请求");
  266. if (a.Expire != -1)
  267. {
  268. if (a.Expire < DateTime.Now.ToTimeStamp())
  269. return BadRequest("已过期");
  270. }
  271. return Ok(b);
  272. }
  273. [HttpGet("getShareInfo/{id}")]
  274. [Produces(typeof(Bundle))]
  275. public async Task<IActionResult> GetShareInfoByBundleId(string id)
  276. {
  277. Bundle b = await _bundlesService.GetDetail(id);
  278. if (b == null)
  279. return BadRequest("非法请求");
  280. return Ok(b);
  281. }
  282. // POST api/values
  283. [HttpPost]
  284. [Produces(typeof(string))]
  285. [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
  286. [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
  287. public async Task<IActionResult> Post([FromBody] Bundle value)
  288. {
  289. foreach(Asset a in value.Assets)
  290. {
  291. if (a.AssetType == AssetType.Image)
  292. {
  293. if (!_imagesService.SaveToDisk(a))
  294. return BadRequest("不是有效的图片格式");
  295. }
  296. //else if (a.AssetType == AssetType.Positon)
  297. // await _assetsService.Add(a);
  298. }
  299. string failsMessage = "";
  300. value.County_id = "004038694b4540c4b218cb22d011e10e";
  301. value.Unit_id = "2014101416995276299";
  302. value.OpenID = HttpContext.Session.GetString("OpenId");
  303. await _bundlesService.Add(value);
  304. if (await _xfApiService.PostLetter(value, HttpContext.Request.Cookies["BaseUrl"], e => failsMessage = e))
  305. {
  306. await _xfApiService.PostAssets(value);
  307. await _wxApiService.Notify(value.OpenID, String.Format("感谢您的来信,请记住您的信件查询码。通过查询码,您可以在浙江政务服务网上查询该信件的受理情况。同样,您可以在我们丽水微信端的12345政务咨询投诉举报平台上的“结果查询”中,查看信件的受理情况。\n查询码:{0}", value.SearchCode));
  308. return Ok();
  309. }
  310. await _bundlesService.Delete(value);
  311. return BadRequest(failsMessage);
  312. }
  313. // PUT api/values/5
  314. [HttpPut("{id}")]
  315. public void Put(int id, [FromBody]string value)
  316. {
  317. }
  318. // DELETE api/values/5
  319. [HttpDelete("{id}")]
  320. public void Delete(int id)
  321. {
  322. }
  323. }
  324. }