WechatController.php 6.1 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;


use app\portal\model\UserModel;
use cmf\controller\BaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Log;

/**
 * 微信服务器接口
 * Class WechatController
 * @package app\portal\controller
 */
class WechatController extends BaseController
{

    public function index(){
        $app=new Application(config('wechat_config'));
        $server = $app->server;

        $server->setMessageHandler(function ($message) {
            switch ($message->MsgType) {
                case 'event':
                    switch ($message->Event) {
                        case 'subscribe':
                            return '欢迎关注';
                            break;
                        case 'CLICK':
                            $content = $message->EventKey; // 获取key
                            return '收到CLICK事件:'.$content;
                            break;
                        default:
                            return '收到event消息';
                            break;
                    }
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                default:
                    return '收到其它消息';
                    break;
            }
        });

        $response = $server->serve();

        $response->send();
    }

    public function callback(){
        $appId=config('wechat_config.app_id');
        $secret=config('wechat_config.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'];
            $findThirdPartyUser = Db::name("third_party_user")
                ->where('openid', $openid)
                ->where('app_id', $appId)
                ->find();
            if ($findThirdPartyUser) {
                $this->wechatUserLogin($findThirdPartyUser,$openid,$appId);
            }else{
                $this->wechatUserRegister($wechat_user,$openid,$appId);
            }
            $target_url=session('target_url');
            $targetUrl = empty($target_url) ? url('/') : $target_url;
            header('location:'. $targetUrl);
        }else{
            Log::write('获取微信用户数据失败');
            $this->error('获取微信用户数据失败');
        }

    }

    /**
     * 微信用户登录
     * @param $findThirdPartyUser
     * @param $openid
     * @param $appId
     */
    protected function wechatUserLogin($findThirdPartyUser,$openid,$appId){
        $currentTime = time();
        $ip          = $this->request->ip(0, true);
        $token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'public');
        $userData = [
            'last_login_ip'   => $ip,
            'last_login_time' => $currentTime,
            'login_times'     => ['exp', 'login_times+1']
        ];
        $row1=Db::name("third_party_user")
            ->where('openid', $openid)
            ->where('app_id', $appId)
            ->update($userData);
        $userInfo=Db::name("third_party_user")
            ->where('openid', $openid)
            ->where('app_id', $appId)->find();
        unset($userData['login_times']);
        $row2=Db::name("user")
            ->where('id', $userInfo['user_id'])
            ->update($userData);
        if($row1!==false&&$row2!==false){
            $userModel=new \app\user\model\UserModel();
            $user=$userModel->getUserInfo(['user_id'=>$userInfo['user_id'],'app_id'=>$appId]);
            cmf_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([
            'create_time'     => $currentTime,
            'user_status'     => 1,
            'user_type'       => 2,
            'sex'             => $wechat_user['original']['sex'],
            'user_nickname'   => $wechat_user['nickname'],
            'avatar'          => $wechat_user['avatar'],
            'last_login_ip'   => $ip,
            'last_login_time' => $currentTime,
        ]);

        $row=Db::name("third_party_user")->insert([
            'openid'          => $openid,
            'user_id'         => $userId,
            'third_party'     => 'public',
            'nickname'        => $wechat_user['nickname'],
            'app_id'          => $appId,
            'last_login_ip'   => $ip,
            'union_id'        => '',
            'last_login_time' => $currentTime,
            'create_time'     => $currentTime,
            'login_times'     => 1,
            'status'          => 1,
            'more'            => json_encode($wechat_user)
        ]);
        if($userId && $row){
            $token = cmf_generate_user_token($userId, 'public');
            $userModel=new \app\user\model\UserModel();
            $user=$userModel->getUserInfo(['user_id'=>$userId,'app_id'=>$appId]);
            cmf_update_current_user($user);
            session('token',$token);
            Db::commit();
        }else{
            Db::rollback();
        }
    }

}