Base.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\api\controller\base;
  9. use app\common\service\WebService;
  10. use think\Controller;
  11. /**
  12. * 接口父类
  13. * Class Api
  14. * @package app\api\controller\base
  15. */
  16. class Base extends Controller
  17. {
  18. protected function _initialize()
  19. {
  20. (new WebService())->checkInstalled();
  21. header('Access-Control-Allow-Origin: *');//允许跨域,*星号表示所有的域都可以接受
  22. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, x-token');//允许访问的header
  23. }
  24. /**
  25. * api成功的响应
  26. * 和success方法相比,没有显示跳转页面,直接响应json
  27. * @param $msg string [提示消息]
  28. * @param $data null [响应数据]
  29. * @param int $code
  30. */
  31. protected function json_success($msg = "success", $data = null, $code = 1)
  32. {
  33. //和success方法返回的code一致,这样后台ajax不用修改就能兼容
  34. $this->result($data, $code, $msg, 'json');
  35. }
  36. /**
  37. * api失败的响应,可定义错误代码
  38. * @param $msg [错误消息]
  39. * @param int $code [自定义错误码,不可为1]
  40. * @param null $data [响应数据]
  41. */
  42. protected function json_error($msg = "error", $data = null, $code = 0)
  43. {
  44. $this->result($data, $code, $msg, 'json');
  45. }
  46. }