Controller.php 5.3 KB

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