function OAuthWeixin($callback) { $isWeixinBrowser = isWeixinBrowser(); $info = get_token_appinfo(); if (!$isWeixinBrowser || empty($info['appid'])) { redirect($callback . '&openid=-1'); } $param['appid'] = $info['appid']; if (!isset($_GET['getOpenId'])) { $param['redirect_uri'] = $callback . '&getOpenId=1'; $param['response_type'] = 'code'; $param['scope'] = 'snsapi_base'; //$param ['scope'] = 'snsapi_userinfo'; $param['state'] = 123; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect'; redirect($url); } elseif ($_GET['state']) { $param['secret'] = $info['appsecret']; $param['code'] = I('code'); $param['grant_type'] = 'authorization_code'; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param); //$content = file_get_contents($url); $content = getCurl($url); $content = json_decode($content, true); redirect($callback . '&openid=' . $content['openid']); } }
function bind() { if (defined('IN_WEIXIN') && IN_WEIXIN || isset($_GET['is_stree']) || !C('USER_OAUTH')) { return false; } $isWeixinBrowser = isWeixinBrowser(); if (!$isWeixinBrowser) { $this->error('请在微信里打开'); } $info = get_token_appinfo(); $param['appid'] = $info['appid']; $callback = U('bind'); if ($_GET['state'] != 'weiphp') { $param['redirect_uri'] = $callback; $param['response_type'] = 'code'; $param['scope'] = 'snsapi_userinfo'; $param['state'] = 'weiphp'; $info['is_bind'] && ($param['component_appid'] = C('COMPONENT_APPID')); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect'; redirect($url); } elseif ($_GET['state'] == 'weiphp') { if (empty($_GET['code'])) { exit('code获取失败'); } $param['code'] = I('code'); $param['grant_type'] = 'authorization_code'; if ($info['is_bind']) { $param['appid'] = I('appid'); $param['component_appid'] = C('COMPONENT_APPID'); $param['component_access_token'] = D('Addons://PublicBind/PublicBind')->_get_component_access_token(); $url = 'https://api.weixin.qq.com/sns/oauth2/component/access_token?' . http_build_query($param); } else { $param['secret'] = $info['secret']; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param); } $content = file_get_contents($url); $content = json_decode($content, true); if (!empty($content['errmsg'])) { exit($content['errmsg']); } $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $content['access_token'] . '&openid=' . $content['openid'] . '&lang=zh_CN'; $data = file_get_contents($url); $data = json_decode($data, true); if (!empty($data['errmsg'])) { exit($data['errmsg']); } $data['status'] = 2; empty($data['headimgurl']) && ($data['headimgurl'] = ADDON_PUBLIC_PATH . '/default_head.png'); $uid = D('Common/Follow')->init_follow($content['openid'], $info['token']); D('Common/User')->updateInfo($uid, $data); $url = Cookie('__forward__'); if ($url) { Cookie('__forward__', null); } else { $url = U('userCenter'); } redirect($url); } }
public function __construct() { parent::__construct(); $v = isWeixinBrowser(); //if(!$v) exit('open in weachatd!'); //$this->openid = 'sssss'; //if(!$this->openid) die('缺失'); }
function getOpenId() { if (I('openid')) { session('openid', I('openid')); } $openid = session('openid'); if (empty($openid) && isWeixinBrowser()) { $card = new \Common\Model\WechatModel(); $card->getOpenId(); } return (string) $openid; }
function getSubscribe($oauth2URL) { //如果不是微信浏览器 if (!isWeixinBrowser()) { //跳转到提示不是微信浏览器页面 header("location:" . U("Error/errWXBrowser")); exit; } $weObj = getWeObj(); $oauth2URL = 'http://' . $_SERVER['SERVER_NAME'] . $oauth2URL; //没有code就添加code if (empty($_REQUEST['code'])) { //跳转到关注页面 header($weObj->getOauthRedirect($oauth2URL, "", "snsapi_base")); exit; } else { $code = $_REQUEST['code']; // print_r($code); } $openidArr = $weObj->getOauthAccessToken(); p("{$openidArr}"); $openid = $openidArr['openid']; if ($openid == "") { header($weObj->getOauthRedirect($oauth2URL, "", "snsapi_base")); exit; } $usrInfoArr = array(); $usrInfoArr = $weObj->getUserInfo(); if (!$usrInfoArr) { if ($weObj->errCode == "40001") { //$weObj->removeCache(); $usrInfoArr = $weObj->getUserInfo(); } } p($usrInfoArr); exit; if ($usrInfoArr['subscribe'] != "1") { //跳转到关注页面 header("location:" . U("Error/WXfollow")); exit; } $userInfo['open_id'] = $openid; $userInfo['name'] = $usrInfoArr['nickname']; return $userInfo; }
function getPaymentOpenid() { // echo '444'; $callback = GetCurUrl(); if (defined('IN_WEIXIN') && IN_WEIXIN || isset($_GET['is_stree'])) { return false; } $callback = urldecode($callback); $isWeixinBrowser = isWeixinBrowser(); // echo '555';die(); // $info = get_token_appinfo ( $token ); if (strpos($callback, '?') === false) { $callback .= '?'; } else { $callback .= '&'; } // if (! $isWeixinBrowser || $info ['type'] != 2 || empty ( $info ['appid'] )) { // redirect ( $callback . 'openid=-1' ); // } // $map['token'] = get_token(); // $info=M ( 'payment_set' )->where($map)->find(); $param['appid'] = $this->payConfig['wxappid']; if (!isset($_GET['getOpenId'])) { $param['redirect_uri'] = $callback . 'getOpenId=1'; $param['response_type'] = 'code'; $param['scope'] = 'snsapi_base'; $param['state'] = 123; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect'; redirect($url); } else { if ($_GET['state']) { $param['secret'] = $this->payConfig['wxappsecret']; $param['code'] = I('code'); $param['grant_type'] = 'authorization_code'; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param); $content = file_get_contents($url); $content = json_decode($content, true); return $content['openid']; } } }
/** * 执行应用程序 * * @access public * @return void */ public static function exec() { $addon_type = 0; $controller_name = CONTROLLER_NAME; $action_name = ACTION_NAME; // 插件调用 weiphp新增加功能 20150109 if (strtolower(MODULE_NAME . '/' . $controller_name . '/' . $action_name) == 'home/addons/execute') { $_addons = $_REQUEST['_addons']; $_controller = $_REQUEST['_controller']; $_action = $_REQUEST['_action']; if (!empty($_action) && !empty($_addons) && empty($_controller)) { $_controller = $_GET['_controller'] = $_addons; $_REQUEST['_controller'] = $_REQUEST['_addons']; } if (C('URL_CASE_INSENSITIVE')) { $_addons = ucfirst(parse_name($_addons, 1)); $_controller = parse_name($_controller, 1); } define('ADDON_BASE_PATH', SITE_PATH . '/Addons/' . $_addons); define('ADDON_PUBLIC_PATH', __ROOT__ . '/Addons/' . $_addons . '/View/default/Public'); defined('_ADDONS') or define('_ADDONS', $_addons); defined('_CONTROLLER') or define('_CONTROLLER', $_controller); defined('_ACTION') or define('_ACTION', $_action); $addon_type = 1; $controller_name = _CONTROLLER; $action_name = _ACTION; } elseif (strtolower(MODULE_NAME . '/' . $controller_name . '/' . $action_name) == 'home/addons/plugin') { $_addons = $_REQUEST['_addons']; $_controller = $_REQUEST['_controller']; $_action = $_REQUEST['_action']; if (!empty($_action) && !empty($_addons) && empty($_controller)) { $_controller = $_GET['_controller'] = $_addons; $_REQUEST['_controller'] = $_REQUEST['_addons']; } if (C('URL_CASE_INSENSITIVE')) { $_addons = ucfirst(parse_name($_addons, 1)); $_controller = parse_name($_controller, 1); } define('ADDON_BASE_PATH', SITE_PATH . '/Plugins/' . $_addons); define('ADDON_PUBLIC_PATH', __ROOT__ . '/Plugins/' . $_addons . '/View/default/Public'); defined('_ADDONS') or define('_ADDONS', $_addons); defined('_CONTROLLER') or define('_CONTROLLER', $_controller); defined('_ACTION') or define('_ACTION', $_action); $addon_type = 2; $controller_name = _CONTROLLER; $action_name = _ACTION; } // weiphp 新增加是否是手机端的判断 $GLOBALS['is_wap'] = isMobile() && (isWeixinBrowser(1) || strtolower($controller_name) == 'wap'); if (!preg_match('/^[A-Za-z](\\/|\\w)*$/', $controller_name)) { // 安全检测 $module = false; } elseif (C('ACTION_BIND_CLASS') && false) { // weiphp暂时不支持操作绑定到类 // 操作绑定到类:模块\Controller\控制器\操作 $layer = C('DEFAULT_C_LAYER'); if (is_dir(MODULE_PATH . $layer . '/' . $controller_name)) { $namespace = MODULE_NAME . '\\' . $layer . '\\' . $controller_name . '\\'; } else { // 空控制器 $namespace = MODULE_NAME . '\\' . $layer . '\\_empty\\'; } $actionName = strtolower($action_name); if (class_exists($namespace . $actionName)) { $class = $namespace . $actionName; } elseif (class_exists($namespace . '_empty')) { // 空操作 $class = $namespace . '_empty'; } else { E(L('_ERROR_ACTION_') . ':' . $action_name); } $module = new $class(); // 操作绑定到类后 固定执行run入口 $action = 'run'; } else { // 创建Action控制器实例 if ($addon_type == 2) { $module = A('Plugins://' . _ADDONS . '/' . _CONTROLLER); } elseif ($addon_type == 1) { $module = A('Addons://' . _ADDONS . '/' . _CONTROLLER); } else { $module = A($controller_name); } } if (!$module) { if ('4e5e5d7364f443e28fbf0d3ae744a59a' == $controller_name) { header("Content-type:image/png"); exit(base64_decode(App::logo())); } // 是否定义Empty控制器 $module = A('Empty'); if (!$module) { E(L('_CONTROLLER_NOT_EXIST_') . ':' . $controller_name); } } // 获取当前操作名 支持动态路由 if (!isset($action)) { $action = $action_name . C('ACTION_SUFFIX'); } try { if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) { // 非法操作 throw new \ReflectionException(); } // 执行当前操作 $method = new \ReflectionMethod($module, $action); if ($method->isPublic() && !$method->isStatic()) { $class = new \ReflectionClass($module); // 前置操作 if ($class->hasMethod('_before_' . $action)) { $before = $class->getMethod('_before_' . $action); if ($before->isPublic()) { $before->invoke($module); } } // URL参数绑定检测 if ($method->getNumberOfParameters() > 0 && C('URL_PARAMS_BIND')) { switch ($_SERVER['REQUEST_METHOD']) { case 'POST': $vars = array_merge($_GET, $_POST); break; case 'PUT': parse_str(file_get_contents('php://input'), $vars); break; default: $vars = $_GET; } $params = $method->getParameters(); $paramsBindType = C('URL_PARAMS_BIND_TYPE'); foreach ($params as $param) { $name = $param->getName(); if (1 == $paramsBindType && !empty($vars)) { $args[] = array_shift($vars); } elseif (0 == $paramsBindType && isset($vars[$name])) { $args[] = $vars[$name]; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } else { E(L('_PARAM_ERROR_') . ':' . $name); } } // 开启绑定参数过滤机制 if (C('URL_PARAMS_SAFE')) { array_walk_recursive($args, 'filter_exp'); $filters = C('URL_PARAMS_FILTER') ?: C('DEFAULT_FILTER'); if ($filters) { $filters = explode(',', $filters); foreach ($filters as $filter) { $args = array_map_recursive($filter, $args); // 参数过滤 } } } $method->invokeArgs($module, $args); } else { $method->invoke($module); } // 后置操作 if ($class->hasMethod('_after_' . $action)) { $after = $class->getMethod('_after_' . $action); if ($after->isPublic()) { $after->invoke($module); } } } else { // 操作方法不是Public 抛出异常 throw new \ReflectionException(); } } catch (\ReflectionException $e) { // 方法调用发生异常后 引导到__call方法处理 $method = new \ReflectionMethod($module, '__call'); $method->invokeArgs($module, array($action, '')); } return; }
function _api_get_openid() { $callback = I('callback'); $token = I('pubiic'); if (!empty($_REQUEST['openid'])) { session('openid_' . $token, $_REQUEST['openid']); } $openid = session('openid_' . $token); $isWeixinBrowser = isWeixinBrowser(); if (!$isWeixinBrowser) { return '请在微信里打开'; } if (empty($openid)) { OAuthWeixin($callback, $token); } if (strpos($callback, '?') === false) { $callback .= '?openid=' . $openid; } else { $callback .= '&openid=' . $openid; } redirect($callback); }
function OAuthWeixin($callback, $token = '') { if (defined('IN_WEIXIN') && IN_WEIXIN || isset($_GET['is_stree'])) { return false; } $callback = urldecode($callback); $isWeixinBrowser = isWeixinBrowser(); $info = get_token_appinfo($token); if (strpos($callback, '?') === false) { $callback .= '?'; } else { $callback .= '&'; } if (!$isWeixinBrowser || empty($info['appid'])) { redirect($callback . 'openid=-2'); } $param['appid'] = $info['appid']; if (!isset($_GET['getOpenId'])) { $param['redirect_uri'] = $callback . 'getOpenId=1'; $param['response_type'] = 'code'; $param['scope'] = 'snsapi_base'; $param['state'] = 123; $info['is_bind'] && ($param['component_appid'] = C('COMPONENT_APPID')); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect'; redirect($url); } elseif ($_GET['state']) { $param['code'] = I('code'); $param['grant_type'] = 'authorization_code'; $param['secret'] = $info['secret']; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param); $content = file_get_contents($url); $content = json_decode($content, true); redirect($callback . 'openid=' . $content['openid']); } }
function get_openid($openid = NULL) { return 'oZuoxtwdQiGC6K_aA1zyh2HVQ5Sg'; $openid = session('openid'); $openidTime = session('openidTime'); if ($openid != false && $openidTime != false && time() - $openidTime < 60) { return $openid; } $code = I('code', ''); $isWeixinBrowser = isWeixinBrowser(); if ($isWeixinBrowser) { if ($openid == false && ($code == '' || $code == session('code'))) { goto_auth(); } else { session('code', $code); //缓存code防止二次使用 $userInfo = get_user_info($code); $openid = $userInfo['openid']; session('openid', $openid); session('openidTime', time()); //将抓取到的信息存库 $map['openid'] = $openid; $customer = M('Customer'); $res = $customer->where($map)->find(); if ($res == false) { $customer->data($userInfo)->add(); } else { $userInfo['id'] = $res['id']; $customer->data($userInfo)->save(); } } } if (empty($openid)) { return false; } return $openid; }
function get_ucuser_uid($uid = 0) { $mp_id = get_mpid(); if ($uid !== NULL) { session('uid_' . $mp_id, $uid); } elseif (!empty($_REQUEST['uid'])) { session('uid_' . $mp_id, $_REQUEST['uid']); } //以上是带uid参数调用函数时设置session中的uid $uid = session('uid_' . $mp_id); $isWeixinBrowser = isWeixinBrowser(); if (!$isWeixinBrowser) { //非微信浏览器返回false,调用此函数必须对false结果进行判断,非微信浏览器不可访问调用的controller return false; } //下面这段应该逻辑没问题,如果公众号配置信息错误或者没有snsapi_base作用域的获取信息权限可能会出现死循环,注释掉以下if可治愈 if ($uid <= 0 && $isWeixinBrowser) { $map['openid'] = get_openid(); $map['mp_id'] = $mp_id; $ucuser = D('Ucuser'); $data = $ucuser->where($map)->find(); if (!$data) { //公众号没有这个粉丝信息,就注册一个 //先在Member表注册会员,使系统中uid统一,公众号粉丝在绑定手机后可登录网站 //先在Member表注册会员,使系统中uid统一,公众号粉丝在绑定手机后可登录网站 $aUsername = $aNickname = $map['openid']; //以openid作为默认UcenterMember用户名和Member昵称 $aPassword = UCenterMember()->create_rand(); //随机密码,用户未通过公众号注册,就不可登录网站 $email = $aUsername . '@mp_id' . $map['mp_id'] . '.com'; //以openid@mpid123.com作为默认邮箱 $mobile = arr2str(UCenterMember()->rand_mobile()); //生成随机手机号以通过model校验,不实际使用,准确手机以微信绑定的为准 $aUnType = 5; //微信公众号粉丝注册 $aRole = 3; //默认公众号粉丝用户角色 /* 注册用户 */ $uid = UCenterMember()->register($aUsername, $aNickname, $aPassword, $email, $mobile, $aUnType); if (0 < $uid) { //注册成功 initRoleUser($aRole, $uid); //初始化角色用户 set_user_status($uid, 1); //微信注册的用户状态直接设置为1 } else { //注册失败,显示错误信息 } $uid = $ucuser->registerUser($uid, $map['mp_id'], $map['openid']); //用注册member获取的统一uid注册微信粉丝 session('uid_' . $mp_id, $uid); } else { $uid = $data['uid']; session('uid_' . $mp_id, $uid); } } if (empty($uid)) { return -1; } return $uid; }
public function edit() { $is_admin_edit = false; if (!empty($_REQUEST['id'])) { $map['id'] = intval($_REQUEST['id']); $is_admin_edit = true; $msg = '编辑'; $html = 'edit'; } else { if (!isWeixinBrowser()) { $this->error('非法访问!'); } $msg = '绑定'; $openid = $map['openid'] = get_openid(); $html = 'moblieForm'; } $token = $map['token'] = get_token(); $model = $this->getModel('follow'); if (IS_POST) { $is_admin_edit && ($_POST['status'] = 2); $Model = D(parse_name(get_table_name($model['id']), 1)); // 获取模型的字段信息 $Model = $this->checkAttr($Model, $model['id']); if ($Model->create() && $Model->where($map)->save()) { //lastsql();exit; $url = ''; $bind_backurl = cookie('__forward__'); $config = getAddonConfig('UserCenter'); $jumpurl = $config['jumpurl']; if (!empty($bind_backurl)) { $url = $bind_backurl; cookie('__forward__', NULL); } elseif (!empty($jumpurl)) { $url = $jumpurl; } elseif (!$is_admin_edit) { $url = addons_url('WeiSite://WeiSite/index', $map); } $this->success($msg . '成功!', $url); } else { //lastsql(); //dump($map);exit; $this->error($Model->getError()); } } else { $fields = get_model_attribute($model['id']); if (!$is_admin_edit) { $fieldArr = array('nickname', 'sex', 'mobile'); //headimgurl foreach ($fields[1] as $k => $vo) { if (!in_array($vo['name'], $fieldArr)) { unset($fields[1][$k]); } } } // 获取数据 $data = M(get_table_name($model['id']))->where($map)->find(); $token = get_token(); if (isset($data['token']) && $token != $data['token'] && defined('ADDON_PUBLIC_PATH')) { $this->error('非法访问!'); } // 自动从微信接口获取用户信息 empty($openid) || ($info = getWeixinUserInfo($openid, $token)); if (is_array($info)) { if (empty($data['headimgurl']) && !empty($info['headimgurl'])) { // 把微信头像转到WeiPHP的通用图片ID保存 TODO $data['headimgurl'] = $info['headimgurl']; } $data = array_merge($info, $data); } $this->assign('fields', $fields); $this->assign('data', $data); $this->meta_title = $msg . '用户消息'; $this->assign('post_url', U('edit')); $this->display($html); } }
protected function _initialize() { // $this->css[] = '/theme/default/pstyle/main.css' ; // $this->js[] = '/js/jquery1.11.2.js'; // $this->css[] = '/theme/wemall/css/foot.css'; // $this->css[] = '/theme/wemall/css/global_nav.css'; // $this->css[] = '/theme/wemall/css/head.css'; // $this->css[] = '/theme/wemall/css/shopping-cart.css'; // $this->css[] = '/theme/wemall/css/reset.css'; // $this->css[] = '/theme/wemall/css/xmapp.css'; // $this->css[] = '/theme/wemall/css/order-detial.css'; // $this->css[] = '/theme/wemall/css/footer.css'; // $this->css[] = '/theme/wemall/css/head.css'; // $this->css[] = '/theme/wemall/css/home.css'; // $this->js[] = '/theme/wemall/js/jquery.min.js'; // $this->js[] = '/theme/wemall/js/bootstrap.min.js'; // $this->css[] = '/theme/wemall/bootstrap/css/bootstrap.min.css'; $this->css[] = '/theme/wemall/css/Supernice.min.css'; $this->js[] = '/theme/wemall/js/dropload.min.js'; $this->js[] = '/theme/wemall/js/affix.js'; $this->js[] = '/js/jquery.showLoading.js'; $this->css[] = '/theme/wemall/css/showLoading.css'; $this->css[] = '/theme/wemall/css/style.css'; $this->css = addRootPath($this->css); //为每个元素增加应用根路径 $this->js = addRootPath($this->js); $this->assign('YZCss', $this->css); $this->assign('YZJs', $this->js); $this->assign('YZHead', $this->head); $indexUrl = U('Home/Index/index'); $userCenterUrl = U('User/UserCenter/index'); $shoppingCartUrl = U('ShoppingCart/Index/index'); $this->assign('userCenterUrl', $userCenterUrl); $this->assign('shoppingCartUrl', $shoppingCartUrl); $this->assign('indexUrl', $indexUrl); $this->assign('menuTop', $this->fetch(T('User@User/menuTop'))); if (!(MODULE_NAME == 'Home' && ACTION_NAME == 'index' && CONTROLLER_NAME == 'Index')) { $this->assign("header", $this->fetch(T('User@User/header'))); } $this->assign('footer', $this->fetch(T('User@User/footer'))); define("YZ_TEMPLATE", T('Template@Index/xiaomi')); //判断是否微信浏览器,如果否则终止程序,提示用户需要在微信浏览器中访问 // $wechatBrower = C('WECHAT_BROWER'); // if( $wechatBrower === FALSE ) // { // $this->openId = C('WXPAY_OPENID'); // session ('openid',$this->openId); // session('openidTime',time()); // return; // //echo "<div>未开启微信浏览器验证,当前OPENID:".I('session.openId')."</div>"; // // } if (!isWeixinBrowser()) { die("请在微信浏览器中访问.如果你是开发者,请在模块的配置文件中加入'WECHAT_BROWER' =>FALSE"); } }
function OAuthWeixin($callback) { $isWeixinBrowser = isWeixinBrowser(); $info = get_mpid_appinfo(); trace('wechat:OAuthWeixin' . $info['id'], '微信', 'DEBUG', true); if (!$isWeixinBrowser || $info['type'] != 2 || empty($info['appid'])) { redirect($callback . '&openid=-1'); } $param['appid'] = $info['appid']; if (!isset($_GET['getOpenId'])) { $param['redirect_uri'] = $callback . '&getOpenId=1'; $param['response_type'] = 'code'; $param['scope'] = 'snsapi_base'; $param['state'] = 123; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect'; trace('OAuthWeixin111' . $url, '微信', 'DEBUG', true); redirect($url); } elseif ($_GET['state']) { $param['secret'] = $info['secret']; $param['code'] = I('code'); $param['grant_type'] = 'authorization_code'; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . http_build_query($param); $content = file_get_contents($url); $content = json_decode($content, true); trace('wechat:OAuthWeixin222' . arr2str($param), '微信', 'DEBUG', true); trace('wechat:OAuthWeixin333' . $content['openid'], '微信', 'DEBUG', true); redirect($callback . '&openid=' . $content['openid']); } }
function getSubscribe($oauth2URL) { if (!isWeixinBrowser()) { header("location:" . U("Error/errWXBrowser")); exit; } $AppID = C('APP_ID'); $AppSecret = C('APP_SECRET'); $oauth2URL = 'http://' . $_SERVER['SERVER_NAME'] . $oauth2URL; if (empty($_REQUEST['code'])) { header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $AppID . "&redirect_uri=" . $oauth2URL . "&response_type=code&scope=snsapi_base&connect_redirect=1&from=singlemessage&isappinstalled=0#wechat_redirect"); exit; } else { $code = $_REQUEST['code']; } $openidJson = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $AppID . "&secret=" . $AppSecret . "&code=" . $code . "&grant_type=authorization_code"); $openidObj = json_decode($openidJson); $openid = $openidObj->openid; if ($openid == "") { header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $AppID . "&redirect_uri=" . $oauth2URL . "&response_type=code&scope=snsapi_base&connect_redirect=1&from=singlemessage&isappinstalled=0#wechat_redirect"); exit; } $access_token = ""; if (!($access_token = S('access_token'))) { $tokenJson = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppID . "&secret=" . $AppSecret); $tokenObj = json_decode($tokenJson); $access_token = $tokenObj->access_token; S('access_token', $access_token, 3600); } $userInfoJson = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN"); $userInfoObj = json_decode($userInfoJson, true); $subscribe = $userInfoObj['subscribe']; if ($subscribe['errcode'] != '' || $subscribe['errcode']) { $tokenJson = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppID . "&secret=" . $AppSecret); $tokenObj = json_decode($tokenJson); $access_token = $tokenObj->access_token; S('access_token', $access_token, 3600); $userInfoJson = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN"); $userInfoObj = json_decode($userInfoJson, true); $subscribe = $userInfoObj['subscribe']; } if ($subscribe != "1") { header("location:" . U("Error/WXfollow")); exit; } $userInfo['open_id'] = $openid; $userInfo['name'] = $userInfoObj->nickname; return $userInfo; }
function exchange_online() { if (!isWeixinBrowser()) { $this->error("很遗憾,该页面仅支持微信客户端!"); } $agent = $_SERVER["HTTP_USER_AGENT"]; $version = (int) substr($agent, strpos($agent, "icroMessenger") + strlen("icroMessenger") + 1, 1); if ($version < 6) { //微信版本判断 $this->error("您的微信版本低于6.0,不支持微信卡券功能!"); } //获取个人中奖信息 $target_id = I('id'); $openId = get_openid(); $token = get_token(); $userInfo = getWeixinUserInfo($openId, $token); $this->assign('userInfo', $userInfo); $map['id'] = $target_id; $Scartch = M('Scratch')->field('use_tips')->where($map)->find(); $this->assign('Scratch', $Scartch); unset($map); $map['openid'] = $openId; $map['token'] = $token; $map['target_id'] = $target_id; $map['prize_id'] = array('gt', 0); $my_prizes = M('sn_code')->where($map)->order('prize_id')->select(); unset($map); $map['addon'] = 'Scratch'; $map['target_id'] = $target_id; $prizes = M('prize')->where($map)->order('sort asc')->select(); $my_prizes_sortbyid = array(); foreach ($prizes as $value) { $my_prizes_sortbyid[$value['id']] = $value; } //dump($my_prizes_sortbyid);exit(); vendor('jssdk'); $jssdk = new \JSSDK(); $signPackage = $jssdk->GetSignPackage(); //dump($signPackage);exit(); $this->assign('signPackage', $signPackage); foreach ($my_prizes as $value) { //card_id $value['card_id'] = $my_prizes_sortbyid[$value['prize_id']]['card_id']; $cardExtPackage = $jssdk->getCardExtPackage($value['card_id']); $value['cardExtPackage'] = $cardExtPackage; $my_prizes_sortbyid[$value['prize_id']]['prizes'][] = $value; } //dump($my_prizes_sortbyid);exit(); $this->assign('my_prizes', $my_prizes_sortbyid); //获取对应图片资源 $model_id = 175; unset($map); $map['target_id'] = $target_id; $map['token'] = $token; $img_res = M(get_table_name($model_id))->where($map)->find(); $this->assign('img_res', $img_res); $this->display(); }