| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services;
- use crmeb\traits\ServicesTrait;
- use crmeb\utils\JwtAuth;
- use think\facade\Db;
- use think\facade\Config;
- use think\facade\Route as Url;
- /**
- * Class BaseServices
- * @package app\services
- */
- abstract class BaseServices
- {
- use ServicesTrait;
- /**
- * 模型注入
- * @var object
- */
- protected $dao;
- /**
- * 获取分页配置
- * @param bool $isPage
- * @param bool $isRelieve
- * @return int[]
- */
- public function getPageValue(bool $isPage = true, bool $isRelieve = true)
- {
- $page = $limit = 0;
- if ($isPage) {
- $page = app()->request->param(Config::get('database.page.pageKey', 'page') . '/d', 0);
- $limit = app()->request->param(Config::get('database.page.limitKey', 'limit') . '/d', 0);
- }
- $limitMax = Config::get('database.page.limitMax');
- $defaultLimit = Config::get('database.page.defaultLimit', 10);
- if ($limit > $limitMax && $isRelieve) {
- $limit = $limitMax;
- }
- return [(int)$page, (int)$limit, (int)$defaultLimit];
- }
- /**
- * 数据库事务操作
- * @param callable $closure
- * @param bool $isTran
- * @return mixed
- */
- public function transaction(callable $closure, bool $isTran = true)
- {
- return $isTran ? Db::transaction($closure) : $closure();
- }
- /**
- * 创建token
- * @param int $id
- * @param $type
- * @param string $pwd
- * @return array
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function createToken(int $id, $type, $pwd = '')
- {
- /** @var JwtAuth $jwtAuth */
- $jwtAuth = app()->make(JwtAuth::class);
- return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]);
- }
- /**
- * 获取路由地址
- * @param string $path
- * @param array $params
- * @param bool $suffix
- * @param bool $isDomain
- * @return \think\route\Url
- */
- public function url(string $path, array $params = [], bool $suffix = false, bool $isDomain = false)
- {
- return Url::buildUrl($path, $params)->suffix($suffix)->domain($isDomain)->build();
- }
- /**
- * 密码hash加密
- * @param string $password
- * @return false|string|null
- */
- public function passwordHash(string $password)
- {
- return password_hash($password, PASSWORD_BCRYPT);
- }
- /**
- * @param $name
- * @param $arguments
- * @return mixed
- */
- public function __call($name, $arguments)
- {
- // TODO: Implement __call() method.
- return call_user_func_array([$this->dao, $name], $arguments);
- }
- }
|