Controller.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 app\BaseController;
  14. use app\api\model\User as UserModel;
  15. use app\api\model\Store as StoreModel;
  16. use app\api\service\User as UserService;
  17. use app\common\exception\BaseException;
  18. use think\response\Json;
  19. /**
  20. * API控制器基类
  21. * Class BaseController
  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. /**
  40. * 获取当前商城id
  41. * @return int|null
  42. * @throws BaseException
  43. */
  44. protected function getStoreId()
  45. {
  46. $storeId = getStoreId(); // app/api/common.php
  47. if (empty($storeId)) {
  48. throwError('缺少必要的参数:storeId');
  49. }
  50. return $storeId;
  51. }
  52. /**
  53. * 验证当前商城状态
  54. * @return bool
  55. * @throws BaseException
  56. */
  57. private function checkStore()
  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. return true;
  68. }
  69. /**
  70. * 获取当前用户信息
  71. * @param bool $isForce 强制验证登录
  72. * @return UserModel|bool|null
  73. * @throws BaseException
  74. */
  75. protected function getLoginUser(bool $isForce = true)
  76. {
  77. return UserService::getCurrentLoginUser($isForce);
  78. }
  79. /**
  80. * 返回封装后的 API 数据到客户端
  81. * @param int|null $status 状态码
  82. * @param string $message
  83. * @param array $data
  84. * @return array|Json
  85. */
  86. protected function renderJson(int $status = null, string $message = '', array $data = [])
  87. {
  88. return json(compact('status', 'message', 'data'));
  89. }
  90. /**
  91. * 返回操作成功json
  92. * @param array|string $data
  93. * @param string $message
  94. * @return array
  95. */
  96. protected function renderSuccess($data = [], string $message = 'success')
  97. {
  98. if (is_string($data)) {
  99. $message = $data;
  100. $data = [];
  101. }
  102. return $this->renderJson(config('status.success'), $message, $data);
  103. }
  104. /**
  105. * 返回操作失败json
  106. * @param string $message
  107. * @param array $data
  108. * @return array
  109. */
  110. protected function renderError(string $message = 'error', array $data = [])
  111. {
  112. return $this->renderJson(config('status.error'), $message, $data);
  113. }
  114. /**
  115. * 获取post数据 (数组)
  116. * @param $key
  117. * @return mixed
  118. */
  119. protected function postData($key = null)
  120. {
  121. return $this->request->post(is_null($key) ? '' : $key . '/a');
  122. }
  123. /**
  124. * 获取post数据 (数组)
  125. * @param $key
  126. * @return mixed
  127. */
  128. protected function postForm($key = 'form')
  129. {
  130. return $this->postData($key);
  131. }
  132. }