123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace api\controllers;
- use common\models\RabbitLog;
- use components\PhpClient;
- use components\RabbitBase;
- use Yii;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use common\models\LoginForm;
- use frontend\models\PasswordResetRequestForm;
- use frontend\models\ResetPasswordForm;
- use frontend\models\SignupForm;
- use frontend\models\ContactForm;
- /**
- * Site controller
- */
- class ApiController extends Controller
- {
- /**
- * @inheritdoc
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- return [
- 'message' => 'API test Ok!',
- 'code' => 100,
- ];
- // return $this->render('index');
- }
- /**
- * login check
- *
- * @return mixed
- */
- public function actionMqinsert()
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- $params=Yii::$app->request->get();//获取参数
- $rabbitdata=$this->validate($params);
- if($rabbitdata){
- PhpClient::CallMq($rabbitdata);
- return [
- 'message' => 'rabbit insert Ok!',
- 'action' => 'insert',
- 'rabbitdata' => $params,
- 'code' => 100,
- ];
- }
- }
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionPage()
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- $params=Yii::$app->request->get();//获取参数
- $rabbitdata=$this->validate($params);
- if($rabbitdata){
- PhpClient::CallMq($rabbitdata);
- return [
- 'message' => 'rabbit insert Ok!',
- 'action' => 'insert',
- 'rabbitdata' => $params,
- 'code' => 100,
- ];
- }
- }
- /**
- * Displays homepage.
- *
- * @return mixed
- *
- *
- * 登入接口修改后的分为两个步骤:
- 1.接收数据api,存到mq里返回ok 异步:队列开始运行调取登入接口,
- 如果json返回队列信息那么跟新消息信息,期间令牌保存处理状态
- 如果登入成功改令牌状态修改为处理完毕
- 2.根据令牌查找这个队列的信息
- *
- * 策略一,2个接口判断
- * 策略二,一个接口判断2个redis 是否还在队列中
- *
- */
- public function actionLogin()
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- $params=Yii::$app->request->get();//获取参数
- //不做加密校验
- unset($params['r']);
- //begin checklogin -------------
- //判断是否已经登入
- //do something 2018-01-24
- //end checklogin -------------
- $isLogin=false;//假设没有登入
- if(!$isLogin){
- //begin login -------------
- //redis 初始化 登入一次设置一次 所以该状态不作为登入的状态信息
- $redis = \Yii::$app->redis;
- $key = 'get-one-login-user-id-by-phone-' . $params['mobile'];
- // $redis->set($key,'');//暂时不需要 //退出登入是做该操作就可以 //可以多次请求接口 保持更新
- // die;
- //取出reidis的值
- $loginAnswer=json_decode($redis->get($key));//取出 redis的值
- //如果 有数据 则判断是否被 队列处理 获取了 登入结果信息
- //开始判断
- if(empty($loginAnswer)){//如果是空说明第一次
- //判断该接口第一次请求登入该用户,并且队里中没有该用户登入的处理队里消息
- //传入mq中同步
- //如果没有请求则 插入请求状态 标识 请求处理中
- $loginAnswer=[
- 'message' => 'handing',
- 'code' => 102,//表示队里还没有处理到 需要再次请求以获取处理结果
- 'status' =>200,
- 'rabbit'=>'handing'
- ];
- //插入redis 标识 改请求已经 在处理中了
- $redis->set($key,json_encode($loginAnswer));
- $insertMq=RabbitBase::CallMq(json_encode($params)) ;//插入队列
- if($insertMq){
- //取出reidis的值
- //因为队列消息处理处理速度可能和 上下文时间同时发生所以在此校验 一下该信息是否已经被队列处理完成避免多次网络请求
- $loginAnswer=json_decode($redis->get($key));
- //获取到储存在redis里面消费队列请求的结果并且返回给前段,如果消息队列里面没有放入到redis里面则判断为没有操作登入返回登入失败处理
- }
- }else{
- //用户显示一次刷新一次 如果是单个用户第二次请求不同的token
- // var_dump($loginAnswer);die;
- if($loginAnswer->code!=102){
- $loginAnswer=json_decode($redis->get($key));
- $redis->set($key,''); //可以多次请求接口 保持更新
- }
- }
- //不是第一次 直接返回redis内容 因为 队列消费已经处理保存进去了
- //end login -------------
- }else{
- $loginAnswer=[
- 'message' => 'have logined',
- 'code' => 101,
- 'status' => 200,//表示队里还没有处理到 需要再次请求以获取处理结果
- 'rabbit'=>'you have logined'
- ];
- }
- return $loginAnswer;
- }
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- private function validate($params)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- if(isset($params["signature"]) && isset($params["timestamp"]) && isset($params["rabbitdata"])){
- $signature = $params["signature"];//本地签名
- $timestamp = $params["timestamp"];//时间戳
- $rabbitdata = $params["rabbitdata"];//rabbitdata 存入mq中的数据
- unset($params['r'],$params['signature'],$params['rabbitdata']);
- //valid signature , option
- if($this->checkSignature($params,$timestamp,$signature)){
- return $rabbitdata;//返回rabbitdata
- }else if(!$this->checkRabbitdata($rabbitdata)){
- return false;
- }else{
- exit(json_encode([
- 'message' => 'signature test fail!',
- 'code' => 201,
- ]));
- }
- }else{
- exit(json_encode([
- 'message' => 'params key canot be null!',
- 'code' => 203,
- ]));
- }
- }
- private static function getSign($params, $appkey, $appSecret, $time)
- {
- $sign = '';
- if (!empty($params)) {
- ksort($params);
- $string = http_build_query($params);
- $result = md5($appkey . $string . $appSecret . $time);
- $sign = strtoupper($result);
- }
- return $sign;
- }
- private function checkSignature($params,$timestamp,$signature)
- {
- defined('APP_ID') or define("APP_ID", "disanbo");
- defined('APP_SECRET') or define("APP_SECRET", "di~sanbo1");
- $appkey = APP_ID;
- $appSecret = APP_SECRET;
- $sign= $this->getSign($params, $appkey, $appSecret, $timestamp);
- // var_dump($sign);die;
- if( $sign == $signature ){
- //do something
- return true;
- }else{
- return false;
- }
- }
- /* rabbitdata
- *
- *
- * */
- private function checkRabbitdata($rabbitdata)
- {
- //其他验证 dosomething
- if(!empty($rabbitdata)){
- return true;
- }else{
- exit(json_encode([
- 'message' => 'rabbitdata cannot be null!',
- 'code' => 202,
- ]));
- }
- }
- }
|