美文网首页
[PHP高可用后端]④④--登陆功能

[PHP高可用后端]④④--登陆功能

作者: 子木同 | 来源:发表于2017-11-30 10:50 被阅读39次

    route.php

    <?php
    
    use think\Route;
    
    Route::get('test', 'api/test/index');
    Route::put('test/:id', 'api/test/update');
    Route::delete('test/:id', 'api/test/delete');
    
    //Route::resource('test', 'api/test');
    
    Route::get('api/:ver/cat', 'api/:ver.cat/read');
    
    Route::get('api/:ver/index', 'api/:ver.index/index');
    Route::get('api/:ver/init', 'api/:ver.index/init');
    
    Route::resource('api/:ver/news', 'api/:ver.news');
    
    Route::get('api/:ver/rank', 'api/:ver.rank/index');
    
    //Route::get('test/testsend', 'api/test/testSend');
    
    //短信验证码相关
    Route::resource('api/:ver/identify', 'api/:ver.identify');
    
    //登录的路由
    Route::post('api/:ver/login', 'api/:ver.login/save');
    

    app.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/20
     * Time: 11:43
     */
    return [
        'password_pre_halt' => '_#sing_ty',//密码加密言
        'aeskey' => 'sgg45747ss223455',
        'apptypes' => [
            'ios',
            'android',
        ],
        'app_sign_time' => 6000,
        'app_sign_cache_time' => 12000,
        'login_time_out_day'=>7,//登陆token的失效时间
    ];
    
    

    User.php(Model)

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/29
     * Time: 17:32
     */
    
    namespace app\common\model;
    
    class User extends Base
    {
    
    }
    

    Login.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/29
     * Time: 17:16
     */
    
    namespace app\api\controller\v1;
    
    use app\api\controller\Common;
    use app\common\lib\Aes;
    use app\common\lib\Alidayu;
    use app\common\lib\IAuth;
    use app\common\model\User;
    
    class Login extends Common
    {
        /**
         * post
         */
        public function save()
        {
            if (!request()->isPost()) {
                return show(config('code.error'), '您没有权限', [], 403);
            }
            $param = input('param.');
            if (empty($param['phone'])) {
                return show(config('code.error'), '手机不合法', [], 404);
            }
            if (empty($param['code'])) {
                return show(config('code.error'), '手机短信验证码不合法', [], 404);
            }
            //validate 严格检验 自行完成
            $code = Alidayu::getInstance()->checkSmsIdentify($param['phone']);
            if ($code != $param['code']) {
                return show(config('code.error'), '手机短信验证码不存在', [], 404);
            }
            $token = IAuth::setAppLoginToken($param['phone']);
            $data = [
                'token' => $token,
                'time_out' => strtotime("+" . config('app.login_time_out_day') . ' days'),
            ];
            //查询这个手机号是否存在
            $user = User::get(['phone' => $param['phone']]);
            //halt($user);
            if ($user && $user->status == 1) {
                $id = model('User')->save($data, ['phone' => $param['phone']]);
            }else{
                //第一次登陆 注册数据
                $data['username']= 'singwa粉-' . $param['phone'];
                $data['status']=1;
                $data['phone']= $param['phone'];
                $id = model('User')->add($data);
    
            }
            $obj = new Aes();
            if ($id) {
                $result = [
                    //'token' => $obj->encryt($token . '||' . $id.time()),
                    'token' => $token,
                ];
                return show(config('code.success'), 'OK', $result);
            } else {
                return show(config('code.error'), 'error', [], 404);
            }
    
        }
    }
    

    Test.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/15
     * Time: 10:23
     */
    
    namespace app\api\controller;
    
    use app\common\lib\Aes;
    use Aliyun\DySDKLite\Sms\SmsApi;
    use app\common\lib\Alidayu;
    use app\common\lib\IAuth;
    
    class Test extends Common
    {
    
        public function index()
        {
            return [
                'sgsg',
                'sgsgs',
            ];
        }
    
        public function update($id = 0)
        {
    //        return $id;//http://singwa.com/test/100
    
            halt(input('put.'));//body提交
    
        }
    
        /**
         * post 新增
         * @return mixed
         */
        public function save()
        {
            $data = input('post.');
            //获取到提交数据 插入库
            //给客户端APP =》 接口数据
    
            //201 创建成功
            return show(1, 'OK',
                (new Aes())->encryt(json_encode(input('post.'))),
                201);
        }
    
        public function testSend()
        {
            $obj=Alidayu::getInstance()->setSmsIdentify('18617156713');
            if($obj){
                echo 'true';
            }
        }
    
        public function token(){
            echo IAuth::setAppLoginToken('');
        }
    }
    
    
    image.png

    相关文章

      网友评论

          本文标题:[PHP高可用后端]④④--登陆功能

          本文链接:https://www.haomeiwen.com/subject/gwkpbxtx.html