Commit f10a8ca13b92a103416c317a40f5f6c8122c8b85

Authored by xu
1 parent 931eed21
Exists in master

1. F API 授权接口调整

app-api/controllers/AuthDeviceController.php
@@ -49,7 +49,7 @@ class AuthDeviceController extends BaseController @@ -49,7 +49,7 @@ class AuthDeviceController extends BaseController
49 $timestamp = $req->post('timestamp'); 49 $timestamp = $req->post('timestamp');
50 $e = new stdClass(); 50 $e = new stdClass();
51 $e->status = 0; 51 $e->status = 0;
52 - $e->message = '授权失败'; 52 + $e->message = 'message';
53 $e->serial_no = '';; 53 $e->serial_no = '';;
54 $e->mac = ''; 54 $e->mac = '';
55 55
@@ -64,6 +64,7 @@ class AuthDeviceController extends BaseController @@ -64,6 +64,7 @@ class AuthDeviceController extends BaseController
64 if ($deviceModel && DeviceStatus::HAS_AUTH == $deviceModel->status) { 64 if ($deviceModel && DeviceStatus::HAS_AUTH == $deviceModel->status) {
65 $e->mac = $deviceModel->mac; 65 $e->mac = $deviceModel->mac;
66 $e->serial_no = $deviceModel->serial_no; 66 $e->serial_no = $deviceModel->serial_no;
  67 + $e->message = 'ok';
67 $e->status = 1; 68 $e->status = 1;
68 return $e; 69 return $e;
69 } 70 }
@@ -72,7 +73,10 @@ class AuthDeviceController extends BaseController @@ -72,7 +73,10 @@ class AuthDeviceController extends BaseController
72 if ($authResult->success) { 73 if ($authResult->success) {
73 $e->mac = $authResult->mac; 74 $e->mac = $authResult->mac;
74 $e->serial_no = $authResult->serial_no; 75 $e->serial_no = $authResult->serial_no;
  76 + $e->message = $authResult->message;
75 $e->status = 1; 77 $e->status = 1;
  78 + } else {
  79 + $e->message = $authResult->message;
76 } 80 }
77 81
78 return $e; 82 return $e;
app-api/controllers/BaseController.php
@@ -21,13 +21,7 @@ class BaseController extends RestController @@ -21,13 +21,7 @@ class BaseController extends RestController
21 /** @var \yii\web\response **/ 21 /** @var \yii\web\response **/
22 public $response; 22 public $response;
23 /** @var \yii\web\User **/ 23 /** @var \yii\web\User **/
24 - public $user;  
25 - /** @var **/  
26 - protected $mina;  
27 - /** @var **/  
28 - protected $site;  
29 24
30 - private $userModel;  
31 25
32 /** @inheritdoc **/ 26 /** @inheritdoc **/
33 public function behaviors() 27 public function behaviors()
@@ -43,45 +37,10 @@ class BaseController extends RestController @@ -43,45 +37,10 @@ class BaseController extends RestController
43 'class' => VerbFilter::className(), 37 'class' => VerbFilter::className(),
44 'actions' => $this->verbs(), 38 'actions' => $this->verbs(),
45 ], 39 ],
46 - 'rateLimiter' => [  
47 - 'class' => RateLimiter::className(),  
48 - ],  
49 - ];  
50 - }  
51 -  
52 - /**  
53 - * 所有小程序端的API请求均需要调用handleMallAccessCtrl  
54 - * @param \yii\base\Action $action  
55 - * @return bool  
56 - * @throws \yii\web\BadRequestHttpException  
57 - */  
58 - public function beforeAction($action)  
59 - {  
60 - if (!parent::beforeAction($action)) {  
61 - return false;  
62 - }  
63 - return true;  
64 - }  
65 40
66 - /**  
67 - * 获取ClientUserId  
68 - * @return int  
69 - */  
70 - public function getClientUserId()  
71 - {  
72 - return Yii::$app->user->id; 41 + ];
73 } 42 }
74 43
75 44
76 - /**  
77 - * @throws Exception  
78 - */  
79 - public function init()  
80 - {  
81 - parent::init();  
82 - $this->request = Yii::$app->getRequest();  
83 - $this->response = Yii::$app->getResponse();  
84 - $this->user = Yii::$app->getUser();  
85 45
86 - }  
87 } 46 }
common/exts/Http.php 0 → 100644
@@ -0,0 +1,263 @@ @@ -0,0 +1,263 @@
  1 +<?php
  2 +namespace common\exts;
  3 +use common\helpers\Log as AppLog;
  4 +
  5 +/**
  6 + * Class Http
  7 + * @package common\exts\wechat
  8 + */
  9 +class Http
  10 +{
  11 + /**
  12 + * GET 请求
  13 + * @param string $url
  14 + * @return bool|mixed
  15 + */
  16 + public static $postStr = '';
  17 + public static function get($url, $header = array())
  18 + {
  19 + $curl = curl_init();
  20 +
  21 + if (stripos($url,"https://") !== FALSE) {
  22 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  23 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  24 + }
  25 + if (!empty($header)) {
  26 + curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  27 + }
  28 + curl_setopt($curl, CURLOPT_URL, $url);
  29 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  30 +
  31 + $result = curl_exec($curl);
  32 + $status = curl_getinfo($curl);
  33 +
  34 + curl_close($curl);
  35 +
  36 + $r = json_decode($result);
  37 + if (isset($r->errcode) && $r->errcode >= 40001) {
  38 + //Log::init();
  39 + AppLog::DEBUG($result);
  40 + }
  41 +
  42 + if (intval($status["http_code"]) == 200) {
  43 + return $result;
  44 + } else {
  45 + return false;
  46 + }
  47 + }
  48 +
  49 + /**
  50 + * POST 请求
  51 + * @param string $url
  52 + * @param array $param
  53 + * @return string content
  54 + */
  55 + public static function post($url, $param, $encode = true, $header = array())
  56 + {
  57 + $curl = curl_init();
  58 +
  59 + if (stripos($url,"https://")!==FALSE) {
  60 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  61 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  62 + curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  63 + }
  64 +
  65 + if (is_string($param)) {
  66 + $strPOST = $param;
  67 + } else {
  68 + $aPOST = array();
  69 + foreach($param as $key=>$val) {
  70 + if ($encode == false) {
  71 + $aPOST[] = $key."=".$val;
  72 + } else {
  73 + $aPOST[] = $key."=".urlencode($val);
  74 + }
  75 +
  76 + }
  77 + $strPOST = join("&", $aPOST);
  78 + }
  79 +
  80 + curl_setopt($curl, CURLOPT_URL, $url);
  81 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  82 + curl_setopt($curl, CURLOPT_POST, true);
  83 + if (!empty($header)) {
  84 + curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  85 + }
  86 + self::$postStr = $strPOST;
  87 + curl_setopt($curl, CURLOPT_POSTFIELDS, $strPOST);
  88 +
  89 + $result = curl_exec($curl);
  90 + $status = curl_getinfo($curl);
  91 + curl_close($curl);
  92 +
  93 + if (intval($status["http_code"]) == 200) {
  94 + return $result;
  95 + } else {
  96 + return false;
  97 + }
  98 + }
  99 +
  100 + /**
  101 + * 上传文件
  102 + * @param $url
  103 + * @param $filedata
  104 + */
  105 + public static function upload($url, $filedata)
  106 + {
  107 + $curl = curl_init ();
  108 + if (class_exists ( '\CURLFile' )) {//php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同
  109 + curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, true );
  110 + } else {
  111 + if (defined ( 'CURLOPT_SAFE_UPLOAD' )) {
  112 + curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false );
  113 + }
  114 + }
  115 + curl_setopt ( $curl, CURLOPT_URL, $url );
  116 + curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
  117 + curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
  118 + if (! empty ( $filedata )) {
  119 + curl_setopt ( $curl, CURLOPT_POST, 1 );
  120 + curl_setopt ( $curl, CURLOPT_POSTFIELDS, $filedata );
  121 + }
  122 + curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  123 + $output = curl_exec ( $curl );
  124 + curl_close ( $curl );
  125 + return $output;
  126 + }
  127 +
  128 + /**
  129 + * 下载网络图片并缓存到指定本地路径
  130 + * @param $url 网络图片地址
  131 + * @param $cachePath 本地缓存路径
  132 + */
  133 + public static function download($url, $cachePath)
  134 + {
  135 + $curl = curl_init();
  136 + $fp = fopen($cachePath, 'wb');
  137 + curl_setopt($curl, CURLOPT_URL, $url);
  138 + curl_setopt($curl, CURLOPT_FILE, $fp);
  139 + curl_setopt($curl, CURLOPT_HEADER, 0);
  140 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  141 + //curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
  142 + curl_setopt($curl,CURLOPT_TIMEOUT, 60);
  143 + curl_exec($curl);
  144 + curl_close($curl);
  145 + fclose($fp);
  146 + }
  147 +
  148 + /**
  149 + * @param $url
  150 + * @return array
  151 + */
  152 + public static function getImageStream($url)
  153 + {
  154 + $curl = curl_init();
  155 +
  156 + curl_setopt($curl, CURLOPT_HEADER, 0);
  157 + curl_setopt($curl, CURLOPT_NOBODY, 0); //只取body头
  158 + if(stripos($url,"https://") !== FALSE){
  159 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  160 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  161 + }
  162 +
  163 + curl_setopt($curl, CURLOPT_URL, $url);
  164 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
  165 +
  166 + $result = curl_exec($curl);
  167 + $status = curl_getinfo($curl);
  168 +
  169 + curl_close($curl);
  170 +
  171 + $imageAll = array_merge(array('header' => $status ), array('body' => $result ));
  172 + return $imageAll;
  173 + }
  174 +
  175 + /**
  176 + * GET 请求
  177 + * @param string $url
  178 + */
  179 + public static function http_get($url)
  180 + {
  181 + $oCurl = curl_init();
  182 + if(stripos($url,"https://")!==FALSE){
  183 + curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  184 + curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  185 + curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  186 + }
  187 + curl_setopt($oCurl, CURLOPT_URL, $url);
  188 + curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  189 + $sContent = curl_exec($oCurl);
  190 + $aStatus = curl_getinfo($oCurl);
  191 + curl_close($oCurl);
  192 + if(intval($aStatus["http_code"])==200){
  193 + return $sContent;
  194 + }else{
  195 + return false;
  196 + }
  197 + }
  198 +
  199 + // xml to array
  200 + public static function xmlToArr($xml)
  201 + {
  202 + libxml_disable_entity_loader(true);
  203 + $xmlString = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  204 + $val = json_decode(json_encode($xmlString),true);
  205 + return $val;
  206 + }
  207 +
  208 + /**
  209 + * POST 请求
  210 + * @param string $url
  211 + * @param array $param
  212 + * @return string content
  213 + */
  214 + public static function post_material($url, $param,$encode=true,$header=array())
  215 + {
  216 + $curl = curl_init();
  217 +
  218 + if(stripos($url,"https://")!==FALSE){
  219 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  220 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  221 + curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  222 + }
  223 +
  224 + if (is_string($param)) {
  225 + $strPOST = $param;
  226 + } else {
  227 + $aPOST = array();
  228 + foreach($param as $key=>$val){
  229 + if(strpos($val, '@') === 0)
  230 + {
  231 + $filename = ltrim($val, '@');
  232 + $param[$key] = new \CURLFile($filename);
  233 + }
  234 + if($encode==false){
  235 + $aPOST[] = $key."=".$val;
  236 + }else{
  237 + $aPOST[] = $key."=".urlencode($val);
  238 + }
  239 +
  240 + }
  241 + $strPOST = join("&", $aPOST);
  242 + }
  243 + curl_setopt($curl, CURLOPT_URL, $url);
  244 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
  245 + curl_setopt($curl, CURLOPT_POST,true);
  246 + if(!empty($header)){
  247 + curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
  248 + }
  249 + self::$postStr = $strPOST;
  250 + curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  251 +
  252 + $result = curl_exec($curl);
  253 + $status = curl_getinfo($curl);
  254 + curl_close($curl);
  255 +
  256 + if(intval($status["http_code"]) == 200){
  257 + return $result;
  258 + }else{
  259 + return false;
  260 + }
  261 + }
  262 +
  263 +}
