一. 前端页面js
1.获取页面传来的数据
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
2.引用
var code = GetQueryString("code");
3.准备
(1).对地址进行UrlEncode编码
var url = encodeURIComponent("http://hamiwangluo.cn/hdw/web/index.html");
// 注意(1): 如果有业务需求,防止页面刷新,业务数据丢失,可以在连接后加参
// 例如:"http://hamiwangluo.cn/hdw/web/index.html?username=wanglei"
// 注意(2):此处的url不加 www、端口
(2).微信公众号的appid
(3).微信API
window.location.href ="https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=APPID&redirect_uri=URL&response_type=code" +
"&scope=snsapi_userinfo&state=STATE#wechat_redirect";
注意: APPID(大写) 替换为公众号appid、URL(大写) 替换为 上述(1)的url.
4.获取code
getCode();
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
function getCode(){
var code = GetQueryString("code");
if(code==null||code==""){
var url = encodeURIComponent("http://hamikeji.cn/hdw/web/index.html);
window.location.href ="https://open.weixin.qq.com/c