Procházet zdrojové kódy

Merge branch 'master' of yb/wxservice into master

杨波 před 3 roky
rodič
revize
33c75c8707

+ 8 - 2
src/main/java/com/ematou/wxservice/api/WeChatApi.java

@@ -10,11 +10,17 @@ public enum WeChatApi {
     /**
      * 微信基础服务获取AccessToken
      */
-    GET_TOKEN("http://61.144.244.114:4040/wxbase/token", "GET"),
+    GET_TOKEN("https://wxbase.ds-bay.com/wxbase/token", "GET"),
+
+    /**
+     * 微信基础服务获取ticket
+     */
+    GET_TICKET("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", "GET"),
+
     /**
      * 微信基础服务生成AccessToken
      */
-    GENERAL_TOKEN("http://61.144.244.114:4040/wxbase/generate/token", "GET"),
+    GENERAL_TOKEN("https://wxbase.ds-bay.com/wxbase/generate/token", "GET"),
     /**
      * 获取用户信息,只能获取到openId,如需要使用unionId,需要将公众号绑定到微信开放平台帐号
      */

+ 64 - 8
src/main/java/com/ematou/wxservice/common/utils/SignUtil.java

@@ -1,13 +1,18 @@
 package com.ematou.wxservice.common.utils;
 
 import com.ematou.wxservice.common.constant.WeChatConstant;
+import org.springframework.util.StringUtils;
 
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
 
 /**
  * 根据服务器配置的令牌校验签名
+ *
  * @author lhm
  * @version 1.0
  * 2021-05-11 11:49
@@ -17,17 +22,18 @@ public class SignUtil {
 
     /**
      * 校验签名
-     * @param signature  签名
+     *
+     * @param signature 签名
      * @param timestamp 时间戳
-     * @param nonce 随机数
+     * @param nonce     随机数
      * @return true 成功,false 失败
      */
-    public static boolean checkSignature(String signature,String timestamp, String nonce){
+    public static boolean checkSignature(String signature, String timestamp, String nonce) {
 
         String checktext = null;
-        if(null != signature){
+        if (null != signature) {
             // 对Token,timestamp nonce 按字典排序
-            String [] paramArr = new String[] {token, timestamp, nonce};
+            String[] paramArr = new String[]{token, timestamp, nonce};
             Arrays.sort(paramArr);
             // 将排序后的结果拼成一个字符串
             String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);
@@ -45,11 +51,60 @@ public class SignUtil {
     }
 
     /**
+     * jsapi签名
+     *
+     * @param ticket    ticket
+     * @param url       url
+     * @param timestamp 时间戳
+     * @param nonce     随机数
+     * @return true 成功,false 失败
+     */
+    public static String jsApiSign(String ticket, String url, String timestamp, String nonce) {
+        String sign = "";
+        SortedMap<String, Object> param = new TreeMap<>();
+        param.put("jsapi_ticket", ticket);
+        param.put("url", url);
+        param.put("timestamp", timestamp);
+        param.put("noncestr", nonce);
+        String signStr = buildSignStr(param);
+        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+                'a', 'b', 'c', 'd', 'e', 'f'};
+        try {
+            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
+            mdTemp.update(signStr.getBytes("UTF-8"));
+            byte[] md = mdTemp.digest();
+            int j = md.length;
+            char buf[] = new char[j * 2];
+            int k = 0;
+            for (int i = 0; i < j; i++) {
+                byte byte0 = md[i];
+                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
+                buf[k++] = hexDigits[byte0 & 0xf];
+            }
+            return new String(buf);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    private static String buildSignStr(SortedMap<String, Object> params) {
+        String signStr = "";
+        for (Map.Entry<String, Object> entry : params.entrySet()) {
+            if (!StringUtils.isEmpty(signStr)) {
+                signStr += "&";
+            }
+            signStr += entry.getKey() + "=" + entry.getValue();
+        }
+        return signStr;
+    }
+
+    /**
      * 将字节数组转化为16进制字符串
+     *
      * @return 字符串
      */
     private static String byteToStr(byte[] byteArrays) {
-        StringBuilder str= new StringBuilder();
+        StringBuilder str = new StringBuilder();
         for (byte byteArray : byteArrays) {
             str.append(byteToHexStr(byteArray));
         }
@@ -58,14 +113,15 @@ public class SignUtil {
 
     /**
      * 将字节转化为十六进制字符串
+     *
      * @param myByte 字节
      * @return 字符串
      */
     private static String byteToHexStr(byte myByte) {
 
-        char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
         char[] tempArr = new char[2];
-        tempArr[0] = Digit[(myByte >>> 4)&0X0F];
+        tempArr[0] = Digit[(myByte >>> 4) & 0X0F];
         tempArr[1] = Digit[myByte & 0x0F];
         return new String(tempArr);
     }

+ 8 - 0
src/main/java/com/ematou/wxservice/controller/WeChatController.java

@@ -1,5 +1,6 @@
 package com.ematou.wxservice.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.ematou.wxservice.common.web.R;
 import com.ematou.wxservice.entity.vo.BindingInfo;
 import com.ematou.wxservice.entity.vo.TemplateMessage;
@@ -36,4 +37,11 @@ public class WeChatController {
         Map<String, Object> map = weChatService.oauth2(code);
         return new R<>().success(map);
     }
+
+    @PostMapping("jsSign")
+    public R<?> jsSign(@RequestBody JSONObject obj){
+        String url = obj.getString("url");
+        Map<String, Object> map = weChatService.jsSign(url);
+        return new R<>().success(map);
+    }
 }

+ 7 - 10
src/main/java/com/ematou/wxservice/controller/WeChatViewController.java

@@ -1,21 +1,18 @@
 package com.ematou.wxservice.controller;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.ematou.wxservice.entity.pojo.ScannVO;
 import com.ematou.wxservice.entity.pojo.UserInfo;
 import com.ematou.wxservice.entity.vo.BindingInfo;
 import com.ematou.wxservice.service.UserInfoService;
-import com.google.gson.annotations.JsonAdapter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.util.HashMap;
-import java.util.Map;
 
 /**
  * @author lhm
@@ -30,24 +27,23 @@ public class WeChatViewController {
 
 
     @GetMapping("/view/")
-    public String main(){
+    public String main() {
         return "hello";
     }
 
     @GetMapping("/view/binding")
-    public String binding(HttpServletRequest request,HttpServletResponse response){
+    public String binding(HttpServletRequest request, HttpServletResponse response) {
         //判断是否注册
         UserInfo msg = userInfoService.queryUserInfoByOpenId(request.getParameter("openid"));
-        if(msg!=null){
+        if (msg != null) {
             return "qrcode";
-        }else{
+        } else {
             return "binding";
         }
 
     }
 
 
-
     @PostMapping("/binding")
     public String binding(HttpServletRequest request, Model model) {
         String phone = request.getParameter("phone1");
@@ -69,6 +65,7 @@ public class WeChatViewController {
 
     /**
      * 扫一扫
+     *
      * @param request
      * @param model
      * @return

+ 4 - 3
src/main/java/com/ematou/wxservice/mp/handler/WeChatScannHandler.java

@@ -33,18 +33,19 @@ public class WeChatScannHandler  implements WeChatMessageHandler {
         UserInfo userInfo = userInfoService.queryUserInfoByOpenId(weChatMessage.getFromUser());
         map.put("phone",userInfo.getTellPhoneNumber());
         map.put("openId",weChatMessage.getFromUser());
-        String wxMpTake = HttpUtils.sendPost("http://68rfyvi.nat.ipyingshe.com/order/collect/wxMpTakeObject",JSON.toJSONString(map));
+        String wxMpTake = HttpUtils.sendPost("http://icsp-api.ds-bay.com/order/collect/wxMpTakeObject",JSON.toJSONString(map));
 
-        Map<String, Object> weChatMapMsg = JSONObject.toJavaObject(JSON.parseObject(weChatMessage.getEventKey()), Map.class);
+        Map<String, Object> weChatMapMsg = JSONObject.toJavaObject(JSON.parseObject(wxMpTake), Map.class);
         if(weChatMapMsg.get("code").equals("50009")){
             return weChatMapMsg.get("msg").toString();
         }else{
             StringBuffer stringBuffer = new StringBuffer();
+            stringBuffer.append("取件成功!\n");
             List<JSONObject> data = JSON.parseArray(weChatMapMsg.get("data").toString(), JSONObject.class);
             for(JSONObject jsonObject : data){
                 stringBuffer.append("\n").append("订单编号:"+jsonObject.get("orderSn")).append("柜子编号"+jsonObject.get("lockerName")).append("\n");
             }
-            return wxMpTake;
+            return stringBuffer.toString();
         }
 
     }

+ 2 - 2
src/main/java/com/ematou/wxservice/mp/handler/WeChatSubscribeEventHandler.java

@@ -26,8 +26,8 @@ public class WeChatSubscribeEventHandler implements WeChatMessageHandler {
         WeChatMpXmlOutNewsMessage.Item item = new WeChatMpXmlOutNewsMessage.Item();
         item.setTitle("欢迎关注【e码头】");
         item.setDescription(">>>点击绑定手机号,立即取件/寄件!");
-        item.setPicUrl("http://mb6x1jv.nat.ipyingshe.com/images/emtnew.png");
-        item.setUrl("http://mb6x1jv.nat.ipyingshe.com/view/binding?openid="+weChatMessage.getFromUser());
+        item.setPicUrl("https://wxservice.ds-bay.com/images/emtnew.png");
+        item.setUrl("https://wxservice.ds-bay.com/view/binding?openid="+weChatMessage.getFromUser());
         return WeChatMpXmlOutMessage.NEWS().addArticle(item).toUser(weChatMessage.getFromUser()).fromUser(weChatMessage.getToUser()).build();
        }
 

+ 22 - 0
src/main/java/com/ematou/wxservice/service/WeChatService.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.ematou.wxservice.api.WeChatApi;
 import com.ematou.wxservice.api.WeChatApiRestTemplate;
 import com.ematou.wxservice.common.constant.WeChatConstant;
+import com.ematou.wxservice.common.utils.SignUtil;
 import com.ematou.wxservice.config.WeChatGeneralConfig;
 import com.ematou.wxservice.entity.vo.AccessToken;
 import com.ematou.wxservice.entity.vo.BindingInfo;
@@ -17,8 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
+import java.text.DecimalFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Random;
 
 /**
  * @author lhm
@@ -197,4 +201,22 @@ public class WeChatService {
         }
         return map;
     }
+
+    public Map<String, Object> jsSign(String url) {
+        AccessToken accessToken = getAccessToken();
+        String getUrl = String.format(WeChatApi.GET_TICKET.getUrl(), accessToken.getAccessToken());
+        String resp = weChatApiRestTemplate.getForOther(getUrl);
+        JSONObject jsonObject = JSONObject.parseObject(resp);
+        String ticket = jsonObject.getString("ticket");
+        String timestamp = String.valueOf(new Date().getTime() / 1000);
+        DecimalFormat df = new DecimalFormat("00000000");
+        String nonceStr = df.format(new Random().nextInt(100000000));
+        String sign = SignUtil.jsApiSign(ticket, url, timestamp, nonceStr);
+        Map<String, Object> result = new HashMap<>();
+        result.put("appId", "wxf9360d70bc1406ee");
+        result.put("timestamp", timestamp);
+        result.put("nonceStr", nonceStr);
+        result.put("signature", sign);
+        return result;
+    }
 }

+ 3 - 3
src/main/resources/application.yml

@@ -11,9 +11,9 @@ spring:
 #    username: tuser
 #    password: Qq!123
 #    url: jdbc:mysql://120.76.84.45:3306/wx_base?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
-    username: tuser
-    password: Qq!123
-    url: jdbc:mysql://120.76.84.45:3306/wx_service?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
+    username: wx_service
+    password: p9ryaBSuA6
+    url: jdbc:mysql://out-rm-wz92efl25x02n44xego.mysql.rds.aliyuncs.com:3306/wx_service?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
   thymeleaf:
     cache: false
     prefix: classpath:/templates/

+ 2 - 2
src/main/resources/static/js/clean.js

@@ -67,7 +67,7 @@ function sendMessage1() {
     $("#btnSendCode1").attr("disabled", "true");
     $("#btnSendCode1").val(+curCount1 + "秒再获取");
 
-    $.get("http://mb6x1jv.nat.ipyingshe.com/user/send?phoneNumber=" + phone+"&openId="+sessionStorage.getItem("openid"), function (res) {
+    $.get("https://wxservice.ds-bay.com/user/send?phoneNumber=" + phone+"&openId="+sessionStorage.getItem("openid"), function (res) {
     })
 
     InterValObj1 = window.setInterval(SetRemainTime1, 1000);
@@ -125,7 +125,7 @@ getOpenId = function (code) {
     $.ajax({
         type: 'GET',
         dataType: 'text',
-        url: 'http://mb6x1jv.nat.ipyingshe.com/oauth2?code='+code,
+        url: 'https://wxservice.ds-bay.com/oauth2?code='+code,
         success: function (res) {
             if (res.status == -1) {
                 // 提示没有关注公众号 没有关注公众号跳转到关注公众号页面

+ 4 - 4
src/main/resources/templates/binding.html

@@ -7,9 +7,9 @@
         <meta content="yes" name="apple-mobile-web-app-capable"/>
         <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
         <meta content="telephone=no" name="format-detection"/>
-        <link href="http://mb6x1jv.nat.ipyingshe.com/css/style.css" rel="stylesheet" type="text/css"/>
-        <script type="text/javascript" src="http://mb6x1jv.nat.ipyingshe.com/js/jquery.min.js"></script>
-        <script type="text/javascript" src="http://mb6x1jv.nat.ipyingshe.com/js/clean.js"></script>
+        <link href="https://wxservice.ds-bay.com/css/style.css" rel="stylesheet" type="text/css"/>
+        <script type="text/javascript" src="https://wxservice.ds-bay.com/js/jquery.min.js"></script>
+        <script type="text/javascript" src="https://wxservice.ds-bay.com/js/clean.js"></script>
     </head>
     <body>
 
@@ -42,7 +42,7 @@
                 <div class="aui-code-box">
                     <h2>绑定手机</h2>
                     <p>登录及找回密码的途径</p>
-                    <form id="submit_form" action="http://mb6x1jv.nat.ipyingshe.com/binding" method="post">
+                    <form id="submit_form" action="https://wxservice.ds-bay.com/binding" method="post">
                         <p class="aui-code-line">
                             <em>+86</em>
                             <input type="text" class="aui-code-line-input" name="phone1" value="" id="phone1" autocomplete="off" placeholder="请输入已绑定的手机号" style="padding-left:28px;"/>

+ 23 - 39
src/main/resources/templates/qrcode.html

@@ -3,8 +3,8 @@
 <head>
 
     <meta charset="UTF-8">
-    <script src="plug-in/qrcode/jquery.min.js"></script>
-    <script src=”http://res.wx.qq.com/open/js/jweixin-1.0.0.js”></script>
+    <script src="/js/jquery.min.js"></script>
+    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
     <title>微信扫一扫</title>
 
 </head>
@@ -12,61 +12,45 @@
 <body>
 
 <script type="application/javascript">
-
-    var purl = encodeURIComponent(location.href);
+    const purl = location.href;
+    const data = {"url": purl};
     $.ajax({
-        url:"http://mb6x1jv.nat.ipyingshe.com/scanning",
-        data:{"url":purl},
-        type:"POST",
-        dataType:"json",
-        success:function(result){
+        url: "https://wxservice.ds-bay.com/jsSign",
+        data: JSON.stringify(data),
+        type: "POST",
+        contentType: 'application/json',
+        dataType: "json",
+        success: function (result) {
             wx.config({
-                debug:false,
-                appId:result.obj.appId,
-                timestamp:result.obj.timestamp, // 必填,生成签名的时间戳
-                nonceStr:result.obj.noncestr,// 必填,生成签名的随机串
-                signature:result.obj.signature,// 必填,签名
-                jsApiList:['scanQRCode']
+                debug: false,
+                appId: result.data.appId,
+                timestamp: result.data.timestamp, // 必填,生成签名的时间戳
+                nonceStr: result.data.nonceStr,// 必填,生成签名的随机串
+                signature: result.data.signature,// 必填,签名
+                jsApiList: ['scanQRCode']
             });
         },
-        error:function(){
+        error: function () {
             alert("请求失败");
         }
     });
 
-    function scanQr(){
-        var ua = navigator.userAgent.toLowerCase();
-        var isWeiXin  =ua.indexOf('micromessenger') != -1;
-        if(!isWeiXin){
-            alert('请用微信打开链接,才可使用扫一扫!');
-        }
+    wx.ready(function () {
         wx.scanQRCode({
-            needResult:1,
-            scanType:["qrCode"],
-            success:function (res) {
-                var scan = res.resultStr;
-                location.href=scan;
+            needResult: 0,
+            scanType: ["qrCode"],
+            success: function (res) {
 
             },
-            error:function(res) {
-                if(res.errMsg.indexOf('function_not_exist') > 0){
+            error: function (res) {
+                if (res.errMsg.indexOf('function_not_exist') > 0) {
                     alert('当前版本低,请进行升级!');
                 }
             }
         });
-    }
-    wx.error(function(res){
-        alert(res.errMsg);
-
-    });
-    wx.ready(function(){
-        // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
     });
 
 </script>
-
-<input type="button" onclick="scanQr()" id="test" value="微信扫一扫" style="font-size: 30px;border: solid 1px;padding: 10px;"/>
-
 </body>
 
 </html>

+ 3 - 3
src/main/resources/templates/return.html

@@ -7,8 +7,8 @@
         <meta content="yes" name="apple-mobile-web-app-capable"/>
         <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
         <meta content="telephone=no" name="format-detection"/>
-        <link href="http://mb6x1jv.nat.ipyingshe.com/css/style_return.css" rel="stylesheet" type="text/css"/>
-        <script type="text/javascript" src="http://mb6x1jv.nat.ipyingshe.com/js/jquery.min.js"></script>
+        <link href="https://wxservice.ds-bay.com/css/style_return.css" rel="stylesheet" type="text/css"/>
+        <script type="text/javascript" src="https://wxservice.ds-bay.com/js/jquery.min.js"></script>
     </head>
     <body>
 
@@ -39,7 +39,7 @@
             <section class="aui-scrollView">
                 <div class="aui-back-box">
                     <div class="aui-back-pitch">
-                        <img src="http://mb6x1jv.nat.ipyingshe.com/images/icon-pitch.png" alt="">
+                        <img src="https://wxservice.ds-bay.com/images/icon-pitch.png" alt="">
                     </div>
                     <div class="aui-back-title">
                         <h2>绑定成功</h2>