BaseServices.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 app\services\user\UserServices;
  13. use crmeb\exceptions\ApiException;
  14. use crmeb\utils\JwtAuth;
  15. use think\facade\Db;
  16. use think\facade\Config;
  17. use think\facade\Route as Url;
  18. use think\Model;
  19. /**
  20. * Class BaseServices
  21. * @package app\services
  22. * @method array|Model|null get($id, ?array $field = []) 获取一条数据
  23. * @method array|Model|null getOne(array $where, ?string $field = '*') 获取一条数据(不走搜素器)
  24. * @method string|null batchUpdate(array $ids, array $data, ?string $key = null) 批量修改
  25. * @method float sum(array $where, string $field, bool $search = false) 求和
  26. * @method mixed update($id, array $data, ?string $field = '') 修改数据
  27. * @method bool be($map, string $field = '') 查询一条数据是否存在
  28. * @method mixed value(array $where, string $field) 获取指定条件下的数据
  29. * @method int count(array $where = []) 读取数据条数
  30. * @method int getCount(array $where = []) 获取某些条件总数(不走搜素器)
  31. * @method array getColumn(array $where, string $field, string $key = '') 获取某个字段数组(不走搜素器)
  32. * @method mixed delete($id, ?string $key = null) 删除
  33. * @method mixed save(array $data) 保存数据
  34. * @method mixed saveAll(array $data) 批量保存数据
  35. * @method Model selectList(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = '', array $with = [], bool $search = false) 获取列表
  36. * @method bool bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
  37. * @method bool bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2) 高精度 减法
  38. * @method mixed decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 减库存加销量
  39. * @method mixed incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 加库存减销量
  40. */
  41. abstract class BaseServices
  42. {
  43. /**
  44. * 模型注入
  45. * @var object
  46. */
  47. protected $dao;
  48. /**
  49. * 获取分页配置
  50. * @param bool $isPage
  51. * @param bool $isRelieve
  52. * @return int[]
  53. */
  54. public function getPageValue(bool $isPage = true, bool $isRelieve = true)
  55. {
  56. $page = $limit = 0;
  57. if ($isPage) {
  58. $page = app()->request->param(Config::get('database.page.pageKey', 'page') . '/d', 0);
  59. $limit = app()->request->param(Config::get('database.page.limitKey', 'limit') . '/d', 0);
  60. }
  61. $limitMax = Config::get('database.page.limitMax');
  62. $defaultLimit = Config::get('database.page.defaultLimit', 10);
  63. if ($limit > $limitMax && $isRelieve) {
  64. $limit = $limitMax;
  65. }
  66. return [(int)$page, (int)$limit, (int)$defaultLimit];
  67. }
  68. /**
  69. * 数据库事务操作
  70. * @param callable $closure
  71. * @param bool $isTran
  72. * @return mixed
  73. */
  74. public function transaction(callable $closure, bool $isTran = true)
  75. {
  76. return $isTran ? Db::transaction($closure) : $closure();
  77. }
  78. /**
  79. * 创建token
  80. * @param int $id
  81. * @param $type
  82. * @param string $pwd
  83. * @return array
  84. * @throws \Psr\SimpleCache\InvalidArgumentException
  85. */
  86. public function createToken(int $id, $type, $pwd = '')
  87. {
  88. /** @var JwtAuth $jwtAuth */
  89. $jwtAuth = app()->make(JwtAuth::class);
  90. if ($type == 'api' && !app()->make(UserServices::class)->value(['uid' => $id], 'status')) {
  91. throw new ApiException(410027);
  92. }
  93. if ($type == 'api') {
  94. $user = app()->make(UserServices::class)->get($id);
  95. //自定义消息-用户登录成功
  96. event('CustomNoticeListener', [$id, $user, 'login_success']);
  97. //自定义事件-用户登录
  98. event('CustomEventListener', ['user_login', [
  99. 'uid' => $user->uid,
  100. 'nickname' => $user->nickname,
  101. 'phone' => $user->phone,
  102. 'add_time' => date('Y-m-d H:i:s', $user->add_time),
  103. 'login_time' => date('Y-m-d H:i:s'),
  104. 'user_type' => $user->user_type,
  105. ]]);
  106. }
  107. return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]);
  108. }
  109. /**
  110. * 获取路由地址
  111. * @param string $path
  112. * @param array $params
  113. * @param bool $suffix
  114. * @param bool $isDomain
  115. * @return \think\route\Url
  116. */
  117. public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
  118. {
  119. return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
  120. }
  121. /**
  122. * 密码hash加密
  123. * @param string $password
  124. * @return false|string|null
  125. */
  126. public function passwordHash(string $password)
  127. {
  128. return password_hash($password, PASSWORD_BCRYPT);
  129. }
  130. /**
  131. * @param $name
  132. * @param $arguments
  133. * @return mixed
  134. */
  135. public function __call($name, $arguments)
  136. {
  137. // TODO: Implement __call() method.
  138. return call_user_func_array([$this->dao, $name], $arguments);
  139. }
  140. }