123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lts
- * Date: 2019-08-08
- * Time: 19:53
- */
- namespace app\clientappapi\controller;
- use app\main\constants\ClientApiConstants;
- use app\main\service\LogService;
- use think\Exception;
- use think\Request;
- use think\Response;
- use think\exception\HttpResponseException;
- class Index
- {
- public function index()
- {
- $code = Request::instance()->param('code');
- if (empty($code)) {
- $this->error('没有接口编码');
- }
- if (empty(ClientApiConstants::APP_PATH_MAPPING[$code])) {
- $this->error('没有找到对应的接口,code:' . $code);
- }
- $apiPath = ClientApiConstants::APP_PATH_MAPPING[$code];
- try {
- action($apiPath);
- } catch (Exception $e) {
- LogService::exception($e);
- $this->error($e->getMessage());
- }
- }
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param int $code 错误代码
- * @param mixed $data 要返回的数据
- * @param array $header 发送的 Header 信息
- */
- protected function error(
- $msg = '',
- $code = ClientApiConstants::CLIENT_API_CODE_FAIL,
- $data = [],
- array $header = []
- ) {
- if (empty($msg)) {
- if (isset(ClientApiConstants::$errMsgList[$code])) {
- $msg = ClientApiConstants::$errMsgList[$code];
- }
- }
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- if (empty($result['data'])) {
- $result['data'] = new \ArrayObject(array());
- }
- $response = Response::create($result, 'json')->header($header);
- throw new HttpResponseException($response);
- }
- }
|