BaseServices.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * @return mixed
  51. */
  52. public function transaction(callable $closure, bool $isTran = true)
  53. {
  54. return $isTran ? Db::transaction($closure) : $closure();
  55. }
  56. /**
  57. * 创建token
  58. * @param int $id
  59. * @param $type
  60. * @return array
  61. */
  62. public function createToken(int $id, $type)
  63. {
  64. /** @var JwtAuth $jwtAuth */
  65. $jwtAuth = app()->make(JwtAuth::class);
  66. return $jwtAuth->createToken($id, $type);
  67. }
  68. /**
  69. * 获取路由地址
  70. * @param string $path
  71. * @param array $params
  72. * @param bool $suffix
  73. * @return \think\route\Url
  74. */
  75. public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
  76. {
  77. return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
  78. }
  79. /**
  80. * 密码hash加密
  81. * @param string $password
  82. * @return false|string|null
  83. */
  84. public function passwordHash(string $password)
  85. {
  86. return password_hash($password, PASSWORD_BCRYPT);
  87. }
  88. /**
  89. * @param $name
  90. * @param $arguments
  91. * @return mixed
  92. */
  93. public function __call($name, $arguments)
  94. {
  95. // TODO: Implement __call() method.
  96. return call_user_func_array([$this->dao, $name], $arguments);
  97. }
  98. }