Blame view

netcore/src/Modularity/Extend/NCC.Extend/LqMallConfigService.cs 3.9 KB
e1dcb3a0   “wangming”   1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  using System;
  using System.Collections.Generic;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Logging;
  using NCC.Common.Core.Manager;
  using NCC.Dependency;
  using NCC.DynamicApiController;
  using NCC.Extend.Entitys.Dto.Mall;
  using NCC.Extend.Helpers;
  using NCC.Extend.Interfaces.BusinessOperationLog;
  using NCC.FriendlyException;
  using NCC.System.Entitys.System;
  using SqlSugar;
  using Yitter.IdGenerator;
  
  namespace NCC.Extend
  {
      /// <summary>
      /// 会员商城-支付配置(管理端)
      /// </summary>
      [ApiDescriptionSettings(Tag = "会员商城-配置", Name = "LqMallConfig", Order = 305)]
      [Route("api/Extend/LqMallConfig")]
      public class LqMallConfigService : IDynamicApiController, ITransient
      {
          private readonly IUserManager _userManager;
          private readonly ILogger<LqMallConfigService> _logger;
          private readonly ISqlSugarClient _db;
          private readonly IBusinessOperationLogService _log;
  
          /// <summary>
          /// 构造函数
          /// </summary>
          public LqMallConfigService(
              IUserManager userManager,
              ILogger<LqMallConfigService> logger,
              ISqlSugarClient db,
              IBusinessOperationLogService log)
          {
              _userManager = userManager;
              _logger = logger;
              _db = db;
              _log = log;
          }
  
          /// <summary>
          /// 获取商城支付配置
          /// </summary>
          /// <returns>支付配置</returns>
          [HttpGet("GetPayConfig")]
          public async Task<dynamic> GetPayConfigAsync()
          {
              var config = await MallPayConfigHelper.GetAsync(_db);
              return new
              {
                  mchId = config.MchId,
                  apiV3Key = config.ApiV3Key,
                  appId = config.AppId,
                  appSecret = config.AppSecret,
                  notifyUrl = config.NotifyUrl,
                  payEnabled = config.PayEnabledRaw
              };
          }
  
          /// <summary>
          /// 保存商城支付配置(逐项 upsert)
          /// </summary>
          /// <param name="input">支付配置</param>
          [HttpPost("SavePayConfig")]
          public async Task SavePayConfigAsync([FromBody] MallPayConfigInput input)
          {
              if (input == null) throw NCCException.Oh("参数不能为空");
  
              var items = new Dictionary<string, string>
              {
                  { MallPayConfigHelper.KeyMchId, input.MchId },
                  { MallPayConfigHelper.KeyApiV3Key, input.ApiV3Key },
                  { MallPayConfigHelper.KeyAppId, input.AppId },
                  { MallPayConfigHelper.KeyAppSecret, input.AppSecret },
                  { MallPayConfigHelper.KeyNotifyUrl, input.NotifyUrl },
                  { MallPayConfigHelper.KeyPayEnabled, input.PayEnabled }
              };
  
              foreach (var kv in items)
              {
                  var existing = await _db.Queryable<SysConfigEntity>()
                      .Where(x => x.Category == MallPayConfigHelper.Category && x.Key == kv.Key)
                      .FirstAsync();
  
                  if (existing == null)
                  {
                      await _db.Insertable(new SysConfigEntity
                      {
                          Id = YitIdHelper.NextId().ToString(),
                          Category = MallPayConfigHelper.Category,
                          Key = kv.Key,
                          Name = kv.Key,
                          Value = kv.Value
                      }).ExecuteCommandAsync();
                  }
                  else
                  {
                      existing.Value = kv.Value;
                      await _db.Updateable(existing).UpdateColumns(x => new { x.Value }).ExecuteCommandAsync();
                  }
              }
  
              await _log.WriteAsync("会员商城", "保存支付配置", "mall_config", MallPayConfigHelper.Category,
                  "保存商城微信支付配置", null);
          }
      }
  }