first(); } /** * @param $phone * @param $code * @param $memberId * @param $visitorId * @return void * @throws Exception */ public static function verify($phone, $code, $memberId, $visitorId): void { $search = ['phone' => $phone, 'code' => $code]; $phoneCode = static::$MODEL::where(static::getWhere($search))->orderByDesc('id')->first(); if (!$phoneCode) throw new Exception("验证码错误", HttpStatus::CUSTOM_ERROR); $time = time(); if ($phoneCode->ext < $time) throw new Exception("验证码过期", HttpStatus::CUSTOM_ERROR); $phoneCode->status = 1; $phoneCode->save(); $user = UserService::findOne(['member_id' => $memberId]); if (!$user) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR); $registerIp = request()->ip(); $user->register_ip = $registerIp; $user->phone = $phone; $user->visitor_id = $visitorId; if (User::where('phone', $phone)->where('member_id', '!=', $memberId)->exists()) { User::where('phone', $phone)->where('member_id', '!=', $memberId)->update(['status' => 1]); $user->status = 1; } if (User::where('register_ip', $registerIp)->where('member_id', '!=', $memberId)->exists()) { User::where('register_ip', $registerIp)->where('member_id', '!=', $memberId)->update(['status' => 1]); $user->status = 1; } if (User::where('visitor_id', $visitorId)->where('member_id', '!=', $memberId)->exists()) { User::where('visitor_id', $visitorId)->where('member_id', '!=', $memberId)->update(['status' => 1]); $user->status = 1; } $user->save(); } public static function add($phone, $code): PhoneCode { $time = time(); $ext = $time + 600; $data = [ 'phone' => $phone, 'code' => $code, 'ext' => $ext, ]; return static::$MODEL::create($data); } }