p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.3 KiB

<?php
namespace app\home\service;
use app\model\AwsS3Model;
use app\model\CountryModel;
use app\model\FileModel;
use app\model\UserModel;
use app\model\UserVerifyLogModel;
use think\facade\Cache;
class UserVerifyService extends BaseHomeService
{
public function add($params, $userId)
{
try {
// 参数校验
if(empty($params['surname']) || !is_string($params['surname'])){
return $this->toData('400', lang('parameter_error'));
}
if(empty($params['name']) || !is_string($params['name'])){
return $this->toData('400', lang('parameter_error'));
}
if(empty($params['code']) || !is_string($params['code'])){
return $this->toData('400', lang('parameter_error'));
}
if(empty($params['country']) || !is_numeric($params['country'])){
return $this->toData('400', lang('parameter_error'));
}
if(empty($params['front_img'])){
return $this->toData('400', lang('parameter_error'));
}
if (empty($params['birth_day'])) {
return $this->toData('400', lang('parameter_error'));
}
if (empty($params['gender'])) {
return $this->toData('400', lang('parameter_error'));
}
// if (empty($params['addr'])) {
// return $this->toData('400','addr 参数无效');
// }
// if (empty($params['zip_code'])) {
// return $this->toData('400','zip_code 参数无效');
// }
if (empty($params['email'])) {
return $this->toData('400', lang('parameter_error'));
}
if (empty($params['email_code'])) {
return $this->toData('400', lang('parameter_error'));
}
// if(empty($params['back_img']) || !is_numeric($params['back_img'])){
// return $this->toData('100400','Invalid back_img');
// }
// 判断用户状态
$user = UserModel::where('user_id', $userId)->find();
if(empty($user)){
return $this->toData('500', lang('data_empty'));
}
// 判断是是否已经认证
if($user->is_real == 1){
return $this->toData('500', lang('already_verified'));
}
// 是否是未认证或者认证失败状态
if($user->real_status == 2 || $user->real_status == 3){
return $this->toData('500','status error');
}
$country = CountryModel::where('id',$params['country'])->find();
if(empty($country)){
return $this->toData('500','country error');
}
// 验证邮箱
$emailKey = 'USER:sendEmailLoginNoTrade:' . $params['email'];
$cacheCode = Cache::store('redis')->get($emailKey);
if (empty($cacheCode) || $cacheCode != $params['email_code']) {
return $this->toData('500', lang('incorrect_verification_code'));
}
// 写入数据库
$userVerify = new UserVerifyLogModel;
$userVerify->user_id = $userId;
$userVerify->country = $params['country'];
$userVerify->code = $params['code'];
$userVerify->surname = $params['surname'];
$userVerify->name = $params['name'];
$userVerify->front_img = $params['front_img'];
$userVerify->back_img = $params['back_img'] ?? "";
$userVerify->create_time = date('Y-m-d H:i:s');
$userVerify->update_time = date('Y-m-d H:i:s');
$userVerify->birth_day = $params['birth_day'];
$userVerify->gender = $params['gender'];
// $userVerify->addr = $params['addr'];
// $userVerify->zip_code = $params['zip_code'];
$userVerify->email = $params['email'];
$userVerify->save();
// 更改用户状态
$user->real_status = 2;
$user->save();
return $this->toData('0','SUCCESS');
}catch (\Exception $exception){
return $this->toData('500', lang('system_busy'));
}
}
public function detail($userId)
{
try {
$user = UserModel::where('user_id', $userId)->find();
if(empty($user)){
return $this->toData('500', lang('user_does_not_exist'));
}
$userVerify = UserVerifyLogModel::where('user_id', $userId)->order('id', 'desc')->find();
if (empty($userVerify)) {
return $this->toData('500', lang('user_real_name_authentication_data_is_empty'));
}
$data = $userVerify->toArray();
$front = AwsS3Model::where('id', $userVerify->front_img)->value('s3_url');
$back = AwsS3Model::where('id', $userVerify->back_img)->value('s3_url');
$country = CountryModel::where('id', $userVerify->country)->find();
$data['country_data'] = $country;
$data['front_img'] = $front;
$data['back_img'] = $back;
return $this->toData('0', 'SUCCESS', $data);
}catch (\Exception $exception){
return $this->toData('500', lang('system_busy'), [$exception->getMessage(), $exception->getTrace()]);
}
}
}