<?php
namespace app\index\controller;
use think\facade\Db;

class Wechat
{
    public $app_id = 'wxc25cb3f1f61af313';
    public $app_secret = '720eb3a45a312af00e52d52e39caee09';
    public function get_phone()
    {
        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$this->get_access_token();
        $json_str = $this->curl_post($url, json_encode(['code'=>input('post.code')]));
        $json = json_decode($json_str, true);
        return json(['phone'=>$json['phone_info']['phoneNumber']]);
    }

    public function get_access_token()
    {
        $json_str = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
        $json = json_decode($json_str, true);
        return $json['access_token'];
    }

    public function login_simple()
    {
        $code    = input('post.code');
        $api     = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->app_id}&secret={$this->app_secret}&js_code={$code}&grant_type=authorization_code";
        $json    = json_decode(file_get_contents($api), true);

        $unionid  = isset($json['unionid']) ? $json['unionid'] : '';
        $openid   = isset($json['openid']) ? $json['openid'] : '';
        if($openid) {
            $checknum  = Db::name('user')->where('user_wx_openid', $openid)->find();
            if($checknum == ''){
                // 注册并登录
                $data['user_wx_openid']  = $openid;
                $data['user_wx_unionid'] = $unionid;
                $data['user_nickname']   = '微信用户';
                $data['user_avatar']     = '/no_avator.png';
                $data['user_addtime']    = time();
                $user_id = Db::name('user')->insertGetId($data);
            } else {
                // 直接登陆
                $user_id = $checknum['user_id'];
                Db::name('user')->where('user_id', $checknum['user_id'])->update(['user_wx_unionid'=>$unionid]);
            }
        } else {
            return error('未能获取unionid或openid');
        }
        $user_hash = md5(md5('e81feb5867d6a0c'.$user_id.'e81feb5867d6a0c'));
        $resp['user_id']   = $user_id;
        $resp['user_hash'] = $user_hash;
        return json($resp);
    }

    public function login()
    {
        $code    = input('post.code');
        $api     = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->app_id}&secret={$this->app_secret}&js_code={$code}&grant_type=authorization_code";
        $json    = json_decode(file_get_contents($api), true);

        $unionid = isset($json['unionid']) ? $json['unionid'] : false;
        $openid  = isset($json['openid']) ? $json['openid'] : false;
        $check   = Db::name('user')->where('user_wx_openid', $openid)->find();
        if($check){
            $user_id   = $check['user_id'];
            $user_hash = md5(md5('e81feb5867d6a0c'.$user_id.'e81feb5867d6a0c'));
            $resp['status']    = 1;
            $resp['user_id']   = $user_id;
            $resp['user_hash'] = $user_hash;
            return json($resp);
        } else {
            $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$this->get_access_token();
            $json_str = $this->curl_post($url, json_encode(['code'=>input('post.phone')]));
            $json = json_decode($json_str, true);
            return json(['status'=>0,'phone'=>$json['phone_info']['phoneNumber'],'openid'=>$openid]);
        }
    }

    public function register()
    {
        $data = input("post.");
        $user_id = Db::name('user')->insertGetId($data);
        $user_hash = md5(md5('e81feb5867d6a0c'.$user_id.'e81feb5867d6a0c'));
        $resp['status']    = 1;
        $resp['user_id']   = $user_id;
        $resp['user_hash'] = $user_hash;
        return json($resp);
    }

    public function curl_post($url, $param = array(), $header = array()){
        $ch = curl_init();
        if(is_array($param)){
            $urlparam = http_build_query($param);
        } else if(is_string($param)){
            $urlparam = $param;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam);
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}