<?php
namespace app\api\controller\h5;

use app\hander\HelpHander;
use think\Controller;
use think\Db;
use think\Exception;

class Oauth extends Controller
{

    public function checkCode(){
        try{
            $type = input('type/d',1); //1=微信公众号 2=微信小城序
            $code = input('code','','trim');

            $openid = "";
            $avatar = "";
            $nickname = "";

            if($type == 1){
                $config = config('app.wx_config');
                $app = \EasyWeChat\Factory::officialAccount($config);
                $user = $app->oauth->user();
                $openid = $user->id;
                $avatar = $user->avatar;
                $nickname = $user->nickname;
            }else if($type == 2){
                $config = config('app.wx_mini_config');
                $app = \EasyWeChat\Factory::miniProgram($config);
                $ret = $app->auth->session($code);
                if(!isset($ret['openid']) || !$ret['openid']){
                    throw new Exception('登录失败');
                }
                $openid = $ret['openid'];
            }

            if(!$openid){
                \exception('参数错误');
            }

            $info = Db::name('wxuser')->where('type',$type)->where('openid',$openid)->find();

            if($info){
                $token = create_token($info['id']);
                $userId = $info['id'];
                if($info['del'] != 0||$info['enable'] != 1){
                    \exception('用户被禁用,请联系管理员');
                }
                $ret = Db::name('wxuser')->where('id',$info['id'])->update([
                    'nickname' => $nickname,
                    'token' => $token,
                    'img' => $avatar,
                    'update_time' => date('Y-m-d H:i:s')
                ]);
                if(!$ret){
                    \exception('登录失败');
                }
            }else{
                $ret = Db::name('wxuser')->insertGetId([
                    'openid' => $openid,
                    'nickname' => $nickname,
                    'img' => $avatar,
                    'type' => $type,
                    'create_time' => date('Y-m-d H:i:s'),
                    'update_time' => date('Y-m-d H:i:s')
                ]);
                if(!$ret){
                    \exception('注册失败');
                }
                $userId = $ret;
                $token = create_token($userId);
                $ret = Db::name('wxuser')->where('id',$userId)->update([
                    'token' => $token,
                ]);
                if(!$ret){
                    \exception('登录失败');
                }

            }
        }catch (Exception $e){
            trace('微信登录:'.$e->getMessage());
            HelpHander::error('登录失败');
        }
        HelpHander::success(['token'=>$token,'user_id' => $userId]);
    }

    // 获取jssdk参数
    public function jssdk(){
        try{
            $url = input('url');
            $config = config('app.wx_config');
            $app = \EasyWeChat\Factory::officialAccount($config);
            $apis = ['checkJsApi','chooseWXPay','hideOptionMenu'];
            $app->jssdk->setUrl(urldecode($url));
            $ret = $app->jssdk->buildConfig($apis, false, false, false);
            trace($ret);
        }catch (\Exception $e){
            trace('获取分享参数:'.$e->getMessage());
            HelpHander::error('获取jssdk配置失败');
        }
        HelpHander::success($ret);
    }

}