CommonController.php 5.7 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\MobileCodeModel;
use think\Db;
class CommonController extends HomeBaseController
{
    private $url = 'http://api.mix2.zthysms.com/v2/sendSmsTp';//短信请求地址
    private $username = 'dujiaoxinghy';//用户名
    private $password = 'eU3OeYmx';//密码
    private $tpId = 345;//模板id
    public function index(){
        return $this->fetch();
    }

    /**
     * 发送手机验证码
     */
    public function sendMobileCode(){
//        Db::startTrans();
        $mobile = $this->request->param('mobile','');
        $mc = new MobileCodeModel();
        $search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
        if (!preg_match($search,$mobile)) {
            $this->apiResponse(0,'手机号格式有误','');
        }
        $mobile_code = rand(100000, 999999);
        $info = $mc->where([
            'mobile' => $mobile,
            'create_date' => date('Y-m-d')
        ])->find();
        if($info){
            if(time() < $info['create_time']+60 && $info['is_use'] == 0){
                $this->apiResponse(0,'不能频繁发送验证码','');
            }
            if($info['count'] > 10){
                $this->apiResponse(0,'今天发送验证码的次数已达到了上限','');
            }
            $res = $mc->where('id',$info['id'])->data([
                'mobile' => $mobile,
                'mobile_code' => $mobile_code,
                'is_use' => 0,
                'expire_time' => time()+300,
                'count' => $info['count'] +1
            ])->update();
        }else{
            $res = $mc->insert([
                'mobile' => $mobile,
                'mobile_code' => $mobile_code,
                'is_use' => 0,
                'expire_time' => time()+300,
                'count' => 1,
                'create_time' => time(),
                'create_date' => date('Y-m-d')
            ]);
        }
        if($res) {
            //发送验证码
            $this->sendCode($mobile, $mobile_code);
//            $is_ok = json_decode($is_ok, true);
//            if ($is_ok['code'] != 200) {
//                Db::rollback();
//                $this->apiResponse(0, $is_ok['msg']);
//            } else {
//                Db::commit();
//                $this->apiResponse(1, '验证码发送成功,请注意查收!');
//            }
            $this->apiResponse(1, '验证码发送成功,请注意查收!');
        }
    }

    /**
     * 验证手机验证码
     */
    public function validateMobileCode($post){
        $check = '/^1[3-9]\d{9}$/';
        $mc = new MobileCodeModel();
        if(empty($post['mobile'])){
            $this->apiResponse(0,'手机号不能为空!');
        }
        if (!preg_match($check, $post['mobile'])) {
            $this->apiResponse(0,'请输入正确的手机号!');
        }
        if(empty($post['mobile_code'])){
            $this->apiResponse(0,'验证码不能为空!');
        }
        $res_find = $mc->where(['mobile' => $post['mobile'], 'mobile_code' => $post['mobile_code'], 'is_use' => 0, 'create_date' => date('Y-m-d'),])
            ->where('expire_time','gt',time())->find();
        if($res_find){
            $res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);
            if($res_update){
                return true;
            }
        }else{
            $this->apiResponse(0,'验证未通过',$post);
        }
    }

    /**
     * 发送短信验证码
     * @param $mobile
     * @param $mobile_code
     * @return mixed
     */
    private function sendCode($mobile,$mobile_code){
        date_default_timezone_set('PRC');//设置时区
        $url = $this->url;//提交地址
        $tKey = time();
        $password = $this->password;//密码
        $data['username'] = $this->username;//用户名
        $data['tKey'] = $tKey;
        $data['password'] = md5(md5($password).$tKey);//原密码
        $data['tpId'] = $this->tpId;//模板id
        $data['records'][0]['mobile'] = $mobile;
        $data['records'][0]['tpContent']['var1'] = $mobile_code;
        $data['signature'] = '【独角星球】';
        $headers = ['Content-Type: application/json;charset=UTF-8'];
        $data = json_encode($data,true);
        $curl = curl_init();// 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, true); // 开启header
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); // 执行操作
        curl_close($curl);
        return $result;
    }
}