小程序授权登陆获取信息

Posted yu-si

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小程序授权登陆获取信息相关的知识,希望对你有一定的参考价值。

小程序授权登陆获取信息

代码:

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;
    }

}

以上是关于小程序授权登陆获取信息的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序登录流程

微信小程序+php 授权登陆,完整代码

小程序-用户授权/授权后的信息读取流程

小程序 判断是不是为新用户 登陆授权 封装request请求

小程序授权登陆解决方案

微信小程序登录授权实现