BaseServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Model selectList(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = '', array $with = [], bool $search = false) 获取列表
  34. * @method bool bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
  35. * @method bool bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2) 高精度 减法
  36. * @method mixed decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 减库存加销量
  37. * @method mixed incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 加库存减销量
  38. */
  39. abstract class BaseServices
  40. {
  41. /**
  42. * 模型注入
  43. * @var object
  44. */
  45. protected $dao;
  46. /**
  47. * @return \crmeb\utils\Cache
  48. * @author 等风来
  49. * @email 136327134@qq.com
  50. * @date 2023/2/8
  51. */
  52. public function cacheDriver()
  53. {
  54. return new \crmeb\utils\Cache($this->dao->getTableName());
  55. }
  56. /**
  57. * 获取分页配置
  58. * @param bool $isPage
  59. * @param bool $isRelieve
  60. * @return int[]
  61. */
  62. public function getPageValue(bool $isPage = true, bool $isRelieve = true)
  63. {
  64. $page = $limit = 0;
  65. if ($isPage) {
  66. $page = app()->request->param(Config::get('database.page.pageKey', 'page') . '/d', 0);
  67. $limit = app()->request->param(Config::get('database.page.limitKey', 'limit') . '/d', 0);
  68. }
  69. $limitMax = Config::get('database.page.limitMax');
  70. $defaultLimit = Config::get('database.page.defaultLimit', 10);
  71. if ($limit > $limitMax && $isRelieve) {
  72. $limit = $limitMax;
  73. }
  74. return [(int)$page, (int)$limit, (int)$defaultLimit];
  75. }
  76. /**
  77. * 数据库事务操作
  78. * @param callable $closure
  79. * @param bool $isTran
  80. * @return mixed
  81. */
  82. public function transaction(callable $closure, bool $isTran = true)
  83. {
  84. return $isTran ? Db::transaction($closure) : $closure();
  85. }
  86. /**
  87. * 创建token
  88. * @param int $id
  89. * @param $type
  90. * @param string $pwd
  91. * @return array
  92. * @throws \Psr\SimpleCache\InvalidArgumentException
  93. */
  94. public function createToken(int $id, $type, $pwd = '')
  95. {
  96. /** @var JwtAuth $jwtAuth */
  97. $jwtAuth = app()->make(JwtAuth::class);
  98. return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]);
  99. }
  100. /**
  101. * 获取路由地址
  102. * @param string $path
  103. * @param array $params
  104. * @param bool $suffix
  105. * @param bool $isDomain
  106. * @return \think\route\Url
  107. */
  108. public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
  109. {
  110. return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
  111. }
  112. /**
  113. * 密码hash加密
  114. * @param string $password
  115. * @return false|string|null
  116. */
  117. public function passwordHash(string $password)
  118. {
  119. return password_hash($password, PASSWORD_BCRYPT);
  120. }
  121. /**
  122. * @param $name
  123. * @param $arguments
  124. * @return mixed
  125. */
  126. public function __call($name, $arguments)
  127. {
  128. // TODO: Implement __call() method.
  129. return call_user_func_array([$this->dao, $name], $arguments);
  130. }
  131. }