BaseServices.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\utils\JwtAuth;
  13. use think\facade\Db;
  14. use think\facade\Config;
  15. use think\facade\Route as Url;
  16. /**
  17. * Class BaseServices
  18. * @package app\services
  19. */
  20. abstract class BaseServices
  21. {
  22. /**
  23. * 模型注入
  24. * @var object
  25. */
  26. protected $dao;
  27. /**
  28. * 获取分页配置
  29. * @param bool $isPage
  30. * @param bool $isRelieve
  31. * @return int[]
  32. */
  33. public function getPageValue(bool $isPage = true, bool $isRelieve = true)
  34. {
  35. $page = $limit = 0;
  36. if ($isPage) {
  37. $page = app()->request->param(Config::get('database.page.pageKey', 'page') . '/d', 0);
  38. $limit = app()->request->param(Config::get('database.page.limitKey', 'limit') . '/d', 0);
  39. }
  40. $limitMax = Config::get('database.page.limitMax');
  41. $defaultLimit = Config::get('database.page.defaultLimit', 10);
  42. if ($limit > $limitMax && $isRelieve) {
  43. $limit = $limitMax;
  44. }
  45. return [(int)$page, (int)$limit, (int)$defaultLimit];
  46. }
  47. /**
  48. * 数据库事务操作
  49. * @param callable $closure
  50. * @param bool $isTran
  51. * @return mixed
  52. */
  53. public function transaction(callable $closure, bool $isTran = true)
  54. {
  55. return $isTran ? Db::transaction($closure) : $closure();
  56. }
  57. /**
  58. * 创建token
  59. * @param int $id
  60. * @param $type
  61. * @return array
  62. */
  63. public function createToken(int $id, $type, $pwd = '')
  64. {
  65. /** @var JwtAuth $jwtAuth */
  66. $jwtAuth = app()->make(JwtAuth::class);
  67. return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]);
  68. }
  69. /**
  70. * 获取路由地址
  71. * @param string $path
  72. * @param array $params
  73. * @param bool $suffix
  74. * @param bool $isDomain
  75. * @return \think\route\Url
  76. */
  77. public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
  78. {
  79. return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
  80. }
  81. /**
  82. * 密码hash加密
  83. * @param string $password
  84. * @return false|string|null
  85. */
  86. public function passwordHash(string $password)
  87. {
  88. return password_hash($password, PASSWORD_BCRYPT);
  89. }
  90. /**
  91. * @param $name
  92. * @param $arguments
  93. * @return mixed
  94. */
  95. public function __call($name, $arguments)
  96. {
  97. // TODO: Implement __call() method.
  98. return call_user_func_array([$this->dao, $name], $arguments);
  99. }
  100. }