小程序授权登陆获取信息
小程序授权登陆获取信息
代码:
package com.dy.getwxinfo; import com.dy.getwxinfo.util.wxGetUser; import com.google.gson.JsonObject; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; import com.jfinal.weixin.sdk.api.ApiResult; import com.jfinal.weixin.sdk.utils.HttpUtils; import java.util.Date; import java.util.HashMap; import java.util.Map; import com.jfinal.wxaapp.api.WxaUserApi; import com.util.*; import com.alibaba.fastjson.JSONObject; public class GetWxPayService { //微信公众号获取用户信息 // public final static String GetPageUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; //微信小程序获取用户信息(地址是固定的) public final static String GetPageUserInfoUrl_XCX = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; // public final static String TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; //app小程序唯一编号 public final static String appID = "wx5756eb8536c5fd81"; //密匙 public final static String appSecret = "566c448f2f7372fe140c09520766dcda"; //微信小程序服务器路径 private static String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?"; //根据前台传的code获取Openid和session_key public Map<String,String> getSessionKeyService(Map<String, String[]> map) { Record record = MapToRecord.getRecord(map); String jsCode = record.get("Code");//获取code String grant_type = "authorization_code"; String params = "appid=" + appID + "&secret=" + appSecret + "&js_code=" + jsCode + "&grant_type=" + grant_type; //向微信服务器发送请求 String data = HttpUtils.get(requestUrl+params); JSONObject json = JSONObject.parseObject(data); //用户的唯一标识(openid) String Openid = String.valueOf(json.get("openid"));//根据code获取 String session_key = String.valueOf(json.get("session_key"));//根据code获取 Map<String,String> data1= new HashMap<>(); data1.put("Openid",Openid); data1.put("session_key",session_key); return data1; } //根据session_key,encryptedData(加密数据),iv获取用户信息 public ReturnData getUserInfoService(Map<String, String[]> map) { Record records = MapToRecord.getRecord(map); String session_key = records.getStr("session_key"); String openid = records.getStr("openid"); String encryptedData = records.getStr("encryptedData"); String iv = records.getStr("iv"); // WxaUserApi wxaUserApi=new WxaUserApi(); // ApiResult apiResult= wxaUserApi.getUserInfo(session_key,encryptedData,iv); JSONObject jsonObject = new GetPhoneNumber().getPhoneNumber(session_key, encryptedData, iv); //String phoneNumber = jsonObject.get("phoneNumber")+""; String avatarUrl = jsonObject.get("avatarUrl").toString(); String nickName = jsonObject.get("nickName").toString(); String sql = "select userId from user_info where openid= ‘" + openid + "‘ "; String userId = Db.queryStr(sql); ReturnData returnData = new ReturnData(); Record record = new Record(); Date date = new Date(); String id = UtilUUID.getUUID(); boolean sa = false; if (userId == null) { record.set("createTime", date).set("upTime", date).set("userName", nickName).set("openId", openid) .set("userPhotos", avatarUrl).set("userPhone", "").set("id", id).set("userId",id); sa = Db.save("user_info", "id", record); returnData.setData(record); returnData.setResult(sa); }else { record.set("upTime", date).set("id",userId).set("userPhotos",avatarUrl).set("userName",nickName); sa = Db.update("user_info", "id", record); returnData.setData(record); returnData.setResult(sa); } return returnData; } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25