php-远程推送数据给另一个端口-》base64加密进行

远程推送数据

1、 base64_encode加密进行推送数据

<?php
namespace app\util;

use app\index\model\AllocationGoods;
use app\index\model\Batch;
use app\index\model\Goods;
use app\index\model\Warehouse;
use think\Db;
use think\Exception;
use think\Request;

class ajy{
 protected $logisticsurl = 'http://distribution.shijizhongyun.com';

  public function pushAllocation($allocation,$nids,$warehouse){
        $logisticsurl = $this->logisticsurl.'/index/open/pushAllocation';
        $result = array();
        $data = array();
        $data['nbatch'] = array();
        foreach ($nids as $k => $v){
            $goods = Goods::get($v->goodsId);
            $data['nbatch'][$k]['batch'] = $v->batch;
            $data['nbatch'][$k]['warehouseCode'] = $warehouse->warehouseCode;
            $data['nbatch'][$k]['goodsCode'] = $goods->goodsCode;
            $data['nbatch'][$k]['expireDate'] = $v->expireDate;
            $data['nbatch'][$k]['type'] = 1;
            $data['nbatch'][$k]['amount'] = $v->amount;
            $data['nbatch'][$k]['bSurStock'] = $v->amount;
            $data['nbatch'][$k]['isFirst'] = $v->isFirst;
            $data['nbatch'][$k]['remark'] = $v->remark;
        }
        $allocationBatch = AllocationGoods::alias('ag')->join('goods g','ag.goodsId=g.id','left')->field('ag.aqty,g.goodsCode')->where(array('ag.allocationId'=>$allocation->allocationId))->select();
        $outWare = Warehouse::get(array('id'=>$allocation->wareOutId));
        foreach ($allocationBatch as $key => $val){
            $data['abatch'][$key]['qty'] = $val->aqty;
            $data['abatch'][$key]['warehouseCode'] = $outWare->warehouseCode;
            $data['abatch'][$key]['goodsCode'] = $val->goodsCode;
        }
        $result['data']=base64_encode(json_encode($data));
        //远程获取
        $oCurl = curl_init();
        curl_setopt($oCurl, CURLOPT_POST, 1);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($oCurl, CURLOPT_URL,$logisticsurl);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $result);
        curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($oCurl, CURLOPT_TIMEOUT,70);

        $sret=curl_exec($oCurl);
        $sContent = json_decode($sret,true);
        curl_close($oCurl);
        Db::startTrans();
        try {
            if(!$sContent){
                $str='调拨'.$allocation->allocationCode.':'.'接口返回失败'.'!'."\r\n";
                setLog($str,'pushAllocation_err','ajy');
            }
            if(isset($sContent['retCode']) && $sContent['retCode'] != 200){
                $str='调拨'.$allocation->allocationCode.':'.'接口返回:'.$sContent['retMessage'].'!'."\r\n";
                setLog($str,'pushAllocation_err','ajy');
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $str='调拨'.$allocation->allocationCode.':'.$e->getLine().$e->getMessage().'!'."\r\n";
            setLog($str,'pushAllocation_err','ajy');
        }
    }
}

