doutui8842 2018-08-10 17:26
浏览 34

重新注册中的问题在我的应用程序中

Hey I Have an application.In which when user register before entering into application.The problem is i am using otp verification.But while making otp request sometimes otp doesn't comes and user fails to register into application and if user want to re-register he is not able to re-register as in database his details are saved and according to this code he is not able to re-request for otp.Here i am embedding my php code.Kindly help me in if otp verified then only data saved into my database.

<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Auth;
use App\User;
use App\Otp;
use App\Referal;
use App\Constant;

class registerController extends Controller
{
    public function apiRegister(Request $request)
    {
        $this->Validate($request,[
            'name' => 'required|string',
            'mobile' => 'required|unique:users,mobile|min:10|max:10',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6',
            'refer_code' => 'nullable|exists:users,refer_code',
            'device_id' => 'required'
        ]);



    $device = User::where('device_id',$request->device_id)->first();

    if (!empty($device) && $device->otp_verified == 'n') {

        $apiToken = str_random(150);
        $otp = mt_rand(1000,9999);
        $msg = 'Your OTP is: '.$otp;
        $ot = sendSms($request->mobile,$msg);
        if($ot == true)
        {
            User::create([
                'name' => $request->name,
                'mobile' => $request->mobile,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'api_token' => $apiToken,
                'user_type' => 'u',
                'points' => 0,
                'refer_code' => refferCode(),
                'otp' => $otp,
                'ip_address' => \Request::getClientIp(true),
                'device_id' => $request->device_id
            ]);

            $user_id = User::where('mobile',$request->mobile)->select('id','refer_code')->first();

            if(isset($request->refer_code)){    // if referal code is entered by user

                $refer_user = User::where('refer_code',$request->refer_code)->first();

                if(strtoupper($refer_user->refer_code) == strtoupper($request->refer_code))
                {
                    $const = Constant::where('constant_name','referal_points')->first();

                    Referal::create([
                        'refer_from_id' => $refer_user->id,
                        'refer_to_id' => $user_id->id,
                        'referal_points' => $const->constant_value,
                        'time' => time()
                    ]);
                }   
                else
                {
                    $response['msg'] = 'Referal Code is invalid!! try with another Referal code!!';
                    return response()->json($response,400);
                }
            }

            Otp::create([
                'user_id' => $user_id->id,
                'time' => time(),
            ]);

            $response['msg'] = 'User Registered Successfully!';
            $response['refer_code'] = 'Your Referal Code :'. $user_id->refer_code;
            return response()->json($response,200);
        }
        else
        {
            $response['msg'] = 'User Registreation Unsuccessful!  Try again!!!';
            return response()->json($response,400);
        }
    }elseif(empty($device))
    {
        $apiToken = str_random(150);
        $otp = mt_rand(1000,9999);
        $msg = 'Your OTP is: '.$otp;
        $ot = sendSms($request->mobile,$msg);
        if($ot == true)
        {
            User::create([
                'name' => $request->name,
                'mobile' => $request->mobile,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'api_token' => $apiToken,
                'user_type' => 'u',
                'points' => 0,
                'refer_code' => refferCode(),
                'otp' => $otp,
                'ip_address' => \Request::getClientIp(true),
                'device_id' => $request->device_id
            ]);

            $user_id = User::where('mobile',$request->mobile)->select('id','refer_code')->first();

            if(isset($request->refer_code)){    // if referal code is entered by user

                $refer_user = User::where('refer_code',$request->refer_code)->first();
                if($refer_user->refer_code == $request->refer_code)
                {
                    $const = Constant::where('constant_name','referal_points')->first();
                    Referal::create([
                        'refer_from_id' => $refer_user->id,
                        'refer_to_id' => $user_id->id,
                        'referal_points' => $const->constant_value,
                        'time' => time(),
                    ]);
                }   
                else
                {
                    $response['msg'] = 'Referal Code is invalid!! try with another Referal code!!';
                    return response()->json($response,400);
                }
            }

            Otp::create([
                'user_id' => $user_id->id,
                'time' => time(),
            ]);

            $response['msg'] = 'User Registered Successfully!';
            $response['refer_code'] = 'Your Referal Code :'. $user_id->refer_code;
            return response()->json($response,200);
        }
        else
        {
            $response['msg'] = 'User Registreation Unsuccessful!  Try again!!!';
            return response()->json($response,400);
        }

    }
    else
    {
        $response['msg'] = 'This device has been used previously. Please try with another device!';
        return response()->json($response,400);
    }

}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算