BaseServices.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. use think\Model;
  17. /**
  18. * Class BaseServices
  19. * @package app\services
  20. * @method array|Model|null get($id, ?array $field = []) 获取一条数据
  21. * @method array|Model|null getOne(array $where, ?string $field = '*') 获取一条数据(不走搜素器)
  22. * @method string|null batchUpdate(array $ids, array $data, ?string $key = null) 批量修改
  23. * @method float sum(array $where, string $field, bool $search = false) 求和
  24. * @method mixed update($id, array $data, ?string $field = '') 修改数据
  25. * @method bool be($map, string $field = '') 查询一条数据是否存在
  26. * @method mixed value(array $where, string $field) 获取指定条件下的数据
  27. * @method int count(array $where = []) 读取数据条数
  28. * @method int getCount(array $where = []) 获取某些条件总数(不走搜素器)
  29. * @method array getColumn(array $where, string $field, string $key = '') 获取某个字段数组(不走搜素器)
  30. * @method mixed delete($id, ?string $key = null) 删除
  31. * @method mixed save(array $data) 保存数据
  32. * @method mixed saveAll(array $data) 批量保存数据
  33. * @method bool bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
  34. * @method bool bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2) 高精度 减法
  35. * @method mixed decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 减库存加销量
  36. * @method mixed incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 加库存减销量
  37. */
  38. abstract class BaseServices
  39. {
  40. /**
  41. * 模型注入
  42. * @var object
  43. */
  44. protected $dao;
  45. /**
  46. * @return \crmeb\utils\Cache
  47. * @author 等风来
  48. * @email 136327134@qq.com
  49. * @date 2023/2/8
  50. */
  51. public function cacheDriver()
  52. {
  53. return new \crmeb\utils\Cache($this->dao->getTableName());
  54. }
  55. /**
  56. * 获取分页配置
  57. * @param bool $isPage
  58. * @param bool $isRelieve
  59. * @return int[]
  60. */
  61. public function getPageValue(bool $isPage = true, bool $isRelieve = true)
  62. {
  63. $page = $limit = 0;
  64. if ($isPage) {
  65. $page = app()->request->param(Config::get('database.page.pageKey', 'page') . '/d', 0);
  66. $limit = app()->request->param(Config::get('database.page.limitKey', 'limit') . '/d', 0);
  67. }
  68. $limitMax = Config::get('database.page.limitMax');
  69. $defaultLimit = Config::get('database.page.defaultLimit', 10);
  70. if ($limit > $limitMax && $isRelieve) {
  71. $limit = $limitMax;
  72. }
  73. return [(int)$page, (int)$limit, (int)$defaultLimit];
  74. }
  75. /**
  76. * 数据库事务操作
  77. * @param callable $closure
  78. * @param bool $isTran
  79. * @return mixed
  80. */
  81. public function transaction(callable $closure, bool $isTran = true)
  82. {
  83. return $isTran ? Db::transaction($closure) : $closure();
  84. }
  85. /**
  86. * 创建token
  87. * @param int $id
  88. * @param $type
  89. * @param string $pwd
  90. * @return array
  91. * @throws \Psr\SimpleCache\InvalidArgumentException
  92. */
  93. public function createToken(int $id, $type, $pwd = '')
  94. {
  95. /** @var JwtAuth $jwtAuth */
  96. $jwtAuth = app()->make(JwtAuth::class);
  97. return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]);
  98. }
  99. /**
  100. * 获取路由地址
  101. * @param string $path
  102. * @param array $params
  103. * @param bool $suffix
  104. * @param bool $isDomain
  105. * @return \think\route\Url
  106. */
  107. public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
  108. {
  109. return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
  110. }
  111. /**
  112. * 密码hash加密
  113. * @param string $password
  114. * @return false|string|null
  115. */
  116. public function passwordHash(string $password)
  117. {
  118. return password_hash($password, PASSWORD_BCRYPT);
  119. }
  120. /**
  121. * @param $name
  122. * @param $arguments
  123. * @return mixed
  124. */
  125. public function __call($name, $arguments)
  126. {
  127. // TODO: Implement __call() method.
  128. return call_user_func_array([$this->dao, $name], $arguments);
  129. }
  130. }