123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\controller;
- use app\BaseController;
- use app\api\model\User as UserModel;
- use app\api\model\Store as StoreModel;
- use app\api\service\User as UserService;
- use app\common\exception\BaseException;
- use think\Cache;
- use think\facade\Log;
- use think\response\Json;
- /**
- * API控制器基类
- * Class BaseController
- * @package app\store\controller
- */
- class Controller extends BaseController
- {
- // 当前商城ID
- protected $storeId;
- const SSO_PREFIX = 'xcx_token_';
- /**
- * API基类初始化
- * @throws BaseException
- */
- public function initialize()
- {
- // 当前商城id
- $this->storeId = $this->getStoreId();
- // 验证当前商城状态
- $this->checkStore();
- //sso单点登陆校验,如果是非登录接口,都进里面
- /* $action = request()->action();
- $exist = method_exists( Passport::class, $action);
- Log::DEBUG('$exist::'.json_encode($exist));
- if ($exist == true){
- $front_token = request()->header('Access-Token');
- $user_id = $this->getLoginUser()['user_id'];
- $exist_token = Cache::get(self::SSO_PREFIX.$user_id);
- if ($exist_token && $front_token != $exist_token){
- throwError('当前账号已在另一个设备登录');
- }
- }*/
- }
- /**
- * 获取当前商城id
- * @return int|null
- * @throws BaseException
- */
- protected function getStoreId()
- {
- $storeId = getStoreId(); // app/api/common.php
- if (empty($storeId)) {
- throwError('缺少必要的参数:storeId');
- }
- return $storeId;
- }
- /**
- * 验证当前商城状态
- * @return bool
- * @throws BaseException
- */
- private function checkStore()
- {
- // 获取当前商城信息
- $store = StoreModel::detail($this->storeId);
- if (empty($store)) {
- throwError('当前商城信息不存在');
- }
- if ($store['is_recycle'] || $store['is_delete']) {
- throwError('当前商城已删除');
- }
- return true;
- }
- /**
- * 获取当前用户信息
- * @param bool $isForce 强制验证登录
- * @return UserModel|bool|null
- * @throws BaseException
- */
- protected function getLoginUser(bool $isForce = true)
- {
- return UserService::getCurrentLoginUser($isForce);
- }
- /**
- * 返回封装后的 API 数据到客户端
- * @param int|null $status 状态码
- * @param string $message
- * @param array $data
- * @return array|Json
- */
- protected function renderJson(int $status = null, string $message = '', array $data = [])
- {
- return json(compact('status', 'message', 'data'));
- }
- /**
- * 返回操作成功json
- * @param array|string $data
- * @param string $message
- * @return array
- */
- protected function renderSuccess($data = [], string $message = 'success')
- {
- if (is_string($data)) {
- $message = $data;
- $data = [];
- }
- return $this->renderJson(config('status.success'), $message, $data);
- }
- /**
- * 返回操作失败json
- * @param string $message
- * @param array $data
- * @return array
- */
- protected function renderError(string $message = 'error', array $data = [])
- {
- return $this->renderJson(config('status.error'), $message, $data);
- }
- /**
- * 获取post数据 (数组)
- * @param $key
- * @return mixed
- */
- protected function postData($key = null)
- {
- return $this->request->post(is_null($key) ? '' : $key . '/a');
- }
- /**
- * 获取post数据 (数组)
- * @param $key
- * @return mixed
- */
- protected function postForm($key = 'form')
- {
- return $this->postData($key);
- }
- /*
- * 自定义验证公共类
- * @param $rules 验证规则(数组)
- */
- protected function baseValidate($rules,$messages=[])
- {
- $params=$this->request->param();
- $result = $this->validate($params,$rules,$messages);
- if(true !== $result){
- // 验证失败 输出错误信息
- return $this->renderError($result);
- }
- return $params;
- }
- }
|