/** * 处理微信事件 */ protected function handle() { global $db, $ecs; require_once ROOT_PATH . 'includes/lib_passport.php'; $allow_agent = false; $user_info = $db->getRow("SELECT * FROM `wxch_user` WHERE `wxid` = '" . $this->openid . "'"); if (empty($user_info)) { $user_id = register_openid($this->openid, TRUE); echo "user_id={$user_id}"; $allow_agent = true; } elseif ($user_info['subscribe'] == 0) { $db->query("UPDATE `wxch_user` SET subscribe = 1, subscribe_time = " . time() . " WHERE `wxid` = '" . $this->openid . "'"); $user_id = $user_info['uid']; $allow_agent = true; } elseif ($user_info['subscribe'] == 2) { $db->query("UPDATE `wxch_user` SET subscribe = 1 WHERE `wxid` = '" . $this->openid . "'"); $user_id = $user_info['uid']; } if (!empty($this->data['EventKey']) && $allow_agent) { $qrscene = substr($this->data['EventKey'], 8); $agent = $db->getOne("SELECT user_id FROM " . $ecs->table('promotion_agent') . " WHERE wx_qrcode = " . intval($qrscene)); if (!empty($agent)) { $sql = "UPDATE " . $ecs->table('users') . " SET parent_id = {$agent} WHERE user_id = {$user_id}"; $db->query($sql); } } }
function weixin_oauth($callback, $scope = 'BASE') { global $db; $rs = $db->getRow("SELECT * FROM `wxch_config` WHERE `id` = 1"); $param['appid'] = $rs['appid']; $oauth = intval($_REQUEST['oauth']); if ($oauth == 0) { $param['redirect_uri'] = $callback . (strpos($callback, '?') > 0 ? '&' : '?') . 'oauth=1'; $param['response_type'] = 'code'; if ($scope == 'INFO') { $param['scope'] = 'snsapi_userinfo'; } else { $param['scope'] = 'snsapi_base'; } $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect'; ecs_header("Location: {$url}\n"); exit; } elseif ($oauth == 1) { $param['secret'] = $rs['appsecret']; $param['code'] = $_REQUEST['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); $token = json_decode($content, true); $user_info = $db->getRow("SELECT * FROM `wxch_user` WHERE `wxid` = '{$token[openid]}'"); if (empty($user_info)) { //register if (register_openid($token['openid'])) { $user_info = $GLOBALS['user']->get_user_info($user_info['openid']); } else { return false; } } else { //login $user_info = $GLOBALS['user']->get_profile_by_id($user_info['uid']); if (!empty($user_info) && $user_info['status'] == 1) { $GLOBALS['user']->set_session($user_info); $GLOBALS['user']->set_cookie($user_info, TRUE); update_user_info(); // 更新用户信息 update_user_cart(); recalculate_price(); // 重新计算购物车中的商品价格 } else { return false; } } if ($token['scope'] == 'snsapi_userinfo') { $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$token[access_token]}&openid={$token[openid]}&lang=zh_CN"; $content = file_get_contents($url); $info = json_decode($content, true); // 更新微信用户数据 $db->autoExecute('wxch_user', array('nickname' => $info['nickname'], 'sex' => $info['sex'], 'city' => $info['city'], 'country' => $info['country'], 'province' => $info['province'], 'language' => $info['language'], 'headimgurl' => $info['headimgurl'], 'dateline' => time()), 'UPDATE', 'uid = ' . $user_info['uid']); } $_SESSION['openid'] = $token['openid']; return $user_info; } }
$response = array('error' => 1, 'msg' => ''); $openid = getPOST('openid'); $scene_id = intval(getPOST('scene_id')); $parent_id = 0; $account = ''; if ($scene_id > 0) { $get_user = '******' . $db->table('member') . ' where `scene_id`=' . $scene_id; $log->record($get_user); $user = $db->fetchRow($get_user); if ($user) { $log->record_array($user); $parent_id = $user['id']; $account = $user['account']; } } if (register_openid($openid, '', $parent_id)) { $access_token = get_access_token($config['appid'], $config['appsecret']); if (get_user_wechat_info($access_token, $openid)) { $log->record('update user info success'); } $log->record('async user list, add new user with: openid=' . $openid . ',scene_id=' . $scene_id); //发放推广奖励 if ($account != '' && $config['recommend_integral'] > 0) { /* if(add_recommend_integral($account, $config['recommend_integral'], '推荐新用户奖励')) { $log->record('add recommend integral success:'.$account.' increment '.$config['recommend_integral']); } else { $log->record('add recommend integral fail:'.$account.' increment '.$config['recommend_integral']); } */
if (!isset($_SESSION['openid'])) { $_SESSION['openid'] = ''; } if (!isset($_SESSION['account'])) { $_SESSION['account'] = ''; } $code = getGET('code'); $state = getGET('state'); $member_info = null; if ($_SESSION['openid'] == '' && $code != '' && $state == 2048) { $wechat_user = get_user_info($code, $config['appid'], $config['appsecret'], 'userinfo'); if ($wechat_user) { $_SESSION['openid'] = $wechat_user->openid; $get_account = 'select `account` from ' . $db->table('member') . ' where `wx_openid`=\'' . $wechat_user->openid . '\''; if (!$db->fetchOne($get_account)) { $_SESSION['account'] = register_openid($wechat_user->openid, $wechat_user->nickname, 1); } else { $_SESSION['account'] = $db->fetchOne($get_account); } $data = array('wx_headimg' => $wechat_user->headimgurl, 'wx_nickname' => $wechat_user->nickname); $db->autoUpdate('member', $data, '`wx_openid`=\'' . $wechat_user->openid . '\''); //$get_account = 'select `account` from '.$db->table('member').' where `wx_openid`=\''.$wechat_user->openid.'\''; // $_SESSION['account'] = $db->fetchOne($get_account); $get_user_info = 'select * from ' . $db->table('member') . ' where `account`=\'' . $_SESSION['account'] . '\''; $member_info = $db->fetchRow($get_user_info); assign('member_info', $member_info); } else { echo 'ERROR 2048: 获取授权信息失败'; exit; } }