准备工作:

1.fastAdmin伪静态设置

参考:ThinkPHP URL重写: https://www.kancloud.cn/manual/thinkphp5/177576

Nginx:/conf/vhosts/hostname.conf

2.FeHelper插件安装

参考:Web开发者助手 FeHelper:https://www.baidufe.com/fehelper/index/index.html


一、api方法解析

1.api/controller/Demo.php中,可以使用基类中的接口方法,来获取想要的参数或数据:

public function test2()
    {
        // $this->success('返回成功', ['action' => 'test2']);
        // $this->success('返回成功', $this->auth->id);
        // $this->success('返回成功', $this->auth->islogin());
        // $this->success('返回成功', $this->auth->getUser());
        // $this->success('返回成功', $this->auth->getToken());
        // $this->success('返回成功', $this->auth->getUserinfo());
        $this->success('返回成功', $this->auth->getRequestUri());
    }

基类:

common/controller/API.php

common/library/Auth.php

更多接口方法,可以在基类中查找。


二、api自动注册登录及退出

1.引入DB类及cookie类

use think\Db;
use think\Cookie;

2.测试小程序用户自动注册及登陆功能

在数据表user中新增小程序openid字段。

    public function test1()
    {
        $openid = "ceshi1144";
        $username = "wxcs1144";
        $search_res = Db::name("user")->whereOr("username", $username)->whereOr("openid", $openid)->find();
        if ($search_res) {
            $loginRes = $this->auth->direct($search_res['id']);
            if ($loginRes) {
                Cookie::set('uid', $this->auth->id);
                Cookie::set('token', $this->auth->getToken());
                $this->success('登录成功', $loginRes);
            } else {
                $this->success('登录失败,未找到用户', $loginRes);
            }
        } else {
            $registerRes = $this->auth->register($username, $username, '', '', [
                'openid' => $openid
            ]);
            // 登录,写入当前id用户的cookie信息
            // Cookie::set('uid', $this->auth->id);
            // Cookie::set('token', $this->auth->getToken());

            $this->auth->logout();  // 注销
            Cookie::delete('uid');
            Cookie::delete('token');
            $this->success("执行注册", $registerRes);
        }
    }

三、api登录后token使用方法和简单分析

目地:同时只能在同一个地方登陆,在另一个地方登陆时,销毁前面的token,创建新的token。

demo/test1和demo/test2两个接口说明:

test1接口不需要登录可以直接访问,执行注册或登陆行为;

test2接口是需要登陆后才可以访问的接口,所以需要先执行test1后拿到当前token,在请求test2接口时,在head头信息中,加上token信息,就可以实现登陆,就可以访问test2接口了。

具体代码如下:

    /**
     * 无需登录的接口
     *
     */
public function test1()
    {
        $openid = "ceshi1144";
        $username = "wxcs1144";
        $search_res = Db::name("user")->whereOr("username", $username)->whereOr("openid", $openid)->find();
        if ($search_res) {

            //清除当前用户的token
            Token::clear($search_res["id"]);
            $loginRes = $this->auth->direct($search_res['id']);
            if ($loginRes) {
                
                //登录成功,获取新的token
                $token = $this->auth->getToken();
                //将token写入浏览器
                Cookie::set('uid', $this->auth->id);
                Cookie::set('token', $token);

                $this->success('登录成功',["token"=>$token]);
            } else {
                $this->success('登录失败,未找到用户', $loginRes);
            }
        } else {
            $registerRes = $this->auth->register($username, $username, '', '', [
                'openid' => $openid
            ]);
            // 登录,写入当前id用户的cookie信息
            // Cookie::set('uid', $this->auth->id);
            // Cookie::set('token', $this->auth->getToken());

            $this->auth->logout();  // 注销
            Cookie::delete('uid');
            Cookie::delete('token');
            $this->success("执行注册", $registerRes);
        }
    }
    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['dataType' => 'user', 'dataValue' => $this->auth->getUserinfo()]);
    }