From b5fe43c533a2a38c101103a5e29806bee4e6e996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=A4=A9?= <442384644@qq.com> Date: Tue, 13 Sep 2022 12:28:14 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91=E6=9B=B4=E6=96=B0=E5=90=8E=E5=8F=B0token=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=EF=BC=8C=E6=9B=B4=E6=96=B0=E9=98=BF=E9=87=8C=E4=BA=91?= =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/app/dao/BaseDao.php | 71 ++++++++++--------- crmeb/app/services/BaseServices.php | 4 +- .../notice/RoutineTemplateListService.php | 2 +- .../app/services/shipping/ExpressServices.php | 8 +-- .../system/admin/AdminAuthServices.php | 5 +- .../system/admin/SystemAdminServices.php | 2 +- .../express/storage/AliyunExpress.php | 7 +- crmeb/crmeb/utils/JwtAuth.php | 5 +- 8 files changed, 54 insertions(+), 50 deletions(-) diff --git a/crmeb/app/dao/BaseDao.php b/crmeb/app/dao/BaseDao.php index 3e9e1c086..83acfa530 100644 --- a/crmeb/app/dao/BaseDao.php +++ b/crmeb/app/dao/BaseDao.php @@ -48,7 +48,7 @@ abstract class BaseDao * @param array $where * @return int */ - public function count(array $where = []): int + public function count(array $where = []) { return $this->search($where)->count(); } @@ -59,17 +59,22 @@ abstract class BaseDao * @param string $field * @param int $page * @param int $limit + * @param bool $search * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function selectList(array $where, $field = '*', $page = 0, $limit = 0) + public function selectList(array $where, string $field = '*', int $page = 0, int $limit = 0, bool $search = false) { - return $this->getModel()->where($where)->field($field) - ->when($page && $limit, function ($query) use ($page, $limit) { - $query->page($page, $limit); - })->select(); + if ($search) { + $model = $this->search($where); + } else { + $model = $this->getModel()->where($where); + } + return $model->field($field)->when($page && $limit, function ($query) use ($page, $limit) { + $query->page($page, $limit); + })->select(); } /** @@ -92,7 +97,7 @@ abstract class BaseDao * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function getDistinctCount(array $where, $field, $search = true) + public function getDistinctCount(array $where, $field, bool $search = true) { if ($search) { return $this->search($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0; @@ -112,7 +117,7 @@ abstract class BaseDao /** * 获取主键 - * @return mixed + * @return array|string */ protected function getPk() { @@ -121,7 +126,7 @@ abstract class BaseDao /** * 获取一条数据 - * @param int|array $id + * @param $id * @param array|null $field * @param array|null $with * @return array|Model|null @@ -136,7 +141,7 @@ abstract class BaseDao } else { $where = [$this->getPk() => $id]; } - return $this->getModel()::where($where)->when(count($with), function ($query) use ($with) { + return $this->getModel()->where($where)->when(count($with), function ($query) use ($with) { $query->with($with); })->field($field ?? ['*'])->find(); } @@ -179,7 +184,7 @@ abstract class BaseDao public function value(array $where, ?string $field = '') { $pk = $this->getPk(); - return $this->getModel()::where($where)->value($field ?: $pk); + return $this->getModel()->where($where)->value($field ?: $pk); } /** @@ -191,7 +196,7 @@ abstract class BaseDao */ public function getColumn(array $where, string $field, string $key = '') { - return $this->getModel()::where($where)->column($field, $key); + return $this->getModel()->where($where)->column($field, $key); } /** @@ -206,7 +211,7 @@ abstract class BaseDao } else { $where = [is_null($key) ? $this->getPk() : $key => $id]; } - return $this->getModel()::where($where)->delete(); + return $this->getModel()->where($where)->delete(); } /** @@ -214,7 +219,7 @@ abstract class BaseDao * @param int|string|array $id * @param array $data * @param string|null $key - * @return mixed + * @return BaseModel */ public function update($id, array $data, ?string $key = null) { @@ -235,7 +240,7 @@ abstract class BaseDao */ public function batchUpdate(array $ids, array $data, ?string $key = null) { - return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data); + return $this->getModel()->whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data); } /** @@ -251,7 +256,8 @@ abstract class BaseDao /** * 插入数据 * @param array $data - * @return mixed + * @return \think\Collection + * @throws \Exception */ public function saveAll(array $data) { @@ -262,7 +268,7 @@ abstract class BaseDao * 获取某个字段内的值 * @param $value * @param string $filed - * @param string $valueKey + * @param string|null $valueKey * @param array|string[] $where * @return mixed */ @@ -281,10 +287,10 @@ abstract class BaseDao { $with = []; $whereKey = []; - $respones = new \ReflectionClass($this->setModel()); + $responses = new \ReflectionClass($this->setModel()); foreach ($withSearch as $fieldName) { $method = 'search' . Str::studly($fieldName) . 'Attr'; - if ($respones->hasMethod($method)) { + if ($responses->hasMethod($method)) { $with[] = $fieldName; } else { $whereKey[] = $fieldName; @@ -297,7 +303,8 @@ abstract class BaseDao * 根据搜索器获取搜索内容 * @param array $withSearch * @param array|null $data - * @return Model + * @return BaseModel + * @throws \ReflectionException */ protected function withSearchSelect(array $withSearch, ?array $data = []) { @@ -308,7 +315,8 @@ abstract class BaseDao /** * 搜索 * @param array $where - * @return BaseModel|mixed + * @return BaseModel + * @throws \ReflectionException */ protected function search(array $where = []) { @@ -325,27 +333,25 @@ abstract class BaseDao * @param string $field * @param bool $search * @return float + * @throws \ReflectionException */ public function sum(array $where, string $field, bool $search = false) { if ($search) { return $this->search($where)->sum($field); } else { - return $this->getModel()::where($where)->sum($field); + return $this->getModel()->where($where)->sum($field); } } /** * 高精度加法 - * @param int|string $key + * @param $key * @param string $incField * @param string $inc * @param string|null $keyField * @param int $acc * @return bool - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException */ public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) { @@ -354,12 +360,11 @@ abstract class BaseDao /** * 高精度 减法 - * @param int|string $uid id - * @param string $decField 相减的字段 - * @param float|int $dec 减的值 - * @param string $keyField id的字段 - * @param bool $minus 是否可以为负数 - * @param int $acc 精度 + * @param $key + * @param string $decField + * @param string $dec + * @param string|null $keyField + * @param int $acc * @return bool */ public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2) @@ -405,7 +410,7 @@ abstract class BaseDao * @param int $num * @param string $stock * @param string $sales - * @return mixed + * @return false * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException diff --git a/crmeb/app/services/BaseServices.php b/crmeb/app/services/BaseServices.php index fa35b8ecb..ee366af72 100644 --- a/crmeb/app/services/BaseServices.php +++ b/crmeb/app/services/BaseServices.php @@ -66,11 +66,11 @@ abstract class BaseServices * @param $type * @return array */ - public function createToken(int $id, $type) + public function createToken(int $id, $type, $pwd = '') { /** @var JwtAuth $jwtAuth */ $jwtAuth = app()->make(JwtAuth::class); - return $jwtAuth->createToken($id, $type); + return $jwtAuth->createToken($id, $type, ['pwd' => md5($pwd)]); } /** diff --git a/crmeb/app/services/message/notice/RoutineTemplateListService.php b/crmeb/app/services/message/notice/RoutineTemplateListService.php index bd3f80656..7449d5ae4 100644 --- a/crmeb/app/services/message/notice/RoutineTemplateListService.php +++ b/crmeb/app/services/message/notice/RoutineTemplateListService.php @@ -202,7 +202,7 @@ class RoutineTemplateListService extends NoticeService $data['thing1'] = $bargain['title']; $data['amount2'] = $bargain['min_price']; $data['thing3'] = '恭喜您,已经砍到最低价了'; - return $this->sendTemplate((int)$uid, $data, '/pages/activity/user_goods_bargain_list/index?id=' . $bargain['id'] . '&bargain=' . $bargainUserId); + return $this->sendTemplate((int)$uid, $data, '/pages/activity/goods_bargain_details/index?id=' . $bargain['id'] . '&bargain=' . $bargainUserId); } /** diff --git a/crmeb/app/services/shipping/ExpressServices.php b/crmeb/app/services/shipping/ExpressServices.php index bd70c7d6a..14fbf16f6 100644 --- a/crmeb/app/services/shipping/ExpressServices.php +++ b/crmeb/app/services/shipping/ExpressServices.php @@ -17,7 +17,7 @@ use app\services\BaseServices; use app\services\serve\ServeServices; use crmeb\exceptions\AdminException; use crmeb\services\CacheService; -use crmeb\services\express\storage\AliyunExpress; +use crmeb\services\express\Express; use crmeb\services\FormBuilder as Form; /** @@ -211,9 +211,9 @@ class ExpressServices extends BaseServices } break; case 2: - /** @var AliyunExpress $services */ - $services = app()->make(AliyunExpress::class); - $result = $services->query($expressNum, $com); + /** @var Express $services */ + $services = app()->make(Express::class, ['aliyun_express']); + $result = $services->query($expressNum, '', sys_config('system_express_app_code')); if (is_array($result) && isset($result['result']) && isset($result['result']['deliverystatus']) && diff --git a/crmeb/app/services/system/admin/AdminAuthServices.php b/crmeb/app/services/system/admin/AdminAuthServices.php index 346420470..aace7ab27 100644 --- a/crmeb/app/services/system/admin/AdminAuthServices.php +++ b/crmeb/app/services/system/admin/AdminAuthServices.php @@ -54,7 +54,7 @@ class AdminAuthServices extends BaseServices /** @var JwtAuth $jwtAuth */ $jwtAuth = app()->make(JwtAuth::class); //设置解析token - [$id, $type] = $jwtAuth->parseToken($token); + [$id, $type, $pwd] = $jwtAuth->parseToken($token); //检测token是否过期 $md5Token = md5($token); @@ -96,6 +96,9 @@ class AdminAuthServices extends BaseServices $this->authFailAfter($id, $type); throw new AuthException(110003); } + if ($pwd !== md5($adminInfo->pwd)) { + throw new AuthException(110003); + } $adminInfo->type = $type; return $adminInfo->hidden(['pwd', 'is_del', 'status'])->toArray(); diff --git a/crmeb/app/services/system/admin/SystemAdminServices.php b/crmeb/app/services/system/admin/SystemAdminServices.php index 102807033..ec8911d84 100644 --- a/crmeb/app/services/system/admin/SystemAdminServices.php +++ b/crmeb/app/services/system/admin/SystemAdminServices.php @@ -92,7 +92,7 @@ class SystemAdminServices extends BaseServices public function login(string $account, string $password, string $type, string $key = '') { $adminInfo = $this->verifyLogin($account, $password); - $tokenInfo = $this->createToken($adminInfo->id, $type); + $tokenInfo = $this->createToken($adminInfo->id, $type, $adminInfo->pwd); /** @var SystemMenusServices $services */ $services = app()->make(SystemMenusServices::class); [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']); diff --git a/crmeb/crmeb/services/express/storage/AliyunExpress.php b/crmeb/crmeb/services/express/storage/AliyunExpress.php index 805348835..3f4306e02 100644 --- a/crmeb/crmeb/services/express/storage/AliyunExpress.php +++ b/crmeb/crmeb/services/express/storage/AliyunExpress.php @@ -12,6 +12,7 @@ namespace crmeb\services\express\storage; use crmeb\services\express\BaseExpress; +use crmeb\services\HttpService; /** * Class AliyunExpress @@ -31,13 +32,11 @@ class AliyunExpress extends BaseExpress * @param string $type * @return bool|mixed */ - public function query(string $no = '', string $type = '') + public function query(string $no = '', string $type = '', string $appCode = '') { - $appCode = sys_config('system_express_app_code'); if (!$appCode) return false; $res = HttpService::getRequest(self::$api['query'], compact('no', 'type'), ['Authorization:APPCODE ' . $appCode]); - $result = json_decode($res, true) ?: false; - return $result; + return json_decode($res, true) ?: false; } public function open() diff --git a/crmeb/crmeb/utils/JwtAuth.php b/crmeb/crmeb/utils/JwtAuth.php index 63765e64a..dfd2e66c0 100644 --- a/crmeb/crmeb/utils/JwtAuth.php +++ b/crmeb/crmeb/utils/JwtAuth.php @@ -43,9 +43,6 @@ class JwtAuth $host = app()->request->host(); $time = time(); $exp_time = strtotime('+ 30day'); -// if (app()->request->isApp()) { -// $exp_time = strtotime('+ 30day'); -// } $params += [ 'iss' => $host, 'aud' => $host, @@ -69,7 +66,7 @@ class JwtAuth $this->token = $jwt; list($headb64, $bodyb64, $cryptob64) = explode('.', $this->token); $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)); - return [$payload->jti->id, $payload->jti->type]; + return [$payload->jti->id, $payload->jti->type, $payload->pwd ?? '']; } /**