| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace App\Module\User\Services;
- use App\Module\User\Dto\UserDto;
- use App\Module\User\Dto\UserInfoDto;
- use App\Module\User\Dto\UserPublicDto;
- use App\Module\User\Enums\STATUS2;
- use App\Module\User\Models;
- use App\Module\User\Unit\UserPublic;
- use Illuminate\Support\Facades\Hash;
- class UserService
- {
- /**
- * 创建用户
- *
- * @param string $username 用户名
- * @param string $password 密码
- * @return UserDto|string 成功返回用户DTO,失败返回错误信息
- * @throws \LogicException
- */
- static public function create($username, $password)
- {
- $user = new \App\Module\User\Models\User();
- $user->username = $username;
- $user->password = Hash::make($password);
- $user->status2 = STATUS2::Normal;
- $res = $user->save();
- if(\App\Module\System\Services\User::isSysUid($user->id)){
- $user->delete();
- throw new \LogicException("错误34");
- }
- if ($res === false) {
- return 'create-error';
- }
- UserService::infoinfo($user->id, true);
- // 返回DTO而不是模型
- return UserDto::fromModel($user);
- }
- /**
- * 使用手机号码注册
- *
- * @param string $phone 手机号
- * @param string $password 密码
- * @return UserDto|string 成功返回用户DTO,失败返回错误信息
- * @throws \LogicException
- */
- static public function createPhone($phone, $password)
- {
- $user = self::create($phone, $password);
- if (is_string($user)) {
- throw new \LogicException("create error");
- }
- $b = Phone::bind($user->id, $phone);
- if ($b === false) {
- throw new \LogicException("create bind error");
- }
- return $user;
- }
- /**
- * 更改密码
- *
- * @param $id
- * @param $password
- * @return string|true
- */
- static public function resetPassword($id, $password)
- {
- $user = \App\Module\User\Models\User::query()->find($id);
- if (!$user) {
- return 'user-notfind';
- }
- $user->password = Hash::make($password);
- $res = $user->save();
- if ($res === false) {
- return 'update-error';
- }
- return true;
- }
- /**
- * 列表
- *
- * @param $page
- * @param $limit
- * @param $where
- * @return \Illuminate\Pagination\LengthAwarePaginator
- */
- static public function lisss($page, $limit, $where)
- {
- $q = \App\Module\User\Models\User::query()->with([
- 'fund', 'merchant', 'info'
- ]);
- $wArr = new Arr($where, $q);
- $wArr->queryNumber('user_id')
- ->queryString('mobile');
- return $q->paginate($limit, [
- 'user_id',
- 'nick_name'
- ], '', $page);
- }
- /**
- * 获取用户信息
- *
- * @param int $id 用户ID
- * @param bool $create 如果不存在是否创建
- * @return UserDto|null 用户DTO或null
- */
- static public function info($id, $create = false)
- {
- $model = \App\Module\User\Logic\User::info($id, $create);
- if (!$model) {
- return null;
- }
- return UserDto::fromModel($model);
- }
- /**
- * 获取用户详细信息
- *
- * @param int $id 用户ID
- * @param bool $create 如果不存在是否创建
- * @return UserInfoDto|null 用户详细信息DTO或null
- */
- static public function infoinfo($id, $create = false)
- {
- $model = \App\Module\User\Logic\User::infoinfo($id, $create);
- if (!$model) {
- return null;
- }
- return UserInfoDto::fromModel($model);
- }
- /**
- * 获取用户公共信息
- *
- * @param array $ids 用户ID数组
- * @return array 用户公共信息DTO数组
- */
- static public function pinfos($ids = [])
- {
- if(empty($ids)){
- return [];
- }
- $ids2 = [];
- foreach ($ids as $id) {
- if(empty($id)){
- continue;
- }
- if(\App\Module\System\Services\User::isSysUid($id)){
- continue;
- }
- $ids2[$id] = $id;
- }
- $res = self::pinfoCaches($ids2);
- if ($ids2) {
- /**
- * @var \App\Module\User\Models\UserInfo[] $users
- */
- $users = \App\Module\User\Models\UserInfo::query()->whereIn('user_id', $ids2)->with([
- 'merchant'
- ])->get();
- foreach ($users as $user) {
- // 使用DTO而不是直接使用UserPublic
- $userDto = UserPublicDto::fromUserInfo($user);
- $res[$userDto->userId] = $userDto;
- \Illuminate\Support\Facades\Cache::put(self::publicInfoKey($user->user_id), $userDto);
- unset($ids2[$userDto->userId]);
- }
- }
- if (count($ids2)) {
- foreach ($ids2 as $id) {
- // 使用DTO而不是直接使用UserPublic
- $userDto = UserPublicDto::createDeleted($id);
- $res[$userDto->userId] = $userDto;
- \Illuminate\Support\Facades\Cache::put(self::publicInfoKey($id), $userDto);
- unset($ids2[$userDto->userId]);
- }
- }
- return $res;
- }
- /**
- * 从缓存中获取用户公共信息
- *
- * @param array $ids 用户ID数组
- * @return array 用户公共信息DTO数组
- */
- static public function pinfoCaches(&$ids)
- {
- $res = [];
- foreach ($ids as $id) {
- $c = \Illuminate\Support\Facades\Cache::get(self::publicInfoKey($id));
- if ($c) {
- // 如果缓存中的是旧的UserPublic对象,转换为DTO
- if ($c instanceof UserPublic) {
- $c = UserPublicDto::fromUserPublic($c);
- }
- $res[$id] = $c;
- unset($ids[$id]);
- }
- }
- return $res;
- }
- /**
- * 用户公共数据缓存key
- *
- * @param $uid
- * @return string
- */
- static public function publicInfoKey($uid)
- {
- return 'lan_userpublic_' . $uid;
- }
- static public function getPhone()
- {
- }
- }
|