Service.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /**
  81. * 获取主键
  82. *
  83. * @param string $name
  84. *
  85. * @return mixed
  86. */
  87. protected function primaryKey($name = 'id')
  88. {
  89. $params = $this->params(\think\facade\Validate::rule([$name => 'require']));
  90. return $params[$name];
  91. }
  92. /**
  93. * 通过主键获取对象
  94. *
  95. * @template T
  96. * @param class-string<T> $objClass
  97. * @param string $name
  98. *
  99. * @return T
  100. */
  101. protected function one(string $objClass, $name = 'id')
  102. {
  103. $id = $this->primaryKey($name);
  104. $obj = (new $objClass)->find($id);
  105. if (!$obj) {
  106. throw $this->exception("未找到对象 $objClass $name=$id", 404);
  107. }
  108. return $obj;
  109. }
  110. /**
  111. * 覆盖params
  112. * - 优先级 function($params) > overrideParams($params) > autoget
  113. *
  114. * @param mixed $params
  115. *
  116. * @return Service
  117. */
  118. public function overrideParams($params)
  119. {
  120. $this->params = $params;
  121. return $this;
  122. }
  123. protected function autoParams($params)
  124. {
  125. $this->params = $params ?: $this->params();
  126. return $this->params;
  127. }
  128. /**
  129. * 分页参数
  130. *
  131. * @param int $max
  132. *
  133. * @return array
  134. */
  135. public function pageParams($max = 100)
  136. {
  137. $this->validate($this->params, \think\facade\Validate::rule([
  138. 'pageParams.page' => '>=:0',
  139. 'pageParams.size' => "<=:{$max}"
  140. ]));
  141. $pageParams = $this->params['pageParams'];
  142. $pageParams['size'] = isset($pageParams['size']) ? $pageParams['size'] : 15;
  143. $pageParams['page'] = isset($pageParams['page']) ? $pageParams['page'] : 1;
  144. return $pageParams;
  145. }
  146. public function tp6Page($max = 100)
  147. {
  148. ['page' => $page, 'size' => $size] = $this->pageParams($max);
  149. return [
  150. 'page' => $page,
  151. 'list_rows' => $size,
  152. ];
  153. }
  154. /**
  155. * 校验
  156. *
  157. * @param mixed $data
  158. * @param Validate $validator
  159. * @param array $msg
  160. *
  161. * @return bool
  162. */
  163. public function validate($data, Validate $validator)
  164. {
  165. if (!$validator->check($data)) {
  166. throw new ValidateException($validator->getError());
  167. }
  168. return true;
  169. }
  170. /**
  171. * 设置或获取内置异常类
  172. *
  173. * @param string $class 异常类
  174. *
  175. * @return self|string
  176. */
  177. public function exceptionClass($class = null)
  178. {
  179. if (empty($class)) {
  180. return $this->exceptionClass;
  181. }
  182. if ($this->isExceptionClassChanged) {
  183. return $this;
  184. }
  185. $this->exceptionClass = $class;
  186. return $this;
  187. }
  188. /**
  189. * 锁定异常类
  190. *
  191. * @return self
  192. */
  193. public function lockException()
  194. {
  195. $this->isExceptionClassChanged = true;
  196. return $this;
  197. }
  198. /**
  199. * 封装异常
  200. *
  201. * @param \Exception $e 父异常
  202. * @param string $prefixMessage 消息前缀
  203. * @param $newCode 新的code
  204. * @param string $class 如有需要使用另一个class
  205. *
  206. * @return \Exception
  207. */
  208. public function warpException(\Exception $e, $prefixMessage = "发生异常:\n ", $newCode = null, $class = null)
  209. {
  210. $class = $class ?: $this->exceptionClass;
  211. $newCode = $newCode ?? $e->getCode();
  212. return new $class("{$prefixMessage}[{$e->getCode()}]:" . $e->getMessage(), $newCode, $e);
  213. }
  214. /**
  215. * 附带新异常
  216. *
  217. * @param \Closure $func 函数
  218. * @param mixed ...$args 参数
  219. *
  220. * @return void
  221. */
  222. public function withException(\Closure $func, ...$args)
  223. {
  224. try {
  225. $func(...$args);
  226. } catch (\Exception $e) {
  227. throw $this->warpException($e);
  228. }
  229. }
  230. /**
  231. * 使用内定的异常类
  232. *
  233. * @param string $message 消息
  234. * @param $code code
  235. * @param \Exception $parent 父异常
  236. * @param string $class 如有需要使用另一个class
  237. *
  238. * @return \Exception
  239. */
  240. public function exception($message = '', $code = -1, $parent = null, $class = null)
  241. {
  242. $class = $class ?: $this->exceptionClass;
  243. return new $class($message, $code, $parent);
  244. }
  245. }