Controller.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\User as UserModel;
  4. use app\api\model\Wxapp as WxappModel;
  5. use app\common\exception\BaseException;
  6. /**
  7. * API控制器基类
  8. * Class BaseController
  9. * @package app\store\controller
  10. */
  11. class Controller extends \think\Controller
  12. {
  13. const JSON_SUCCESS_STATUS = 1;
  14. const JSON_ERROR_STATUS = 0;
  15. /* @ver $wxapp_id 小程序id */
  16. protected $wxapp_id;
  17. /**
  18. * API基类初始化
  19. * @throws BaseException
  20. * @throws \think\exception\DbException
  21. */
  22. public function _initialize()
  23. {
  24. // 当前小程序id
  25. $this->wxapp_id = $this->getWxappId();
  26. // 验证当前小程序状态
  27. $this->checkWxapp();
  28. }
  29. /**
  30. * 获取当前小程序ID
  31. * @return mixed
  32. * @throws BaseException
  33. */
  34. private function getWxappId()
  35. {
  36. if (!$wxapp_id = $this->request->param('wxapp_id')) {
  37. throw new BaseException(['msg' => '缺少必要的参数:wxapp_id']);
  38. }
  39. return $wxapp_id;
  40. }
  41. /**
  42. * 验证当前小程序状态
  43. * @throws BaseException
  44. * @throws \think\exception\DbException
  45. */
  46. private function checkWxapp()
  47. {
  48. $wxapp = WxappModel::detail($this->wxapp_id);
  49. if (empty($wxapp)) {
  50. throw new BaseException(['msg' => '当前小程序信息不存在']);
  51. }
  52. if ($wxapp['is_recycle'] || $wxapp['is_delete']) {
  53. throw new BaseException(['msg' => '当前小程序已删除']);
  54. }
  55. }
  56. /**
  57. * 获取当前用户信息
  58. * @param bool $is_force
  59. * @return UserModel|bool|null
  60. * @throws BaseException
  61. * @throws \think\exception\DbException
  62. */
  63. protected function getUser($is_force = true)
  64. {
  65. if (!$token = $this->request->param('token')) {
  66. $is_force && $this->throwError('缺少必要的参数:token', -1);
  67. return false;
  68. }
  69. if (!$user = UserModel::getUser($token)) {
  70. $is_force && $this->throwError('没有找到用户信息', -1);
  71. return false;
  72. }
  73. return $user;
  74. }
  75. /**
  76. * 输出错误信息
  77. * @param int $code
  78. * @param $msg
  79. * @throws BaseException
  80. */
  81. protected function throwError($msg, $code = 0)
  82. {
  83. throw new BaseException(['code' => $code, 'msg' => $msg]);
  84. }
  85. /**
  86. * 返回封装后的 API 数据到客户端
  87. * @param int $code
  88. * @param string $msg
  89. * @param array $data
  90. * @return array
  91. */
  92. protected function renderJson($code = self::JSON_SUCCESS_STATUS, $msg = '', $data = [])
  93. {
  94. return compact('code', 'msg', 'data');
  95. }
  96. /**
  97. * 返回操作成功json
  98. * @param array $data
  99. * @param string|array $msg
  100. * @return array
  101. */
  102. protected function renderSuccess($data = [], $msg = 'success')
  103. {
  104. return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg, $data);
  105. }
  106. /**
  107. * 返回操作失败json
  108. * @param string $msg
  109. * @param array $data
  110. * @return array
  111. */
  112. protected function renderError($msg = 'error', $data = [])
  113. {
  114. return $this->renderJson(self::JSON_ERROR_STATUS, $msg, $data);
  115. }
  116. /**
  117. * 获取post数据 (数组)
  118. * @param $key
  119. * @return mixed
  120. */
  121. protected function postData($key = null)
  122. {
  123. return $this->request->post(is_null($key) ? '' : $key . '/a');
  124. }
  125. }