php定时任务管理控制器

2022-03-08 07:24:28 浏览数 (1)

业务场景

后台php写个定时任务控制器,在服务器端每天定时执行来进行一些特殊的操作

如每天定时执行来获取第三方平台账单并对系统用户余额进行一次扣除操作

实现方法
代码语言:javascript复制
<?php
/**
 *
 * 版权所有:安德玛(underArmour)
 * 作    者:1554324405@qq.com
 * 日    期:2021-12-10
 * 功能说明:定时任务控制器。
 *
 **/
namespace ApiController;
use thinkLog;
class CrontabController extends BaseController
{
    /**
     */
    public function index()
    {
		/*
		 * 查询加油订单
		*/
		$params1['username'] = '13696630855';
		$params1['password'] = 'l953677738';
		$apiurl1 = 'https://life-api.cars.shengxintech.com/api/auth/login';
		$content = $this->send_post2($apiurl1,$params1);
		$result = json_decode($content, true); //对JSON格式的字符串进行编码
		if($result['code'] == '200'){
			$sxToken = $result['data']['auth']['access_token'];
			$pay = 1;
			$to = date("Y-m-d H:i:s",time());
			$from = date('Y-m-d H:i:s',strtotime("$to -1 day"));
			$apiurl2 = "https://life-api.cars.shengxintech.com/api/czbOrders?pay=".$pay."&from=".$from."&to=".$to;
			$content = $this->send_get($apiurl2,$sxToken);
			$result2 = json_decode($content, true); //对JSON格式的字符串进行编码
			if($result2['code'] == '200'){
				$items = $result2['data']['items'];
				foreach ($items as $k=>&$v){
					if($v['status'] == 2){
						$username = $v['user']['mobile'];
						$money = ($v['origin_price'] - $v['price'])/100;
						M('user')->where('username='.$username)->setDec('money',$money);
						$orderInfo = "用户名:".$username."折扣金额:".$money;
						addlog('加油订单,购买信息:'.$orderInfo);
					}
				}
			}
		}
		/*
		 * 查询话费订单
		*/
	   //  千猪请求参数
	   $params['platformId'] = 10482;
	   $params['action'] = "mobileOrders.pagedQuery";
	   $params['traceId'] = 'biggift12345678901biggift12345678901';
	   $params['version'] = '1.0';
	   $params['timestamp'] = $this->getUnixTimestamp();
	   $params['content'] = '{"updateTimeBeginTime":"'.date('Y-m-d H:i:s',strtotime("$to -1 day")).'","updateTimeEndTime":"'.date("Y-m-d H:i:s",time()).'"}';
	   // $params['content'] ="{"updateTimeBeginTime":"2022-01-11 01:01:01","updateTimeEndTime":"2022-01-17 01:01:01"}";
	   $params['sign'] = $this->MakeSign1($params);
	   $apiurl = 'https://nf.qianzhu8.com/gateway';
	   $params = json_encode($params);
	   $header = ['Content-Type: application/json'];
	   $content = $this->send_post2($apiurl,$params,$header);
	   $result = json_decode($content, true); //对JSON格式的字符串进行编码
	   if($result['code'] == '200'){
	   	$items = $result['data']['items'];
	   	foreach ($items as $k=>&$v){
			if($v['status'] == 15){
				$username = $v['userMobile'];
				$money = ($v['rechargeDenomination'] - $v['platformSettlePrice']);
				M('user')->where('username='.$username)->setDec('money',$money);
				$orderInfo = "用户名:".$username."折扣金额:".$money;
				addlog('话费订单,购买信息:'.$orderInfo);
				}
			}
	   }
	   /*
	    * 查询电影订单
	   */
	  //  千猪请求参数
	  $params2['pageSize'] = 10;
	  $params2['pageIndex'] = 1;
	  $params2['platformId'] = 10482;
	  $params2['timestamp'] = $this->getUnixTimestamp();
	  $params2['updateTimeBeginTime'] = date('Y-m-d H:i:s',strtotime("$to -1 day"));
	  $params2['updateTimeEndTime'] = date("Y-m-d H:i:s",time());
	  $params2['sign'] = $this->MakeSign1($params2);
	  $apiurl = 'https://live.qianzhu8.com/api/v1/platform/movieOrder/pagedQuery';
	  $params2 = json_encode($params2);
	  $header = ['Content-Type: application/json'];
	  $content = $this->send_post2($apiurl,$params2,$header);
	  $result = json_decode($content, true); //对JSON格式的字符串进行编码
	  if($result['code'] == '10000'){
	  	$items = $result['data'];
	  	foreach ($items as $k=>&$v){
	  			if($v['status'] == 15 && $v['drawMode'] == 0){
	  				$userId = $v['userId'];
					Log::write("输出userIduserIduserId密码".$userId,'DEBUGE');
					$res = M('qz_mapping')->where('userid='.$userId)->order('t desc')->limit('1')->find();
					if($res){
						$username = $res['username'];
						$money = ($v['marketUnitPrice'] - $v['unitPrice'])*$v['seatsCount'];
						Log::write("输出signsignsign订单密码".$money,'DEBUGE');
						M('user')->where('username='.$username)->setDec('money',$money);
						$orderInfo = "用户名:".$username."折扣金额:".$money;
						addlog('电影订单,购买信息:'.$orderInfo);
					}
	  				}
	  			}
	  }
    }
	function getUnixTimestamp ()
	{
	    list($s1, $s2) = explode(' ', microtime());
	    return (float)sprintf('%.0f',(floatval($s1)   floatval($s2)) * 1000);
	}
	/**
	 * 生成签名千猪
	 * @return 签名
	 */    		
	public function MakeSign1($params)
	   {
		   $secret = 'q04kipf4yy7l6lb9';
	       //签名步骤一:按字典序排序数组参数
	       ksort($params);
	       $string = $this->ToUrlParams1($params);
	       //签名步骤二:在string后加入KEY
	       $string =$string.$secret;
	       //签名步骤三:MD5加密
	       $string = md5($string);
	       //签名步骤四:所有字符转为大写
	       return $string;
	   }
	/**
	 * 将参数拼接为url: key=value&key=value
	 * @param   $params
	 * @return  string
	 */      
	  public function ToUrlParams1($params)
	  {
	      //print_r($params);
	      $string = '';
	      if (!empty($params)) {
	          $array = array();
	          foreach ($params as $key => $value) {
	              $array[] = $key . '=' . $value;
	          }
	          //print_r($array);exit;
	          $string = implode("&", $array);
	          //$string = str_replace("---","&" ,$string);
	      //  file_put_contents('1.txt',$string);
	      }
	      //print_r($string);exit;
	      return $string;
	  }
	/**
	* POST提交
	* @param string $url 地址
	* @param string $data 提交的数据
	* @return string 返回结果
	*/
	public function send_post2($url, $data,$header=null)
	{
	$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_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
	//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
	//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
	if(!empty($header))
	{
	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
	}
	curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
	curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制 防止死循环
	curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
	
	$tmpInfo = curl_exec($curl); // 执行操作
	if(curl_errno($curl))
	{
	echo 'Errno'.curl_error($curl);//捕抓异常
	}
	curl_close($curl); // 关闭CURL会话
	return $tmpInfo; // 返回数据
	}  
	
	
	/**
	* GET提交
	* @param string $url 地址
	* @param string $data 提交的数据
	* @return string 返回结果
	*/
	public function send_get($url, $token)
	{
	try{
	$info=curl_init();
	$headers[]  =  "Accept:application/json";
	$headers[]  =  "Authorization: Bearer ". $token;
	if( !empty($headers) ){
	    curl_setopt( $info, CURLOPT_HTTPHEADER, $headers );
	}
	curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($info,CURLOPT_ENCODING,"");
	curl_setopt($info,CURLOPT_MAXREDIRS,3);
	curl_setopt($info,CURLOPT_TIMEOUT,0);
	curl_setopt($info,CURLOPT_FOLLOWLOCATION,true);
	curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);                
	curl_setopt($info, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($info,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
	curl_setopt($info,CURLOPT_CUSTOMREQUEST,"GET");
	curl_setopt($info,CURLOPT_URL,$url);
	$output= curl_exec($info);
	curl_close($info);
	  }catch(Exception $e){
	    var_dump("catch");
	   $result= $client->__getMessage();
	}  
	return $output;
	}
}
服务器每天定时脚本执行请求上述控制器即可

0 人点赞