小程序thinkphp5登录信息获取

小程序thinkphp5登录信息获取

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9 0009
 * Time: 下午 5:05
 */

namespace app\index\model;

use think\Model;
use think\Paginator;
use think\Cache;

class MpUser extends Model
{
    private $appid = '*****';
    private $secret = '*****';

    protected $hidden = [

        'add_time'
    ];


    public static function loadInfo($userid)
    {
        //echo $userid;
        return json(self::get(['userid' => $userid]));

    }

    public function getGenderAttr($value)
    {
        $status = [0 => '未知', 1 => '男', 2 => '女'];
        return $status[$value];
    }


    //登录
    public function login($code, $user)
    {


        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=*******&secret=**************&js_code=" . $code . "&grant_type=authorization_code";

        $data = get_object_vars(json_decode(file_get_contents($url)));
        $openid = $data['openid'];
        if (Cache::get('wx_user' . $openid)){
            return json(Cache::get('wx_user' . $openid));
        }

        $nickName = self::where(['openid' => $data['openid']])->field('nickName')->find();

        /*if (self::where(['openid' => $data['openid']])->count() >= 1) {
            $data = self::get(['openid' => $data['openid']]);
            Cache::set('wx_user' . $openid, $data);
            return json($data);
        }*/
        if (!empty($nickName)) {
            $data = self::get(['openid' => $data['openid']]);
            Cache::set('wx_user' . $openid, $data);
            return json($data);
        }else{
            if (self::where(['openid' => $data['openid']])->count() >= 1) {
                return self::upda($user, $openid);
            }
            return self::register($user, $openid);
        }

    }


    //注册成为小程序用户
    public static function register($user, $openid)
    {

        self::create([
            'openid' => $openid,
            'nickName' => $user['nickName'],
            'gender' => $user['gender'],
            'avatarUrl' => $user['avatarUrl'],
            'country' => $user['country'],
            'province' => $user['province'],
            'city' => $user['city'],
            'add_time' => time()
        ]);

        $data = self::get(['openid' => $openid]);
        Cache('wx_user' . $openid, $data);

        return json($user);


    }

    //授权更新小程序用户
    public static function upda($openid ,$user )
    {
        self::where(['openid' => $openid])->update
        ([
            'nickName' => $user['nickName'],
            'gender' => $user['gender'],
            'avatarUrl' => $user['avatarUrl'],
            'country' => $user['country'],
            'province' => $user['province'],
            'city' => $user['city'],
            'add_time' => time()
        ]);

        $data = self::get(['openid' => $openid]);
        Cache('wx_user' . $openid, $data);

        return json($user);


    }

    public function getCreateTimeAttr($time)
    {
        return $time;
    }


    public function openid($code)
    {
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->appid . "&secret=" . $this->secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $data = file_get_contents($url);
        $object = json_decode($data);
        return json($object);
    }


    //登录

     public function newLogin($code)
    {

        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=*******&secret=************&js_code=" . $code . "&grant_type=authorization_code";

        $data = get_object_vars(json_decode(file_get_contents($url)));
        $openid = $data['openid'];

        /*if (Cache::get('wx_user' . $openid)){
            return json(Cache::get('wx_user' . $openid));
        }*/
        //Cache::rm('wx_user' . $openid);
        if (self::where(['openid' => $data['openid']])->count() >= 1) {
            /*$nickname = self::where(['openid' => $data['openid']])->field('nickaName')->find();
            if(empty($nickname)){
                if($data['nickName']){
                    self::where(['openid' => $data['openid']])->update([
                        'nickName'=>$data['nickName']
                    ]);
                }

            }*/
            $data = self::get(['openid' => $data['openid']]);
            Cache::set('wx_user' . $openid, $data);
            return json($data);
        }
        return self::newRegister($openid);

    }

    //注册成为小程序用户
    public static function newRegister($openid)
    {
        self::create([
            'openid' => $openid,
            'add_time' => time()
        ]);

        $data = self::get(['openid' => $openid]);
        Cache('wx_user' . $openid, $data);

        return json($data);


    }


    public static function userTest($openid)
    {
        $count =self::where(['openid' => $openid])->count()>=1;
        var_dump($count);
        die;
        $data = self::get(['openid' => $openid]);
       
        return json($data);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值