CommonController.php 1.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/9/19
 * Time: 11:07
 */

namespace app\user\controller;

use app\member\model\MemberModel;
use app\user\model\CodeModel;
use cmf\controller\HomeBaseController;

class CommonController extends HomeBaseController
{

    /**
     * 发送手机验证码
     * @param $mobile 手机号码
     */
    public function sendCode() {
        if($this->request->isAjax()) {
            $mobile = $this->request->param('mobile');
            $is_login = $this->request->param('is_login',0,'intval');
            $member_model = new MemberModel();
            $where = [
                'mobile' => $mobile,
                'delete_time' => 0
            ];
            $member = $member_model->where($where)->find();
            if($is_login == 1) {
                if(!$member) {
                    $this->error('该手机号码未注册');
                }
            } else {
                if($member) {
                    $this->error('该手机号码已注册');
                }
            }
            $code = $this->generate_code();
            // 记录验证码数据
            $code_insert = [
                'mobile' => $mobile,
                'code' => $code,
                'over_time' => time()+600
            ];
            $code_model = new CodeModel();
            $code_model->addCode($code_insert);
            $result = explode(',',$this->sendSMS($mobile,$code));
            if($result[0] != 1) {
                $this->error($result[0]);
            }
            $this->success('发送成功');
        }
    }
}