EncryDemoDataMiddleware.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace app\adminapi\http\middleware;
  16. /**
  17. * 演示环境数据加密
  18. * Class DemoDataMiddleware
  19. * @package app\adminapi\http\middleware
  20. */
  21. class EncryDemoDataMiddleware
  22. {
  23. // 需要过滤的接口
  24. protected $needCheck = [
  25. // 存储配置
  26. 'setting.storage/detail',
  27. // 短信配置
  28. 'notice.smsConfig/detail',
  29. // 公众号配置
  30. 'channel.official_account_setting/getConfig',
  31. // 小程序配置
  32. 'channel.mnp_settings/getConfig',
  33. // 开放平台配置
  34. 'channel.open_setting/getConfig',
  35. // 支付配置
  36. 'setting.pay.pay_config/getConfig',
  37. ];
  38. // 可以排除字段
  39. protected $excludeParams = [
  40. 'name',
  41. 'icon',
  42. 'image',
  43. 'qr_code',
  44. 'interface_version',
  45. 'merchant_type',
  46. ];
  47. public function handle($request, \Closure $next)
  48. {
  49. $response = $next($request);
  50. // 非需校验的接口 或者 未开启演示模式
  51. $accessUri = strtolower($request->controller() . '/' . $request->action());
  52. if (!in_array($accessUri, lower_uri($this->needCheck)) || !env('project.demo_env')) {
  53. return $response;
  54. }
  55. // 非json数据
  56. if (!method_exists($response, 'header') || !in_array('application/json; charset=utf-8', $response->getHeader())) {
  57. return $response;
  58. }
  59. $data = $response->getData();
  60. if (!is_array($data) || empty($data)) {
  61. return $response;
  62. }
  63. foreach ($data['data'] as $key => $item) {
  64. // 字符串
  65. if (is_string($item)) {
  66. $data['data'][$key] = $this->getEncryData($key, $item);
  67. continue;
  68. }
  69. // 数组
  70. if (is_array($item)) {
  71. foreach ($item as $itemKey => $itemValue) {
  72. $data['data'][$key][$itemKey] = $this->getEncryData($itemKey, $itemValue);
  73. }
  74. }
  75. }
  76. return $response->data($data);
  77. }
  78. /**
  79. * @notes 加密配置
  80. * @param $key
  81. * @param $value
  82. * @return mixed|string
  83. * @author 段誉
  84. * @date 2023/3/6 11:49
  85. */
  86. protected function getEncryData($key, $value)
  87. {
  88. // 非隐藏字段
  89. if (in_array($key, $this->excludeParams)) {
  90. return $value;
  91. }
  92. // 隐藏字段
  93. if (is_string($value)) {
  94. return '******';
  95. }
  96. return $value;
  97. }
  98. }