oauth 授权跳转接口
public getOauthRedirect ( string $callback, $state = '', $scope = 'snsapi_userinfo' ) : string | ||
$callback | string | 回调URI |
return | string |
public function wxoauth() { $scope = 'snsapi_base'; $code = isset($_GET['code']) ? $_GET['code'] : ''; $token_time = isset($_SESSION['token_time']) ? $_SESSION['token_time'] : 0; if (!$code && isset($_SESSION['open_id']) && isset($_SESSION['user_token']) && $token_time > time() - 3600) { if (!$this->wxuser) { $this->wxuser = $_SESSION['wxuser']; } $this->open_id = $_SESSION['open_id']; return $this->open_id; } else { $options = array('token' => $this->options["token"], 'appid' => $this->options["appid"], 'appsecret' => $this->options["appsecret"]); $we_obj = new Wechat($options); if ($code) { $json = $we_obj->getOauthAccessToken(); if (!$json) { unset($_SESSION['wx_redirect']); die('获取用户授权失败,请重新确认'); } $_SESSION['open_id'] = $this->open_id = $json["openid"]; $access_token = $json['access_token']; $_SESSION['user_token'] = $access_token; $_SESSION['token_time'] = time(); $userinfo = $we_obj->getUserInfo($this->open_id); if ($userinfo && !empty($userinfo['nickname'])) { $this->wxuser = array('open_id' => $this->open_id, 'nickname' => $userinfo['nickname'], 'sex' => intval($userinfo['sex']), 'location' => $userinfo['province'] . '-' . $userinfo['city'], 'avatar' => $userinfo['headimgurl']); } elseif (strstr($json['scope'], 'snsapi_userinfo') !== false) { $userinfo = $we_obj->getOauthUserinfo($access_token, $this->open_id); if ($userinfo && !empty($userinfo['nickname'])) { $this->wxuser = array('open_id' => $this->open_id, 'nickname' => $userinfo['nickname'], 'sex' => intval($userinfo['sex']), 'location' => $userinfo['province'] . '-' . $userinfo['city'], 'avatar' => $userinfo['headimgurl']); } else { return $this->open_id; } } if ($this->wxuser) { $_SESSION['wxuser'] = $this->wxuser; $_SESSION['open_id'] = $json["openid"]; unset($_SESSION['wx_redirect']); return $this->open_id; } $scope = 'snsapi_userinfo'; } if ($scope == 'snsapi_base') { $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $_SESSION['wx_redirect'] = $url; } else { $url = $_SESSION['wx_redirect']; } if (!$url) { unset($_SESSION['wx_redirect']); die('获取用户授权失败'); } $oauth_url = $we_obj->getOauthRedirect($url, "wxbase", $scope); header('Location: ' . $oauth_url); } }
public function get_weixininfo($type = 'index') { $agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($agent, "icroMessenger")) { import('Wechat', APP_PATH . 'Common/Wechat', '.class.php'); $config = M("Wxconfig")->where(array("id" => "1"))->find(); $options = array('token' => $config["token"], 'encodingaeskey' => $config["encodingaeskey"], 'appid' => $config["appid"], 'appsecret' => $config["appsecret"], 'partnerid' => $config["partnerid"], 'partnerkey' => $config["partnerkey"], 'paysignkey' => $config["paysignkey"]); $weObj = new Wechat($options); $info = $weObj->getOauthAccessToken(); if (!$info) { $callback = 'http://' . $_SERVER['SERVER_NAME'] . U("App/Index/{$type}", $_GET); $url = $weObj->getOauthRedirect($callback, '', 'snsapi_base'); header("Location: {$url}"); exit; } else { $_SESSION['uid'] = $_POST['uid'] = $_GET['uid'] = $info['openid']; } } if (!empty($_SESSION["uid"]) && empty($_GET['uid'])) { $_GET['uid'] = $_SESSION["uid"]; } }
/** * 微信OAuth操作 */ static function do_oauth() { // 默认公众号信息 $wxinfo = model('Base')->model->table('wechat')->field('id, token, appid, appsecret, oauth_redirecturi, type, oauth_status')->where('default_wx = 1 and status = 1')->find(); if (!empty($wxinfo) && $wxinfo['type'] == 2) { $config['token'] = $wxinfo['token']; $config['appid'] = $wxinfo['appid']; $config['appsecret'] = $wxinfo['appsecret']; // 微信通验证 $weObj = new Wechat($config); // 微信浏览器浏览 if (self::is_wechat_browser() && ($_SESSION['user_id'] === 0 || empty($_SESSION['openid'])) && !empty($wxinfo['oauth_status'])) { if ($_GET['sale']) { session('sale_id', I('get.sale')); } if (!isset($_SESSION['redirect_url'])) { session('redirect_url', __HOST__ . $_SERVER['REQUEST_URI']); } $url = $weObj->getOauthRedirect($wxinfo['oauth_redirecturi'], 1); if (isset($_GET['code']) && !empty($_GET['code'])) { $token = $weObj->getOauthAccessToken(); if ($token) { $userinfo = $weObj->getOauthUserinfo($token['access_token'], $token['openid']); self::update_weixin_user($userinfo, $wxinfo['id'], $weObj); if (!empty($_SESSION['redirect_url'])) { $redirect_url = session('redirect_url'); header('Location:' . $redirect_url, true, 302); exit; } } else { header('Location:' . $url, true, 302); exit; } } else { header('Location:' . $url, true, 302); exit; } } } }
include "../qy-sdk/wechat.class.php"; function _log($text) { //$result = file_put_contents('wechat.log.'.date('Y-m-d'), $text."\n", FILE_APPEND); //if (!$result) { openlog("wechat", LOG_PID | LOG_PERROR, LOG_LOCAL0); syslog(LOG_DEBUG, $text); closelog(); //} } $options = ['token' => 'nizhidaoma', 'encodingaeskey' => 'IXR7m0K8Q7KOIC4z9s1CRHNQBqrJfFGTUT86CECPaXM', 'appid' => 'wx6bc0c53a3a1c150d', 'appsecret' => '3888ccc01ec4e5d6e0e673308e92e627', 'debug' => true, 'logcallback' => '_log']; $pe_url_base = 'https://wechat.gds-services.com'; $pe_url_alarm_view = $pe_url_base . '/dynamic/index_pe.php?bcb=pe_alarm_view'; $pe_url_dc_view = $pe_url_base . '/dynamic/index_pe.php?bcb=pe_dc_view'; $pe_url_dc_report = $pe_url_base . '/dynamic/index_pe.php?bcb=pe_dc_report'; $pe_url_urgent_view = $pe_url_base . '/dynamic/index_pe.php?bcb=pe_urgent_view'; $pe_url_ppm_view = $pe_url_base . '/dynamic/index_pe.php?bcb=pe_ppm_view'; $pe_url_training = $pe_url_base . '/dynamic/index_pe.php?bcb=pe_training'; $pe_url_bind_check = $pe_url_base . '/dynamic/index_pe.php?bcb=user_bind_check_pe'; $pe_url_notice_pre = $pe_url_base . '/dynamic/index_pe.php?bcb=user_notice_prefer_pe'; $weObj = new Wechat($options); $result = $weObj->createMenu(['button' => [0 => ['name' => '运营管理', 'sub_button' => [0 => ['type' => 'view', 'name' => '运营异动监测', 'url' => $weObj->getOauthRedirect($pe_url_alarm_view, '', 'snsapi_base')], 1 => ['type' => 'view', 'name' => '数据中心档案', 'url' => $weObj->getOauthRedirect($pe_url_dc_view, '', 'snsapi_base')]]], 1 => ['name' => '功能中心', 'sub_button' => [0 => ['type' => 'view', 'name' => '场地资源概览', 'url' => $weObj->getOauthRedirect($pe_url_dc_report, '', 'snsapi_base')], 1 => ['type' => 'view', 'name' => '应急管理系统', 'url' => $weObj->getOauthRedirect($pe_url_urgent_view, '', 'snsapi_base')], 2 => ['type' => 'view', 'name' => 'PPM维护保养', 'url' => $weObj->getOauthRedirect($pe_url_ppm_view, '', 'snsapi_base')], 3 => ['type' => 'view', 'name' => '视频培训', 'url' => $weObj->getOauthRedirect($pe_url_training, '', 'snsapi_base')]]], 2 => ['name' => '我的账号', 'sub_button' => [0 => ['type' => 'view', 'name' => '绑定账号', 'url' => $weObj->getOauthRedirect($pe_url_bind_check, '', 'snsapi_base')], 1 => ['type' => 'view', 'name' => '通知设置', 'url' => $weObj->getOauthRedirect($pe_url_notice_pre, '', 'snsapi_base')]]]], $agentid = '3']); echo var_export($result, true); echo '\\n'; if ($result == false) { echo $weObj->errCode; echo '\\n'; echo $weObj->errMsg; echo '\\n'; } die;
public function pay() { $totalprice = $_GET['totalprice']; $cart_names = $_GET['cart_name']; //$openid = $_GET['uid']; $openid = $_SESSION['uid']; $orderid = $_GET['orderid']; $agent = $_SERVER['HTTP_USER_AGENT']; if (!strpos($agent, "icroMessenger")) { $alipay = M("Alipay")->find(); $url = 'http://' . $_SERVER['SERVER_NAME'] . __ROOT__ . '/api/wapalipay/alipayapi.php?WIDseller_email=' . $alipay['alipayname'] . '&WIDout_trade_no=' . $orderid . '&WIDsubject=' . $orderid . '&WIDtotal_fee=' . $totalprice; header("Location: {$url}"); exit; } import('Wechat', APP_PATH . 'Common/Wechat', '.class.php'); $config = M("Wxconfig")->where(array("id" => "1"))->find(); $options = array('token' => $config["token"], 'encodingaeskey' => $config["encodingaeskey"], 'appid' => $config["appid"], 'appsecret' => $config["appsecret"], 'partnerid' => $config["partnerid"], 'partnerkey' => $config["partnerkey"], 'paysignkey' => $config["paysignkey"]); $weObj = new Wechat($options); if (strlen($openid) <= 10) { $info = $weObj->getOauthAccessToken(); if (!$info) { $callback = 'http://' . $_SERVER['SERVER_NAME'] . U("App/Index/pay", $_GET); $url = $weObj->getOauthRedirect($callback, '', 'snsapi_base'); header("Location: {$url}"); exit; } else { $openid = $info['openid']; } } $order_info = M('Order')->where(array('orderid' => $orderid))->find(); if (empty($order_info)) { exit('订单信息错误'); } $cartdata = json_decode($order_info['cartdata'], true); $cart_name = $cartdata[0]['name']; $cart_num = $cartdata[0]['num']; $cart_price = $cartdata[0]['price']; $userdata = M("User")->where(array("uid" => $_SESSION['uid']))->find(); if (empty($userdata)) { exit('用户信息错误'); } $username = $userdata['username']; $phone = $userdata['phone']; $address = $userdata['address']; $this->assign("username", $username); $this->assign("phone", $phone); $this->assign("address", $address); $this->assign("cart_name", $cart_name); $this->assign("cart_num", $cart_num); $this->assign("cart_price", $cart_price); $appid = $options['appid']; $mch_id = $options['partnerid']; $out_trade_no = $orderid; $body = $cart_names; $total_fee = $cart_price * $cart_num * 100; $notify_url = 'http://' . $_SERVER['SERVER_NAME']; $spbill_create_ip = $_SERVER["REMOTE_ADDR"]; $nonce_str = $weObj->generateNonceStr(); $pay_xml = $weObj->createPackageXml($appid, $mch_id, $nonce_str, $body, $out_trade_no, $total_fee, $spbill_create_ip, $notify_url, $openid); $pay_xml = $weObj->get_pay_id($pay_xml); if ($pay_xml['err_code'] == "ORDERPAID") { $this->redirect('App/Index/payover', array('out_trade_no' => $out_trade_no, 'uid' => $_SESSION['uid'])); eixt(); } $prepay_id = $pay_xml['prepay_id']; $jsApiObj["appId"] = $appid; $timeStamp = time(); $jsApiObj["timeStamp"] = "{$timeStamp}"; $jsApiObj["nonceStr"] = $nonce_str; $jsApiObj["package"] = "prepay_id={$prepay_id}"; $jsApiObj["signType"] = "MD5"; $jsApiObj["paySign"] = $weObj->getPaySignature($jsApiObj); $url = json_encode($jsApiObj); $returnUrl = 'http://' . $_SERVER['SERVER_NAME'] . U('App/Index/payover', array('out_trade_no' => $out_trade_no, 'uid' => $_SESSION['uid'])); $info = R("Api/Api/gettheme"); C("DEFAULT_THEME", $info["theme"]); $this->assign("price", $cart_price * $cart_num); $this->assign("info", $info); $this->assign("url", $url); $this->assign("returnUrl", $returnUrl); $this->display(); }
openlog("wechat", LOG_PID | LOG_PERROR, LOG_LOCAL0); syslog(LOG_DEBUG, $text); closelog(); //} } $options = ['token' => 'nizhidaoma', 'encodingaeskey' => 'xc8rRK4iOQjTJK9hOYhRrC9sPBJvF5Jhjq203iMC7vF', 'appid' => 'wxe85832e232e73f41', 'appsecret' => '4c8f1aa381675dd2569fc9482052afdc', 'debug' => true, 'logcallback' => '_log']; $csc_url_base = 'https://wechat.gds-services.com'; $csc_url_submit_srv = $csc_url_base . '/dynamic/index_csc.php?bcb=submit_srv_req'; $csc_url_query_srv = $csc_url_base . '/dynamic/index_csc.php?bcb=query_srv_req'; $csc_url_find_doc = $csc_url_base . '/dynamic/index_csc.php?bcb=extra_docs'; $csc_url_spitslot = $csc_url_base . '/dynamic/index_csc.php?bcb=user_spitslot_1_csc'; $csc_url_complain = $csc_url_base . '/dynamic/index_csc.php?bcb=user_complain_1_csc'; $csc_url_bind_check = $csc_url_base . '/dynamic/index_csc.php?bcb=user_bind_check_csc'; $csc_url_notice_pre = $csc_url_base . '/dynamic/index_csc.php?bcb=user_notice_prefer_csc'; $csc_url_task_list = $csc_url_base . '/dynamic/index_csc.php?bcb=user_tasks'; $csc_url_dc_list = $csc_url_base . '/dynamic/index_csc.php?bcb=user_dcs'; $csc_url_service_neighbour = $csc_url_base . '/dynamic/index_csc.php?bcb=extra_neighbour'; $csc_url_about_us = $csc_url_base . '/dynamic/index_csc.php?bcb=dc_guide'; $csc_url_contact_us = $csc_url_base . '/dynamic/index_csc.php?bcb=extra_contact'; $csc_url_survey = $csc_url_base . '/dynamic/index_csc.php?bcb=survey_1_csc'; $weObj = new Wechat($options); $result = $weObj->createMenu(['button' => [0 => ['name' => '我要', 'sub_button' => [0 => ['type' => 'view', 'name' => '我要提交', 'url' => $weObj->getOauthRedirect($csc_url_submit_srv, '', 'snsapi_base')], 1 => ['type' => 'view', 'name' => '我要查询', 'url' => $weObj->getOauthRedirect($csc_url_query_srv, '', 'snsapi_base')], 2 => ['type' => 'view', 'name' => '我要吐槽', 'url' => $weObj->getOauthRedirect($csc_url_spitslot, '', 'snsapi_base')]]], 1 => ['name' => '我的', 'sub_button' => [0 => ['type' => 'view', 'name' => '我的个人信息', 'url' => $weObj->getOauthRedirect($csc_url_bind_check, '', 'snsapi_base')], 1 => ['type' => 'view', 'name' => '我的通知设置', 'url' => $weObj->getOauthRedirect($csc_url_notice_pre, '', 'snsapi_base')], 2 => ['type' => 'view', 'name' => '我的工作安排', 'url' => $weObj->getOauthRedirect($csc_url_task_list, '', 'snsapi_base')], 3 => ['type' => 'view', 'name' => '我的数据中心', 'url' => $weObj->getOauthRedirect($csc_url_dc_list, '', 'snsapi_base')]]], 2 => ['name' => '随便看看', 'sub_button' => [0 => ['type' => 'view', 'name' => '数据中心集群', 'url' => $weObj->getOauthRedirect($csc_url_about_us, '', 'snsapi_base')], 1 => ['type' => 'view', 'name' => '联系我们', 'url' => $weObj->getOauthRedirect($csc_url_contact_us, '', 'snsapi_base')], 2 => ['type' => 'view', 'name' => '用户体验调研', 'url' => $weObj->getOauthRedirect($csc_url_survey, '', 'snsapi_base')]]]], $agentid = '3']); echo var_export($result, true); echo '\\n'; if ($result == false) { echo $weObj->errCode; echo '\\n'; echo $weObj->errMsg; echo '\\n'; } die;