1、获取公众号信息,如上图显示“获取失败”
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
buttonDisabled: false,
modalHidden: true,
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
gongMessage: [], //存储公众号所有信息
inputContent: '',
inputContentxml: '',
id:'',
lock: false,
},
//事件处理函数 登录日志
bindViewTap: function() {
wx.navigateTo({
})
},
//跳转文章页面
to_articel_list: function (event) {
//检查锁
if (this.data.lock) {
return;
}
var that = this;
var index = event.currentTarget.dataset.index;
var g_id = that.data.gongMessage[index].id
console.log("公众号id为:" + g_id)
wx.navigateTo({
url: '../../pages/artical/artical?g_id=' + g_id
})
},
touchend: function () {
var that = this;
if (that.data.lock) {
//开锁
console.log("离开")
that.setData({
lock: false
});
}
},
// 长按复制公众号名称
copy: function (event) {
//锁住
this.setData({ lock: true });
var that = this;
var index = event.currentTarget.dataset.index;
console.log(event);
wx.setClipboardData({
data: that.data.gongMessage[index].name,
success: function (res) {
wx.showToast({
title: '复制成功',
});
}
});
},
onLoad: function () {
var that = this;
// 如果获取到用户信息就存储
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
wx.setStorageSync('username', that.data.userInfo.nickName)
wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
console.log("在index页面全局app1中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
} else if (this.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
// console.log("用户名2:" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
// wx.setStorageSync('username', res.userInfo.nickName)
// wx.setStorageSync('headpath', res.userInfo.avatarUrl)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
wx.setStorageSync('username', that.data.userInfo.nickName)
wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
console.log("在index页面全局app2中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
}
} else {
// 没有获取到用户信息就发起授权窗口
wx.getUserInfo({
success: res => {
// console.log("用户名3:" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
wx.setStorageSync('username', that.data.userInfo.nickName)
wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
console.log("在index页面全局app3中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
},
})
}
// wx.setStorageSync('username', that.data.userInfo.nickName)
// wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
//获取公众号信息
that.getGongInfo();
},
//点击按钮授权
getUserInfo: function (e) {
var that = this;
if (e.detail.userInfo) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
wx.setStorageSync('username', that.data.userInfo.nickName)
wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
console.log("在index页面临时授权中获取到的用户信息为:" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
} else {
console.log('用户取消授权');
return;
}
},
//获取公众号信息
getGongInfo: function () {
var that = this;
wx.request({
url: 'http://localhost:8080/my/index.jsp', // url: '自己服务器API', //获取公众号信息
data: {
},
header: {
'content-type': 'application/json' // 数据格式(默认值)
},
method: 'post', //上传方式
success: function (res) { //回调成功
console.log(res.data.result)
if (res.statusCode == 200) {
if (res.data.result == '1') {
var posts_content = res.data;
console.log(posts_content)
that.setData({
gongMessage: res.data.content,
})
} else {
wx.showToast({
title: '获取失败',
icon: 'none',
})
}
} else {
wx.showModal({
title: '服务器错误',
content: 'none',
})
}
},
//回调失败
fail: function (res) {
console.log(res.errMsg)
wx.showToast({
title: '联网失败',
icon: 'fail',
})
},
})
},
//弹出回复框
showModal: function (event) {
this.setData({
modalHidden: !this.data.modalHidden,
})
},
// 获取弹出框密码
getInputContent: function (e) {
console.log(e.detail.value)
this.setData({
inputContent: e.detail.value
})
},
//确定
modalBindaconfirm: function () {
var that = this;
console.log(that.data.inputContent)
if (that.data.inputContent == "") {
wx.showToast({
title: '秘钥不能为空!',
icon: 'none',
})
} else {
//通过秘钥换取公众号 id
that.getIdentifyId();
}
},
//取消
modalBindcancel: function () {
this.setData({
modalHidden: !this.data.modalHidden,
})
},
//通过后台秘钥获取公众号 id
getIdentifyId: function () {
var that = this;
wx.request({
url: 'http://localhost:8080/my/index.jsp', //url: '自己服务器API', //获取公众号信息
data: {
password: that.data.inputContent //登录后台密码
},
header: {
'content-type': 'application/json' // 数据格式(默认值)
},
method: 'post', //上传方式
success: function (res) { //回调成功
console.log(res.data)
if (res.statusCode == 200) {
if (res.data.result == '1') {
//弹出框消失
that.setData({
modalHidden: !that.data.modalHidden,
buttonDisabled: !that.data.buttonDisabled,
id: res.data.content[0].id,
})
//上传openid
// that.postOpenid();
//跳转后台管理界面
wx.navigateTo({
url: '../../pages/select/select?id=' + that.data.id + "&avatarUrl=" + that.data.userInfo.avatarUrl + "&nickName=" + that.data.userInfo.nickName + "&headpath=" + res.data.content[0].headpath + "&name=" + res.data.content[0].name + "&describe=" + res.data.content[0].describes
})
} else {
wx.showToast({
title: '你不是管理员或者密码错误',
icon: 'none',
})
}
} else {
wx.showModal({
title: '服务器错误',
content: 'none',
})
}
},
//回调失败
fail: function (res) {
console.log(res.errMsg)
wx.showToast({
title: '联网失败',
icon: 'fail',
})
},
})
},
// 上传管理员openid
postOpenid: function () {
var that = this;
wx.request({
url: 'http://localhost:8080/my/index.jsp', //url: '自己服务器API', //获取公众号信息
data: {
openid: wx.getStorageSync('openid'),
id: that.data.id
},
header: {
'content-type': 'application/json' // 数据格式(默认值)
},
method: 'post', //上传方式
success: function (res) { //回调成功
console.log(res.data.result)
if (res.statusCode == 200) {
if (res.data.result == '1') {
console.log("openid加入成功!")
} else {
console.log("openid加入失败!")
}
} else {
wx.showModal({
title: '服务器错误',
content: 'none',
})
}
},
//回调失败
fail: function (res) {
console.log(res.errMsg)
wx.showToast({
title: '联网失败',
icon: 'fail',
})
},
})
},
//下拉刷新
onPullDownRefresh: function () {
var that = this;
//在标题栏中显示加载图标
wx.showNavigationBarLoading();
//获取公众号信息
that.getGongInfo();
//完成停止加载
wx.hideNavigationBarLoading();
//停止下拉刷新
wx.stopPullDownRefresh();
},
//wx.showNavigationBarLoading(); //在标题栏中显示加载图标
// wx.hideNavigationBarLoading(); //完成停止加载
// 动态设置导航条标题
// wx.stopPullDownRefresh(); //停止下拉刷新
//后天切换到前台调用 onShow
onShow:function () {
}
})
app.js
//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
//获取openid
var user = wx.getStorageSync('user') || {};
if (!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) { //不要在30天后才更换openid-尽量提前 10 分钟更新
wx.login({
success: function (res) {
// success
// var d = that.globalData.wxData;//这里存储了appid、secret、token串
// var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appId + '&secret=' + d.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code';http://192.168.43.205:8080
wx.request({
url: 'http://localhost:8080/my/index.jsp', //url: '自己服务器API',//获取唯一标识 openId
data: {
code: res.code
},
header: {
'content-type': 'application/json' // 数据格式(默认值)
},
method: 'post', //上传方式
success: function (res) {
console.log(res.data)
wx.setStorageSync('token', res.data.token)//消息模板会用到
wx.setStorageSync('openid', res.data.openid);//存储openid
}
});
}
});
} else {
wx.showToast({
title: '登录失败',
icon: 'none',
})
console.log(user);
return;
}
//查看当前用户是否已经授权
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 授权成功后,直接将信息传到全局变量中
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}else{
return;
}
}
})
},
globalData: {
userInfo: null
}
})
第二个bug见下图:
后台的代码见:
AppIDCodeServlet.java
package com.xiaolu.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xiaolu.ThreadGetdata.TokenThread;
import com.xiaolu.dao.Tools;
import com.xiaolu.util.AccessTokenUtil;
import net.sf.json.JSONObject;
/**
* 功能:获取小程序的AppID和Code
* @author Boy Baby
*
*/
@WebServlet("/AppIDCodeServlet")
public class AppIDCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String appID = "wx1110d4dd4fdaddb92b"; // 填写你小程序的 APPid
public static final String appScret = "4cc6811127335ae83325c4d40a75c1c3ab3";// 你小程序的 AppScret(微信小程序官网获取)
public static String openid = "";
public static String token = "";
public AppIDCodeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//启动定时获取access_token的线程
// new Thread(new TokenThread()).start();
//接受数据
StringBuffer sbJson = new StringBuffer();
String line = "";
try{
//读取网络传输过来的数据
BufferedReader br = request.getReader();
//遍历数据并拼接
while((line = br.readLine())!= null){
//如果读到数据,把他添加到字符流中
sbJson.append(line);
}
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("client json data =" + sbJson);
// 功能:把读取到的数据封装 JSON
System.out.println("JSON = "+ sbJson.toString());
JSONObject jsonobject = JSONObject.fromObject(sbJson.toString());
String code = jsonobject.getString("code");//js_code
openid = AccessTokenUtil.getOpenid(appID, appScret,code);
token = TokenThread.access_token.getAccess_token();
System.out.println("微信API-openid:"+openid+ "token:" + token);
if(!token.equals("")&&!openid.equals("")){
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("result", "1");
jsonObject2.put("openid", openid);
jsonObject2.put("token", token);
String str = jsonObject2.toString();
System.out.print("openid"+openid);
response.getWriter().write(str);
}else{
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("result", "0");
String str = jsonObject2.toString();
response.getWriter().write(str);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
https://blog.csdn.net/qq_36903042/article/details/90632714
上面的程序我是参考:微信小程序 — — 实现微信公众号留言功能(附前、后端源码)