BasicApi.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace controller;
  14. use service\ToolsService;
  15. use think\Request;
  16. use think\Response;
  17. /**
  18. * 数据接口通用控制器
  19. * Class BasicApi
  20. * @package controller
  21. */
  22. class BasicApi
  23. {
  24. /**
  25. * 访问请求对象
  26. * @var Request
  27. */
  28. public $request;
  29. /**
  30. * 当前访问身份
  31. * @var string
  32. */
  33. public $token;
  34. /**
  35. * 基础接口SDK
  36. * @param Request|null $request
  37. */
  38. public function __construct(Request $request = null)
  39. {
  40. // CORS 跨域 Options 检测响应
  41. ToolsService::corsOptionsHandler();
  42. // 输入对象
  43. $this->request = is_null($request) ? Request::instance() : $request;
  44. }
  45. /**
  46. * 输出返回数据
  47. * @param string $msg 提示消息内容
  48. * @param string $code 业务状态码
  49. * @param mixed $data 要返回的数据
  50. * @param string $type 返回类型 JSON XML
  51. * @return Response
  52. */
  53. public function response($msg, $code = 'SUCCESS', $data = [], $type = 'json')
  54. {
  55. $result = ['msg' => $msg, 'code' => $code, 'data' => $data, 'type' => strtolower($type)];
  56. return Response::create($result, $type)->header(ToolsService::corsRequestHander())->code(200);
  57. }
  58. }