0 \ No newline at end of file 264 \ No newline at end of file
console/controllers/TestController.php
@@ -8,64 +8,10 @@ namespace console\controllers; @@ -8,64 +8,10 @@ namespace console\controllers;
8 */ 8 */
9 9
10 10
11 -  
12 -use app\wx\modules\user\helpers\UserOrderHelper;  
13 -use common\business\AdminCloseOrder;  
14 -use common\business\CancelOrder;  
15 -use common\business\Deduction;  
16 -  
17 -use common\exts\Invoice\baiwang\InvoiceApi;  
18 -use common\exts\privatenumber\PrivateNumber;  
19 -use common\exts\wechat\Http;  
20 -  
21 -use common\helpers\DeviceSearch;  
22 -use common\helpers\Distance;  
23 -use common\helpers\FileManager;  
24 -  
25 -use common\helpers\ImageManager;  
26 -use common\helpers\MinaHelper;  
27 -use common\helpers\OrderSqlHelper;  
28 -use common\helpers\UserMPHelper;  
29 -use common\helpers\WxHelper;  
30 -use common\models\BindDeviceApply;  
31 -use common\models\Engineer;  
32 -use common\models\EngineerEvent;  
33 -use common\models\EngineerPickOrder;  
34 -use common\models\Invoice;  
35 -use common\models\PrivateNumberBindRecord;  
36 -use common\models\PrivateNumberVoice;  
37 -use common\models\RepairOrder;  
38 -  
39 -use common\models\RepairOrderDeduction;  
40 -  
41 -  
42 -use common\services\wxs\SmsMessage;  
43 -use console\models\ClearFileTask;  
44 -use console\models\EngineerTask;  
45 -  
46 -use domain\data\EngineerReport;  
47 -use domain\device\BindQrcodeApplyRepository;  
48 -use domain\engineer\EngineerAchievementPro;  
49 -use domain\engineer\EngineerRepository;  
50 -use domain\finance\EngineerCashFlow;  
51 -use domain\finance\EngineerCashFlowStatus;  
52 -use domain\finance\EngineerCashFlowType;  
53 -use domain\marketing\activity\DeductionActivityRepository;  
54 -  
55 -use domain\marketing\subsidy\ApplyAwardRule;  
56 -use domain\marketing\thankfee\ThankFee;  
57 -use domain\system\message\CustomMessage;  
58 -use domain\trade\dob\DispatchOrder;  
59 -  
60 -use domain\trade\OnsiteOrder;  
61 -use domain\trade\privateorder\PrivateOrderUserOp;  
62 -use domain\trade\RepairOrderRepository; 11 +use common\exts\Http;
63 use GuzzleHttp\Psr7; 12 use GuzzleHttp\Psr7;
64 -use Workerman\Worker;  
65 use yii\console\Controller; 13 use yii\console\Controller;
66 -use yii\db\Query;  
67 -use yii\base\Exception;  
68 - 14 +use GuzzleHttp\Psr7\Request;
69 use function chr; 15 use function chr;
70 use yii\helpers\ArrayHelper; 16 use yii\helpers\ArrayHelper;
71 17
@@ -77,6 +23,31 @@ class TestController extends Controller @@ -77,6 +23,31 @@ class TestController extends Controller
77 echo sprintf('%04x', 1); 23 echo sprintf('%04x', 1);
78 } 24 }
79 25
  26 + public function actionDeviceAuth()
  27 + {
  28 + $url = 'http://kingb:8012/app-api/web/authDevice';
  29 + $manufactureNo = '0002';
  30 + $device_id = 'SZDEVICE000036';
  31 + $projectNo = '002';
  32 + $modelNo = '002';
  33 + $productionNo = '0001';
  34 +
  35 + $timestamp = time();
  36 + $salt = 13456;
  37 + $sign = md5($manufactureNo. $projectNo. $modelNo . $productionNo . $timestamp . $salt);
  38 + $params = [
  39 + 'manufacture' => $manufactureNo,
  40 + 'device_id' => $device_id,
  41 + 'project' => $projectNo,
  42 + 'model' => $modelNo,
  43 + 'production' => $productionNo,
  44 + 'timestamp' => $timestamp,
  45 + 'sign' => $sign,
  46 + ];
  47 + $postResult = Http::POST($url, $params);
  48 + echo $postResult;
  49 +
  50 + }
