Base.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace addons\exam\controller;
  3. use app\common\controller\Api;
  4. use think\Lang;
  5. class Base extends Api
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->loadCommonFile();
  11. $controller = strtolower($this->request->controller());
  12. $this->loadlang($controller);
  13. $this->getAppVersion();
  14. }
  15. /**
  16. * 加载公共函数库文件
  17. */
  18. protected function loadCommonFile()
  19. {
  20. require_once ROOT_PATH . 'addons/exam/helper.php';
  21. }
  22. /**
  23. * 加载后台语言包
  24. * @param string $name
  25. */
  26. protected function loadlang($name)
  27. {
  28. $lang = $this->request->langset();
  29. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  30. Lang::load(APP_PATH . '/admin/lang/' . $lang . '/exam/' . str_replace('.', '/', $name) . '.php');
  31. }
  32. /**
  33. * 加载用户信息
  34. */
  35. protected function loadUserData()
  36. {
  37. if (!$this->auth->isLogin()) {
  38. return;
  39. }
  40. }
  41. /**
  42. * 接口执行后统一的返回格式
  43. * @param Closure $closure
  44. * @param string $error_msg
  45. * @param array $success_data 带return_result时返回结果给前端
  46. * @return array|void
  47. */
  48. protected function operateResult(\Closure $closure, string $error_msg = '操作失败,请重试', array $success_data = [])
  49. {
  50. if ($result = $closure()) {
  51. if ($success_data && isset($success_data['return_result'])) {
  52. succ($result);
  53. }
  54. succ($success_data);
  55. }
  56. fail($error_msg);
  57. }
  58. /**
  59. * 获取前端版本号
  60. * @return int
  61. */
  62. protected function getAppVersion()
  63. {
  64. $app_version = $this->request->header('app-version', '1.0.0');
  65. $app_version = str_replace('.', '', $app_version);
  66. $app_version = is_numeric($app_version) ? intval($app_version) : 100;
  67. if (!defined('APP_VERSION')) {
  68. define('APP_VERSION', $app_version);
  69. }
  70. return $app_version;
  71. }
  72. }