SyncMjWarehouse(); // $this->diffRealPriceSkuListArr = array( // 0=>array('code'=>'banjiexia-03','oldPrice'=>'10','newPrice'=>'20'), // 1=>array('code'=>'csxpxx-01','oldPrice'=>'100','newPrice'=>'50'), // ); if (count($this->diffRealPriceSkuListArr) > 0) { $this->skuRealPriceMonitor(); } }catch (\Exception $exception){ echo 'line:'.$exception->getLine().' message:'.$exception->getMessage(); } } public function SyncMjWarehouse(){ $params = array(); $params['eshopCode'] = config('constants.ESHOP_CODE'); $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码 $params['pageIndex'] = '1'; $params['pageSize'] = $this->limit; $mjRes = OrderScript::mjWarehouseSkuGet($params); if($mjRes == false){ exit('获取卖家云数据出错'); } //插入第一页: $this->insertData( $mjRes['resultSet']['skuList'] ); $count = $mjRes['resultSet']['totalNum']; $pages = ceil($count / $this->limit); //插入其他页面 for($i=2; $i<=$pages; $i++){ $params['pageIndex'] = $i; $mjRes = OrderScript::mjWarehouseSkuGet($params); $this->insertData( $mjRes['resultSet']['skuList'] ); } } /** * 获取订单信息,可用来判断订单是否存在,同步订单状态 */ public function insertData($data){ foreach($data as $k=>$sku){ $code = $sku['skuProductCode']; $up = array(); //1.更新商品库存数量(有可能为负数,有可能为0) //2.更新商品库存总成本(有可能为空) //3.计算商品真实价格 //4.计算商品外部价格 $cost = DB::table('goods_skus')->where('code', $code)->select('code', 'referenceCost', 'is_weigh', 'outPrice' ,'realPrice')->first(); //获取规格成本/是否称重 if($cost){ $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量 //实际库存=库存-冻结 if(isset($sku['fi']) && ($sku['fi'] < 0)){ $real_quantity = $sku['quantity']; } else { $real_quantity = $sku['quantity'] - $sku['fi']; } $real_quantity = $cost->is_weigh == 1 ? $real_quantity/2 : $real_quantity; //对应规格数量 $up['quantity'] = $real_quantity; $up['totalCost'] = isset($sku['totalCost']) ? $sku['totalCost'] : 0; if($sku['quantity'] != 0 ){ if(isset($sku['totalCost'])){ $up['realPrice'] = $sku['totalCost'] / $quantity; #监控realPrice变化是否超过10% $newRealPrice = $up['realPrice']; $oldRealPrice = $cost->realPrice; if($oldRealPrice != 0){ $result = $this->getNumberDiffAbs($newRealPrice,$oldRealPrice); if($result >= 0.1){ $json['code'] = $code; $json['oldPrice'] = $oldRealPrice; $json['newPrice'] = $newRealPrice; $this->diffRealPriceSkuListArr[] = $json; } } } } // if($cost->outPrice == 0){ // if(isset($up['realPrice']) && $up['realPrice'] != 0){ // $up['outPrice'] = ceil($up['realPrice']*1.15); // } // } $up['cost'] = isset($sku['cost']) ? $sku['cost'] : 0; $up_re = DB::table('goods_skus')->where('code', $code)->update($up); } } return true; } /** * 发送短信验证码通知 */ public function skuRealPriceMonitor(){ $text = json_encode($this->diffRealPriceSkuListArr); #发送验证码报警 $this->sendMsg('13161864516',$text); $this->sendMsg('18410900527',$text); // error_log(date('Y-m-d H:i:s',time()).' skuList:'.$text." \n ",3,"/log/seafood_log/script_success/".date('Y-m-d',time()).'MonitorRealPrice.log'); } /** * 计算两个数之间差值的绝对值 * @param $newNumber * @param $oldNumber * @return float|int */ public function getNumberDiffAbs($newNumber,$oldNumber){ $diff = abs(($newNumber-$oldNumber)); return $diff/$oldNumber; } private function init(){ $ch = curl_init(); /* 设置验证方式 */ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8')); /* 设置返回结果为流 */ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); /* 设置超时时间*/ curl_setopt($ch, CURLOPT_TIMEOUT, 10); /* 设置通信方式 */ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); return $ch; } public function sendMsg($phone,$params){ $tpl_id = $this->YP_TPL_ID; $ch=$this->init(); //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone); $data = [ 'apikey' => YP_SMS_KEY, 'mobile' => $phone, 'tpl_id' => $tpl_id, 'tpl_value' => ('#sku_id#').'='.$params, ]; $json_data = $this->tpl_send($ch,$data); //print_r($json_data); ******************************maybe影响验证码发出 $array = json_decode($json_data,true); // echo '
';print_r($array);
        curl_close($ch);
        return $array;
    }

    private function tpl_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        $this->checkErr($result,$error);
        return $result;
    }

    private static function checkErr($result,$error) {
        if($result === false)
        {
            echo 'Curl error: ' . $error;
        }
//        else
//        {
//            echo '操作完成没有任何错误';
//        }
    }

}