80 51
81 } 52 }
82 53
domain/device/Device.php
@@ -38,18 +38,33 @@ class Device @@ -38,18 +38,33 @@ class Device
38 $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]); 38 $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]);
39 if (empty($batchModel)) { 39 if (empty($batchModel)) {
40 $e->message = '没有该批次'; 40 $e->message = '没有该批次';
  41 + return $e;
41 } 42 }
42 - $count = DeviceRepository::rowsCount(['batch_id' => $batchModel->batch_id, 'is_delete' => 0]);  
43 - if ($count > $batchModel->num) { 43 + $batchId = $batchModel->id;
  44 + $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'is_delete' => 0]);
  45 +
  46 + if (($count *1) >= ($batchModel->num * 1)) {
44 // 超过了限制数,记录到另外一个表里面 47 // 超过了限制数,记录到另外一个表里面
45 // to do 记录到表里面 48 // to do 记录到表里面
46 - 49 + $failRecord = DeviceAuthFailRepository::findOne(['device_id' => $deviceId]);
  50 + if (!$failRecord) {
  51 + $item = [
  52 + 'manufacture_no' => $manufactureNo,
  53 + 'project_no' => $projectNo,
  54 + 'model_no' => $modelNo,
  55 + 'production_no' => $productionNo,
  56 + 'device_id' => $deviceId,
  57 + ];
  58 + DeviceAuthFail::create($item);
  59 + }
47 $e->message = '授权失败'; 60 $e->message = '授权失败';
48 return $e; 61 return $e;
49 } 62 }
50 - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]); 63 +
  64 +
  65 + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]);
