自定义控制器模板,

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using RG3.DO.Abstractions.Interfaces;
using RG3.PF.Abstractions.Entity;
using RG3.PF.Abstractions.Enums;
using RG3.PF.Utilities;
using RG3.PF.Utilities.Services;
using RG3.PF.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace RG3.PF.Qu.Controller
{
    /// <summary>
    ///  地图 GIS  2021-08-04 cbg
    /// </summary>
    [Route("bo/api/v3/map")]
    [ApiController]
    [ApiExplorerSettings(IgnoreApi = true)]
    public partial class MapController : BaseController
    {
        /// <summary>
        /// http请求上下文对象
        /// </summary>
        private readonly IHttpContextAccessor _accessor;

        /// <summary>
        /// 配置文件读取
        /// </summary>
        public readonly IConfigurationBuilderProvider _configurationBuilderService;

        /// <summary>
        /// 第三方http/https等接口调用
        /// </summary>
        private readonly IHttpClientFactory _httpClientFactory;


        /// <summary>
        /// 数据库操作
        /// </summary>
        private readonly IDbContext _dbContext;

        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="httpClientFactory"></param>
        /// <param name="accessor"></param>
        /// <param name="configurationBuilderService"></param>
        public MapController(IDbContext dbContext,IHttpClientFactory httpClientFactory, IHttpContextAccessor accessor, IConfigurationBuilderProvider configurationBuilderService)
        {
            _accessor = accessor;
            _configurationBuilderService = configurationBuilderService;
            _httpClientFactory = httpClientFactory;
            _dbContext = dbContext;
        }


        //https://cdn.rg1008.com/data/area/nation/100000.json
        //https://cdn.rg1008.com/data/area/province/350000.json
        //https://cdn.rg1008.com/data/area/city/350100.json
        //https://cdn.rg1008.com/data/area/county/310109.json


        //"cdn": {
        //  "root": "http://localhost:8800",
        //  "map": {
        //    "url": "{root}/data/area/{dir}/{code}.json",
        //    "varsion": "2020"
        //  }
        //},
        /// <summary>
        /// 获取 行政区静态数据 解决跨域等问题
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("{dir}/{code}")]
        public async Task<IActionResult> Get(string dir, string code)
        {
            var pf = new PFGlobalParameter { OwnerId = RequestPFUtil.GetOwnerId(this.HttpContext), SysId = RequestPFUtil.GetSysId(this.HttpContext) };

            IConfiguration _conf = _configurationBuilderService.GetConfigurationApp(pf.OwnerId, pf.SysId);

            var root = _conf.GetValue<string>("cdn:root") ?? "http://localhost:8800";
            var map = _conf.GetValue<string>("cdn:map:url") ?? "{root}/data/area/{dir}/{code}.json";
            var mapVersion = _conf.GetValue<string>("cdn:map:version");

            StringBuilder sbUrl = new StringBuilder(map);
            sbUrl.Replace("{root}", root);
            sbUrl.Replace("{ownerId}", pf.OwnerId);
            sbUrl.Replace("{dir}", dir);
            sbUrl.Replace("{code}", code);
            sbUrl.Replace("{mapVersion}", mapVersion);


            HttpClient httpClient = _httpClientFactory.CreateClient(HttpClientEnum.Owner.ToString());
            string tokenResult = Encoding.UTF8.GetString(httpClient.GetByteArrayAsync(sbUrl.ToString()).Result);

            var result = JsonUtil.Deserialize<JToken>(tokenResult);

            result["code"] = "0";
            result["success"] = true;
            result["retCode"] = "0";

            return new JsonResult(result);
        }
    }
}
文档更新时间: 2022-09-18 13:25   作者:admin