Controller.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 cores\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. empty($storeId) && throwError('缺少必要的参数:storeId');
  50. return $storeId;
  51. }
  52. /**
  53. * 验证当前商城状态
  54. * @return void
  55. * @throws BaseException
  56. */
  57. private function checkStore(): void
  58. {
  59. // 获取当前商城信息
  60. $store = StoreModel::detail($this->storeId);
  61. if (empty($store)) {
  62. throwError('当前商城信息不存在');
  63. }
  64. if ($store['is_recycle'] || $store['is_delete']) {
  65. throwError('当前商城已删除');
  66. }
  67. }
  68. /**
  69. * 验证当前客户端是否允许访问
  70. * @throws BaseException
  71. */
  72. private function checkClient()
  73. {
  74. $client = getPlatform();
  75. $settingClass = [
  76. 'H5' => [\app\api\model\h5\Setting::class, 'checkStatus', 'H5端']
  77. ];
  78. if (!isset($settingClass[$client])) {
  79. return;
  80. }
  81. $status = call_user_func([$settingClass[$client][0], $settingClass[$client][1]]);
  82. $status === false && throwError('很抱歉,当前' . $settingClass[$client][2] . '端暂不支持访问');
  83. }
  84. /**
  85. * 获取当前用户信息
  86. * @param bool $isForce 强制验证登录
  87. * @return UserModel|bool|null
  88. * @throws BaseException
  89. */
  90. protected function getLoginUser(bool $isForce = true)
  91. {
  92. return UserService::getCurrentLoginUser($isForce);
  93. }
  94. /**
  95. * 返回封装后的 API 数据到客户端
  96. * @param int|null $status 状态码
  97. * @param string $message
  98. * @param array $data
  99. * @return Json
  100. */
  101. protected function renderJson(int $status = null, string $message = '', array $data = []): Json
  102. {
  103. return json(compact('status', 'message', 'data'));
  104. }
  105. /**
  106. * 返回操作成功json
  107. * @param array|string $data
  108. * @param string $message
  109. * @return Json
  110. */
  111. protected function renderSuccess($data = [], string $message = 'success'): Json
  112. {
  113. if (is_string($data)) {
  114. $message = $data;
  115. $data = [];
  116. }
  117. return $this->renderJson(config('status.success'), $message, $data);
  118. }
  119. /**
  120. * 返回操作失败json
  121. * @param string $message
  122. * @param array $data
  123. * @return Json
  124. */
  125. protected function renderError(string $message = 'error', array $data = []): Json
  126. {
  127. return $this->renderJson(config('status.error'), $message, $data);
  128. }
  129. /**
  130. * 获取post数据 (数组)
  131. * @param $key
  132. * @return mixed
  133. */
  134. protected function postData($key = null)
  135. {
  136. return $this->request->post(is_null($key) ? '' : $key . '/a');
  137. }
  138. /**
  139. * 获取post数据 (数组)
  140. * @param string $key
  141. * @return mixed
  142. */
  143. protected function postForm(string $key = 'form')
  144. {
  145. return $this->postData($key);
  146. }
  147. }