123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- use think\facade\Db;
- // 这是系统自动生成的公共文件
- function make_verify(){
- $code = rand(100000,999999);
- return $code;
- }
- /**
- * 发送短信接口
- * @param type $phone
- * @param type $code
- */
- function sendMessage($phone, $code) {
- //1.下列选项必填
- $url = 'https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1'; //APP接入地址+接口访问URI
- $APP_KEY = 'ww3mKZEWh3fhboNZ791pL5fhSNfJ'; //APP_Key
- $APP_SECRET = 'nvXKrQQeEkQRp5750M2p85ILs4xC'; //APP_Secret
- $sender = '99200620888880002777'; //国内短信签名通道号或国际/港澳台短信通道号
- $TEMPLATE_ID = '54b09b74ee764cca90571b65bfb20f9f'; //模板ID
- //$sender = '8821032432899'; //国内短信签名通道号或国际/港澳台短信通道号
- //$TEMPLATE_ID = 'faa13c0b2deb4646a31304434c07b856'; //模板ID
- $signature = '泰康广源'; //签名名称
- //必填,全局号码格式(包含国家码),示例:+8615123456789,多个号码之间用英文逗号分隔
- $receiver = '+86'.$phone; //短信接收人号码
- //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
- $statusCallback = '';
- /**
- * 选填,使用无变量模板时请赋空值 $TEMPLATE_PARAS = '';
- * 单变量模板示例:模板内容为"您的验证码是${NUM_6}"时,$TEMPLATE_PARAS可填写为'["369751"]'
- * 双变量模板示例:模板内容为"您有${NUM_2}件快递请到${TXT_32}领取"时,$TEMPLATE_PARAS可填写为'["3","人民公园正门"]'
- * 查看更多模板变量规则:常见问题>业务规则>短信模板内容审核标准
- * @var string $TEMPLATE_PARAS
- */
- $TEMPLATE_PARAS = '["'.$code.'"]'; //模板变量,根据自身使用的模板,其值长度和个数与模板对应
- //请求Headers
- $headers = [
- 'Content-Type: application/x-www-form-urlencoded',
- 'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
- 'X-WSSE: '.buildWsseHeader($APP_KEY, $APP_SECRET)
- ];
- //请求Body
- $data = http_build_query([
- 'from' => $sender,
- 'to' => $receiver,
- 'templateId' => $TEMPLATE_ID,
- 'templateParas' => $TEMPLATE_PARAS,
- 'statusCallback' => $statusCallback,
- 'signature' => $signature //使用国内短信通用模板时,必须填写签名名称
- ]);
- $ch = curl_init();
- curl_setopt ($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
- $response = curl_exec($ch);
- return $response;
- }
- /**
- * 构造X-WSSE参数值
- * @param string $appKey
- * @param string $appSecret
- * @return string
- */
- function buildWsseHeader($appKey, $appSecret)
- {
- date_default_timezone_set('Asia/Shanghai');
- $now = date('Y-m-d\TH:i:s\Z'); //Created
- $nonce = uniqid(); //Nonce
- $base64 = base64_encode(hash('sha256', ($nonce . $now . $appSecret))); //PasswordDigest
- return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"",
- $appKey, $base64, $nonce, $now);
- }
- /**手机号验证
- * @param $mobile
- * @return bool
- */
- function checkMobile($mobile){
- if (!is_numeric($mobile)) {
- return false;
- }
- return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
- }
- /**邮箱验证
- * @param $email
- * @return bool
- */
- function checkEmail($email){
- if (!$email) {
- return false;
- }
- return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
- }
- /**
- * @param
- * @return int
- */
- function makeSalt(){
- $salt = rand(10000000,99999999);
- return $salt;
- }
- /**
- * @param $account
- * @param float|int $expire
- * @return string
- */
- function makeToken($account,$expire=0){
- $tokeninfo = Db::table("sys_token")->where(['accountid'=>$account['id']])->find();
- $date = date("Y-m-d H:i:s");
- $token = sha1($account['username'].$account['mobile'].$date);
- if(!$tokeninfo){
- $data=['token'=>$token,"accountid"=>$account['id'],"addtime"=>$date,"expire"=>$expire];
- }else{
- $data=$tokeninfo;
- $data['token']=$token;
- $data['addtime']=$date;
- $data['expire']=$expire;
- }
- $resulr=Db::table("sys_token")->save($data);
- return $resulr? $token:"";
- }
- /**
- * @param $token
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- function VerifyToken($token){
- $tokeninfo = Db::table("sys_token")->where(['token'=>$token])->find();
- if(!$tokeninfo){
- return ['code'=>101,"message"=>'token不存在'];
- }
- if($tokeninfo['expire']<time()){
- return ['code'=>102,"message"=>'token已失效'];
- }
- $acc = Db::name("view_userinfo")->where("id","=",$tokeninfo['accountid'])->find();
- if(!$acc){
- return ['code'=>103,"message"=>'未找到对应的账户'];
- }
- $verify=sha1($acc['username'].$acc['mobile'].$tokeninfo['addtime']);
- if($verify!=$token){
- return ['code'=>104,"message"=>'token无效'];
- }
- $tokeninfo['expire'] = time()+1800;
- Db::table("sys_token")->save($tokeninfo);
- return ['code'=>0,"message"=>'验证通过',"user"=>['id'=>$tokeninfo['accountid']]];
- }
- /**
- * POST 请求
- * @param string $url
- * @param array $param
- * @param boolean $post_file 是否文件上传
- * @return string content
- */
- function post($url,$data,$header=[])
- {
- //对空格进行转义
- $url = str_replace(' ','+',$url);
- $ch = curl_init();
- //设置选项,包括URL
- curl_setopt($ch, CURLOPT_URL, "$url");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // POST数据
- // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1);
- // 把post的变量加上
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //所需传的数组用http_bulid_query()函数处理一下,就ok了
- curl_setopt($ch, CURLOPT_HEADER, true);
- //执行并获取url地址的内容
- $output = curl_exec($ch);
- $errorCode = curl_errno($ch);
- //释放curl句柄
- $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
- // 根据头大小去获取头信息内容
- $header = substr($output, 0, $headerSize);
- curl_close($ch);
- if(0 !== $errorCode) {
- return false;
- }
- return $header;
- }
- function post2($url,$data,$header=[])
- {
- //对空格进行转义
- $url = str_replace(' ','+',$url);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- //设置选项,包括URL
- curl_setopt($ch, CURLOPT_URL, "$url");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- // POST数据
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1);
- // 把post的变量加上
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //所需传的数组用http_bulid_query()函数处理一下,就ok了
- $output = curl_exec($ch);
- $errorCode = curl_errno($ch);
- curl_close($ch);
- if(0 !== $errorCode) {
- return false;
- }
- return $output;
- }
|