123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using Winsoft.GOV.XF.WebApi.WXCore.Services;
- using Senparc.Weixin.Entities;
- using Microsoft.Extensions.Logging;
- using Winsoft.GOV.XF.WebApi.WXCore.Models;
- using Senparc.Weixin.MP.CoreMvcExtension;
- using Winsoft.GOV.XF.WebApi.WXCore.Filters;
- using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
- using System.Net.Http.Headers;
- using System.IO;
- using Microsoft.AspNetCore.Http;
- using Newtonsoft.Json;
- using System.Text.RegularExpressions;
- using Newtonsoft.Json.Linq;
- // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
- namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
- {
- [Route("api/[controller]")]
- public class BundleController : BaseController
- {
- UsersService _usersService;
- BundlesService _bundlesService;
- AssetsService _assetsService;
- ImagesService _imagesService;
- XFApiService _xfApiService;
- WXApiService _wxApiService;
- public BundleController(IOptions<SenparcWeixinSetting> senparcWeixinSetting,
- ILoggerFactory loggerFactory, UsersService usersService,
- BundlesService bundlesService, AssetsService assetsService,
- ImagesService imagesService, XFApiService xfApiService,
- WXApiService wxApiService) : base(senparcWeixinSetting, loggerFactory)
- {
- _usersService = usersService;
- _bundlesService = bundlesService;
- _assetsService = assetsService;
- _imagesService = imagesService;
- _xfApiService = xfApiService;
- _wxApiService = wxApiService;
- }
- // GET: api/values
- [HttpGet("{id}")]
- [Produces(typeof(Bundle))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> Get(string id)
- {
- Bundle b = await _bundlesService.Get(id);
- if (b == null)
- return BadRequest();
- return Ok(b);
- }
- [HttpGet("detail/{id}")]
- [Produces(typeof(Bundle))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> GetDetail(string id)
- {
- Bundle b = await _bundlesService.Get(id);
- string failsMessage = String.Empty;
- if (b == null)
- return BadRequest();
- if (b.IsAssessed == 0)
- {
- Bundle r = await _xfApiService.GetBundleByQueryCode(b.SearchCode, e => failsMessage = e);
- if (r == null)
- return Ok(b);
- b.LetterId = r.LetterId;
- b.Reply = r.Reply;
- b.ResultFromXF = r.ResultFromXF;
- b.Status = r.Status;
- XFApiIsEvaluateResultData er = await _xfApiService.IsEvaluate(b.LetterId, e => failsMessage = e);
- if (er == null)
- b.IsEvaluate = 0;
- else
- {
- b.IsEvaluate = er.XFBMPJZT && er.ZRDWPJZT ? 1 : 0;
- }
-
- await _bundlesService.UpdateReply(b);
- }
- return Ok(b);
- }
- [HttpGet("QueryEvaluate/{id}")]
- [Produces(typeof(EvaluateModel))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> QueryEvaluate(string id)
- {
- Bundle b = await _bundlesService.Get(id);
- if (b == null)
- return BadRequest();
- string msg;
- EvaluateModel e = await _xfApiService.QueryEvaluate(b.SearchCode, m => msg = m);
- if (e == null)
- return BadRequest();
- return Ok(e);
- }
- //[HttpGet("reply/{id}")]
- //[Produces(typeof(Bundle))]
- //[WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- //[WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- //public async Task<IActionResult> GetReply(string id)
- //{
- // Bundle b = await _bundlesService.GetDetail(id);
- // string failsMessage = String.Empty;
- // if (b == null)
- // return BadRequest();
- // if (b.IsAssessed == 0)
- // {
- // Bundle r = await _xfApiService.GetBundleByQueryCode(b.SearchCode, e => failsMessage = e);
- // if (r == null)
- // return Ok(b);
- // b.LetterId = r.LetterId;
- // b.Reply = r.Reply;
- // b.ResultFromXF = r.ResultFromXF;
- // b.Status = r.Status;
- // //b.IsEvaluate = await _xfApiService.IsEvaluate(b.LetterId, e => failsMessage = e) ? 1 : 0;
- // }
- // return Ok(b);
- //}
- [HttpGet("bySearchCode/{searchCode}")]
- [Produces(typeof(Bundle))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> GetBySearchCode(string searchCode)
- {
- Bundle b = await _bundlesService.GetBySearchCode(searchCode);
- string failsMessage = String.Empty;
- if (b != null)
- return Ok(b);
- b = await _xfApiService.GetBundleByQueryCode(searchCode, e => failsMessage = e);
- if (b == null)
- return BadRequest("查无此信息");
- //b.IsAssessed = await _xfApiService.IsEvaluate(b.LetterId, e => failsMessage = e) ? 1 : 0;
- JObject jo = JsonConvert.DeserializeObject<JObject>(b.ResultFromXF);
- b.Describe = jo["letter"]["content"].ToString();
- b.Name = jo["letter"]["complainant"].ToString();
- b.Title = jo["letter"]["title"].ToString();
- b.OpenID = HttpContext.Session.GetString("OpenId");
- b.SearchCode = searchCode;
- WXUser u = await _usersService.GetUser(b.OpenID);
- b.UserId = u.Id;
- await _bundlesService.Add(b);
- return Ok(b);
- }
- [HttpPost("evaluate")]
- [Produces(typeof(bool))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> Evaluate([FromBody]EvaluateModel e)
- {
- string failsMessage = String.Empty;
- if (String.IsNullOrEmpty(e.BundleId))
- return BadRequest("无效评价目标");
- Bundle b = await _bundlesService.GetDetail(e.BundleId);
- if (b == null && String.IsNullOrEmpty(b.SearchCode))
- return BadRequest("无效评价目标");
- //if (!await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m))
- // return BadRequest("还不能对此信件的办理结果评价");
- e.cxm = b.SearchCode;
- bool r = await _xfApiService.SubmitEvaluate(e, m => failsMessage = m);
- if (r)
- {
- b.IsAssessed = 1;
- await _bundlesService.UpdateAssessed(b);
- }
- return Ok(r);
- }
- //[HttpPost("isevaluate/{id}")]
- //[Produces(typeof(XFApiIsEvaluateResultData))]
- //[WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- //[WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- //public async Task<IActionResult> IsEvaluate(string id)
- //{
- // string failsMessage = String.Empty;
- // if (String.IsNullOrEmpty(id))
- // return BadRequest("无效评价目标");
- // Bundle b = await _bundlesService.Get(id);
- // if (b == null && String.IsNullOrEmpty(b.LetterId))
- // return BadRequest("无效评价目标");
- // //if (!await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m))
- // // return BadRequest("还不能对此信件的办理结果评价");
- // XFApiIsEvaluateResultData r = await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m);
- // return Ok(r);
- //}
- //[HttpGet("IsEvaluate/{id}")]
- //[Produces(typeof(EvaluateModel))]
- //[WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- //[WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- //public async Task<IActionResult> IsEvaluate(string id)
- //{
- // string failsMessage = String.Empty;
- // if (String.IsNullOrEmpty(id))
- // return BadRequest("此信访信件不正确");
- // Bundle b = await _bundlesService.Get(id);
- // if (b == null && String.IsNullOrEmpty(b.SearchCode))
- // return BadRequest("此信访信件异常");
- // if (!await _xfApiService.IsEvaluate(b.LetterId, m => failsMessage = m))
- // return BadRequest("还不能对此信件的办理结果评价");
- // //EvaluateModel r = await _xfApiService.QueryEvaluate(b.SearchCode, m => failsMessage = m);
- // return Ok(r);
- //}
- [HttpGet("all")]
- [Produces(typeof(IEnumerable<Bundle>))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> GetAll()
- {
- IEnumerable<Bundle> list ;
- WXUser u = await _usersService.GetUser(HttpContext.Session.GetString("OpenId"));
- if (u != null)
- {
- list = await _bundlesService.GetByUserId(u.Id);
- if (list != null)
- return Ok(list);
- }
- return Ok(new List<Bundle>());
- }
- [HttpGet("generate/{id}/{ciphertext}")]
- [Produces(typeof(string))]
- public async Task<IActionResult> GenerateShareURL(string id, string ciphertext)
- {
- Bundle b = await _bundlesService.Get(id);
- if (b == null)
- return BadRequest("非法请求");
- string data = ciphertext.AESEncrypt(b.Key);
- BundleAccessToken a = JsonConvert.DeserializeObject<BundleAccessToken>(data);
- if (a == null)
- return BadRequest("非法请求");
- if (a.Expire != -1)
- {
- return BadRequest("您无权限分享");
- }
- string multimediaURL = _bundlesService.GenerateShareURL(b, a, HttpContext.Request.GetBaseURL().Replace("api/Bundle/generate/" + id + "/" + ciphertext, "", StringComparison.CurrentCultureIgnoreCase));
- if (!String.IsNullOrEmpty(multimediaURL))
- return Ok(multimediaURL);
- return BadRequest("无权限发布");
- }
- [HttpGet("generate/{id}")]
- [Produces(typeof(string))]
- public async Task<IActionResult> GenerateSampleShareURL(string id)
- {
- Bundle b = await _bundlesService.Get(id);
- if (b == null)
- return BadRequest("非法请求");
- string multimediaURL = _bundlesService.GenerateSampleShareURL(b, HttpContext.Request.GetBaseURL().Replace("api/Bundle/generate/" + id, "", StringComparison.CurrentCultureIgnoreCase));
- if (!String.IsNullOrEmpty(multimediaURL))
- return Ok(multimediaURL);
- return BadRequest("无权限发布");
- }
- [HttpGet("getShareInfo/{id}/{ciphertext}")]
- [Produces(typeof(Bundle))]
- public async Task<IActionResult> GetShareInfo(string id, string ciphertext)
- {
- Bundle b = await _bundlesService.GetDetail(id);
- if (b == null)
- return BadRequest("非法请求");
- string data = ciphertext.AESDecrypt(b.Key);
- BundleAccessToken a = JsonConvert.DeserializeObject<BundleAccessToken>(data);
- if (a == null)
- return BadRequest("非法请求");
- if (a.Expire != -1)
- {
- if (a.Expire < DateTime.Now.ToTimeStamp())
- return BadRequest("已过期");
- }
- return Ok(b);
- }
- [HttpGet("getShareInfo/{id}")]
- [Produces(typeof(Bundle))]
- public async Task<IActionResult> GetShareInfoByBundleId(string id)
- {
- Bundle b = await _bundlesService.GetDetail(id);
- if (b == null)
- return BadRequest("非法请求");
- return Ok(b);
- }
- // POST api/values
- [HttpPost]
- [Produces(typeof(string))]
- [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
- [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
- public async Task<IActionResult> Post([FromBody] Bundle value)
- {
- foreach(Asset a in value.Assets)
- {
- if (a.AssetType == AssetType.Image)
- {
- if (!_imagesService.SaveToDisk(a))
- return BadRequest("不是有效的图片格式");
- }
- //else if (a.AssetType == AssetType.Positon)
- // await _assetsService.Add(a);
- }
- string failsMessage = "";
- value.County_id = "004038694b4540c4b218cb22d011e10e";
- value.Unit_id = "2014101416995276299";
- value.OpenID = HttpContext.Session.GetString("OpenId");
- await _bundlesService.Add(value);
- if (await _xfApiService.PostLetter(value, HttpContext.Request.Cookies["BaseUrl"], e => failsMessage = e))
- {
- await _xfApiService.PostAssets(value);
- await _wxApiService.Notify(value.OpenID, String.Format("感谢您的来信,请记住您的信件查询码。通过查询码,您可以在浙江政务服务网上查询该信件的受理情况。同样,您可以在我们丽水微信端的12345政务咨询投诉举报平台上的“结果查询”中,查看信件的受理情况。\n查询码:{0}", value.SearchCode));
- return Ok();
- }
- await _bundlesService.Delete(value);
- return BadRequest(failsMessage);
- }
- // PUT api/values/5
- [HttpPut("{id}")]
- public void Put(int id, [FromBody]string value)
- {
- }
- // DELETE api/values/5
- [HttpDelete("{id}")]
- public void Delete(int id)
- {
- }
- }
- }
|