微信小程序用户登录与信息获取

发布时间:2025-10-30 01:32

关注超市微信公众号获取活动信息 #生活技巧# #节省生活成本# #购物优惠技巧# #超市促销信息获取#

app.js

1 检查本地是不是有openid,如果没后那么去获取

2 为什么需要openid ?因为在后续对微信的接口的调用会用到,比如支付,比如地图之类的。

const config = require("./utils/config.js")

App({

onLaunch: function () {

let that = this;

that.checkOpenId()

},

checkOpenId:function(){

var openId = wx.getStorageSync("openId");

if(!openId){

this.getOpenId();

}

},

getOpenId:function(){

var that = this;

wx.login({

success(res) {

if(res.code){

wx.request({

url:config.loginUrl,

data:{

code:res.code,

},

success(res){

console.log(res.data.openid);

wx.setStorageSync("openid",JSON.stringify(res.data.openid));

},

fail(){

wx.showToast({

title: '链接失败',

})

}

})

}

},

fail(res){

console.log("登录失败");

}

})

},

})

javascript

运行

首页,里面

1 本地获取userinfo(用户信息),如果不存在,那么显示一个登录面板,点击之后调用微信api 获取用户,

2 把用户信息存储在本地

3 把用户信息发送到自己的服务器存储。

Page({

data: {

canIUse: wx.canIUse('button.open-type.getUserInfo'),

userInfo:false

},

checkUserInfo(){

if (wx.getStorageSync("userInfo")){

this.setData({

userInfo:true,

})

}

},

getUserInfo(e) {

console.log(e.detail);

wx.setStorageSync("userInfo",JSON.stringify(e.detail.userInfo))

this.setData({

userInfo:true

})

},

goDetail(e){

var data = e.currentTarget.dataset;

wx.navigateTo({

url: '../detail/detail?name='+data.name+'&age='+data.age,

})

},

onLoad: function (options) {

this.checkUserInfo()

},

javascript

运行

登录面板

<view class="userInfo" wx:if="{{!userInfo}}">

<button wx:if="{{canIUse}}" open-type="getUserInfo"

bindgetuserinfo="getUserInfo">授权登录</button>

<view wx:else>请升级微信版本</view>

<button>

<navigator open-type="exit" target="miniProgram">取消并退出</navigator>

</button>

</view>

html

后台 java

用 code ,appid 和appsecrt 从腾讯的接口获取 openid 和 sessiojkey

public static String AppSecret = "747e4550714d43499ebe4200da781b91";

public static String appid = "wx1f045119090a0dc0";

@GetMapping("/getOpenId")

public String getOpenId(String code) throws IOException {

System.out.println(code);

return HttpGet(code);

}

public String HttpGet(String code) {

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+AppSecret+"&js_code="+code+"&grant_type=authorization_code");

CloseableHttpResponse response = null;

try {

response = httpClient.execute(httpGet);

HttpEntity responseEntity = response.getEntity();

System.out.println("响应状态为:" + response.getStatusLine());

if (responseEntity != null) {

System.out.println("响应内容长度为:" + responseEntity.getContentLength());

String responseData = EntityUtils.toString(responseEntity);

System.out.println("响应内容为:" + responseData);

return responseData;

}else{

return null;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

return null;

} catch (ParseException e) {

e.printStackTrace();

return null;

} catch (IOException e) {

e.printStackTrace();

return null;

} finally {

try {

httpClient.close();

response.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

java

运行

网址:微信小程序用户登录与信息获取 https://www.yuejiaxmz.com/news/view/1395969

相关内容

微信小程序之登录页
微信小程序vue+nodejs+uniapp校园生活信息服务系统
微信小程序毕业设计
基于php微信小程序购物商城 校园二手商品 图书鲜花商城 毕业设计(3)微信用户登录
基于微信小程序的时间管理小程序
springboot毕设基于微信小程序的睡眠信息管理系统论文+程序+部署
微信小程序获取input框的值
基于微信小程序社区旧衣物品回收系统小程序设计与实现
微信小程序常用控件汇总
微信小程序 iOS type=“nickname”无效

随便看看