Common.php 4.9 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\api\controller;

use app\admin\model\MobileCode;
use think\Db;
/**
 * 公共方法
 */
class Common
{
    /**
     * 查找单条数据(有排序)
     * @ApiInternal
     */
    public static function findWhereData($table,$where,$field,$order='id desc'){
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->order($order)
            ->find();
        return $res;
    }

    /**
     * 查找多条数据(无条件)
     * @ApiInternal
     */
    public static function selectData($table,$field,$order='id desc'){
        $res = Db::name($table)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }

    /**
     * 查找多条数据
     * @ApiInternal
     */
    public static function selectWhereData($table,$where,$field,$order='id desc'){
        $res = Db::name($table)
            ->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }

    /**
     * 查找多条数据
     * @ApiInternal
     */
    public static function selectWherePageData($table,$where,$page,$field,$order='id desc'){
        $limit = config('verify.limit');
        $res = Db::name($table)
            ->where($where)
            ->page($page,$limit)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }

    /**
     * 统计总页码
     * @ApiInternal
     */
    public static function totalPage($table,$where){
        $limit = config('verify.limit');
        $count = Db::name($table)->where($where)->count();
        $totalPage = ceil($count/$limit);
        return $totalPage;
    }



    /**
     * 单张相对路径转绝对路径
     * @ApiInternal
     */
    public static function absolutionUrlOne($image){
        $url = '';
        if(!empty($image)){
            $host = config('verify.host');
            $url = $host.$image;
        }
        return $url;
    }

    /**
     * 发送手机验证码
     * @ApiInternal
     */
    public static function sendCode($mobile,$content){
        date_default_timezone_set('PRC');//设置时区
        $url 		= "http://www.ztsms.cn/sendNSms.do";//提交地址
        $username 	= 'jiazhengcm';//用户名
        $password 	= 'Cxz307312';//原密码
        //短信平台绑定手机号18612918096
        $data = array(
            'content' 	=> $content,//短信内容
            'mobile' 	=> $mobile,//手机号码
            'productid' => '676767',//产品id
            'xh'		=> ''//小号
        );
        $isTranscoding = false;
        $data['content'] = $isTranscoding === true ? mb_convert_encoding($data['content'], "UTF-8") : $data['content'];
        $data['username']=$username;
        $data['tkey'] 	= date('YmdHis');
        $data['password'] = md5(md5($password) . $data['tkey']);
        $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,  http_build_query($data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); // 执行操作
        return $result;
    }

    /**
     * 验证验证码是否正确
     * @ApiInternal
     */
    public static function validateMobileCode($post){
        $mobileCodeModel = new MobileCode();
        $res_find = $mobileCodeModel->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 = $mobileCodeModel->where('id',$res_find['id'])->setField('is_use',1);
            if($res_update){
                return true;
            }
        }
        return false;
    }
}