123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- /**
- * Created by PhpStorm
- * User:林志杰
- * Email:[email protected]
- * Motto:纵有疾风起,人生不言弃!
- * Time:2020/2/8 1:24
- */
- declare(strict_types=1);
- namespace app\common\business;
- use app\common\lib\Str;
- use app\common\lib\Time;
- use app\common\model\mysql\User as UserModel;
- class User
- {
- public $userObj = null;
- public function __construct()
- {
- $this->userObj = new UserModel();
- }
- /**
- * 用户登录
- * @param array $data
- * @return array|bool
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function login(array $data)
- {
- $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
- if (empty($redisCode) || $redisCode !== $data['code']) {
- throw new \think\Exception('验证码不存在', -1009);
- }
- // 需要去判断表 是否有 用户记录phoneNumber
- // 生成token
- $user = $this->userObj->getUserByPhoneNumber($data['phoneNumber']);
- if (!$user) {
- $username = "singewa粉-{$data['phoneNumber']}";
- $userData = [
- 'username' => $username,
- 'phone_number' => $data['phoneNumber'],
- 'type' => $data['type'],
- 'status' => config('status.mysql.table_normal'),
- ];
- try {
- $this->userObj->save($userData);
- $userId = $this->userObj->id;
- } catch (\Exception $e) {
- throw new \think\Exception('数据库内部异常');
- }
- } else {
- // 用户存在,更新表数据
- $userId = $user->id;
- $username = $user->username;
- }
- $token = Str::getLoginToken($data['phoneNumber']);
- $redisData = [
- 'id' => $userId,
- 'username' => $username,
- ];
- $res = cache(config('redis.token_pre') . $token, $redisData, Time::userLoginExpiresTime($data['type']));
- return $res ? ['token' => $token, 'username' => $username] : false;
- }
- /**
- * 返回正常用户数据
- * @param int $id
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getNormalUserById(int $id): array
- {
- $user = $this->userObj->getUserById($id);
- if (!$user || $user->status !== config('status.mysql.table_normal')) {
- return [];
- }
- return $user->toArray();
- }
- public function getNormalUserByUsername(string $username): array
- {
- $user = $this->userObj->getUserByUsername($username);
- if (!$user || $user->status !== config('status.mysql.table_normal')) {
- return [];
- }
- return $user->toArray();
- }
- public function update(int $id, array $data)
- {
- $user = $this->getNormalUserById($id);
- if (!$user) {
- throw new \think\Exception('不存在该用户');
- }
- // 检查用户名是否存在
- $userResult = $this->getNormalUserByUsername($data['username']);
- if ($userResult && $userResult['id'] !== $id) {
- throw new \think\Exception('该用户已经存在请重新设置');
- }
- return $this->userObj->updateById($id, $data);
- }
- }
|