审查视图

application/home/controller/User.php 5.9 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/22
 * Time: 17:06
 */
namespace app\home\controller;

use app\api\controller\Validate;
use app\common\controller\WechatBase;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Log;

class User extends WechatBase
{
    public function callback(){
        $appId=config('wechat.app_id');
        $secret=config('wechat.secret');
        $config = [
            'app_id'  => $appId,
            'secret'  => $secret,
        ];

        $app = new Application($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $wechat_user = $user->toArray();
        if(isset($wechat_user['id'])){
            $openid=$wechat_user['id'];
            $findThird = Db::name("third")
                ->where('openid', $openid)
                ->where('app_id', $appId)
                ->find();
            if ($findThird) {
                $this->wechatUserLogin($findThird,$openid,$appId);
            }else{
                $this->wechatUserRegister($wechat_user,$openid,$appId);
            }
            $target_url=session('target_url');
            $targetUrl = empty($target_url) ? '/' : $target_url;
            header('location:'. $targetUrl);
        }else{
            Log::write('获取微信用户数据失败');
            $this->error('获取微信用户数据失败');
        }

    }

    /**
     * 微信用户登录
     * @param $findThird
     * @param $openid
     * @param $appId
     */
    protected function wechatUserLogin($findThird,$openid,$appId){
        $currentTime = time();
        $ip          = $this->request->ip(0, true);
        $token = generate_user_token($findThird['user_id']);
        $thirdData = [
            'loginip'   => $ip,
            'logintime' => $currentTime,
            'login_times'     => Db::raw('login_times+1')
        ];
        $row1=Db::name("third")
            ->where('openid', $openid)
            ->where('app_id', $appId)
            ->update($thirdData);
        $userInfo=Db::name("third")
            ->where('openid', $openid)
            ->where('app_id', $appId)->find();
        $user = Db::name("user")
            ->where('id', $userInfo['user_id'])
            ->find();
        $userData = [
            'loginip'   => $ip,
            'prevtime'  => $user['logintime'],
            'logintime' => $currentTime,
        ];
        $row2=Db::name("user")
            ->where('id', $userInfo['user_id'])
            ->update($userData);
        if($row1!==false&&$row2!==false){
王晓刚 authored
85
            $userModel=new \app\home\model\User();
王晓刚 authored
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
            $user=$userModel->getUserInfo(['user_id'=>$userInfo['user_id'],'app_id'=>$appId]);
            update_current_user($user);
            session('token',$token);
            Db::commit();
        }else{
            Db::rollback();
        }
    }

    /**
     * 微信用户注册
     * @param $wechat_user
     * @param $openid
     * @param $appId
     */
    protected function wechatUserRegister($wechat_user,$openid,$appId){
        $currentTime = time();
        $ip          = $this->request->ip(0, true);
        Db::startTrans();
        $userId = Db::name("user")->insertGetId([
            'status'     => 'normal',
            'gender'     => $wechat_user['original']['sex'],
            'nickname'   => $wechat_user['nickname'],
            'avatar'     => $wechat_user['avatar'],
            'joinip'     => $ip,
            'jointime'   => $currentTime,
            'createtime' => $currentTime,
            'updatetime' => $currentTime,
            'loginip'    => $ip,
            'logintime'  => $currentTime,
王晓刚 authored
116
            'type'=> 1,
王晓刚 authored
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        ]);

        $row=Db::name("third")->insert([
            'openid'          => $openid,
            'user_id'         => $userId,
            'nickname'        => $wechat_user['nickname'],
            'app_id'          => $appId,
            'loginip'         => $ip,
            'union_id'        => '',
            'logintime'       => $currentTime,
            'createtime'      => $currentTime,
            'login_times'     => 1,
            'more'            => json_encode($wechat_user)
        ]);
        if($userId && $row){
            $token = generate_user_token($userId);
王晓刚 authored
133
            $userModel=new \app\home\model\User();
王晓刚 authored
134 135 136 137 138 139 140 141 142 143 144
            $user=$userModel->getUserInfo(['user_id'=>$userId,'app_id'=>$appId]);
            update_current_user($user);
            session('token',$token);
            Db::commit();
        }else{
            Db::rollback();
        }
    }

    /**
     * 授权页面
王晓刚 authored
145
     * @param null $target_url
王晓刚 authored
146 147
     * @return mixed
     */
王晓刚 authored
148
    public function authorization_view($target_url = null){
王晓刚 authored
149
        $this->assign('title','授权页');
王晓刚 authored
150
        $this->assign('target_url',$target_url);
王晓刚 authored
151 152 153 154 155 156 157
        return $this->fetch();
    }

    /**
     * 授权
     */
    public function authorization(){
王晓刚 authored
158 159 160 161
        $target_url = $this->request->param('target_url');
        if(empty($target_url)){
            $target_url = url('home/index/index','',false,true);
        }else{
王晓刚 authored
162
            $target_url = rawurldecode($target_url);
王晓刚 authored
163 164
        }
        $this->checkWeChatUserLogin($target_url);
王晓刚 authored
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    }

    /**
     * 定位当前位置判断
     */
    public function location(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'longitude' => 'require',
            'latitude' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'longitude.require' => 'longitude参数错误!',
            'latitude.require' => 'latitude参数错误!',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $where['id'] = ['eq',$param['user_id']];
王晓刚 authored
186
        unset($param['user_id']);
王晓刚 authored
187 188 189 190 191
        $userModel = new \app\home\model\User();
        $result = $userModel->updateData($where,$param);
        $this->success('SUCCESS');
    }
}