yii中的restful方式输出并调用接口和判断用户是否登录状态

 1 //创建一个控制器接口  返回的是restful方式
 2 <?php
 3 namespace frontend\controllers;
 4 use frontend\models\Fenlei;
 5 use frontend\models\Goods;
 6 use frontend\models\Power;
 7 use frontend\models\User;
 8 use yii;
 9 use yii\web\Controller;
10 use yii\rest\ActiveController;
11 class RestfulController extends ActiveController
12 {
13     protected  $result=[
14         'code'=>0,
15         'data'=>'',
16         'error'=>''
17     ];
18     public $modelClass='';
19 
20     public function actions()
21     {
22         $actions=parent::actions();
23         unset($actions['view'],$actions['index'],$actions['create'],$actions['delete'],$actions['update']);
24         return $actions;
25     }
26 
27     //登录页面
28     public function actionLogin()
29     {
30         $data['name']=Yii::$app->request->post('name');
31         $data['pwd']=Yii::$app->request->post('pwd');
32         if(empty( $data['name']) || empty($data['pwd']))
33         {
34             $this->result['error']="用户名密码不能为空";
35             $this->result['code']="1001";
36             return $this->result;
37         }
38         $aa=User::user($data);
39         if($aa)
40         {
41             Yii::$app->cache->set('name',1,7200);
42             $token=md5($data['name'].$data['pwd']);
43             $this->result['data']=$token;
44             $this->result['error']="登录成功";
45         }else{
46             $this->result['error']="登录失败";
47         }
48         return $this->result;
49     }
50 }
//然后通过另一个控制器来调用接口
<?php
namespace frontend\controllers;
use frontend\models\Curl;
use yii;
use yii\web\Controller;
use yii\rest\ActiveController;
class LoginController extends Controller
{
    //登录页面
    public function actionLogin()
    {
        $name=Yii::$app->request->get('name');
        $pwd=Yii::$app->request->get('pwd');
        $curl = new Curl();
        $url='http://weixin.api.com/index.php/restful/login';
        $data=[
            'name'=>$name,
            'pwd'=>$pwd
        ];
        $aa=$curl->getCurl($url,'post',$data);
        //返回的是json数据
        $a=json_decode($aa,true);
        $token=$a['data'];
        file_put_contents('login.txt',$token);
        var_dump($token);
    }
}
//这是curl的方式调用
<?php
namespace frontend\models;
class Curl
{
    public function getCurl($url,$type='get',$data,$https='true')
    {
        $ch = curl_init($url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        if($type=='post')
        {
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }
        if($https=='false')
        {
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        }
        $data=curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
//判断用户是否登录的状态
<?php
namespace frontend\controllers;
use frontend\models\Curl;
use yii;
use yii\web\Controller;
class CatController extends Controller
{
   //判断用户是否存在
    public function init()
    {
        $name=Yii::$app->cache->get('name');
        if($name !=1)
        {
            Yii::$app->cache->set('name',2);
            return "请登录";
        }
    }

    //水果的添加
    public function actionAdd()
    {
        $data['g_goods']=Yii::$app->request->get('g_goods');
        $data['g_kc']=Yii::$app->request->get('g_kc');
        $data['id']=Yii::$app->request->get('id');
        $url='http://weixin.api.com/index.php/restful/add';
        $curl = new Curl();
        $aa=$curl->getCurl($url,'post',$data);
        var_dump($aa);
    }
}

 

posted @ 2019-03-12 18:49  PHP小媛  阅读(687)  评论(0编辑  收藏  举报