You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

当前弹窗数据库取数错误提示配置及OTP页前置手机号校验需求

解决OTP流程中的两个需求:错误提示与手机号预校验

我来帮你搞定这两个需求,结合你给出的控制器代码片段,咱们一步步来实现:


1. 进入OTP输入页前校验手机号是否已注册

咱们要在用户提交手机号后、弹出OTP输入框之前,先判断这个手机号有没有注册过。可以在你的VerificationOTP方法里先完成这个校验逻辑:

修改控制器代码

public function VerificationOTP() {
    $postData = $this->input->post();
    // 先校验手机号是否存在且有效
    if (isset($postData['mobileNumber']) && !empty(trim($postData['mobileNumber']))) {
        $this->load->model('DoctorSearch');
        
        try {
            // 调用模型方法检查手机号注册状态
            $isRegistered = $this->DoctorSearch->checkPhoneNumber($postData, 3);
            
            // 如果手机号未注册,直接在当前弹窗返回错误提示
            if (!$isRegistered) {
                echo json_encode([
                    'status' => 'error',
                    'message' => '该手机号未注册,请先完成注册'
                ]);
                return;
            }
            
            // 手机号已注册,准备OTP页面所需数据
            $data['mobileNumber'] = trim($postData['mobileNumber']);
            $data['loginOTP'] = $this->input->post('loginOTP') ?? null;
            
            // 加载OTP输入弹窗视图
            $this->load->view('otp_input_popup', $data);
            
        } catch (Exception $e) {
            // 捕获数据库操作错误,返回提示
            echo json_encode([
                'status' => 'error',
                'message' => '查询用户信息失败:' . $e->getMessage()
            ]);
            return;
        }
    } else {
        // 手机号为空的错误提示
        echo json_encode([
            'status' => 'error',
            'message' => '请输入有效的手机号'
        ]);
        return;
    }
}

完善模型的checkPhoneNumber方法

确保模型能正确返回注册状态,同时抛出数据库异常:

public function checkPhoneNumber($postData, $type) {
    try {
        // 根据你的业务逻辑编写查询,比如查询医生表中的手机号
        $this->db->where('mobile_number', trim($postData['mobileNumber']));
        $query = $this->db->get('doctors');
        
        // 存在记录则说明已注册
        return $query->num_rows() > 0;
    } catch (Exception $e) {
        // 抛出数据库错误信息,让控制器捕获
        throw new Exception($this->db->error()['message']);
    }
}

2. 在弹窗中显示数据库操作的错误提示

通过上面的控制器代码,我们已经用try-catch捕获了数据库异常,并且把错误信息以JSON格式返回给前端。接下来只需要在前端通过AJAX接收响应,在当前弹窗中展示错误:

前端AJAX交互代码

// 绑定手机号提交按钮的点击事件
$('#submit-phone-btn').on('click', function() {
    const mobileNumber = $('#mobile-input').val().trim();
    if (!mobileNumber) {
        $('#popup-error').text('请输入手机号').show();
        return;
    }
    
    $.ajax({
        url: '/your-controller-path/VerificationOTP',
        type: 'POST',
        data: {mobileNumber: mobileNumber},
        dataType: 'json',
        success: function(res) {
            if (res.status === 'error') {
                // 在当前弹窗显示错误提示
                $('#popup-error').text(res.message).show();
            } else {
                // 隐藏当前弹窗,显示OTP输入弹窗
                $('#phone-popup').hide();
                $('#otp-popup').html(res).show();
            }
        },
        error: function() {
            $('#popup-error').text('网络请求失败,请稍后重试').show();
        }
    });
});

弹窗HTML结构示例

<!-- 手机号输入弹窗 -->
<div id="phone-popup" class="modal">
    <input type="tel" id="mobile-input" placeholder="请输入手机号">
    <button id="submit-phone-btn">获取OTP</button>
    <!-- 错误提示容器 -->
    <div id="popup-error" class="error-text" style="display: none; color: red;"></div>
</div>

<!-- OTP输入弹窗 -->
<div id="otp-popup" class="modal" style="display: none;">
    <input type="text" id="otp-input" placeholder="请输入6位OTP">
    <button id="submit-otp-btn">验证OTP</button>
</div>

这样就能满足你的两个需求:

  • 进入OTP页前自动校验手机号注册状态,未注册则在当前弹窗提示
  • 数据库操作出错时,把错误信息返回并显示在弹窗中

内容的提问来源于stack exchange,提问作者user8769728

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:01:47