123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- use think\facade\Db;
- function make_verify(){
- $code = rand(100000,999999);
- return $code;
- }
- function sendMessage($phone, $code) {
-
- $url = 'https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1';
- $APP_KEY = 'ww3mKZEWh3fhboNZ791pL5fhSNfJ';
- $APP_SECRET = 'nvXKrQQeEkQRp5750M2p85ILs4xC';
- $sender = '99200620888880002777';
- $TEMPLATE_ID = '54b09b74ee764cca90571b65bfb20f9f';
-
-
- $signature = '泰康广源';
-
- $receiver = '+86'.$phone;
-
- $statusCallback = '';
-
- $TEMPLATE_PARAS = '["'.$code.'"]';
-
- $headers = [
- 'Content-Type: application/x-www-form-urlencoded',
- 'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
- 'X-WSSE: '.buildWsseHeader($APP_KEY, $APP_SECRET)
- ];
-
- $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;
- }
- function buildWsseHeader($appKey, $appSecret)
- {
- date_default_timezone_set('Asia/Shanghai');
- $now = date('Y-m-d\TH:i:s\Z');
- $nonce = uniqid();
- $base64 = base64_encode(hash('sha256', ($nonce . $now . $appSecret)));
- return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"",
- $appKey, $base64, $nonce, $now);
- }
- 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;
- }
- function checkEmail($email){
- if (!$email) {
- return false;
- }
- return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
- }
- function makeSalt(){
- $salt = rand(10000000,99999999);
- return $salt;
- }
- 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:"";
- }
- 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']]];
- }
- function post($url,$data,$header=[])
- {
-
- $url = str_replace(' ','+',$url);
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_URL, "$url");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch,CURLOPT_TIMEOUT,3);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-
-
- curl_setopt($ch, CURLOPT_POST, 1);
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_HEADER, true);
-
- $output = curl_exec($ch);
- $errorCode = curl_errno($ch);
-
- $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);
-
- curl_setopt($ch, CURLOPT_URL, "$url");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
-
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1);
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $output = curl_exec($ch);
- $errorCode = curl_errno($ch);
- curl_close($ch);
- if(0 !== $errorCode) {
- return false;
- }
- return $output;
- }
|