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); } }
<?php header("Content-Type: text/html; charset=UTF-8"); include "wechat.class.php"; $options = array('token' => 'vonvon', 'encodingaeskey' => 'encodingaeskey', 'appid' => 'wx519f23f4a45e8c37', 'appsecret' => 'ea8f0b17b3a0882bf5fda7ed27758482'); $weObj = new Wechat($options); $data = $weObj->getOauthAccessToken(); //$new = $weObj->getOauthRefreshToken($data['refresh_token']); $info = $weObj->getOauthUserinfo($data['access_token'], $data['openid']); switch ($info['sex']) { case '1': $info['sex'] = '男'; break; case '2': $info['sex'] = '女'; break; default: $info['sex'] = '未知'; } require_once "js/jssdk.php"; $jssdk = new JSSDK("wx519f23f4a45e8c37", "ea8f0b17b3a0882bf5fda7ed27758482"); $signPackage = $jssdk->GetSignPackage(); ?> <!DOCTYPE html><html lang="zh-CN" class="ua-wk ua-win ua-win-nt6 ua-win-nt63"> <!-- date: 2015-07-17 --> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0" /> <title>哆啦A梦:最后的礼物</title> <link rel="shortcut icon" type="image/x-icon" href="http://lxfm-s.malmam.com/images/lvxingfm_favicon.ico" /> <link rel="icon" type="image/x-icon" href="http://lxfm-s.malmam.com/images/lvxingfm_favicon.ico" />
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; } } } }
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(); }
function getToken($url) { //引入微信类库 APP::load(APP_FILE . 'common/class/wechat.class.php'); $weObj = new Wechat(APP::$config['wechat']); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $uri = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $signPackage = $weObj->getJsSign($uri); if (!isset($_GET['code'])) { header("Location: " . $url); exit; } $data = $weObj->getOauthAccessToken(); if (!$data) { header("Location: " . $url); exit; } $userinfo = $weObj->getOauthUserinfo($data['access_token'], $data['openid']); $this->assign('signPackage', $signPackage); $_SESSION['openid'] = $data['openid']; }
<?php 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']; $csc_host_base = 'https://wechat.gds-services.com'; $csc_url_base = 'https://wechat.gds-services.com'; $callback = $_REQUEST['bcb']; $weObj = new Wechat($options); $access_token = $weObj->getOauthAccessToken(); if ($access_token !== false) { $openid = $access_token['openid']; header('Location: ' . $csc_url_base . '/dynamic/user_isbind_pe.php?openid=' . $openid . '&bcb=' . $callback . '&rand=' . rand()); } else { header('Location: ' . $csc_url_base . '/static/html/error_message.html?msg=' . rawurldecode('好像不是从微信访问的呢!') . '&bcb=' . $callback . '&rand=' . rand()); } die;