2、 json_decode解密进行获取数据

    public function pushAllocation(){
        $data=request()->post('data/s')?request()->post('data/s'):'';
        if(!$data){
            $ret['retCode']=400;
            $ret['retMessage']='没有接收到调拨数据!';
            echo json_encode($ret);
            exit();
        }

        $dataArr=json_decode(base64_decode($data),true);
        setlog(date('Y-m-d H:i:s')."\r\n".base64_decode($data),'push_batch');

        try {
            $batchArr = array();
            foreach ($dataArr['nbatch'] as $key => $val){
                $warehouse = \app\index\model\Warehouse::where(array('warehouseCode'=>$val['warehouseCode']))->find();
                if(!$warehouse){
                    $result['retCode'] = 400;
                    $result['retMessage'] = '仓库编码'.$val['warehouseCode'].'不存在!';
                    echo json_encode($result);
                    exit();;
                }
                $goods = \app\index\model\Goods::where(array('goodsCode'=>$val['goodsCode']))->find();
                if(!$goods){
                    $result['retCode'] = 400;
                    $result['retMessage'] = '商品编码'.$val['goodsCode'].'不存在!';
                    echo json_encode($result);
                    exit();;
                }
                $batchArr[$key]['batch'] = $val['batch'];
                $batchArr[$key]['wareId'] = $warehouse->id;
                $batchArr[$key]['goodsId'] = $goods->id;
                $batchArr[$key]['expireDate'] = $val['expireDate'];
                $batchArr[$key]['type'] = $val['type'];
                $batchArr[$key]['amount'] = $val['amount'];
                $batchArr[$key]['bSurStock'] = $val['bSurStock'];
                $batchArr[$key]['isFirst'] = $val['isFirst'];
                $batchArr[$key]['remark'] = $val['remark'];
            }
            Db::startTrans();
            try {
                $nbatch = new Batch();
                $nbatch->saveAll($batchArr);
                foreach ($dataArr['abatch'] as $k => $v){
                    $warehouse = \app\index\model\Warehouse::where(array('warehouseCode'=>$v['warehouseCode']))->find();
                    if(!$warehouse){
                        $result['retCode'] = 400;
                        $result['retMessage'] = '扣除仓库编码'.$v['warehouseCode'].'不存在!';
                        echo json_encode($result);
                        exit();
                    }
                    $goods = \app\index\model\Goods::where(array('goodsCode'=>$v['goodsCode']))->find();
                    if(!$goods){
                        $result['retCode'] = 400;
                        $result['retMessage'] = '扣除商品编码'.$v['goodsCode'].'不存在!';
                        echo json_encode($result);
                        exit();
                    }
                    $bwhere = array();
                    $bwhere[] = ['wareId','=',$warehouse->id];
                    $bwhere[] = ['goodsId','=',$goods->id];
                    $bwhere[] = ['bSurStock','>',0];
                    $bwhere[] = ['status','=',1];
                    $batchCount = Batch::where($bwhere)->order('isFirst desc,expireDate asc')->sum('bSurStock');
                    $batch = Batch::where($bwhere)->order('isFirst desc,expireDate asc')->select();
                    if($batchCount < $v['qty']){
                        $result['retCode'] = 400;
                        $result['retMessage'] = '该订单下商品:'.$v['goodsCode'].'.'.$goods->goodsName.',找不到可用商品数量!';
                        echo json_encode($result);
                        exit();
                    }
                    $cqty = 0;
                    foreach ($batch as $ba){
                        if($v['qty'] == 0){
                            break;
                        }
                        if($ba->bSurStock < $v['qty']){
                            $cqty = $ba->bSurStock;
                            $v['qty'] -= $ba->bSurStock;
                            $ba->amount -= $ba->bSurStock;
                            $ba->bSurStock -= $ba->bSurStock;
                        }else{
                            $cqty = $v['qty'];
                            $ba->bSurStock -= $v['qty'];
                            $ba->amount -= $v['qty'];
                            $v['qty'] -= $v['qty'];
                        }
                        if($ba->bSurStock < 0){
                            $result['retCode'] = 400;
                            $result['retMessage'] = '批次编码'.$v['batch'].'库存不足!';
                            echo json_encode($result);
                            exit();
                        }
                        Db::table('db_batch')->where(array('batchId'=>$ba->batchId))->update(array('bSurStock'=>$ba->bSurStock,'amount'=>$ba->amount));

                        $actionArr['tableName'] = 'db_batch';
                        $actionArr['tableId'] = $ba->batchId;
                        $actionArr['adminId'] = '0';
                        $actionArr['remark'] = 'erp调拨批次:' . $ba->batch.',扣除商品数量:'.$cqty;
                        $action = new Action();
                        $action->save($actionArr);
                    }

                }
                Db::commit();
                $result['retCode'] = 200;
                $result['retMessage'] = '成功';
                echo json_encode($result);
                exit();
            } catch (Exception $e) {
                Db::rollback();
                $result['retCode'] = 400;
                $result['retMessage'] =$e->getLine().'.'.$e->getMessage();
                echo json_encode($result);
                exit();
            }


        }catch (Exception $e){
            $result['retCode'] = 400;
            $result['retMessage'] =$e->getLine().'.'.$e->getMessage();
            echo json_encode($result);
            exit();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值