public function savexsbilldd()
    {
        $data = request()->param();
        $p_id = $data['p_id'];
        $s_ids = $data['s_ids'];
        $shopid = $data['shop_id'];
        
        //遍历所有单据
        $s_count = count($s_ids);  //子单个数
        foreach ($s_ids  as $shop_order_id) {
          $resStr =  $this->getxsbillddFromDY($shop_order_id,$shopid);  
          $data = $this->jsonToArr($resStr)['data']['shop_order_detail'];
          //保存到数据库
          $create_time =$this->formatDateTime($data['create_time']); 
          $shop_name = $data['shop_name'];
          $order_id = $data['order_id'];

          $order_status = $data['order_status']; //订单状态1 待确认/待支付(订单创建完毕)105 已支付 2 备货中 101 部分发货 3 已发货(全部发货)4 已取消5 已完成(已收货)
          $order_status_desc = $data['order_status_desc'];  //订单状态描述
          $exp_ship_time = $this->formatDateTime($data['exp_ship_time']); //预计发货时间
          $ship_time = $this->formatDateTime($data['ship_time']); //发货时间

          $seller_words = $data['seller_words']; //商家备注
          $buyer_words = $data['buyer_words']; //买家留言

          $order_amount = $data['order_amount']; // 订单金额(分)
          $post_amount = $data['post_amount']; //快递费(分)
          $post_insurance_amount = $data['post_insurance_amount']; //运费险金额(分)
          $pay_amount = $data['pay_amount']; //支付金额(分)

          //收件人信息 密文
          $cipher_textArr = array();
          $cipher_textArr[count($cipher_textArr )] = $data['encrypt_post_tel'];
          $cipher_textArr[count($cipher_textArr )] = $data['encrypt_post_receiver'];
          $cipher_textArr[count($cipher_textArr )] = $data['post_addr']['encrypt_detail'];
          
          $decrypt_infos =$this->jsonToArr( $this->orderVatchDecrypt($cipher_textArr,$order_id,$shopid));
          foreach($cipher_textArr as $itemEW)
          {
              foreach($decrypt_infos['data']['decrypt_infos'] as $itemDW)
              {
                  if($itemEW == $data['encrypt_post_tel'] && $itemDW['cipher_text']==$itemEW )
                  {
                      $post_tel = $itemDW['decrypt_text'];
                  }
                  else if($itemEW  == $data['encrypt_post_receiver'] && $itemDW['cipher_text']==$itemEW )
                  {
                      $post_receiver = $itemDW['decrypt_text'];
                  }
                  else if($itemEW  == $data['post_addr']['encrypt_detail'] && $itemDW['cipher_text']==$itemEW )
                  {
                      $post_addr_detail = $itemDW['decrypt_text'];
                  }
              }
          }
          //地址处理
          $province = $data['post_addr']['province']['name']; //省/直辖市
          $city = $data['post_addr']['city']['name']; //市
          $town = $data['post_addr']['town']['name']; //区县
          $street = $data['post_addr']['street']['name']; //街道

          //产品信息
          $parent_order_id = $data['sku_order_list'][0]['parent_order_id']; 
          $order_id = $data['sku_order_list'][0]['order_id'];  
          $code = $data['sku_order_list'][0]['code']; //商家后台商品编码
          $origin_amount = $data['sku_order_list'][0]['origin_amount']; //商品现价
          $item_num = $data['sku_order_list'][0]['item_num'];    //订单商品数量
          $sum_amount = $data['sku_order_list'][0]['sum_amount'];  //商品现价*件数

          $res = [  'shop_name' => $shop_name, 'create_time' => $create_time,
                    'shop_id' => $shopid, 'parent_order_id' => $parent_order_id, 's_count' => $s_count, 
                    'order_status' => $order_status, 'order_status_desc' => $order_status_desc, 'exp_ship_time' => $exp_ship_time,
                    'ship_time' => $ship_time, 'seller_words' => $seller_words, 'buyer_words' => $buyer_words,
                    'order_amount' => $order_amount, 'pay_amount' => $pay_amount, 'post_amount' => $post_amount,
                    'post_insurance_amount' => $post_insurance_amount, 'post_tel' => $post_tel, 'post_receiver' => $post_receiver,
                    'post_addr_detail' => $post_addr_detail, 'province' => $province, 'city' => $city,
                    'town' => $town, 'street' => $street, 'order_id' => $order_id,
                    'code' => $code, 'origin_amount' => $origin_amount, 'item_num' => $item_num, 'sum_amount' => $sum_amount]; //字段名称区分大小写
          $b =  $this->saveData($res,'dyOrderDetail');
        }  
        if($b){
            return self::showResCode('操作成功',['res'=>'',"flag"=>true]); 
        }
        else
        {
            return self::showResCode('操作失败',['res'=>'',"flag"=>false]); 
        }
        
        
    }
private function saveData($res,$tableName):bool
    {
        Db::startTrans();
        try{
            Db::table($tableName)->insert($res);
            Db::commit();
            $this->log("数据保存成功");
            return true;
        }catch(Exception $e){
            Db::rollback();
            $this->log("数据保存失败".$e);
            printf("数据保存失败".$e);
            return false;
        }
        
    }
// 解密
    private function orderVatchDecrypt($cipher_textArr,$auth_id,$shopid):string{
        $accessToken = $this->getDYAccessToken($shopid);
        $method = 'order.batchDecrypt'; 
        $timestamp = time();
        $m = array(
            'cipher_infos' => array(
                0 => array(
                    'cipher_text' => $cipher_textArr[0],
                    'auth_id' => $auth_id
                ),
                1 => array(
                    'cipher_text' => $cipher_textArr[1],
                    'auth_id' => $auth_id
                ),
                2 => array(
                    'cipher_text' => $cipher_textArr[2],
                    'auth_id' => $auth_id
                )
            )
        );
 
        // 序列化参数
        $paramJson = $this->marshal($m);

        // 计算签名
        $signVal = $this->sign($this->appKey, $this->appSecret, $method, $timestamp, $paramJson);

        // 发起请求
        $res = $this->fetchdy($this->appKey, $this->host, $method, 
        $timestamp, $paramJson, $accessToken, $signVal);
        return $res;
    }
// 根据订单P_id 获取订单消息
    private function getxsbillddFromDY($shop_order_id,$shopid):string
    {
        $accessToken = $this->getDYAccessToken($shopid);
        $method = 'order.orderDetail';
        $timestamp = time();
        $m = array(
            'shop_order_id' => $shop_order_id
        );
        // 序列化参数
        $paramJson = $this->marshal($m);
        // 计算签名
        $signVal = $this->sign($this->appKey, $this->appSecret, $method, $timestamp, $paramJson);

        // 发起请求
        $res = $this->fetchdy($this->appKey, $this->host, $method, 
        $timestamp, $paramJson, $accessToken, $signVal);
        return $res;
    }

    private function jsonToArr($jsonstr)
    {
        return json_decode(json_encode(json_decode($jsonstr)),true);
    }

时间格式化

private function formatDateTime($datatime):string
    {
        return date('Y-m-d H:i:s',$datatime); 
    }

日记打印

private function log($content){
        $timeFile = date("Y-m-d");
        $fopen = fopen($timeFile,'a');
        fwrite($fopen,$content."\r\n");
        fclose($fopen);
    }

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