ZskkApi.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\library\Gm;
  4. use app\zskk\model\Mapping;
  5. use think\facade\Cache;
  6. use think\facade\Config;
  7. use Throwable;
  8. use think\exception\HttpResponseException;
  9. class ZskkApi extends Api
  10. {
  11. /**
  12. * 控制器中间件
  13. * @var array
  14. */
  15. protected array $middleware = [
  16. // 前置中间件先配置先执行
  17. // 解密中间件 执行顺序 before-1
  18. \app\common\middleware\Decrypt::class,
  19. // 解码中间件 执行顺序 before-2
  20. \app\common\middleware\Decode::class,
  21. // 后置中间件先配置后执行
  22. // 加密中间件 执行顺序 after-3
  23. \app\common\middleware\Encrypt::class,
  24. // 编码中间件 执行顺序 after-2
  25. \app\common\middleware\Encode::class,
  26. // 模糊中间件 执行顺序 after-1
  27. \app\common\middleware\Blur::class,
  28. ];
  29. /**
  30. * 无需解密的方法
  31. * 访问本控制器的此方法,无需解密
  32. * @var array
  33. */
  34. protected array $noNeedDecrypt = [];
  35. /**
  36. * 无需解码的方法
  37. * 访问本控制器的此方法,无需解码
  38. * @var array
  39. */
  40. protected array $noNeedDecode = [];
  41. /**
  42. * 无需模糊的方法
  43. * @var array
  44. */
  45. protected array $noNeedBlur = [];
  46. /**
  47. * 无需编码的方法
  48. * @var array
  49. */
  50. protected array $noNeedEncode = [];
  51. /**
  52. * 无需加密的方法
  53. * @var array
  54. */
  55. protected array $noNeedEncrypt = [];
  56. /**
  57. * 初始化
  58. * @throws Throwable
  59. * @throws HttpResponseException
  60. */
  61. public function initialize(): void
  62. {
  63. parent::initialize();
  64. // 接口调用初始化完成标签位
  65. // Event::trigger('zskkApiInit');
  66. $this->_initNeeds();
  67. }
  68. private function _initNeeds(): void
  69. {
  70. $noNeedDecrypt = action_in_arr($this->noNeedDecrypt);
  71. $noNeedDecode = action_in_arr($this->noNeedDecode);
  72. $noNeedEncode = action_in_arr($this->noNeedEncode);
  73. $noNeedEncrypt = action_in_arr($this->noNeedEncrypt);
  74. $noNeedBlur = action_in_arr($this->noNeedBlur);
  75. $this->request->noNeedDecrypt = $noNeedDecrypt;
  76. $this->request->noNeedDecode = $noNeedDecode;
  77. $this->request->noNeedEncode = $noNeedEncode;
  78. $this->request->noNeedEncrypt = $noNeedEncrypt;
  79. $this->request->noNeedBlur = $noNeedBlur;
  80. }
  81. /**
  82. * $params $data
  83. */
  84. public function makeModelData($data,$mapping): array
  85. {
  86. $info = [];
  87. foreach ($data as $k=>$v)
  88. {
  89. if($info[$mapping[$k]] ?? '')
  90. {
  91. continue;
  92. }
  93. $info[$mapping[$k]] = $v;
  94. }
  95. return $info;
  96. }
  97. public function getDecryptData($data): array
  98. {
  99. $key = 'zLxapoeqWYpoeqWY';
  100. $info = Gm::decrypt($key,$data);
  101. return json_decode(base64_decode($info),true);
  102. }
  103. public function makeEncryptData($data)
  104. {
  105. $key = Config::get('gm.key');
  106. $info = Gm::encrypt($key,(base64_encode(json_encode($data))));
  107. return $info;
  108. }
  109. public function getTokenData($token)
  110. {
  111. $data = Cache::get($token);
  112. return $data;
  113. }
  114. }