first(); if (is_null($user)) { $user = new User(); $user->openid = $openId; $user->status = User::STATUS_OK; $user->save(); } return $this->encryptToken($user->id); } public function login($params) { $user = User::where("email", $params['userinfo'])->orWhere("phone", $params['userinfo'])->first(); if (is_null($user)) { throw new ClientException("用户不存在"); } if ($user->status == User::STATUS_STOP) { throw new ClientException("账户已禁用"); } if (!$user->checkPassword($params['password'])) { throw new ClientException("账户或密码错误"); } return $user; } public function register($params) { if (User::where("email", $params['email'])->first()) { throw new ClientException("该邮件已经注册了"); } if (User::where("phone", $params['phone'])->first()) { throw new ClientException("该手机已经注册了"); } $user = new User(); $user->email = $params['email']; $user->phone = $params['phone']; $user->group_id = 0; $user->status = User::STATUS_OK; $user->password = $user->hashPassword($params['password']); $user->save(); return $user; } public function encryptToken($uid) { return md5($uid . time()) . $uid; } public function decryptToken($token) { return substr($token, 32); } public function resetPassword($params) { $user = User::where("email", $params['email'])->first(); $user->password = $user->hashPassword($params['password']); $user->save(); return $user; } public function updateUserInfo(User $user, $params) { if ($params['avatar']) { $user->extra = array_merge($user->extra ?? [], [ "wechat" => [ "avatar" => $params['avatar'], ], ]); } if ($email = Arr::get($params, "email")) { $user->email = $email; } if ($name = Arr::get($params, "name")) { $user->name = $name; } if ($name = Arr::get($params, "password")) { $user->password = $user->hashPassword($name); } $user->save(); return $user; } }