⼩程序-getUserProfile⽰例与后台(已纠正)
前段时间,更改了授权⽅式,采⽤⽤户主动式授权,也就是可授权多次,并不像之前的取消就不弹了
下⾯代码效果:检测是否授权,未授权时出现授权按钮,已授权则直接显⽰⽤户信息,⽅式使⽤了缓存,有更好的⽅案请在评论区回复⼀下,(⽂中appid,appsecret均为测试⼩程序)index.js
Page({
data: {
hasUserInfo: false,
canIUseGetUserProfile: false,
},
onLoad() {
/* 从缓存中取出去数据 */
let userInfo = wx.getStorageSync('userInfo');
let wxInfos = wx.getStorageSync('wxInfos');
if (0 == Object.keys(userInfo).length) {
this.setData({canIUseGetUserProfile: true})
wx.login({success:(rs) => {wx.setStorageSync("code",rs.code); } });
}else{
this.setData({
hasUserInfo:true,
userInfo:userInfo,
wxInfos:wxInfos
})
}
},
getUserProfile(e) {
desc: '授权',
success: (wrs) => {
var code = wx.getStorageSync("code")
if(code){
let dataJson = {
appId: app.globalData.appId,
code : code,
iv : wrs.iv,
encryptedData : ptedData
}
/* 发起⽤户开放信息请求 */
if(data.stateCode=="0"){
var result = JSON.sult);
_self.setData({
hasUserInfo: true,
userInfo:result,
wxInfos:JSON.parse(result.wxInfos)
})
/* 使⽤缓存记录信息 */
wx.setStorageSync('userInfo', result);
wx.setStorageSync('wxInfos', JSON.parse(result.wxInfos));
}else {
console.log(data)
}
});
}
},fail:()=>{
console.log("⽤户拒绝授权");
}
});
}
})
index.wxml
<view class="container">
<view class="userinfo">
<block wx:if="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{wxInfos.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{wxInfos.nickName}}</text>
<text class="userinfo-nickname">openId=> {{userInfo.openid}}</text>
<text class="userinfo-nickname">unionId=> {{userInfo.unionid}}</text>
</block>
</view>
</view>
后台:
GetWxInfoServlet.java
import org.apachemons.lang.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.src.minback.utils.HttpRequest;
import com.src.minback.utils.WxAesUtil;
/**
* 模拟获取⽤户信息及⼩程序版本信息
*
* @author liuwl
*/
public class GetWxInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static String wxspAppid = "wxfc5a797bc1295c71";
static String wxspSecret = "47067172d687958df709f9396b83e3ed";
static String grant_type = "authorization_code";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置字符集
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
// 获取输出
PrintWriter out = Writer();
JSONObject jsonObj = new JSONObject();
JSONObject result = new JSONObject();
System.out.println("wx.login 返回的code:" + Parameter("code"));
String code = Parameter("code");
// 登录凭证不能为空
if (code != null && code.length() > 0) {
// 请求参数
String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + code + "&grant_type=" + grant_type;
// 发送请求
String sr = HttpRequest.sendGet("api.weixin.qq/sns/jscode2session", params);
// 解析相应内容(转换成json对象)
JSONObject json = JSONObject.parseObject(sr);
// 获取会话密钥(session_key)
String session_key = ("session_key").toString();
System.out.println("session_key:" + session_key);
// ⽤户的唯⼀标识(openid)
String openid = (String) ("openid");
System.out.println("openid:" + openid);
result.put("openid", openid);
// 开放平台的唯⼀标识符(unionid)
String unionid = (String) ("unionid");
System.out.println("unionid:" + unionid);
result.put("unionid", unionid);
if (StringUtils.isNotEmpty(openid)) {
jsonObj.put("stateCode", "0");
jsonObj.put("stateDesc", "操作成功");
// 解密encryptedData
try {
String encryptedData = Parameter("encryptedData");
String iv = Parameter("iv");
System.out.println("iv:" + iv);
System.out.println("encryptedData:" + encryptedData);
JSONObject decryptData = WxAesUtil.decrypt(encryptedData, session_key, iv, "UTF-8");
System.out.println("-->" + decryptData);
if (decryptData!=null) {
result.put("wxInfos", String());
}
jsonObj.put("result", JSONString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
jsonObj.put("stateCode", "1");
jsonObj.put("stateDesc", "获取失败");
}
} else {
jsonObj.put("stateCode", "3");
jsonObj.put("stateDesc", "code为空");
}
System.out.println("仅获取⽤户所有信息(userData):" + jsonObj);
out.print(jsonObj);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
⼯具类:WxAesUtil.java
import dec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.alibaba.fastjson.JSONObject;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.NoSuchPaddingException;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidParameterSpecException;
import java.util.Arrays;
public class WxAesUtil {
static {
//BouncyCastle是⼀个开源的加解密解决⽅案,主页在/
Security.addProvider(new BouncyCastleProvider());
}
/**
* AES解密
*
* @param data //密⽂,被加密的数据
* @param key //秘钥
* @param iv //偏移量
* @param encodingFormat //解密后的结果需要进⾏的编码
* @return
* @throws Exception
*/
public static JSONObject decrypt(String data, String key, String iv, String encodingFormat) throws Exception { //被加密的数据
byte[] dataByte = Base64.decodeBase64(data);
//加密秘钥
byte[] keyByte = Base64.decodeBase64(key);
//偏移量
byte[] ivByte = Base64.decodeBase64(iv);
try {
// 如果密钥不⾜16位,那么就补⾜. 这个if 中的内容很重要
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
Cipher cipher = Instance("AES/CBC/PKCS7Padding", "BC");
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters parameters = Instance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters); // 初始化
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
昵称代码转换return JSONObject.parseObject(result);
}
return null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
效果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论