<?php

namespace app\controller;

use app\model\EnterpriseUser;

use think\facade\Db;

class WxGetPhone
{
    public function getWxPhone($openid, $code)
    {
        $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . config("wx.AppID") . '&secret=' . config("wx.AppSecret");
        $tmptoken = $this->getCurl($url_get);
        $token = $tmptoken['access_token'];
        
        
        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $token;
        $data['code'] = $code;
        $info = $this->Post(json_encode($data), $url);
        $tmpinfo = json_decode($info, true);


        $phoneNumber = $tmpinfo['phone_info']['phoneNumber'];

        if ($tmpinfo['errcode'] != 0) {
            return json(['code' => 1, 'msg' => $tmpinfo['errmsg']]);
        }

        $table = 'zxc_user';
        $checkPhone = Db::table($table)->where('openid', $openid)->value('phone');
        if (empty($checkPhone)) {
            Db::table($table)->where('openid', $openid)->save(['phone' => $phoneNumber]);
            return json(['code' => 200, 'msg' => '获取手机号成功', 'phone' => $phoneNumber]);
        } else {
            $type = Db::table($table)->where('openid', $openid)->value('type');
            if ($type == 3 && $checkPhone != $phoneNumber) {
                Db::startTrans();
                try {
                    EnterpriseUser::where('openid', $openid)->save(['phone' => $phoneNumber]);
                    Db::table($table)->where('openid', $openid)->save(['phone' => $phoneNumber]);
                    // 提交事务
                    Db::commit();
                    return json(['code' => 200, 'msg' => '更改手机号成功', 'phone' => $phoneNumber]);
                } catch (\Exception $e) {
                    Db::rollback();
                }
            } else {
                Db::table($table)->where('openid', $openid)->save(['phone' => $phoneNumber]);
                return json(['code' => 200, 'msg' => '获取手机号成功', 'phone' => $phoneNumber]);
            }
        }


    }

    private function getCurl($url)
    {
        $headerArray = array("Content-type:application/json;", "Accept:application/json");
        $ch = curl_init();//初始化CURL
        curl_setopt($ch, CURLOPT_URL, $url);//设置访问地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//HTTPS访问设置 关闭监视
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//HTTPS访问设置 关闭监视访问地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);//设置访问头部信息
        $output = curl_exec($ch);//获取结果
        curl_close($ch);//关闭连接
        $output = json_decode($output, true);//json数据转换
        return $output;
    }

    private function Post($curlPost, $url, $ssl = false)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        if (!$ssl) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
}