Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

72 rader
2.0 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/10/22
  6. * Time: 9:34 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use app\common\controller\Api;
  10. use app\common\library\Auth;
  11. use think\Cache;
  12. use think\Lang;
  13. class Base extends Api
  14. {
  15. /**
  16. * 无需鉴权的方法,但需要登录
  17. * @var array
  18. */
  19. protected $noNeedRight = ['*'];
  20. /**
  21. * 允许频繁访问的接口(方法格式:小写)
  22. * @var array
  23. */
  24. protected $frequently = [];
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->loadUniShopLang();
  29. $this->limitVisit();
  30. }
  31. /**
  32. * 限制接口访问频率
  33. * @param int $millisecond
  34. */
  35. public function limitVisit($millisecond = 200) {
  36. $millisecond = $this->request->request('millisecond', $millisecond);
  37. // 限制200毫秒 防止1秒两刀 (双击甚至三击,同一时间导致接口请求两次以上)
  38. $action = $this->request->action();
  39. if (!in_array($action, $this->frequently) && $this->auth && $this->auth->isLogin() && $millisecond > 0) {
  40. $controller = $this->request->controller();
  41. if (Cache::has($controller.'_'.$action.'_'.$this->auth->id)) {
  42. if (Cache::get($controller.'_'.$action.'_'.$this->auth->id) + $millisecond > \addons\unishop\model\Config::getMillisecond()) {
  43. $this->error(__('Frequent interface requests'));
  44. }
  45. }
  46. Cache::set($controller.'_'.$action.'_'.$this->auth->id, \addons\unishop\model\Config::getMillisecond(), 1);
  47. }
  48. }
  49. /**
  50. * 加载语言文件
  51. */
  52. protected function loadUniShopLang()
  53. {
  54. $route = $this->request->route();
  55. $lang = $this->request->header('lang') ?: 'zh-cn';
  56. $path = ADDON_PATH . $route['addon'] . '/lang/' . $lang . '/' . str_replace('.', '/', $route['controller']) . '.php';
  57. Lang::load(ADDON_PATH . $route['addon'] . '/lang/'.$lang.'.php'); // 默认语言包
  58. Lang::load($path);
  59. }
  60. }