Selaa lähdekoodia

新增获取微信步数代码

赵冬冬 4 vuotta sitten
vanhempi
commit
bb49b0325e

+ 55 - 0
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/config/wx/util/WxUtils.java

@@ -0,0 +1,55 @@
+package com.hcloud.microserver.h5.config.wx.util;
+
+import org.apache.commons.net.util.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.spec.AlgorithmParameterSpec;
+
+/**
+ * by zdd
+ */
+public class WxUtils {
+
+    private static String APP_ID = "小程序APPID";
+    private static String SECRET = "小程序SECRET";
+
+    String openId = "";
+
+
+
+    public static String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APP_ID + "&secret=" + SECRET + "&js_code=";
+    public static String url0 = "&grant_type=authorization_code";
+
+    // 步数转卡路里
+    public static float getDistanceByStep(long steps) {
+        return steps * 0.6f / 1000;
+    }
+
+    /**
+     * 微信解密运动步数
+     *
+     * @param sessionKey
+     * @param encryptedData
+     * @param iv
+     * @return
+     */
+    public static String decryptWeChatRunInfo(String sessionKey, String encryptedData, String iv) {
+        String result = null;
+        byte[] encrypData = Base64.decodeBase64(encryptedData);
+        byte[] ivData = Base64.decodeBase64(iv);
+        byte[] sessionKeyB = Base64.decodeBase64(sessionKey);
+        try {
+            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivData);
+            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+            SecretKeySpec keySpec = new SecretKeySpec(sessionKeyB, "AES");
+            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
+            byte[] doFinal = cipher.doFinal(encrypData);
+            result = new String(doFinal);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return result;
+    }
+}

+ 41 - 28
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/controller/web/WechatAppController.java

@@ -6,28 +6,18 @@ import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
-import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
-import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
-import com.github.binarywang.wxpay.constant.WxPayConstants;
-import com.github.binarywang.wxpay.exception.WxPayException;
-import com.github.binarywang.wxpay.service.WxPayService;
-import com.hcloud.microserver.h5.entity.LoginRequest;
-import com.hcloud.microserver.h5.service.CustomerInfoService;
-import com.hcloud.microserver.commoncore.annotation.AuthCarbonValidate;
+import com.alibaba.fastjson.JSONObject;
 import com.hcloud.microserver.commoncore.base.BaseController;
 import com.hcloud.microserver.commoncore.base.ResponseBase;
 import com.hcloud.microserver.commoncore.base.ResultVO;
-import com.hcloud.microserver.commoncore.base.Token;
-import com.hcloud.microserver.commoncore.constant.GlobleConstant;
-import com.hcloud.microserver.commoncore.enums.ResultEnum;
-import com.hcloud.microserver.commoncore.util.*;
+import com.hcloud.microserver.h5.config.wx.util.WxUtils;
+import com.hcloud.microserver.h5.entity.LoginRequest;
 import com.hcloud.microserver.h5.facade.carbon.entity.CustomerInfo;
 import com.hcloud.microserver.h5.facade.carbon.forms.CustomerInfoForm;
 import com.hcloud.microserver.h5.facade.carbon.vo.MyWxMpUser;
+import com.hcloud.microserver.h5.service.CustomerInfoService;
 import com.hcloud.microserver.h5.service.CustomerService;
 import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiImplicitParam;
-import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
@@ -35,22 +25,14 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.security.GeneralSecurityException;
-import java.util.HashMap;
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
+import java.util.Objects;
 
 @Slf4j
 @Controller
@@ -84,7 +66,7 @@ public class WechatAppController extends BaseController {
             if (null == session) {
                 throw new RuntimeException("login handler error");
             }
-            WxMaUserInfo wxUserInfo = JSON.parseObject(request.getRawData(),WxMaUserInfo.class);
+            WxMaUserInfo wxUserInfo = JSON.parseObject(request.getRawData(), WxMaUserInfo.class);
             if (null == wxUserInfo) {
                 throw new RuntimeException("wxUser not exist");
             }
@@ -133,6 +115,37 @@ public class WechatAppController extends BaseController {
         return resultVOSuccess();
     }
 
+    /**
+     * 登陆接口
+     */
+    @ApiOperation(value = "小程序获取微信步数", notes = "小程序获取微信步数")
+    @PostMapping("/steps")
+    @ResponseBody
+    public ResponseBase steps(@RequestBody LoginRequest request) throws WxErrorException {
+        // 获取微信用户session
+        WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(request.getCode());
+        String decrypt = null;
+        //解密
+        try {
+            decrypt = WxUtils.decryptWeChatRunInfo(session.getSessionKey(), request.getEncryptedData(), request.getIv());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        if (decrypt == null) {
+            log.info("解密失败");
+        } else {
+            log.info("解析成功");
+        }
+        JSONObject stepInfoListJson = JSON.parseObject(decrypt);
+        //解析json,获取stepInfoList下面的步数
+        JSONArray stepInfoList = JSON.parseArray(stepInfoListJson.getString("stepInfoList"));
+        //获取今天的步数
+        JSONObject today = Objects.requireNonNull(stepInfoList).getJSONObject(0);
+        //登录系统获取本地
+        return resultVOSuccess(today);
+    }
+
 //    private Map<String, Object> getStringObjectMap(String customerId) {
 //        CustomerInfoForm customerInfoForm = customerInfoService.queryByPrimaryKey(customerId);
 //        CustomerInfo data = BeanCopyUtil.convertBean(customerInfoForm, CustomerInfo.class);