LoginController.php 2.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/26
 * Time: 14:49
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
use think\Db;

/**
 * @title 登录相关接口
 * @description 登录相关接口
 * @group 登录相关接口
 */
class LoginController extends HomeBaseController{
    private $appId="wx75855734b0730d64";
    private $appSecret="4c20fc2db6e2324e4d0a85ec9a6692e7";
    /**
     * @title 登录凭证校验
     * @description 接口说明
     * @author 董瑞恩
     * @url /portal/login/login
     * @method post
     *
     * @param name:code  type:String require:1 default:null  desc:登录凭证
     *
     * @return users_id:用户id
     * @return type:是否注册(0、未注册,1、已注册)
     * @return is_use:是否在使用设备(0:空闲,1:在用)
     */
    public function login(){
        $code=$this->request->param('code');
        //登录凭证校验接口
        $loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appId."&secret=".$this->appSecret."&js_code=".$code."&grant_type=authorization_code";
        $loginData=json_decode(file_get_contents($loginUrl),true);
        if(isset($loginData['openid'])){
            $users = Db::name('users')->where('open_id', $loginData['openid'])->find();
            if (empty($users)){
                $users_id=Db::name('users')->insertGetId(['open_id'=>$loginData['openid'],'create_time'=>time()]);
                $this->apiResponse(200,'success',['users_id'=>$users_id,'type'=>0,'is_use'=>0]);
            }else{
                if (empty($users['phone'])){
                    $this->apiResponse(200,'success',['users_id'=>$users['id'],'type'=>0,'is_use'=>$users['is_use']]);
                }else{
                    $this->apiResponse(200,'success',['users_id'=>$users['id'],'type'=>1,'is_use'=>$users['is_use']]);
                }
            }
        }else{
            $this->apiResponse(301,'获取登录凭证失败',$loginData);
        }
    }
}