51 if (empty($deviceModel)) { 66 if (empty($deviceModel)) {
52 - $newDeviceModel = DeviceRepository::findOne(['device_id'=> null, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]); 67 + $newDeviceModel = DeviceRepository::findOne(['device_id'=> null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
53 $newDeviceModel->device_id = $deviceId; 68 $newDeviceModel->device_id = $deviceId;
54 $newDeviceModel->status = DeviceStatus::HAS_AUTH; 69 $newDeviceModel->status = DeviceStatus::HAS_AUTH;
55 $newDeviceModel->apply_at = $tt ; 70 $newDeviceModel->apply_at = $tt ;
domain/device/DeviceAuthFail.php
@@ -2,13 +2,31 @@ @@ -2,13 +2,31 @@
2 2
3 namespace domain\device; 3 namespace domain\device;
4 4
5 -use yii\behaviors\TimestampBehavior;  
6 -use yii\db\ActiveRecord; 5 +use Yii;
  6 +use domain\device\models\DeviceAuthFail as DeviceAuthFailModel;
7 7
8 class DeviceAuthFail 8 class DeviceAuthFail
9 { 9 {
  10 + /**
  11 + * @param $item
  12 + * @return null|object
  13 + * @throws \yii\base\InvalidConfigException
  14 + */
10 static function create($item) 15 static function create($item)
11 { 16 {
12 - 17 + $failModel = Yii::createObject([
  18 + 'class' => DeviceAuthFailModel::className(),
  19 + 'manufacture_no' => $item['manufacture_no'],
  20 + 'project_no' => $item['project_no'],
  21 + 'model_no' => $item['model_no'],
  22 + 'production_no' => $item['production_no'],
  23 + 'device_id' => $item['device_id'],
  24 + 'apply_at' => time(),
  25 + ]);
  26 + if ($failModel->save()) {
  27 + return $failModel;
  28 + } else {
  29 + return null;
  30 + }
13 } 31 }
14 } 32 }
15 \ No newline at end of file 33 \ No newline at end of file