Controller.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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('Sorry,' . $settingClass[$client][2] . ' not support');
  92. }
  93. /**
  94. * 获取当前用户信息
  95. * @return UserModel|bool|null
  96. * @throws BaseException
  97. */
  98. protected function getLoginUserId()
  99. {
  100. return UserService::getCurrentLoginUserId();
  101. }
  102. protected static function getCurrentLoginUser()
  103. {
  104. return UserService::getCurrentLoginUser();
  105. }
  106. /**
  107. * 返回封装后的 API 数据到客户端
  108. * @param int|null $status 状态码
  109. * @param string $message
  110. * @param array $data
  111. * @return Json
  112. */
  113. protected function renderJson(int $status = null, string $message = '', array $data = []): Json
  114. {
  115. return json(compact('status', 'message', 'data'));
  116. }
  117. /**
  118. * 返回操作成功json
  119. * @param array|string $data
  120. * @param string $message
  121. * @return Json
  122. */
  123. protected function renderSuccess($data = [], string $message = 'success'): Json
  124. {
  125. if (is_string($data)) {
  126. $message = $data;
  127. $data = [];
  128. }
  129. return $this->renderJson(config('status.success'), $message, $data);
  130. }
  131. /**
  132. * 返回操作失败json
  133. * @param string $message
  134. * @param array $data
  135. * @return Json
  136. */
  137. protected function renderError(string $message = 'error', array $data = []): Json
  138. {
  139. return $this->renderJson(config('status.error'), $message, $data);
  140. }
  141. /**
  142. * 获取post数据 (数组)
  143. * @param $key
  144. * @return mixed
  145. */
  146. protected function postData($key = null)
  147. {
  148. return $this->request->post(is_null($key) ? '' : $key . '/a');
  149. }
  150. /**
  151. * 获取post数据 (数组)
  152. * @param string $key
  153. * @return mixed
  154. */
  155. protected function postForm(string $key = 'form')
  156. {
  157. return $this->postData($key);
  158. }
  159. }