Files
duolamaojiazhen/application/api/controller/Order.php
2025-12-22 13:59:40 +08:00

918 lines
35 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\controller;
use app\api\logic\OrderLogic;
use app\common\model\Client_;
use think\Db;
use app\common\server\WeChatServer;
use app\common\model\NoticeSetting;
use EasyWeChat\Factory;
/**
* 订单
* Class Order
* @package app\api\controller
*/
class Order extends ApiBase
{
public $like_not_need_login = ['wxmassin', 'lists', 'appInit', 'downLine', 'share', 'config','pcLists','getpege','ttorder'];
//抖音订单回调
public function ttorder(){
$ttPay = new ttPay();
$res = $ttPay->run('notify');
}
//订单列表
public function lists()
{
$type = $this->request->get('type', 'all');
$order_list = OrderLogic::getOrderList($this->user_id, $type, $this->page_no, $this->page_size);
$this->_success('获取成功', $order_list);
}
public function stafflist()
{
$type = $this->request->get('type', 'all');
$order_list = OrderLogic::stafflist($this->user_id, $type, $this->page_no, $this->page_size);
$this->_success('获取成功', $order_list);
}
//员工订单详情页面
public function numberlist()
{
$type = $this->request->get('type', 'all');
$tiem= $this->request->get();
if(isset($tiem['id'])){
$order_list = OrderLogic::timelist($this->user_id, $type,$tiem,$this->page_no, $this->page_size);
$this->_success('获取成功', $order_list);
}else{
$order_list = OrderLogic::numberlist($this->user_id, $type, $this->page_no, $this->page_size);
$this->_success('获取成功', $order_list);
}
}
//员工服务订单列表
//下单接口
public function buy()
{
$post = $this->request->post();
$post['user_id'] = $this->user_id;
$post['client'] = $this->client;
// $openid = 'oehgp4zzyK34d3TgnXD1ytpeNRjI';
// //发送下单成功通知
// $template = [
// 'touser'=>$openid,
// 'template_id'=>'qTmpP2ZnGMpgAFgNsmcVMfTjCeSE7GXEQQaFTUERAuU',
// 'page'=>'',//点击模板消息 打开小程序页面
// 'data'=>[
// 'thing2'=>['value'=>'擦玻璃服务'],
// 'amount8'=>['value'=>'金额2元'],
// 'time10'=>['value'=>date('Y-m-d H:i',time())]
// ]
// ];
// $r = send_mini_template($template);
$check = $this->validate($post, 'app\api\validate\Order.buy');
if (true !== $check) {
$this->_error($check);
}
$action = $post['action'];
$info = OrderLogic::info($post, $this->user_id);
if ($info['code'] == 0) {
$this->_error($info['msg']);
}
if ($action == 'info') {
$this->_success('', $info['data']);
}
if ($this->client != Client_::pc && empty($post['pay_way'])) {
$this->_error('请联系管理员配置支付方式');
}
$order = OrderLogic::add($this->user_id, $info['data'], $post);
return $order;
}
//订单详情
public function detail()
{
$order_id = $this->request->get('id');
if (!$order_id){
$this->_error('请选择订单');
}
$order_detail = OrderLogic::getOrderDetail($order_id, $this->user_id);
if (!$order_detail) {
$this->_error('订单不存在了!', '');
}
$this->_success('获取成功', $order_detail);
}
//取消订单
public function cancel()
{
$order_id = $this->request->post('id');
if (empty($order_id)) {
$this->_error('参数错误');
}
return OrderLogic::cancel($order_id, $this->user_id);
}
//删除订单
public function del()
{
$order_id = $this->request->post('id');
if (empty($order_id)) {
$this->_error('参数错误');
}
return OrderLogic::del($order_id, $this->user_id);
}
//确认订单
public function confirm()
{
$order_id = $this->request->post('id');
if (empty($order_id)) {
$this->_error('参数错误');
}
return OrderLogic::confirm($order_id, $this->user_id);
}
public function orderTraces()
{
$order_id = $this->request->get('id');
$tips = '参数错误';
if ($order_id) {
$traces = OrderLogic::orderTraces($order_id, $this->user_id);
if ($traces) {
$this->_success('获取成功', $traces);
}
$tips = '暂无物流信息';
}
$this->_error($tips);
}
public function Get_Chinese_WeekDay($Join_Date)
{
$WeekDay = date('w', strtotime($Join_Date));
$WeekList = array('日', '一', '二', '三', '四', '五', '六');
return '星期' . $WeekList[$WeekDay];
}
public function get3day(){
$goods_id = $this->request->post('id');
$order=Db::name('order')->where('id',$goods_id)->find(); //获取到订单的信息
$goods=Db::name('goods')->where('id',$order['goods_id'])->find(); //获取到商品的信息
// $staff=Db::name('staff')->where('goods_id',$goods['brand_id'])->select(); //查询到所有的保洁师傅
// dump($staff);die;
$days = [];
$current = date('Y-m-d');
for($i = 1 ; $i <= 3 ;$i++){
$current = date("Y-m-d", strtotime($current . "+1 day"));
$days[] = $current;
}
$data = [];
foreach ($days as $k=>$v){
$start = strtotime($v); //开始时间
$end = strtotime($v.' 23:59:59'); //结束时间
//统计这个时间请假的用户的数据
$orders=Db::name('order_exe')->where('addtime',1)->where('autotime','>=',$start)->where('autotime','<=',$end)->whereNotNull('staff_id')->field('staff_id')->group('staff_id')->select(); //查询有订单的保洁师傅
$oneDimensionalArray = array_column($orders, 'staff_id'); // 根据自己的表格字段名修改'字段名'部分
$lent=Db::name('leave')->where('addtime',1)->where('time','>=',$start)->where('time','<=',$end)->whereNotNull('user_id')->field('user_id')->group('user_id')->select();
$lentuser = array_column($lent,'user_id');
//判断上午
$numbersw=Db::name('staff')
->where('onwork',1)
->where('goods_id',$goods['brand_id'])
->whereNotIn('id',$oneDimensionalArray) // $array为要比较的数组
->whereNotIn('id',$lentuser) // $array为要比较的数组
->count(); //获取到请假的人数
$order_numbersw=Db::name('order_exe')->where('autotime','>=',$start)->where('autotime','<=',$end)->where('staff_id',0)->where('addtime',1)->count();// 没有派单的保洁师
$numbersw=$numbersw-$order_numbersw;
//统计这个时间请假的用户的数据
$ordersxw=Db::name('order_exe')->where('addtime',2)->where('autotime','>=',$start)->where('autotime','<=',$end)->whereNotNull('staff_id')->field('staff_id')->group('staff_id')->select(); //查询有订单的保洁师傅
$oneDimensionalArrayxw= array_column($ordersxw, 'staff_id'); // 根据自己的表格字段名修改'字段名'部分
$lentxw=Db::name('leave')->where('addtime',2)->where('time','>=',$start)->where('time','<=',$end)->whereNotNull('user_id')->field('user_id')->group('user_id')->select();
$lentuserxw = array_column($lentxw,'user_id');
//判断下午
$numberxw=Db::name('staff')
->where('onwork',1)
->whereNotIn('id',$oneDimensionalArrayxw) // $array为要比较的数组
->where('goods_id',$goods['brand_id'])
->whereNotIn('id',$lentuserxw) // $array为要比较的数组
->count(); //获取到请假的人数
$order_numberxw=Db::name('order_exe')->where('autotime','>=',$start)->where('autotime','<=',$end)->where('staff_id',0)->where('addtime',2)->count();// 没有派单的保洁师
$numberxw=$numberxw-$order_numberxw;
if($numbersw>0){
$coied=false;
} else{
$coied=true;
}
if($numberxw>0){
$coiedxw=false;
} else{
$coiedxw=true;
}
$data[] = [
'date'=>$v,
'week'=>$this->Get_Chinese_WeekDay($v),
'hourLists'=>[
['hour'=>"上午",'select'=>false,'disabled'=>$coied,'selectDay'=>$v,'staffnumber'=>$numbersw],
['hour'=>'下午','select'=>false,'disabled'=>$coiedxw,'selectDay'=>$v,'staffnumber'=>$numberxw],
]
];
}
$data[0]['select'] = true;
$this->_success('获取成功', $data);
}
public function getday(){
$post = $this->request->post();
$order=Db::name('order')->where('id',$post['data']['id'])->find(); //获取到订单的信息
$goods=Db::name('goods')->where('id',$order['goods_id'])->find(); //获取到商品的信息
$day = $post['data']['day'];
$start = strtotime($day);
$end = strtotime($day.' 23:59:59');
//统计这个时间请假的用户的数据
$orders=Db::name('order_exe')->where('addtime',1)->where('autotime','>=',$start)->where('autotime','<=',$end)->whereNotNull('staff_id')->field('staff_id')->group('staff_id')->select(); //查询有订单的保洁师傅
$oneDimensionalArray = array_column($orders, 'staff_id'); // 根据自己的表格字段名修改'字段名'部分
$lent=Db::name('leave')->where('addtime',1)->where('time','>=',$start)->where('time','<=',$end)->whereNotNull('user_id')->field('user_id')->group('user_id')->select();
$lentuser = array_column($lent,'user_id');
$numbersw=Db::name('staff')
->where('onwork',1)
->where('goods_id',$goods['brand_id'])
->whereNotIn('id',$oneDimensionalArray) // $array为要比较的数组
->whereNotIn('id',$lentuser) // $array为要比较的数组
->count();
$order_numbersw=Db::name('order_exe')->where('autotime','>=',$start)->where('autotime','<=',$end)->where('staff_id',0)->where('addtime',1)->count();// 没有派单的保洁师
$numbersw=$numbersw-$order_numbersw;
//统计这个时间请假的用户的数据
$ordersxw=Db::name('order_exe')->where('addtime',2)->where('autotime','>=',$start)->where('autotime','<=',$end)->whereNotNull('staff_id')->field('staff_id')->group('staff_id')->select(); //查询有订单的保洁师傅
$oneDimensionalArrayxw= array_column($ordersxw, 'staff_id'); // 根据自己的表格字段名修改'字段名'部分
$lentxw=Db::name('leave')->where('addtime',2)->where('time','>=',$start)->where('time','<=',$end)->whereNotNull('user_id')->field('user_id')->group('user_id')->select();
$lentuserxw = array_column($lentxw,'user_id');
//判断下午
$numberxw=Db::name('staff')
->where('onwork',1)
->whereNotIn('id',$oneDimensionalArrayxw) // $array为要比较的数组
->whereNotIn('id',$lentuserxw) // $array为要比较的数组
->where('goods_id',$goods['brand_id'])
->count(); //获取到请假的人数
$order_numberxw=Db::name('order_exe')->where('autotime','>=',$start)->where('autotime','<=',$end)->where('staff_id',0)->where('addtime',2)->count();// 没有派单的保洁师
$numberxw=$numberxw-$order_numberxw;
if($numbersw>0){
$coied=false;
}else{
$coied=true;
}
if($numberxw>0){
$coiedxw=false;
}else{
$coiedxw=true;
}
$data[] = [
'date'=>$day,
'week'=>$this->Get_Chinese_WeekDay($day),
'hourLists'=>[
['hour'=>'上午','select'=>false,'disabled'=>$coied,'selectDay'=>$day,'staffnumber'=>$numbersw],
['hour'=>'下午','select'=>false,'disabled'=>$coiedxw,'selectDay'=>$day,'staffnumber'=>$numberxw],
]
];
$data[0]['select'] = true;
$this->_success('获取成功', $data);
}
public function sendgzhmsg($orderid = ''){
$uid = $this->user_id;
// $uid = 366;
$openids=Db::name('user_auth')->where('user_id',$uid)->find(); //获取用户的ID
$adder=Db::name('user_address')->where('user_id',$uid)->find();
// $openid = 'o_XDv5_Cq72XDOx1qvzq2kcaOYWA';
$openid = $openids['openid'];
$data = [
'touser'=>$openid,
'url'=>'',
'template_id'=>'MJBxsfAT18v9fQ8LYK2v1AF4iRkprNp1KPVm1ses64I', //模板id
'data'=>[
'time4'=>[
'value'=>date('Y-m-d H:i',time())
],
'thing1'=>[
'value'=>$adder['contact']
]
]
];
$config = WeChatServer::getOaConfig();
$app = Factory::officialAccount($config);
$r = $app->template_message->send($data);
return true;
}
public function shensatff($id){
dump($id);
}
public function putday(){
$post = $this->request->post();
$sw = $post['data']['hourLists'][0]['select'];
$xw = $post['data']['hourLists'][1]['select'];
$orderid=$post['data']['id']; //传递进来的订单id
$day = $post['data']['date'];
$order=Db::name('order')->where('id',$orderid)->find(); //获取到订单的基本信息
$goods=Db::name('goods')->where('id',$order['goods_id'])->find();
if($order['code']<=0){
$this->_error('订单次数已不足无法预约');
}
if($sw && $xw){
$code=Db::name('order')->where('id',$orderid)->find();
if($code['code']<=2){
return 20;
}else{
$data=[
'order_sn'=>$order['order_sn'], //订单编号
'create_time'=>time(),
// 'fworder_sn'=> $post['fworder_sn'],
'date'=>$day,
'xq'=>$this->Get_Chinese_WeekDay($day),
'type'=>3,
'status'=>0,
'addtime'=>1,
'autotime'=>strtotime($day),
];
$list=Db::name('order_exe')->data($data)->insert();
$datas=[
'order_sn'=>$order['order_sn'], //订单编号
'create_time'=>time(),
// 'fworder_sn'=> $post['fworder_sn'],
'date'=>$day,
'xq'=>$this->Get_Chinese_WeekDay($day),
'type'=>3,
'status'=>0,
'addtime'=>2,
'autotime'=>strtotime($day),
];
$list=Db::name('order_exe')->data($datas)->insert();
$order=Db::name('order')->where('id',$orderid)->update(['code'=>$order['code']-2]); //获取到订单的基本信息
$this->sendgzhmsg($day);
if($order['admin_id']!=NULL){
$admin=Db::name('admin')->where('id',$order['admin_id'])->find();
$openids=Db::name('user_auth')->where('user_id',$admin['user_id'])->find(); //获取用户的ID
//$time=date('Y-m-d',$orders['autotime']);
//$openid = 'o_XDv5_Cq72XDOx1qvzq2kcaOYWA';
$openid = $openids['openid'];
$data = [
'touser'=>$openid,
'url'=>'',
'template_id'=>'9ESlNoZweXTioNwyaDuSY9fIXCKhnrBi2Gu0Hl8cmAo', //模板id
'data'=>[
'keyword1'=>[
'value'=>$order['order_sn']
],
'keyword2'=>[
'value'=>$goods['name']
],
'keyword3'=>[
'value'=>$order['consignee']
],
'keyword4'=>[
'value'=>$order['mobile']
],
'keyword5'=>[
'value'=>$day
],
]
];
$config = WeChatServer::getOaConfig();
$app = Factory::officialAccount($config);
$r = $app->template_message->send($data);
}
$this->_success('预约成功,待上门', $list);
}
}
if($sw){
if($order>0){
$data=[
'order_sn'=>$order['order_sn'], //订单编号
'create_time'=>time(),
// 'fworder_sn'=> $post['fworder_sn'],
'date'=>$day,
'xq'=>$this->Get_Chinese_WeekDay($day),
'type'=>3,
'status'=>0,
'addtime'=>1,
'autotime'=>strtotime($day),
];
$list=Db::name('order_exe')->data($data)->insert();
$orderss=Db::name('order')->where('id',$orderid)->update(['code'=>$order['code']-1]); //获取到订单的基本信息
$this->sendgzhmsg($day);
if($order['admin_id']!=NULL){
$admin=Db::name('admin')->where('id',$order['admin_id'])->find();
$openids=Db::name('user_auth')->where('user_id',$admin['user_id'])->find(); //获取用户的ID
//$time=date('Y-m-d',$orders['autotime']);
//$openid = 'o_XDv5_Cq72XDOx1qvzq2kcaOYWA';
$openid = $openids['openid'];
$data = [
'touser'=>$openid,
'url'=>'',
'template_id'=>'9ESlNoZweXTioNwyaDuSY9fIXCKhnrBi2Gu0Hl8cmAo', //模板id
'data'=>[
'keyword1'=>[
'value'=>$order['order_sn']
],
'keyword2'=>[
'value'=>$goods['name']
],
'keyword3'=>[
'value'=>$order['consignee']
],
'keyword4'=>[
'value'=>$order['mobile']
],
'keyword5'=>[
'value'=>$day.'.8:00-12:00'
],
]
];
$config = WeChatServer::getOaConfig();
$app = Factory::officialAccount($config);
$r = $app->template_message->send($data);
}
$this->_success('预约成功,待上门', $list);
}else{
return 20;
}
}
if($xw){
if($order>0){
$data=[
'order_sn'=>$order['order_sn'], //订单编号
'create_time'=>time(),
// 'fworder_sn'=> $post['fworder_sn'],
'date'=>$day,
'xq'=>$this->Get_Chinese_WeekDay($day),
'type'=>3,
'status'=>0,
'addtime'=>2,
'autotime'=>strtotime($day),
];
$list=Db::name('order_exe')->data($data)->insert();
$order=Db::name('order')->where('id',$orderid)->update(['code'=>$order['code']-1]); //获取到订单的基本信息
$this->sendgzhmsg($day);
if($order['admin_id']!=NULL){
$admin=Db::name('admin')->where('id',$order['admin_id'])->find();
$openids=Db::name('user_auth')->where('user_id',$admin['user_id'])->find(); //获取用户的ID
//$time=date('Y-m-d',$orders['autotime']);
//$openid = 'o_XDv5_Cq72XDOx1qvzq2kcaOYWA';
$openid = $openids['openid'];
$data = [
'touser'=>$openid,
'url'=>'',
'template_id'=>'9ESlNoZweXTioNwyaDuSY9fIXCKhnrBi2Gu0Hl8cmAo', //模板id
'data'=>[
'keyword1'=>[
'value'=>$order['order_sn']
],
'keyword2'=>[
'value'=>$goods['name']
],
'keyword3'=>[
'value'=>$order['consignee']
],
'keyword4'=>[
'value'=>$order['mobile']
],
'keyword5'=>[
'value'=>$day.'.14:00-16:00'
],
]
];
$config = WeChatServer::getOaConfig();
$app = Factory::officialAccount($config);
$r = $app->template_message->send($data);
}
$this->_success('预约成功,待上门', $list);
}else{
return 20;
}
}
}
//保洁师开始接单
public function take(){
$order_id = $this->request->post('params');
$order=Db::name('order_exe')->where('id',$order_id)->find();
if($order['autotime']-time()>24*3600){
$this->_error('无法接单,未到时间');
}else{
$toke=Db::name('order_exe')->where('id', $order_id )->update(['status'=>1]);
$this->_success('接单成功');
}
}
//保洁师开始接单
public function wego(){
$order_id = $this->request->post('params');
$toke=Db::name('order_exe')->where('id', $order_id )->update(['status'=>1,'timeadd'=>time(),'staff_status'=>1]);
$this->_success('接单成功');
}
//保洁师结束服务
public function ends(){
$order_id = $this->request->post('params');
$toke=Db::name('order_exe')->where('id', $order_id )->update(['status'=>2,'timeout'=>time(),'staff_status'=>1]);
$this->_success('接单成功');
}
//订单的详情显示
public function orderinfo(){
$type = $this->request->post('type', 'all');
$post=$this->request->post();
$order_sn=$post['id']['order_sn'];
$order_list = OrderLogic::orderinfo($this->user_id, $type,$order_sn,$this->page_no, $this->page_size);
$this->_success('获取成功', $order_list);
}
//微信的通知
public function wxmassin(){
$start = strtotime(date("Y-m-d",strtotime("+1 day")));
$end =$start+24*3600-1;
$order=Db::name('order_exe')->where('autotime','>=',$start)->where('autotime','<=',$end)->where('staff_id','<>',0)->select();
foreach ($order as &$orders) {
$custom=Db::name('order')->where('order_sn',$orders['order_sn'])->find();
$goods=Db::name('goods')->where('id',$custom['goods_id'])->find();
$user=Db::name('staff')->where('id',$orders['staff_id'])->find();
if($orders['addtime']=1){
$timesw='上午';
}else{
$timesw='下午';
}
// $uid = 366;
$openids=Db::name('user_auth')->where('user_id',$user['user_id'])->find(); //获取用户的ID
$time=date('Y-m-d',$orders['autotime']);
// $openid = 'o_XDv5_Cq72XDOx1qvzq2kcaOYWA';
$openid = $openids['openid'];
$data = [
'touser'=>$openid,
'url'=>'',
'template_id'=>'YYsFPwTCrPcxi0JA_zg0xnz6uFaVAlobfTGvhQVDglE', //模板id
'data'=>[
'keyword1'=>[
'value'=>$goods['name']
],
'keyword2'=>[
'value'=> $time.$timesw
],
'keyword3'=>[
'value'=>$custom['address']
],
'keyword4'=>[
'value'=>$custom['consignee']
],
'keyword5'=>[
'value'=>$custom['mobile']
],
]
];
$config = WeChatServer::getOaConfig();
$app = Factory::officialAccount($config);
$r = $app->template_message->send($data);
}
}
public function getpege(){
$id = $this->request->get('id');
$date=Db::name('pageweb')->where('id',$id)->find();
$this->_success('获取数据成功',$date);
// dump($id);
}
}
Class ttPay{
private $api_url='https://developer.toutiao.com/api/apps/ecpay/v1/';
private $app_id;
private $token;
private $salt;
public function __construct() {
$this->app_id = 'tt0523739e9a12236501';
$this->token='123qazqweeesdflomswwe';
$this->salt='ejAuHvbI472euyhb34aVyLD92ll9tgZCqWnMi0tX';
}
public function run($action,$order = null){
$action=$action?$action:'order';
if(!in_array($action,['order','query','refund','settle','notify','set'])){
echo '非法请求';die;
}
return $this->$action($order);
}
//下单
private function order($order){
$data=[
'out_order_no'=>$order['order_sn'],
'total_amount'=>1,
'subject'=>'购买服务',
'body'=>'购买商品',
'valid_time'=>3600,
];
return $this->post('create_order',$data);
}
//查询订单
private function query(){
$data=[
'out_order_no'=>'2021110117254573565'
];
$res=$this->post('query_order',$data,false);
echo json_encode($res);die;
}
//订单退款
private function refund(){
$data=[
'out_order_no'=>'2021110118351347832',
'out_refund_no'=>$this->order_number(),
'reason'=>'退款原因',
'refund_amount'=>1,
];
$res=$this->post('create_refund',$data);
echo json_encode($res);die;
}
//订单分账
private function settle(){
$data=[
'out_order_no'=>'2021110118301265990',
'out_settle_no'=>$this->order_number(),
'settle_desc'=>'分账描述',
'settle_params'=>json_encode([]),//分润方参数 如[['merchant_uid'=>'商户号','amount'=>'10']] 可以有多个分账商户
];
$res=$this->post('settle',$data);
echo json_encode($res);die;
}
//支付设置回调测试
private function set(){
$content=file_get_contents('php://input');
$this->log('log.txt',$content);
}
//回调
private function notify(){
$content=input();
if(empty($content)) return false;
$this->log('notify.txt',json_encode($content));
if(is_array($content)){
$content=$content;
}else{
$content=json_decode($content,true);
}
$sign=$this->handler($content);
if(!empty($content['msg_signature']) && $sign==$content['msg_signature']){
if(is_array($content['msg'])){
$msg=$content['msg'];
}else{
$msg=json_decode($content['msg'],true);
}
if($content['type'] == 'payment'){
$order_sn = $msg['payment_order_no'];
Db::name('order')->where('order_sn',$order_sn)->update(['pay_status'=>1]);
}
//这里更新应用业务逻辑代码,使用$msg跟应用订单比对更新订单,可以用 $content['type']判断是支付回调还是退款回调payment支付回调 refund退款回调。
$res=['err_no'=>0,'err_tips'=>'success'];
echo json_encode($res);
exit;
}else{
echo '验签失败';
}
}
/**
* 测试订单号,实际应用根据自己应用实际生成
* @return string
*/
private function order_number(){
return date('YmdHis').rand(10000,99999);
}
/**
* 请求小程序平台服务端
* @param string $url 接口地址
* @param array $data 参数内容
* @param boolean $notify 是否有回调
* @return array
*/
private function post($method,$data,$notify=true){
$data['app_id']=$this->app_id;
$data['sign']=$this->sign($data);
$url=$this->api_url.$method;
$res=$this->http('POST',$url,json_encode($data),['Content-Type: application/json'],true);
return json_decode($res,true);
}
/**
* 回调验签
* @param array $map 验签参数
* @return stirng
*/
private function handler($map){
$rList = array();
array_push($rList, $this->token);
foreach($map as $k =>$v) {
if ( $k == "type" || $k=='msg_signature')
continue;
$value = trim(strval($v));
if ($value == "" || $value == "null")
continue;
array_push($rList, $value);
}
sort($rList,2);
return sha1(implode($rList));
}
/**
* 请求签名
* @param array $map 请求参数
* @return stirng
*/
private function sign($map) {
$rList = array();
foreach($map as $k =>$v) {
if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
continue;
$value = trim(strval($v));
$len = strlen($value);
if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
$value = substr($value,1, $len-1);
$value = trim($value);
if ($value == "" || $value == "null")
continue;
array_push($rList, $value);
}
array_push($rList, $this->salt);
sort($rList, 2);
return md5(implode('&', $rList));
}
/**
* 写日志
* @param string $path 日志路径
* @param string $content 内容
*/
private function log($path, $content){
$file=fopen($path, "a");
fwrite($file, date('Y-m-d H:i:s').'-----'.$content."\n");
fclose($file);
}
/**
* 网络请求
* @param stirng $method 请求模式
* @param stirng $url请求网关
* @param array $params 请求参数
* @param stirng $header 自定义头
* @param boolean $multi 文件上传
* @return array
*/
private function http( $method = 'GET', $url,$params,$header = array(), $multi = false){
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);
/* 根据请求类型设置特定参数 */
switch(strtoupper($method)){
case 'GET':
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
break;
case 'POST':
//判断是否传输文件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的请求方式!');
}
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) throw new Exception('请求发生错误:' . $error);
return $data;
}
}