licailing 5 лет назад
Родитель
Сommit
e751c30f34

+ 7 - 0
pom.xml

@@ -279,11 +279,18 @@
             <version>1.1</version>
         </dependency>
 
+        <!-- 腾讯api -->
         <dependency>
             <groupId>com.tencentcloudapi</groupId>
             <artifactId>tencentcloud-sdk-java</artifactId>
             <version>3.1.100</version>
         </dependency>
 
+        <!-- 地图 -->
+        <dependency>
+            <groupId>org.gavaghan</groupId>
+            <artifactId>geodesy</artifactId>
+            <version>1.1.3</version>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 0
src/main/java/com/izouma/dingdong/security/WebSecurityConfig.java

@@ -75,6 +75,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/**/excel").permitAll()
                 .antMatchers("/wx/**").permitAll()
                 .antMatchers("/sms/sendVerify").permitAll()
+                .antMatchers("/sms/tencentSend").permitAll()
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to

+ 0 - 1
src/main/java/com/izouma/dingdong/service/ConsumptionService.java

@@ -1,6 +1,5 @@
 package com.izouma.dingdong.service;
 
-import com.alibaba.fastjson.JSONObject;
 import com.izouma.dingdong.config.KsherPaySdk;
 import com.izouma.dingdong.config.WxPayProperties;
 import com.izouma.dingdong.domain.FeeRecord;

+ 1 - 1
src/main/java/com/izouma/dingdong/service/UserService.java

