Service.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\common\service;
  3. use think\exception\ValidateException;
  4. use think\facade\Request;
  5. use think\Service as ThinkService;
  6. use think\Validate;
  7. /**
  8. * 服务基类
  9. */
  10. class Service extends ThinkService
  11. {
  12. protected $exceptionClass = \Exception::class;
  13. protected $isExceptionClassChanged = false;
  14. protected $params = [];
  15. /**
  16. * 默认参数
  17. *
  18. * @return mixed
  19. */
  20. protected function params($validator = null)
  21. {
  22. $params = [];
  23. if ($this->params) {
  24. $params = $this->params;
  25. } else {
  26. $params = Request::instance()->param('', null, 'trim');
  27. }
  28. // 尝试校验
  29. if ($validator) {
  30. $this->validate($params, $validator);
  31. }
  32. return $params;
  33. }
  34. /**
  35. * 使用该方法需要先调用overrideParams
  36. *
  37. * @param mixed $default 默认值
  38. * @param string|int ...$index 数组下标
  39. *
  40. * @return mixed
  41. */
  42. protected function pgd($default = null, ...$index)
  43. {
  44. $params = $this->params();
  45. foreach ($index as $i) {
  46. if (isset($params[$i])) {
  47. return $params[$i];
  48. }
  49. }
  50. return $default;
  51. }
  52. /**
  53. * 使用该方法需要先调用overrideParams
  54. *
  55. * @param string|int ...$index 数组下标
  56. *
  57. * @return mixed
  58. */
  59. protected function pg(...$index)
  60. {
  61. $params = $this->params();
  62. foreach ($index as $i) {
  63. if (isset($params[$i])) {
  64. return $params[$i];
  65. }
  66. }
  67. return null;
  68. }
  69. protected function req(...$index)
  70. {
  71. $params = $this->params();
  72. foreach ($index as $i) {
  73. if (isset($params[$i])) {
  74. return $params[$i];
  75. }
  76. }
  77. $desc = implode('或', $index);
  78. throw $this->exception("缺少参数 $desc");
  79. }
  80. protected function array(...$index)
  81. {
  82. $arg = $this->pg(...$index);
  83. if (is_null($arg)) {
  84. $arg = [];
  85. }
  86. if (!is_array($arg)) {
  87. $desc = implode('或', $index);
  88. throw $this->exception("参数 $desc 必须为数组");
  89. }
  90. return $arg;
  91. }
  92. /**
  93. * 获取主键
  94. *
  95. * @param string $name
  96. *
  97. * @return mixed
  98. */
  99. protected function primaryKey($name = 'id')
  100. {
  101. $params = $this->params(\think\facade\Validate::rule([$name => 'require']));
  102. return $params[$name];
  103. }
  104. /**
  105. * 通过主键获取对象
  106. *
  107. * @template T
  108. * @param class-string<T> $objClass
  109. * @param string $name
  110. *
  111. * @return T
  112. */
  113. protected function one(string $objClass, $name = 'id')
  114. {
  115. $id = $this->primaryKey($name);
  116. $obj = (new $objClass)->find($id);
  117. if (!$obj) {
  118. throw $this->exception("未找到对象 $objClass $name=$id", 404);
  119. }
  120. return $obj;
  121. }
  122. /**
  123. * 覆盖params
  124. * - 优先级 function($params) > overrideParams($params) > autoget
  125. *
  126. * @param mixed $params
  127. *
  128. * @return Service
  129. */
  130. public function overrideParams($params)
  131. {
  132. $this->params = $params;
  133. return $this;
  134. }
  135. protected function autoParams($params)
  136. {
  137. $this->params = $params ?: $this->params();
  138. return $this->params;
  139. }
  140. /**
  141. * 分页参数
  142. *
  143. * @param int $max
  144. *
  145. * @return array
  146. */
  147. public function pageParams($max = 100)
  148. {
  149. $this->validate($this->params, \think\facade\Validate::rule([
  150. 'pageParams.page' => '>=:0',
  151. 'pageParams.size' => "<=:{$max}"
  152. ]));
  153. $pageParams = $this->params['pageParams'];
  154. $pageParams['size'] = isset($pageParams['size']) ? $pageParams['size'] : 15;
  155. $pageParams['page'] = isset($pageParams['page']) ? $pageParams['page'] : 1;
  156. return $pageParams;
  157. }
  158. public function tp6Page($max = 100)
  159. {
  160. ['page' => $page, 'size' => $size] = $this->pageParams($max);
  161. return [
  162. 'page' => $page,
  163. 'list_rows' => $size,
  164. ];
  165. }
  166. /**
  167. * 校验
  168. *
  169. * @param mixed $data
  170. * @param Validate $validator
  171. * @param array $msg
  172. *
  173. * @return bool
  174. */
  175. public function validate($data, Validate $validator)
  176. {
  177. if (!$validator->check($data)) {
  178. throw new ValidateException($validator->getError());
  179. }
  180. return true;
  181. }
  182. /**
  183. * 设置或获取内置异常类
  184. *
  185. * @param string $class 异常类
  186. *
  187. * @return self|string
  188. */
  189. public function exceptionClass($class = null)
  190. {
  191. if (empty($class)) {
  192. return $this->exceptionClass;
  193. }
  194. if ($this->isExceptionClassChanged) {
  195. return $this;
  196. }
  197. $this->exceptionClass = $class;
  198. return $this;
  199. }
  200. /**
  201. * 锁定异常类
  202. *
  203. * @return self
  204. */
  205. public function lockException()
  206. {
  207. $this->isExceptionClassChanged = true;
  208. return $this;
  209. }
  210. /**
  211. * 封装异常
  212. *
  213. * @param \Exception $e 父异常
  214. * @param string $prefixMessage 消息前缀
  215. * @param $newCode 新的code
  216. * @param string $class 如有需要使用另一个class
  217. *
  218. * @return \Exception
  219. */
  220. public function warpException(\Exception $e, $prefixMessage = "发生异常:\n ", $newCode = null, $class = null)
  221. {
  222. $class = $class ?: $this->exceptionClass;
  223. $newCode = $newCode ?? $e->getCode();
  224. return new $class("{$prefixMessage}[{$e->getCode()}]:" . $e->getMessage(), $newCode, $e);
  225. }
  226. /**
  227. * 附带新异常
  228. *
  229. * @param \Closure $func 函数
  230. * @param mixed ...$args 参数
  231. *
  232. * @return void
  233. */
  234. public function withException(\Closure $func, ...$args)
  235. {
  236. try {
  237. $func(...$args);
  238. } catch (\Exception $e) {
  239. throw $this->warpException($e);
  240. }
  241. }
  242. /**
  243. * 使用内定的异常类
  244. *
  245. * @param string $message 消息
  246. * @param $code code
  247. * @param \Exception $parent 父异常
  248. * @param string $class 如有需要使用另一个class
  249. *
  250. * @return \Exception
  251. */
  252. public function exception($message = '', $code = -1, $parent = null, $class = null)
  253. {
  254. $class = $class ?: $this->exceptionClass;
  255. return new $class($message, $code, $parent);
  256. }
  257. }