架构之美
架构之美,如蓝图之于建筑师,图画之于画家
微信开放平台 - 网页登录

首先要搞清楚微信开放平台 和 微信公众平台是两个东西。

微信开放平台的文档地址:

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN

网页登录首先需要申请 网页应用的,申请完后使用网页应用的appid 和 appsecret调用接口完成用户登录,具体的微信用户登录过程:

1、服务器设置参数跳转到微信网页,微信网页展示二维码

2、用户微信扫描二维码,确认登录

3、微信回调 第1步服务器设置参数的回调地址,回调的url会带上code参数

4、服务器根据code参数获取openid(至此登录完成)

可以根据openid 获取用户的详细信息,写入数据库,或者其他逻辑等;


php源代码:

<?php

namespace Home\Controller;
use Think\Controller;

/**
 * 微信网站应用 趣味中国字 网页登录
 */
class WxwebappController extends Controller {
	private $cfg = [
		'AppID' => '网页应用id',
		'AppSecret' => '网页应用secret',
	];
	function index() {echo 'hi';}
	// 网页授权
	function webauth() {
		if (!isset($_GET["u"])) {echo '回调地址不能为空;';return;}
		$usr = $this->auth_our($scope);

		if (!$usr || !is_array($usr)) {
			echo $usr;return;
		}

		$this->addAuthUser($usr); // 写入数据库
		foreach ($usr as $k => $v) {
			setcookie('swxusr_' . $k, urlencode($v), time() + 30 * 24 * 3600, '/', 'cleey.com');
		}
		$url = $_GET['u'];
		Header('Location:' . $url);exit;
	}

	// 获取 code
	public function auth_our() {
		$code = $_GET["code"];
		if (!$code) {
			// 授权并返回CODE
			$AppID = $this->cfg['AppID'];
			$AppSecret = $this->cfg['AppSecret'];
			$REDIRECT_URI = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
			$url = "https://open.weixin.qq.com/connect/qrconnect?appid={$AppID}&redirect_uri={$REDIRECT_URI}&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect";
			Header('Location:' . $url);exit;
		} else {
			$AppID = $this->cfg['AppID'];
			$AppSecret = $this->cfg['AppSecret'];
			$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$AppID}&secret={$AppSecret}&code={$code}&grant_type=authorization_code";
			$auth = file_get_contents($url);
			$auth = json_decode($auth, true);
			$access_token = $auth['access_token'];
			$openid = $auth['openid'];
			$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}";
			$usr = file_get_contents($url);
			$usr = json_decode($usr, true);
			if (!array_key_exists('openid', $usr)) {
				return ('微信服务器错误,刷新重试2');
			}
			return $usr;
		}
	}

	protected function addAuthUser($usr) {
		if (!$usr['openid']) {
			return;
		}

		$wx_auth_user = M('weixin_user');
		$one = $wx_auth_user->where(['openid' => $usr['openid']])->select();
		if ($one) {
			return;
		}

		if ($usr['unionid']) {
			$one = $wx_auth_user->where(['unionid' => $usr['unionid']])->select();
			if ($one) {
				return;
			}
		}

		$info = array();
		$info['openid'] = $usr['openid'];
		$info['nickname'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $usr['nickname']);
		$info['sex'] = $usr['sex'];
		$info['language'] = $usr['language'];
		$info['country'] = $usr['country'];
		$info['province'] = $usr['province'];
		$info['city'] = $usr['city'];
		$info['headimgurl'] = $usr['headimgurl'];
		$info['unionid'] = $usr['unionid'];
		$info['auth_time'] = time();

		$wx_auth_user->add($info);
	}
}
<< 上一篇 支付宝支付接口集成 Solr 中导入并索引mysql数据 下一篇 >>
文章标签
随意 | Created At 2014 By William Clinton | Contact Me: 1619488373@qq.com | 蜀ICP备14002619号 |