@@ -39,7 +39,7 @@ public class UserService {
     private UserRepo       userRepo;
     private WxMaService    wxMaService;
     private WxMpService    wxMpService;
-    private SmsService     smsService;
+//    private SmsService     smsService;
     private StorageService storageService;
     private JwtTokenUtil   jwtTokenUtil;
 

+ 13 - 36
src/main/java/com/izouma/dingdong/service/merchant/MerchantService.java

@@ -285,62 +285,39 @@ public class MerchantService {
         merchantSettingsRepo.save(merchant);
     }
 
+
     /**
-     * 距离排序,如果有范围,范围内进行排
+     * 带距离的商家,已按距离排好
      *
-     * @param merchants 商列表
+     * @param merchants 商列表
      * @param longitude 经度
      * @param latitude  纬度
      * @param range     范围
-     * @return 商家及距离
+     * @return 商家列表
      */
-    public Map<Merchant, Double> distanceSorting(Set<Merchant> merchants, Double longitude, Double latitude, Double range) {
+    public List<MerchantDTO> withDistance(Set<Merchant> merchants, Double longitude, Double latitude, Double range) {
         //所有商家要按距离排序规则
-        Map<Merchant, Double> map = new HashMap<>();
+        Map<Merchant, Double> merchantMap = new HashMap<>();
         //算距离
         for (Merchant m : merchants) {
             if (ObjectUtil.isNotNull(m.getLatitude()) && ObjectUtil.isNotNull(m.getLongitude())) {
                 Double distance = MapUtils.distance(m.getLongitude(), m.getLatitude(), longitude, latitude);
                 if (range != null) {
                     if (distance < range) {
-                        map.put(m, distance);
+                        merchantMap.put(m, distance);
                     }
                 } else {
-                    map.put(m, distance);
+                    merchantMap.put(m, distance);
                 }
             }
         }
 
-        //排序
-        List<Map.Entry<Merchant, Double>> list = new ArrayList<>(map.entrySet());
-        list.sort(Comparator.comparing(Map.Entry<Merchant, Double>::getValue));
-        //List<Merchant> mers = new ArrayList<>();
-
-        Map<Merchant, Double> merchantMap = new HashMap<>();
-        for (Map.Entry<Merchant, Double> m : list) {
-            //mers.add(m.getKey());
-            merchantMap.put(m.getKey(), m.getValue());
-        }
-
-        return merchantMap;
-    }
-
-    /**
-     * 带距离的商家
-     *
-     * @param merchants 商户列表
-     * @param longitude 经度
-     * @param latitude  纬度
-     * @param range     范围
-     * @return 商家列表
-     */
-    public List<MerchantDTO> withDistance(Set<Merchant> merchants, Double longitude, Double latitude, Double range) {
-        Map<Merchant, Double> merchantMap = this.distanceSorting(merchants, longitude, latitude, range);
         List<MerchantDTO> dtos = new ArrayList<>();
         for (Map.Entry<Merchant, Double> m : merchantMap.entrySet()) {
             merchantSettingsRepo.findByMerchantId(m.getKey().getId())
                     .ifPresent(s -> dtos.add(new MerchantDTO(m.getKey(), s, m.getValue())));
         }
+        dtos.sort(Comparator.comparing(MerchantDTO::getDistance));
         return dtos;
     }
 
@@ -351,13 +328,13 @@ public class MerchantService {
         List<HeatMapDTO> list = new ArrayList<>();
 
         Set<Merchant> merchantSet = new HashSet<>(merchantRepo.findAll());
-        Map<Merchant, Double> map = this.distanceSorting(merchantSet, longitude, latitude, 3000.0);
-        map.keySet().forEach(m -> {
-            long sum = orderInfoRepo.findAllByMerchantId(m.getId()).stream().mapToLong(OrderInfo::getId).count();
+        List<MerchantDTO> map = this.withDistance(merchantSet, longitude, latitude, 3000.0);
+
+        map.forEach(m -> {
+            long sum = orderInfoRepo.findAllByMerchantId(m.getMid()).stream().mapToLong(OrderInfo::getId).count();
             list.add(new HeatMapDTO(sum, longitude, latitude));
         });
 
         return list;
-
     }
 }

+ 81 - 0
src/main/java/com/izouma/dingdong/service/sms/TencentSmsService.java

@@ -0,0 +1,81 @@
+package com.izouma.dingdong.service.sms;
+
+import com.izouma.dingdong.domain.SmsRecord;
+import com.izouma.dingdong.exception.BusinessException;
+import com.izouma.dingdong.repo.SmsRecordRepo;
+import com.izouma.dingdong.service.sms.SmsService;
+import com.tencentcloudapi.common.Credential;
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
+import com.tencentcloudapi.sms.v20190711.SmsClient;
+import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
+import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
+import com.tencentcloudapi.sms.v20190711.models.SendStatus;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.websocket.SendResult;
+import java.time.LocalDateTime;
+
+/**
+ * Tencent Cloud Sms Sendsms
+ * https://cloud.tencent.com/document/product/382/38778
+ */
+@Service
+@Slf4j
+public class TencentSmsService {
+    @Value("${tencent.secret_id}")
+    private String        secretId;
+    @Value("${tencent.secret_key}")
+    private String        secretKey;
+    @Value("${tencent.sms_appid}")
+    private String        appId;
+    private SmsRecordRepo smsRecordRepo;
+
+
+    public String sendVerify(String phone) {
+        // 短信模板ID,需要在短信应用中申请
+        String templateId = "676939";
+        // 签名,使用的是`签名内容`,而不是`签名ID`
+        String smsSign = "弗雷登";
+        String[] phones = {phone};
+
+        String[] param = {RandomStringUtils.randomNumeric(4)};
+        try {
+            //发送请求
+            SendSmsRequest req = new SendSmsRequest();
+            req.setPhoneNumberSet(phones);
+            req.setTemplateID(templateId);
+            req.setSign(smsSign);
+            req.setSmsSdkAppid(appId);
+            req.setTemplateParamSet(param);
+            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
+            Credential cred = new Credential(secretId, secretKey);
+            //实例化要请求产品的client对象,clientProfile是可选的
+            SmsClient client = new SmsClient(cred, "");
+            client.SendSms(req);
+            SendSmsResponse resp = client.SendSms(req);
+
+            SendStatus sendStatus = resp.getSendStatusSet()[0];
+            String code = sendStatus.getCode();
+            if (!code.equals("Ok")) {
+                throw new BusinessException("发送失败,请稍后再试", code + "," + sendStatus.getMessage());
+            }
+            smsRecordRepo.expire(phone);
+            String sessionId = RandomStringUtils.randomAlphabetic(10);
+            smsRecordRepo.save(SmsRecord.builder()
+                    .sessionId(sessionId)
+                    .phone(phone)
+                    .code(param[0])
+                    .expiresAt(LocalDateTime.now().plusMinutes(5))
+                    .expired(false)
+                    .build());
+            return sessionId;
+
+        } catch (TencentCloudSDKException e) {
+            e.printStackTrace();
+            throw new BusinessException("发送失败,请稍后再试", e.getMessage());
+        }
+    }
+}

+ 11 - 3
src/main/java/com/izouma/dingdong/utils/MapUtils.java

@@ -1,10 +1,13 @@
 package com.izouma.dingdong.utils;
 
 import com.izouma.dingdong.exception.BusinessException;
+import org.gavaghan.geodesy.Ellipsoid;
+import org.gavaghan.geodesy.GeodeticCalculator;
+import org.gavaghan.geodesy.GlobalCoordinates;
 
 public class MapUtils {
     //地球半径
-    private static final Double EARTH_RADIUS = 6378.137;
+//    private static final Double EARTH_RADIUS = 6378.137;
 
     /**
      * 计算距离
@@ -19,7 +22,7 @@ public class MapUtils {
         if (latitude1 == null || latitude2 == null || longitude1 == null || longitude2 == null) {
             throw new BusinessException("经纬度为空");
         }
-        double radLat1 = latitude1 * Math.PI / 180.0;
+/*        double radLat1 = latitude1 * Math.PI / 180.0;
         double radLat2 = latitude2 * Math.PI / 180.0;
         double a = radLat1 - radLat2;
         double b = longitude1 * Math.PI / 180.0 - longitude2 * Math.PI / 180.0;
@@ -28,6 +31,11 @@ public class MapUtils {
                 Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
         s = s * EARTH_RADIUS;
         s = Math.round(s * 1000);
-        return s;
+        return s;*/
+
+        GlobalCoordinates sou = new GlobalCoordinates(latitude1, longitude1);
+        GlobalCoordinates tar = new GlobalCoordinates(latitude2, longitude2);
+        return new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, sou, tar)
+                .getEllipsoidalDistance();
     }
 }

+ 0 - 1
src/main/java/com/izouma/dingdong/web/BankCardController.java

@@ -12,7 +12,6 @@ import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
-import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;

+ 11 - 1
src/main/java/com/izouma/dingdong/web/SmsController.java

@@ -3,6 +3,7 @@ package com.izouma.dingdong.web;
 import com.izouma.dingdong.config.Constants;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.service.sms.SmsService;
+import com.izouma.dingdong.service.sms.TencentSmsService;
 import lombok.AllArgsConstructor;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -15,7 +16,8 @@ import java.util.regex.Pattern;
 @RequestMapping("/sms")
 @AllArgsConstructor
 public class SmsController {
-    private SmsService smsService;
+    private SmsService        smsService;
+    private TencentSmsService tencentSmsService;
 
     @GetMapping("/sendVerify")
     public String sendVerify(@RequestParam String phone) {
@@ -29,4 +31,12 @@ public class SmsController {
     public void verify(@RequestParam String phone, @RequestParam String code) throws SmsService.SmsVerifyException {
         smsService.verify(phone, code);
     }
+
+    @GetMapping("/tencentSend")
+    public String tencentSend(@RequestParam String phone) {
+        if (!Pattern.matches(Constants.Regex.PHONE, phone)) {
+            throw new BusinessException("请输入正确的手机号");
+        }
+        return tencentSmsService.sendVerify(phone);
+    }
 }

+ 6 - 0
src/main/resources/application.yaml

@@ -85,6 +85,12 @@ ksher:
     rAwvzvEpOJV5GxROt5Q4HwFwiZHsMprcC/pSK2P5Ik6ByW0kGcUhZTScl5Ttakhz+YouqF9F0id0DAj0AmLy9Z5sZQyzZVXcqeIsBJfmTYYznPKwvp
     tgP8DViBncwzIU6RDkrbfi47NCHfM6tDDjSw3ydovRR9fQPAkUAtIEVG4Xbm4kgixYF9NuDwPSsEh3MePD4lHhoACjV9SlVpmCOnCP6guvcziLrmPg
     FZUL4HL/31DBbNvk5WYlgirCewsY=
+
+tencent:
+  secret_id: AKIDNZU9ZiEJn03rPgNNq1wbhXIo43nFpqRl
+  secret_key: OWZNuCNK2UorFOG5mGXYCakenjfBAsZ0
+  sms_appid: 1400405705
+  sms_appkey: dd98bf6e6dc2f1f481702d1d5a303ddb
 ---
 
 spring:

+ 1 - 1
src/main/vue/src/views/Login.vue

@@ -220,7 +220,7 @@ export default {
             }
             this.sending = true;
             this.$http
-                .get("/sms/sendVerify", {
+                .get("/sms/tencentSend", {
                     phone: this.userInfo.phone
                 })
                 .then(res => {

+ 15 - 5
src/test/java/com/izouma/dingdong/service/MerchantServiceTest.java

@@ -10,6 +10,9 @@ import com.izouma.dingdong.service.merchant.MerchantSettingsService;
 import com.izouma.dingdong.utils.MapUtils;
 import com.izouma.dingdong.web.merchant.MerchantController;
 
+import org.gavaghan.geodesy.Ellipsoid;
+import org.gavaghan.geodesy.GeodeticCalculator;
+import org.gavaghan.geodesy.GlobalCoordinates;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -98,8 +101,12 @@ public class MerchantServiceTest {
         System.out.println(s);*/
 
         System.out.println(MapUtils.distance(lon3, lat3, lon5, lat5));
-        System.out.println(MapUtils.distance(lon3, lat3, lon4, lat4));
+//        System.out.println(MapUtils.distance(lon3, lat3, lon4, lat4));
 
+        GlobalCoordinates sou = new GlobalCoordinates(lat3, lon3);
+        GlobalCoordinates tar = new GlobalCoordinates(lat5, lon5);
+        System.out.println(new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, sou, tar)
+                .getEllipsoidalDistance());
 
     }
 
@@ -172,16 +179,19 @@ public class MerchantServiceTest {
 
     @Test
     public void test5() {
-        System.out.println(merchantSettingsService.recommended(1.0, 1.0, null, null, null, null, null, null, 82L).size());
+        System.out.println(merchantSettingsService.recommended(1.0, 1.0, null, null, null, null, null, null, 82L)
+                .size());
     }
 
     @Test
-    public void test6(){
-        System.out.println(merchantSettingsRepo.findById(190L).orElseThrow(new BusinessException("无商家")).getAutomaticOrder());
+    public void test6() {
+        System.out.println(merchantSettingsRepo.findById(190L)
+                .orElseThrow(new BusinessException("无商家"))
+                .getAutomaticOrder());
     }
 
     @Test
-    public void test7(){
+    public void test7() {
         System.out.println(merchantService.heatMap(118.774621, 31.977269));
     }
 

+ 1 - 0
src/test/java/com/izouma/dingdong/service/sms/SmsServiceTest.java

@@ -18,4 +18,5 @@ public class SmsServiceTest extends ApplicationTests {
     public void verify() throws SmsService.SmsVerifyException {
         smsService.verify("15077886171", "5274");
     }
+
 }

+ 64 - 0
src/test/java/com/izouma/dingdong/service/sms/TencentSmsTest.java

@@ -0,0 +1,64 @@
+package com.izouma.dingdong.service.sms;
+import com.izouma.dingdong.exception.BusinessException;
+import com.tencentcloudapi.common.Credential;
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
+import com.tencentcloudapi.sms.v20190711.SmsClient;
+import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
+import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
+import com.tencentcloudapi.sms.v20190711.models.SendStatus;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.websocket.SendResult;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class TencentSmsTest {
+    @Autowired
+    private TencentSmsService smsService;
+
+    @Test
+    public void test() {
+        // 短信应用SDK AppID   // 1400开头
+        String appid = "1400405705";
+        // 短信应用SDK AppKey
+        String appkey = "dd98bf6e6dc2f1f481702d1d5a303ddb";
+        // 短信模板ID,需要在短信应用中申请
+        String templateId = "676939";
+        // 签名,使用的是`签名内容`,而不是`签名ID`
+        String smsSign = "弗雷登";
+        String[] phone = {"+8618205083565"};
+
+        String[] param = {"1234"};
+        try {
+            SendSmsRequest req = new SendSmsRequest();
+            req.setPhoneNumberSet(phone);
+            req.setTemplateID(templateId);
+            req.setSign(smsSign);
+            req.setSmsSdkAppid(appid);
+            req.setTemplateParamSet(param);
+
+            Credential cred = new Credential("AKIDNZU9ZiEJn03rPgNNq1wbhXIo43nFpqRl", "OWZNuCNK2UorFOG5mGXYCakenjfBAsZ0");
+            SmsClient client = new SmsClient(cred,"");
+            client.SendSms(req);
+            SendSmsResponse resp = client.SendSms(req);
+            SendStatus sendStatus = resp.getSendStatusSet()[0];
+            if (!sendStatus.getCode().equals("Ok")) {
+                throw new BusinessException("发送失败,请稍后再试", sendStatus.getCode() + "," + sendStatus.getMessage());
+            }
+            System.out.println(sendStatus);
+
+        } catch (TencentCloudSDKException e) {
+            System.out.println(e.toString());
+        }
+    }
+
+    @Test
+    public void test1(){
+        SendResult result = new SendResult();
+        System.out.println(result.isOK());
+    }
+}