Controller.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\index\controller;
  13. use think\facade\Log;
  14. use think\facade\Session;
  15. use think\response\Json;
  16. use cores\BaseController;
  17. use app\api\model\User as UserModel;
  18. use app\api\model\Store as StoreModel;
  19. use app\index\service\User as UserService;
  20. use cores\exception\BaseException;
  21. /**
  22. * API控制器基类
  23. * Class Controller
  24. * @package app\store\controller
  25. */
  26. class Controller extends BaseController
  27. {
  28. // 当前商城ID
  29. protected $storeId;
  30. /**
  31. * API基类初始化
  32. * @throws BaseException
  33. */
  34. public function initialize()
  35. {
  36. // 当前商城id
  37. $this->storeId = $this->getStoreId();
  38. // 验证当前商城状态
  39. $this->checkStore();
  40. // 验证当前客户端状态
  41. $this->checkClient();
  42. $contrName = $this->request->controller();
  43. if (strtolower($contrName) == 'cart'){
  44. $userId = Session::get('user_id');
  45. if (empty($userId)){
  46. return view('passport/login');
  47. }
  48. }
  49. }
  50. /**
  51. * 获取当前商城ID
  52. * @return int|null
  53. * @throws BaseException
  54. */
  55. protected function getStoreId(): ?int
  56. {
  57. $storeId = getStoreId(); // app/api/common.php
  58. empty($storeId) && throwError('缺少必要的参数:storeId');
  59. return $storeId;
  60. }
  61. /**
  62. * 验证当前商城状态
  63. * @return void
  64. * @throws BaseException
  65. */
  66. private function checkStore(): void
  67. {
  68. // 获取当前商城信息
  69. $store = StoreModel::detail($this->storeId);
  70. if (empty($store)) {
  71. throwError('当前商城信息不存在');
  72. }
  73. if ($store['is_recycle'] || $store['is_delete']) {
  74. throwError('当前商城已删除');
  75. }
  76. }
  77. /**
  78. * 验证当前客户端是否允许访问
  79. * @throws BaseException
  80. */
  81. private function checkClient()
  82. {
  83. $client = getPlatform();
  84. $settingClass = [
  85. 'H5' => [\app\index\model\h5\Setting::class, 'checkStatus', 'H5端']
  86. ];
  87. if (!isset($settingClass[$client])) {
  88. return;
  89. }
  90. $status = call_user_func([$settingClass[$client][0], $settingClass[$client][1]]);
  91. $status === false && throwError('很抱歉,当前' . $settingClass[$client][2] . '端暂不支持访问');
  92. }
  93. /**
  94. * 获取当前用户信息
  95. * @return UserModel|bool|null
  96. * @throws BaseException
  97. */
  98. protected function getLoginUserId()
  99. {
  100. return UserService::getCurrentLoginUserId();
  101. }
  102. /**
  103. * 返回封装后的 API 数据到客户端
  104. * @param int|null $status 状态码
  105. * @param string $message
  106. * @param array $data
  107. * @return Json
  108. */
  109. protected function renderJson(int $status = null, string $message = '', array $data = []): Json
  110. {
  111. return json(compact('status', 'message', 'data'));
  112. }
  113. /**
  114. * 返回操作成功json
  115. * @param array|string $data
  116. * @param string $message
  117. * @return Json
  118. */
  119. protected function renderSuccess($data = [], string $message = 'success'): Json
  120. {
  121. if (is_string($data)) {
  122. $message = $data;
  123. $data = [];
  124. }
  125. return $this->renderJson(config('status.success'), $message, $data);
  126. }
  127. /**
  128. * 返回操作失败json
  129. * @param string $message
  130. * @param array $data
  131. * @return Json
  132. */
  133. protected function renderError(string $message = 'error', array $data = []): Json
  134. {
  135. return $this->renderJson(config('status.error'), $message, $data);
  136. }
  137. /**
  138. * 获取post数据 (数组)
  139. * @param $key
  140. * @return mixed
  141. */
  142. protected function postData($key = null)
  143. {
  144. return $this->request->post(is_null($key) ? '' : $key . '/a');
  145. }
  146. /**
  147. * 获取post数据 (数组)
  148. * @param string $key
  149. * @return mixed
  150. */
  151. protected function postForm(string $key = 'form')
  152. {
  153. return $this->postData($key);
  154. }
  155. }