作者 董瑞恩
1 个管道 的构建 通过 耗费 1 秒

order

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: ruidiudiu
  5 + * Date: 2018/11/26
  6 + * Time: 14:49
  7 + */
  8 +
  9 +namespace app\portal\controller;
  10 +
  11 +
  12 +use cmf\controller\HomeBaseController;
  13 +use think\Db;
  14 +
  15 +/**
  16 + * @title 登录相关接口
  17 + * @description 登录相关接口
  18 + * @group 登录相关接口
  19 + */
  20 +class LoginController extends HomeBaseController{
  21 + private $appId="wx75855734b0730d64";
  22 + private $appSecret="106883ca189dd07894e79d5d94bcd6a0";
  23 + /**
  24 + * @title 登录凭证校验
  25 + * @description 接口说明
  26 + * @author 董瑞恩
  27 + * @url /portal/login/login
  28 + * @method post
  29 + *
  30 + * @param name:code type:String require:1 default:null desc:登录凭证
  31 + *
  32 + * @return users_id:用户id
  33 + * @return type:是否注册(0、未注册,1、已注册)
  34 + * @return openid:openid
  35 + * @return is_use:是否在使用设备(0:空闲,1:在用)
  36 + */
  37 + public function login(){
  38 + $code=$this->request->param('code');
  39 + //登录凭证校验接口
  40 + $loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appId."&secret=".$this->appSecret."&js_code=".$code."&grant_type=authorization_code";
  41 + $loginData=json_decode(file_get_contents($loginUrl),true);
  42 + if(isset($loginData['openid'])){
  43 + $users = Db::name('users')->where('open_id', $loginData['openid'])->find();
  44 + if (empty($users)){
  45 + $users_id=Db::name('users')->insertGetId(['open_id'=>$loginData['openid'],'create_time'=>date('Y-m-d H:i:s')]);
  46 + $this->apiResponse(200,'success',['users_id'=>$users_id,'type'=>0,'openid'=>$loginData['openid'],'is_use'=>0]);
  47 + }else{
  48 + $this->apiResponse(200,'success',['users_id'=>$users['id'],'type'=>1,'openid'=>$users['open_id'],'is_use'=>$users['is_use']]);
  49 + }
  50 + }else{
  51 + $this->apiResponse(301,'获取登录凭证失败',$loginData);
  52 + }
  53 + }
  54 +}