Blame view

addons/exam/controller/Base.php 2.13 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
  <?php

  

  namespace addons\exam\controller;

  

  use app\common\controller\Api;

  use think\Lang;

  

  class Base extends Api

  {

      public function _initialize()

      {

          parent::_initialize();

  

          $this->loadCommonFile();

  

          $controller = strtolower($this->request->controller());

          $this->loadlang($controller);

  

          $this->getAppVersion();

      }

  

      /**

       * 加载公共函数库文件

       */

      protected function loadCommonFile()

      {

          require_once ROOT_PATH . 'addons/exam/helper.php';

      }

  

      /**

       * 加载后台语言包

       * @param string $name

       */

      protected function loadlang($name)

      {

          $lang = $this->request->langset();

          $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';

          Lang::load(APP_PATH . '/admin/lang/' . $lang . '/exam/' . str_replace('.', '/', $name) . '.php');

      }

  

      /**

       * 加载用户信息

       */

      protected function loadUserData()

      {

          if (!$this->auth->isLogin()) {

              return;

          }

  

      }

  

      /**

       * 接口执行后统一的返回格式

       * @param Closure $closure

       * @param string  $error_msg

       * @param array   $success_data 带return_result时返回结果给前端

       * @return array|void

       */

      protected function operateResult(\Closure $closure, string $error_msg = '操作失败,请重试', array $success_data = [])

      {

          if ($result = $closure()) {

              if ($success_data && isset($success_data['return_result'])) {

                  succ($result);

              }

              succ($success_data);

          }

  

          fail($error_msg);

      }

  

      /**

       * 获取前端版本号

       * @return int

       */

      protected function getAppVersion()

      {

          $app_version = $this->request->header('app-version', '1.0.0');

          $app_version = str_replace('.', '', $app_version);

          $app_version = is_numeric($app_version) ? intval($app_version) : 100;

  

          if (!defined('APP_VERSION')) {

              define('APP_VERSION', $app_version);

          }

  

          return $app_version;

      }

  

  }