Переглянути джерело

fixed 后台店铺接口优化

fushengqian 3 місяців тому
батько
коміт
1381384a9b

+ 8 - 0
fuint-application/src/main/java/com/fuint/common/service/StoreService.java

@@ -132,4 +132,12 @@ public interface StoreService extends IService<MtStore> {
      * */
     void deleteStoreByMerchant(Integer merchantId);
 
+    /**
+     * 根据地址获取经纬度
+     *
+     * @param addr 地址
+     * @return
+     * */
+    Map<String, Object> getLatAndLngByAddress(String addr);
+
 }

+ 90 - 0
fuint-application/src/main/java/com/fuint/common/service/impl/StoreServiceImpl.java

@@ -1,5 +1,8 @@
 package com.fuint.common.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -29,13 +32,22 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
+import org.springframework.core.env.Environment;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
+import java.net.MalformedURLException;
 import java.util.*;
+import java.net.URL;
+import java.net.URLConnection;
 
 /**
  * 店铺管理业务实现类
@@ -49,6 +61,11 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
 
     private static final Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);
 
+    /**
+     * 系统环境变量
+     * */
+    private Environment env;
+
     private MtStoreMapper mtStoreMapper;
 
     private MtMerchantMapper mtMerchantMapper;
@@ -490,4 +507,77 @@ public class StoreServiceImpl extends ServiceImpl<MtStoreMapper, MtStore> implem
         mtStoreMapper.deleteStoreByMerchant(merchantId);
     }
 
+    /**
+     * 根据地址获取经纬度
+     *
+     * @param addr 地址
+     * @return
+     * */
+    public Map<String, Object> getLatAndLngByAddress(String addr) {
+        String key = env.getProperty("amap.key");
+        Map<String, Object> map = new HashMap<>();
+        if (key == null) {
+            map.put("lat", "");
+            map.put("lng", "");
+            return map;
+        }
+
+        String address = "";
+        try {
+            address = java.net.URLEncoder.encode(addr,"UTF-8");
+        } catch (UnsupportedEncodingException e1) {
+            e1.printStackTrace();
+        }
+
+        // key如果失效了就去高德地图官网申请
+        String url =  "https://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key="+key;
+
+        URL myURL = null;
+        URLConnection httpsConn;
+        // 进行转码
+        try {
+            myURL = new URL(url);
+        } catch (MalformedURLException e) {
+            // empty
+        }
+        StringBuffer sb = new StringBuffer();
+        try {
+            httpsConn = myURL.openConnection();
+            if (httpsConn != null) {
+                InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
+                BufferedReader br = new BufferedReader(insr);
+                String data = null;
+                while ((data = br.readLine()) != null) {
+                    sb.append(data);
+                }
+                insr.close();
+            }
+        } catch (IOException e) {
+            logger.error("根据地址获取经纬度失败:{}", e.getMessage());
+        }
+
+        JSONObject resultJson = JSON.parseObject(sb.toString());
+        JSONArray geocodes = resultJson.getJSONArray("geocodes");
+
+        String lat = "";
+        String lng = "";
+
+        if (geocodes != null) {
+            JSONObject jsonObject = geocodes.getJSONObject(0);
+            String location = jsonObject.getString("location");
+            if (org.apache.commons.lang.StringUtils.isNotEmpty(location)) {
+                String latAndLng[] = location.split(",");
+                if (latAndLng.length == 2) {
+                    lat = latAndLng[1];
+                    lng = latAndLng[0];
+                }
+            }
+        }
+
+        map.put("lat", lat);
+        map.put("lng", lng);
+
+        return map;
+    }
+
 }

+ 0 - 71
fuint-application/src/main/java/com/fuint/common/util/CommonUtil.java

@@ -1,8 +1,5 @@
 package com.fuint.common.util;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.JSONArray;
 import com.fuint.utils.StringUtil;
 import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
@@ -323,74 +320,6 @@ public class CommonUtil {
         return dateFormater.format(date);
     }
 
