Controller.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\BaseController;
  14. use app\api\model\User as UserModel;
  15. use app\api\model\Store as StoreModel;
  16. use app\api\service\User as UserService;
  17. use app\common\exception\BaseException;
  18. use think\Cache;
  19. use think\facade\Log;
  20. use think\response\Json;
  21. /**
  22. * API控制器基类
  23. * Class BaseController
  24. * @package app\store\controller
  25. */
  26. class Controller extends BaseController
  27. {
  28. // 当前商城ID
  29. protected $storeId;
  30. const SSO_PREFIX = 'xcx_token_';
  31. /**
  32. * API基类初始化
  33. * @throws BaseException
  34. */
  35. public function initialize()
  36. {
  37. // 当前商城id
  38. $this->storeId = $this->getStoreId();
  39. // 验证当前商城状态
  40. $this->checkStore();
  41. //sso单点登陆校验,如果是非登录接口,都进里面
  42. /* $action = request()->action();
  43. $exist = method_exists( Passport::class, $action);
  44. Log::DEBUG('$exist::'.json_encode($exist));
  45. if ($exist == true){
  46. $front_token = request()->header('Access-Token');
  47. $user_id = $this->getLoginUser()['user_id'];
  48. $exist_token = Cache::get(self::SSO_PREFIX.$user_id);
  49. if ($exist_token && $front_token != $exist_token){
  50. throwError('当前账号已在另一个设备登录');
  51. }
  52. }*/
  53. }
  54. /**
  55. * 获取当前商城id
  56. * @return int|null
  57. * @throws BaseException
  58. */
  59. protected function getStoreId()
  60. {
  61. $storeId = getStoreId(); // app/api/common.php
  62. if (empty($storeId)) {
  63. throwError('缺少必要的参数:storeId');
  64. }
  65. return $storeId;
  66. }
  67. /**
  68. * 验证当前商城状态
  69. * @return bool
  70. * @throws BaseException
  71. */
  72. private function checkStore()
  73. {
  74. // 获取当前商城信息
  75. $store = StoreModel::detail($this->storeId);
  76. if (empty($store)) {
  77. throwError('当前商城信息不存在');
  78. }
  79. if ($store['is_recycle'] || $store['is_delete']) {
  80. throwError('当前商城已删除');
  81. }
  82. return true;
  83. }
  84. /**
  85. * 获取当前用户信息
  86. * @param bool $isForce 强制验证登录
  87. * @return UserModel|bool|null
  88. * @throws BaseException
  89. */
  90. protected function getLoginUser(bool $isForce = true)
  91. {
  92. return UserService::getCurrentLoginUser($isForce);
  93. }
  94. /**
  95. * 返回封装后的 API 数据到客户端
  96. * @param int|null $status 状态码
  97. * @param string $message
  98. * @param array $data
  99. * @return array|Json
  100. */
  101. protected function renderJson(int $status = null, string $message = '', array $data = [])
  102. {
  103. return json(compact('status', 'message', 'data'));
  104. }
  105. /**
  106. * 返回操作成功json
  107. * @param array|string $data
  108. * @param string $message
  109. * @return array
  110. */
  111. protected function renderSuccess($data = [], string $message = 'success')
  112. {
  113. if (is_string($data)) {
  114. $message = $data;
  115. $data = [];
  116. }
  117. return $this->renderJson(config('status.success'), $message, $data);
  118. }
  119. /**
  120. * 返回操作失败json
  121. * @param string $message
  122. * @param array $data
  123. * @return array
  124. */
  125. protected function renderError(string $message = 'error', array $data = [])
  126. {
  127. return $this->renderJson(config('status.error'), $message, $data);
  128. }
  129. /**
  130. * 获取post数据 (数组)
  131. * @param $key
  132. * @return mixed
  133. */
  134. protected function postData($key = null)
  135. {
  136. return $this->request->post(is_null($key) ? '' : $key . '/a');
  137. }
  138. /**
  139. * 获取post数据 (数组)
  140. * @param $key
  141. * @return mixed
  142. */
  143. protected function postForm($key = 'form')
  144. {
  145. return $this->postData($key);
  146. }
  147. /*
  148. * 自定义验证公共类
  149. * @param $rules 验证规则(数组)
  150. */
  151. protected function baseValidate($rules,$messages=[])
  152. {
  153. $params=$this->request->param();
  154. $result = $this->validate($params,$rules,$messages);
  155. if(true !== $result){
  156. // 验证失败 输出错误信息
  157. return $this->renderError($result);
  158. }
  159. return $params;
  160. }
  161. }