Controller.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use think\response\Json;
  14. use cores\BaseController;
  15. use app\api\model\User as UserModel;
  16. use app\api\model\Store as StoreModel;
  17. use app\api\service\User as UserService;
  18. use app\common\exception\BaseException;
  19. /**
  20. * API控制器基类
  21. * Class Controller
  22. * @package app\store\controller
  23. */
  24. class Controller extends BaseController
  25. {
  26. // 当前商城ID
  27. protected $storeId;
  28. /**
  29. * API基类初始化
  30. * @throws BaseException
  31. */
  32. public function initialize()
  33. {
  34. // 当前商城id
  35. $this->storeId = $this->getStoreId();
  36. // 验证当前商城状态
  37. $this->checkStore();
  38. // 验证当前客户端状态
  39. $this->checkClient();
  40. }
  41. /**
  42. * 获取当前商城id
  43. * @return int|null
  44. * @throws BaseException
  45. */
  46. protected function getStoreId(): ?int
  47. {
  48. $storeId = getStoreId(); // app/api/common.php
  49. if (empty($storeId)) {
  50. throwError('缺少必要的参数:storeId');
  51. }
  52. return $storeId;
  53. }
  54. /**
  55. * 验证当前商城状态
  56. * @return void
  57. * @throws BaseException
  58. */
  59. private function checkStore(): void
  60. {
  61. // 获取当前商城信息
  62. $store = StoreModel::detail($this->storeId);
  63. if (empty($store)) {
  64. throwError('当前商城信息不存在');
  65. }
  66. if ($store['is_recycle'] || $store['is_delete']) {
  67. throwError('当前商城已删除');
  68. }
  69. }
  70. /**
  71. * 验证当前客户端是否允许访问
  72. * @throws BaseException
  73. */
  74. private function checkClient()
  75. {
  76. $client = getPlatform();
  77. $settingClass = [
  78. 'H5' => [\app\api\model\h5\Setting::class, 'checkStatus', 'H5端']
  79. ];
  80. if (!isset($settingClass[$client])) {
  81. return;
  82. }
  83. $status = call_user_func([$settingClass[$client][0], $settingClass[$client][1]]);
  84. $status === false && throwError('很抱歉,当前' . $settingClass[$client][2] . '端暂不支持访问');
  85. }
  86. /**
  87. * 获取当前用户信息
  88. * @param bool $isForce 强制验证登录
  89. * @return UserModel|bool|null
  90. * @throws BaseException
  91. */
  92. protected function getLoginUser(bool $isForce = true)
  93. {
  94. return UserService::getCurrentLoginUser($isForce);
  95. }
  96. /**
  97. * 返回封装后的 API 数据到客户端
  98. * @param int|null $status 状态码
  99. * @param string $message
  100. * @param array $data
  101. * @return array|Json
  102. */
  103. protected function renderJson(int $status = null, string $message = '', array $data = [])
  104. {
  105. return json(compact('status', 'message', 'data'));
  106. }
  107. /**
  108. * 返回操作成功json
  109. * @param array|string $data
  110. * @param string $message
  111. * @return array
  112. */
  113. protected function renderSuccess($data = [], string $message = 'success')
  114. {
  115. if (is_string($data)) {
  116. $message = $data;
  117. $data = [];
  118. }
  119. return $this->renderJson(config('status.success'), $message, $data);
  120. }
  121. /**
  122. * 返回操作失败json
  123. * @param string $message
  124. * @param array $data
  125. * @return array
  126. */
  127. protected function renderError(string $message = 'error', array $data = [])
  128. {
  129. return $this->renderJson(config('status.error'), $message, $data);
  130. }
  131. /**
  132. * 获取post数据 (数组)
  133. * @param $key
  134. * @return mixed
  135. */
  136. protected function postData($key = null)
  137. {
  138. return $this->request->post(is_null($key) ? '' : $key . '/a');
  139. }
  140. /**
  141. * 获取post数据 (数组)
  142. * @param $key
  143. * @return mixed
  144. */
  145. protected function postForm($key = 'form')
  146. {
  147. return $this->postData($key);
  148. }
  149. }