-    /**
-     * 根据地址获取经纬度
-     *
-     * @param addr 地址
-     * @return
-     * */
-    public static Map<String, Object> getLatAndLngByAddress(String addr) {
-        String address = "";
-        try {
-            address = java.net.URLEncoder.encode(addr,"UTF-8");
-        } catch (UnsupportedEncodingException e1) {
-            e1.printStackTrace();
-        }
-
-        // key如果失效了就去高德地图官网申请
-        String url =  "https://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key="+"4d57813b7b9157d66899cc4c1f22dc04";
-
-        URL myURL = null;
-        URLConnection httpsConn;
-        // 进行转码
-        try {
-            myURL = new URL(url);
-        } catch (MalformedURLException e) {
-            // empty
-        }
-        StringBuffer sb = new StringBuffer();
-        try {
-            httpsConn = myURL.openConnection();
-            if (httpsConn != null) {
-                InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
-                BufferedReader br = new BufferedReader(insr);
-                String data = null;
-                while ((data = br.readLine()) != null) {
-                    sb.append(data);
-                }
-                insr.close();
-            }
-        } catch (IOException e) {
-
-        }
-
-        Map<String, Object> map = new HashMap<>();
-        JSONObject resultJson = JSON.parseObject(sb.toString());
-        JSONArray geocodes = resultJson.getJSONArray("geocodes");
-
-
-        String lat = "";
-        String lng = "";
-
-        if (geocodes != null) {
-            JSONObject jsonObject = geocodes.getJSONObject(0);
-            String location = jsonObject.getString("location");
-
-            if (org.apache.commons.lang.StringUtils.isNotEmpty(location)) {
-                String latAndLng[] = location.split(",");
-                if (latAndLng.length == 2) {
-                    lat = latAndLng[1];
-                    lng = latAndLng[0];
-                }
-            }
-        }
-
-        map.put("lat", lat);
-        map.put("lng", lng);
-
-        return map;
-    }
-
     /**
      * 去除待带script、src的语句,转义替换后的value值
      *

+ 19 - 72
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendStoreController.java

@@ -15,12 +15,14 @@ import com.fuint.framework.pagination.PaginationRequest;
 import com.fuint.framework.pagination.PaginationResponse;
 import com.fuint.framework.web.BaseController;
 import com.fuint.framework.web.ResponseObject;
+import com.fuint.module.backendApi.request.StoreSubmitRequest;
 import com.fuint.repository.model.MtMerchant;
 import com.fuint.repository.model.MtStore;
 import com.fuint.utils.StringUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.BeanUtils;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletRequest;
@@ -176,95 +178,40 @@ public class BackendStoreController extends BaseController {
     @RequestMapping(value = "/save", method = RequestMethod.POST)
     @CrossOrigin
     @PreAuthorize("@pms.hasPermission('store:add')")
-    public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
-        String token = request.getHeader("Access-Token");
-        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
-
-        StoreDto storeInfo = new StoreDto();
-        String storeId = params.get("id").toString();
-        String storeName = CommonUtil.replaceXSS(params.get("name").toString());
-        String contact = CommonUtil.replaceXSS(params.get("contact").toString());
-        String phone = CommonUtil.replaceXSS(params.get("phone").toString());
-        String description = params.get("description") == null ? "" : CommonUtil.replaceXSS(params.get("description").toString());
-        String isDefault = params.get("isDefault") == null ? YesOrNoEnum.NO.getKey() : CommonUtil.replaceXSS(params.get("isDefault").toString());
-        String address = params.get("address") == null ? "" : CommonUtil.replaceXSS(params.get("address").toString());
-        String hours = params.get("hours") == null ? "" : CommonUtil.replaceXSS(params.get("hours").toString());
-        String latitude = params.get("latitude") == null ? "" : CommonUtil.replaceXSS(params.get("latitude").toString());
-        String longitude = params.get("longitude") == null ? "" : CommonUtil.replaceXSS(params.get("longitude").toString());
-        String wxMchId = params.get("wxMchId") == null ? null : CommonUtil.replaceXSS(params.get("wxMchId").toString());
-        String wxApiV2 = params.get("wxApiV2") == null ? null : CommonUtil.replaceXSS(params.get("wxApiV2").toString());
-        String wxCertPath = params.get("wxCertPath") == null ? null : CommonUtil.replaceXSS(params.get("wxCertPath").toString());
-        String alipayAppId = params.get("alipayAppId") == null ? null : CommonUtil.replaceXSS(params.get("alipayAppId").toString());
-        String alipayPrivateKey = params.get("alipayPrivateKey") == null ? null : CommonUtil.replaceXSS(params.get("alipayPrivateKey").toString());
-        String alipayPublicKey = params.get("alipayPublicKey") == null ? null : CommonUtil.replaceXSS(params.get("alipayPublicKey").toString());
-        String logo = params.get("logo") == null ? "" : CommonUtil.replaceXSS(params.get("logo").toString());
-        String license = params.get("license") == null ? "" : CommonUtil.replaceXSS(params.get("license").toString());
-        String creditCode = params.get("creditCode") == null ? "" : CommonUtil.replaceXSS(params.get("creditCode").toString());
-        String bankName = params.get("bankName") == null ? "" : CommonUtil.replaceXSS(params.get("bankName").toString());
-        String bankCardName = params.get("bankCardName") == null ? "" : CommonUtil.replaceXSS(params.get("bankCardName").toString());
-        String bankCardNo = params.get("bankCardNo") == null ? "" : CommonUtil.replaceXSS(params.get("bankCardNo").toString());
-        String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
-        String merchantId = params.get("merchantId").toString();
+    public ResponseObject saveHandler(HttpServletRequest request, @RequestBody StoreSubmitRequest storeParam) throws BusinessCheckException {
+        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(request.getHeader("Access-Token"));
+        StoreDto storeDto = new StoreDto();
 
-        if ((StringUtil.isEmpty(latitude) || StringUtil.isEmpty(longitude)) && StringUtil.isNotEmpty(address)) {
-            Map<String, Object> latAndLng = CommonUtil.getLatAndLngByAddress(address);
-            latitude = latAndLng.get("lat").toString();
-            longitude = latAndLng.get("lng").toString();
+        if ((StringUtil.isEmpty(storeParam.getLatitude()) || StringUtil.isEmpty(storeParam.getLongitude())) && StringUtil.isNotEmpty(storeParam.getAddress())) {
+            Map<String, Object> latAndLng = storeService.getLatAndLngByAddress(storeParam.getAddress());
+            storeParam.setLatitude(latAndLng.get("lat").toString());
+            storeParam.setLongitude(latAndLng.get("lng").toString());
         }
 
         if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
-            if (StringUtil.isEmpty(storeId)) {
+            if (storeParam.getId() == null) {
                 return getFailureResult(201, "店铺帐号不能新增店铺,请使用商户帐号添加!");
             }
-            storeId = accountInfo.getStoreId().toString();
+            storeParam.setId(accountInfo.getStoreId());
         }
         if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
-            merchantId = accountInfo.getMerchantId().toString();
+            storeParam.setMerchantId(accountInfo.getMerchantId());
         }
 
-        storeInfo.setName(storeName);
-        storeInfo.setLogo(logo);
-        storeInfo.setContact(contact);
-        storeInfo.setPhone(phone);
-        storeInfo.setDescription(description);
-        storeInfo.setIsDefault(isDefault);
-        storeInfo.setAddress(address);
-        storeInfo.setHours(hours);
-        storeInfo.setLatitude(latitude);
-        storeInfo.setLongitude(longitude);
-        storeInfo.setStatus(status);
-        storeInfo.setWxMchId(wxMchId);
-        storeInfo.setWxApiV2(wxApiV2);
-        storeInfo.setWxCertPath(wxCertPath);
-        storeInfo.setLicense(license);
-        storeInfo.setCreditCode(creditCode);
-        storeInfo.setBankName(bankName);
-        storeInfo.setBankCardName(bankCardName);
-        storeInfo.setBankCardNo(bankCardNo);
-        storeInfo.setAlipayAppId(alipayAppId);
-        storeInfo.setAlipayPrivateKey(alipayPrivateKey);
-        storeInfo.setAlipayPublicKey(alipayPublicKey);
-        if (StringUtil.isNotEmpty(merchantId)) {
-            storeInfo.setMerchantId(Integer.parseInt(merchantId));
-        }
-        if (StringUtil.isEmpty(storeName)) {
+        BeanUtils.copyProperties(storeParam, storeDto);
+        if (StringUtil.isEmpty(storeParam.getName())) {
             return getFailureResult(201, "店铺名称不能为空");
         } else {
-            if (!StringUtil.isNotEmpty(storeName)) {
-                StoreDto storeDto = storeService.queryStoreByName(storeName);
-                if (storeDto != null && storeDto.getName().equals(storeName) && !storeDto.getId().equals(storeId)) {
+            if (!StringUtil.isNotEmpty(storeParam.getName())) {
+                StoreDto store = storeService.queryStoreByName(storeParam.getName());
+                if (store != null && store.getName().equals(storeParam.getName()) && !store.getId().equals(storeParam.getId())) {
                     return getFailureResult(201, "该店铺名称已经存在");
                 }
             }
         }
 
-        // 修改店铺
-        if (StringUtil.isNotEmpty(storeId)) {
-            storeInfo.setId(Integer.parseInt(storeId));
-        }
-
-        storeInfo.setOperator(accountInfo.getAccountName());
-        storeService.saveStore(storeInfo);
+        storeDto.setOperator(accountInfo.getAccountName());
+        storeService.saveStore(storeDto);
 
         return getSuccessResult(true);
     }

+ 88 - 0
fuint-application/src/main/java/com/fuint/module/backendApi/request/StoreSubmitRequest.java

@@ -0,0 +1,88 @@
+package com.fuint.module.backendApi.request;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import java.io.Serializable;
+
+/**
+ * 提交店铺信息请求参数
+ *
+ * Created by FSQ
+ * CopyRight https://www.fuint.cn
+ */
+@Data
+public class StoreSubmitRequest implements Serializable {
+
+    @ApiModelProperty(value="店铺ID", name="id")
+    private Integer id;
+
+    @ApiModelProperty(value="商户ID", name="merchantId")
+    private Integer merchantId;
+
+    @ApiModelProperty(value="店铺名称", name="name")
+    private String name;
+
+    @ApiModelProperty(value="联系人", name="contact")
+    private String contact;
+
+    @ApiModelProperty(value="联系手机号", name="phone")
+    private String phone;
+
+    @ApiModelProperty(value="备注信息", name="description")
+    private String description;
+
+    @ApiModelProperty(value="是否默认店铺", name="isDefault")
+    private String isDefault;
+
+    @ApiModelProperty(value="店铺地址", name="address")
+    private String address;
+
+    @ApiModelProperty(value="营业时间", name="hours")
+    private String hours;
+
+    @ApiModelProperty(value="维度", name="latitude")
+    private String latitude;
+
+    @ApiModelProperty(value="经度", name="longitude")
+    private String longitude;
+
+    @ApiModelProperty(value="微信商户号", name="wxMchId")
+    private String wxMchId;
+
+    @ApiModelProperty(value="支付秘钥apiV2", name="wxApiV2")
+    private String wxApiV2;
+
+    @ApiModelProperty(value="微信支付证书", name="wxCertPath")
+    private String wxCertPath;
+
+    @ApiModelProperty(value="支付宝appId", name="alipayAppId")
+    private String alipayAppId;
+
+    @ApiModelProperty(value="支付宝私钥", name="alipayPrivateKey")
+    private String alipayPrivateKey;
+
+    @ApiModelProperty(value="支付宝公钥", name="alipayPublicKey")
+    private String alipayPublicKey;
+
+    @ApiModelProperty(value="店铺LOGO", name="logo")
+    private String logo;
+
+    @ApiModelProperty(value="营业执照", name="license")
+    private String license;
+
+    @ApiModelProperty(value="统一社会信用码", name="creditCode")
+    private String creditCode;
+
+    @ApiModelProperty(value="银行名称", name="bankName")
+    private String bankName;
+
+    @ApiModelProperty(value="银行户名", name="bankCardName")
+    private String bankCardName;
+
+    @ApiModelProperty(value="银行卡号", name="bankCardNo")
+    private String bankCardNo;
+
+    @ApiModelProperty(value="状态", name="status")
+    private String status;
+
+}