栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

云闪付小程序授权登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

云闪付小程序授权登录

package com.pmh.common.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xzh.common.model.Bind;
import com.xzh.common.service.UnionPayServiceInf;
import com.xzh.common.util.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class UnionPayServiceImpl implements UnionPayServiceInf {
private static final Logger logger = Logger.getLogger(UnionPayServiceImpl.class);
//应用Id
private static final String appId = ResourcesConfig.getValue(“appId”);
//密钥
private static final String secret = ResourcesConfig.getValue(“secret”);
//对称密钥
private static final String symmetricKey = ResourcesConfig.getValue(“symmetricKey”);

@Override
public ResultBody getLoginUserAuth(String code) throws Exception {
    //获取backendToken
    String backendToken = getBackendToken();

    // 获取accessToken和OpenId
    Map accMap = getAccessTokenAndOpenID(backendToken,code);

    //获取用户实名信息
    return getUserAuth(backendToken,accMap.get("accessToken"),accMap.get("openId"));
}

public Map getAccessTokenAndOpenID(String backendToken, String code) throws Exception{
    Map map = new HashMap();
    //调用地址
    String url = "https://open.95516.com/open/access/1.0/token";

    //拼装参数Map
    Map params = new HashMap();
    params.put("appId", appId);//应用ID
    params.put("backendToken", backendToken);
    //登录获取的code
    params.put("code", code);

    //接入方直接填写常量字符串authorization_code
    params.put("grantType", "authorization_code");

    //发送请求
    String resultStr = Utils.sendPostGson(url, params);
    System.out.println("-------获取accessToken和openId:"+resultStr+"---------");

    //解析返回的json
    JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
    map.put("accessToken",jsonObject.get("params").getAsJsonObject().get("accessToken").getAsString());
    map.put("openId",jsonObject.get("params").getAsJsonObject().get("openId").getAsString());

    return map;

}


public ResultBody getUserAuth(String backendToken,String accessToken,String openId) throws Exception{
    //调用接口地址
    String mobileUrl = "https://open.95516.com/open/access/1.0/user.mobile";
    //调用接口地址
    String url = "https://open.95516.com/open/access/1.0/user.auth";

    //拼装参数Map
    Map params = new HashMap(0);
    params.put("appId", appId);
    params.put("accessToken", accessToken);
    params.put("openId", openId);
    params.put("backendToken", backendToken);
    //发送请求
    String mobileResultStr = Utils.sendPostGson(mobileUrl, params);
    //发送请求
    String resultStr = Utils.sendPostGson(url, params);
    System.out.println("-------获取用户敏感信息-姓名+证件号:"+resultStr+"---------");
    //解析返回的json
    JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
    Map map=new HashMap();
    //解析接口返回的json
    String realName=  jsonObject.get("params").getAsJsonObject().get("realName").getAsString();
    String certId=  jsonObject.get("params").getAsJsonObject().get("certId").getAsString();
    //解析返回的json
    JsonObject jsonObject2 = (JsonObject) new JsonParser().parse(mobileResultStr);
    //判断是否返回成功
    //解析接口返回的json
    String mobile = jsonObject2.get("params").getAsJsonObject().get("mobile").getAsString();
    //解密后手机号,使用对称密钥进行解密
    mobile = Utils.getDecryptedValue(mobile,symmetricKey);

    //解密后用户实名信息,使用对称密钥进行解密
    realName = Utils.getDecryptedValue(realName,symmetricKey);
    certId = Utils.getDecryptedValue(certId,symmetricKey);
    int bindId = (int) System.currentTimeMillis();
    Bind bind=new Bind();
    bind.setaName(realName);
    bind.setName(realName);
    bind.setCardnumber(certId);
    bind.setMobilephone(mobile);
    bind.setaCardNumber(certId);
    bind.setId(bindId);
    map.put("bind", bind);
    String token = UUID.randomUUID().toString().replaceAll("-", "");
    ApiCache.setToken(bindId,token);
    return new ResultBody(JsonKey.SUCCESS, "登录成功", map, token);
}


public String getMobile(String backendToken,String accessToken,String openId) throws Exception{

    //调用接口地址
    String url = "https://open.95516.com/open/access/1.0/user.mobile";

    //拼装参数Map
    Map params = new HashMap(0);
    params.put("appId", appId);//应用ID
    params.put("accessToken", accessToken);//登录token
    params.put("openId", openId);//用户ID
    params.put("backendToken", backendToken);//backendToken

    //发送请求
    String resultStr = Utils.sendPostGson(url, params);

    System.out.println("-------获取用户敏感信息-登录手机号:"+resultStr+"---------");

    //解析返回的json
    JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
    //判断是否返回成功
    if(!"00".equals(jsonObject.get("resp").getAsString())){
        return jsonObject.get("msg").getAsString();
    }else{
        //解析接口返回的json
        String mobile = jsonObject.get("params").getAsJsonObject().get("mobile").getAsString();
        //解密后手机号,使用对称密钥进行解密
        String deMobile = Utils.getDecryptedValue(mobile,symmetricKey);
        return deMobile;
    }

}


public static String getBackendToken() {
    String url = "https://open.95516.com/open/access/1.0/backendToken";
    //拼装参数Map
    Map params = new HashMap();
    params.put("appId", appId);//应用ID
    params.put("secret", secret);//应用密钥
    //生成随即字符串
    String nonceStr = Utils.createNonceStr();
    params.put("nonceStr", nonceStr);
    //生成时间戳
    String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
    params.put("timestamp", timestamp);
    //签名 (appId,secret,nonceStr,timestamp)
    String value = Utils.sortMap(params);//排序
    String signature = Utils.sha256(value.getBytes());//签名加密
    params.put("signature", signature);
    String param = new Gson().toJson(params);
    System.out.println("生成入参和签名:" + param);
    //发送请求
    String resultStr = Utils.sendPostGson(url, params);
    System.out.println("-------获取backendToken信息:" + resultStr + "---------");
    //解析返回的json
    JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
    return jsonObject.get("params").getAsJsonObject().get("backendToken").getAsString();
}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号