Controller.php 3.5 KB

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