BaseServices.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services;
  12. use crmeb\traits\ServicesTrait;
  13. use crmeb\utils\JwtAuth;
  14. use think\facade\Db;
  15. use think\facade\Config;
  16. use think\facade\Route as Url;
  17. /**
  18. * Class BaseServices
  19. * @package app\services
  20. */
  21. abstract class BaseServices
  22. {
  23. use ServicesTrait;
  24. /**
  25. * 模型注入
  26. * @var object
  27. */
  28. protected $dao;
  29. /**
  30. * 获取分页配置
  31. * @param bool $isPage
  32. * @param bool $isRelieve
  33. * @return int[]
  34. */
  35. public function getPageValue(bool $isPage = true, bool $isRelieve = true)
  36. {
  37. $page = $limit = 0;
  38. if ($isPage) {
  39. $page = app()->request->param(Config::get('database.page.pageKey', 'page') . '/d', 0);
  40. $limit = app()->request->param(Config::get('database.page.limitKey', 'limit') . '/d', 0);
  41. }
  42. $limitMax = Config::get('database.page.limitMax');
  43. $defaultLimit = Config::get('database.page.defaultLimit', 10);
  44. if ($limit > $limitMax && $isRelieve) {
  45. $limit = $limitMax;
  46. }
  47. return [(int)$page, (int)$limit, (int)$defaultLimit];
  48. }
  49. /**
  50. * 数据库事务操作
  51. * @param callable $closure
  52. * @param bool $isTran
  53. * @return mixed
  54. */
  55. public function transaction(callable $closure, bool $isTran = true)
  56. {
  57. return $isTran ? Db::transaction($closure) : $closure();
  58. }
  59. /**
  60. * 创建token
  61. * @param int $id
  62. * @param $type
  63. * @param string $pwd
  64. * @return array
  65. * @throws \Psr\SimpleCache\InvalidArgumentException
  66. */
  67. public function createToken(int $id, $type, $pwd = '')
  68. {
  69. /** @var JwtAuth $jwtAuth */
  70. $jwtAuth = app()->make(JwtAuth::class);
  71. return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]);
  72. }
  73. /**
  74. * 获取路由地址
  75. * @param string $path
  76. * @param array $params
  77. * @param bool $suffix
  78. * @param bool $isDomain
  79. * @return \think\route\Url
  80. */
  81. public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
  82. {
  83. return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
  84. }
  85. /**
  86. * 密码hash加密
  87. * @param string $password
  88. * @return false|string|null
  89. */
  90. public function passwordHash(string $password)
  91. {
  92. return password_hash($password, PASSWORD_BCRYPT);
  93. }
  94. /**
  95. * @param $name
  96. * @param $arguments
  97. * @return mixed
  98. */
  99. public function __call($name, $arguments)
  100. {
  101. // TODO: Implement __call() method.
  102. return call_user_func_array([$this->dao, $name], $arguments);
  103. }
  104. }