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

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容