From f10a8ca13b92a103416c317a40f5f6c8122c8b85 Mon Sep 17 00:00:00 2001 From: xu Date: Tue, 29 Oct 2019 20:14:17 +0800 Subject: [PATCH] 1. F API 授权接口调整 --- app-api/controllers/AuthDeviceController.php | 6 +++++- app-api/controllers/BaseController.php | 43 +------------------------------------------ common/exts/Http.php | 263 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console/controllers/TestController.php | 83 +++++++++++++++++++++++++++-------------------------------------------------------- domain/device/Device.php | 25 ++++++++++++++++++++----- domain/device/DeviceAuthFail.php | 24 +++++++++++++++++++++--- 6 files changed, 337 insertions(+), 107 deletions(-) create mode 100644 common/exts/Http.php diff --git a/app-api/controllers/AuthDeviceController.php b/app-api/controllers/AuthDeviceController.php index 583b8e1..881e864 100644 --- a/app-api/controllers/AuthDeviceController.php +++ b/app-api/controllers/AuthDeviceController.php @@ -49,7 +49,7 @@ class AuthDeviceController extends BaseController $timestamp = $req->post('timestamp'); $e = new stdClass(); $e->status = 0; - $e->message = '授权失败'; + $e->message = 'message'; $e->serial_no = '';; $e->mac = ''; @@ -64,6 +64,7 @@ class AuthDeviceController extends BaseController if ($deviceModel && DeviceStatus::HAS_AUTH == $deviceModel->status) { $e->mac = $deviceModel->mac; $e->serial_no = $deviceModel->serial_no; + $e->message = 'ok'; $e->status = 1; return $e; } @@ -72,7 +73,10 @@ class AuthDeviceController extends BaseController if ($authResult->success) { $e->mac = $authResult->mac; $e->serial_no = $authResult->serial_no; + $e->message = $authResult->message; $e->status = 1; + } else { + $e->message = $authResult->message; } return $e; diff --git a/app-api/controllers/BaseController.php b/app-api/controllers/BaseController.php index 1228006..1dcf9c0 100644 --- a/app-api/controllers/BaseController.php +++ b/app-api/controllers/BaseController.php @@ -21,13 +21,7 @@ class BaseController extends RestController /** @var \yii\web\response **/ public $response; /** @var \yii\web\User **/ - public $user; - /** @var **/ - protected $mina; - /** @var **/ - protected $site; - private $userModel; /** @inheritdoc **/ public function behaviors() @@ -43,45 +37,10 @@ class BaseController extends RestController 'class' => VerbFilter::className(), 'actions' => $this->verbs(), ], - 'rateLimiter' => [ - 'class' => RateLimiter::className(), - ], - ]; - } - - /** - * 所有小程序端的API请求均需要调用handleMallAccessCtrl - * @param \yii\base\Action $action - * @return bool - * @throws \yii\web\BadRequestHttpException - */ - public function beforeAction($action) - { - if (!parent::beforeAction($action)) { - return false; - } - return true; - } - /** - * 获取ClientUserId - * @return int - */ - public function getClientUserId() - { - return Yii::$app->user->id; + ]; } - /** - * @throws Exception - */ - public function init() - { - parent::init(); - $this->request = Yii::$app->getRequest(); - $this->response = Yii::$app->getResponse(); - $this->user = Yii::$app->getUser(); - } } diff --git a/common/exts/Http.php b/common/exts/Http.php new file mode 100644 index 0000000..ba03a6b --- /dev/null +++ b/common/exts/Http.php @@ -0,0 +1,263 @@ +errcode) && $r->errcode >= 40001) { + //Log::init(); + AppLog::DEBUG($result); + } + + if (intval($status["http_code"]) == 200) { + return $result; + } else { + return false; + } + } + + /** + * POST 请求 + * @param string $url + * @param array $param + * @return string content + */ + public static function post($url, $param, $encode = true, $header = array()) + { + $curl = curl_init(); + + if (stripos($url,"https://")!==FALSE) { + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 + } + + if (is_string($param)) { + $strPOST = $param; + } else { + $aPOST = array(); + foreach($param as $key=>$val) { + if ($encode == false) { + $aPOST[] = $key."=".$val; + } else { + $aPOST[] = $key."=".urlencode($val); + } + + } + $strPOST = join("&", $aPOST); + } + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_POST, true); + if (!empty($header)) { + curl_setopt($curl, CURLOPT_HTTPHEADER, $header); + } + self::$postStr = $strPOST; + curl_setopt($curl, CURLOPT_POSTFIELDS, $strPOST); + + $result = curl_exec($curl); + $status = curl_getinfo($curl); + curl_close($curl); + + if (intval($status["http_code"]) == 200) { + return $result; + } else { + return false; + } + } + + /** + * 上传文件 + * @param $url + * @param $filedata + */ + public static function upload($url, $filedata) + { + $curl = curl_init (); + if (class_exists ( '\CURLFile' )) {//php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同 + curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, true ); + } else { + if (defined ( 'CURLOPT_SAFE_UPLOAD' )) { + curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false ); + } + } + curl_setopt ( $curl, CURLOPT_URL, $url ); + curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); + curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); + if (! empty ( $filedata )) { + curl_setopt ( $curl, CURLOPT_POST, 1 ); + curl_setopt ( $curl, CURLOPT_POSTFIELDS, $filedata ); + } + curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); + $output = curl_exec ( $curl ); + curl_close ( $curl ); + return $output; + } + + /** + * 下载网络图片并缓存到指定本地路径 + * @param $url 网络图片地址 + * @param $cachePath 本地缓存路径 + */ + public static function download($url, $cachePath) + { + $curl = curl_init(); + $fp = fopen($cachePath, 'wb'); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_FILE, $fp); + curl_setopt($curl, CURLOPT_HEADER, 0); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); + //curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来 + curl_setopt($curl,CURLOPT_TIMEOUT, 60); + curl_exec($curl); + curl_close($curl); + fclose($fp); + } + + /** + * @param $url + * @return array + */ + public static function getImageStream($url) + { + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_HEADER, 0); + curl_setopt($curl, CURLOPT_NOBODY, 0); //只取body头 + if(stripos($url,"https://") !== FALSE){ + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); + } + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 ); + + $result = curl_exec($curl); + $status = curl_getinfo($curl); + + curl_close($curl); + + $imageAll = array_merge(array('header' => $status ), array('body' => $result )); + return $imageAll; + } + + /** + * GET 请求 + * @param string $url + */ + public static function http_get($url) + { + $oCurl = curl_init(); + if(stripos($url,"https://")!==FALSE){ + curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 + } + curl_setopt($oCurl, CURLOPT_URL, $url); + curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); + $sContent = curl_exec($oCurl); + $aStatus = curl_getinfo($oCurl); + curl_close($oCurl); + if(intval($aStatus["http_code"])==200){ + return $sContent; + }else{ + return false; + } + } + + // xml to array + public static function xmlToArr($xml) + { + libxml_disable_entity_loader(true); + $xmlString = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); + $val = json_decode(json_encode($xmlString),true); + return $val; + } + + /** + * POST 请求 + * @param string $url + * @param array $param + * @return string content + */ + public static function post_material($url, $param,$encode=true,$header=array()) + { + $curl = curl_init(); + + if(stripos($url,"https://")!==FALSE){ + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 + } + + if (is_string($param)) { + $strPOST = $param; + } else { + $aPOST = array(); + foreach($param as $key=>$val){ + if(strpos($val, '@') === 0) + { + $filename = ltrim($val, '@'); + $param[$key] = new \CURLFile($filename); + } + if($encode==false){ + $aPOST[] = $key."=".$val; + }else{ + $aPOST[] = $key."=".urlencode($val); + } + + } + $strPOST = join("&", $aPOST); + } + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt($curl, CURLOPT_POST,true); + if(!empty($header)){ + curl_setopt($curl, CURLOPT_HTTPHEADER,$header); + } + self::$postStr = $strPOST; + curl_setopt($curl, CURLOPT_POSTFIELDS, $param); + + $result = curl_exec($curl); + $status = curl_getinfo($curl); + curl_close($curl); + + if(intval($status["http_code"]) == 200){ + return $result; + }else{ + return false; + } + } + +} \ No newline at end of file diff --git a/console/controllers/TestController.php b/console/controllers/TestController.php index b48f65c..bc5d48d 100644 --- a/console/controllers/TestController.php +++ b/console/controllers/TestController.php @@ -8,64 +8,10 @@ namespace console\controllers; */ - -use app\wx\modules\user\helpers\UserOrderHelper; -use common\business\AdminCloseOrder; -use common\business\CancelOrder; -use common\business\Deduction; - -use common\exts\Invoice\baiwang\InvoiceApi; -use common\exts\privatenumber\PrivateNumber; -use common\exts\wechat\Http; - -use common\helpers\DeviceSearch; -use common\helpers\Distance; -use common\helpers\FileManager; - -use common\helpers\ImageManager; -use common\helpers\MinaHelper; -use common\helpers\OrderSqlHelper; -use common\helpers\UserMPHelper; -use common\helpers\WxHelper; -use common\models\BindDeviceApply; -use common\models\Engineer; -use common\models\EngineerEvent; -use common\models\EngineerPickOrder; -use common\models\Invoice; -use common\models\PrivateNumberBindRecord; -use common\models\PrivateNumberVoice; -use common\models\RepairOrder; - -use common\models\RepairOrderDeduction; - - -use common\services\wxs\SmsMessage; -use console\models\ClearFileTask; -use console\models\EngineerTask; - -use domain\data\EngineerReport; -use domain\device\BindQrcodeApplyRepository; -use domain\engineer\EngineerAchievementPro; -use domain\engineer\EngineerRepository; -use domain\finance\EngineerCashFlow; -use domain\finance\EngineerCashFlowStatus; -use domain\finance\EngineerCashFlowType; -use domain\marketing\activity\DeductionActivityRepository; - -use domain\marketing\subsidy\ApplyAwardRule; -use domain\marketing\thankfee\ThankFee; -use domain\system\message\CustomMessage; -use domain\trade\dob\DispatchOrder; - -use domain\trade\OnsiteOrder; -use domain\trade\privateorder\PrivateOrderUserOp; -use domain\trade\RepairOrderRepository; +use common\exts\Http; use GuzzleHttp\Psr7; -use Workerman\Worker; use yii\console\Controller; -use yii\db\Query; -use yii\base\Exception; - +use GuzzleHttp\Psr7\Request; use function chr; use yii\helpers\ArrayHelper; @@ -77,6 +23,31 @@ class TestController extends Controller echo sprintf('%04x', 1); } + public function actionDeviceAuth() + { + $url = 'http://kingb:8012/app-api/web/authDevice'; + $manufactureNo = '0002'; + $device_id = 'SZDEVICE000036'; + $projectNo = '002'; + $modelNo = '002'; + $productionNo = '0001'; + + $timestamp = time(); + $salt = 13456; + $sign = md5($manufactureNo. $projectNo. $modelNo . $productionNo . $timestamp . $salt); + $params = [ + 'manufacture' => $manufactureNo, + 'device_id' => $device_id, + 'project' => $projectNo, + 'model' => $modelNo, + 'production' => $productionNo, + 'timestamp' => $timestamp, + 'sign' => $sign, + ]; + $postResult = Http::POST($url, $params); + echo $postResult; + + } } diff --git a/domain/device/Device.php b/domain/device/Device.php index 7dd31b3..a6413b2 100644 --- a/domain/device/Device.php +++ b/domain/device/Device.php @@ -38,18 +38,33 @@ class Device $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]); if (empty($batchModel)) { $e->message = '没有该批次'; + return $e; } - $count = DeviceRepository::rowsCount(['batch_id' => $batchModel->batch_id, 'is_delete' => 0]); - if ($count > $batchModel->num) { + $batchId = $batchModel->id; + $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'is_delete' => 0]); + + if (($count *1) >= ($batchModel->num * 1)) { // 超过了限制数,记录到另外一个表里面 // to do 记录到表里面 - + $failRecord = DeviceAuthFailRepository::findOne(['device_id' => $deviceId]); + if (!$failRecord) { + $item = [ + 'manufacture_no' => $manufactureNo, + 'project_no' => $projectNo, + 'model_no' => $modelNo, + 'production_no' => $productionNo, + 'device_id' => $deviceId, + ]; + DeviceAuthFail::create($item); + } $e->message = '授权失败'; return $e; } - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]); + + + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]); if (empty($deviceModel)) { - $newDeviceModel = DeviceRepository::findOne(['device_id'=> null, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); + $newDeviceModel = DeviceRepository::findOne(['device_id'=> null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); $newDeviceModel->device_id = $deviceId; $newDeviceModel->status = DeviceStatus::HAS_AUTH; $newDeviceModel->apply_at = $tt ; diff --git a/domain/device/DeviceAuthFail.php b/domain/device/DeviceAuthFail.php index e9695ee..0e4346d 100644 --- a/domain/device/DeviceAuthFail.php +++ b/domain/device/DeviceAuthFail.php @@ -2,13 +2,31 @@ namespace domain\device; -use yii\behaviors\TimestampBehavior; -use yii\db\ActiveRecord; +use Yii; +use domain\device\models\DeviceAuthFail as DeviceAuthFailModel; class DeviceAuthFail { + /** + * @param $item + * @return null|object + * @throws \yii\base\InvalidConfigException + */ static function create($item) { - + $failModel = Yii::createObject([ + 'class' => DeviceAuthFailModel::className(), + 'manufacture_no' => $item['manufacture_no'], + 'project_no' => $item['project_no'], + 'model_no' => $item['model_no'], + 'production_no' => $item['production_no'], + 'device_id' => $item['device_id'], + 'apply_at' => time(), + ]); + if ($failModel->save()) { + return $failModel; + } else { + return null; + } } } \ No newline at end of file -- libgit2 0.21.0