ソースを参照

增加一个登陆接口

slambb 4 年 前
コミット
bc5d7e1759

+ 132 - 2
src/main/java/com/td/boss/game/comusers/controller/ComUsersController.java

@@ -9,18 +9,26 @@ import com.td.boss.game.comusers.vo.ComUsersVo;
 import com.td.boss.game.comusers.service.ComUsersService;
 import com.td.boss.sys.syssetting.vo.SysSettingVo;
 import com.td.boss.util.JwtTokenUtil;
+import com.td.boss.util.MD5Util;
 import com.td.boss.util.RsaUtil;
 import com.td.boss.util.SysSettingUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.Resource;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
 
 import javax.script.ScriptException;
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 @Slf4j
 @RestController
@@ -32,6 +40,9 @@ public class ComUsersController extends CommonController<ComUsersVo, ComUsers, S
     @Autowired
     private JwtTokenUtil jwtTokenUtil;
 
+
+    private static String Key = "DH08hdf2n9df9hg2e";
+
     @Value("${spring.profiles.active}")
     private String active;
 
@@ -68,6 +79,12 @@ public class ComUsersController extends CommonController<ComUsersVo, ComUsers, S
         return Result.of(map);
     }
 
+    /**
+     * 该方法已废弃,后续使用 loginTokenAndVerification 来代替
+     * @param loginId
+     * @return
+     */
+    @Deprecated
     @GetMapping("loginToken")
     public Result<Map> userLogin(@RequestParam(value = "loginId",required = false)String loginId) {
         log.info("login active:"+active);
@@ -104,6 +121,119 @@ public class ComUsersController extends CommonController<ComUsersVo, ComUsers, S
         return Result.of(map);
     }
 
+    //    {
+    //        "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 签名地址
+    //        "id": 1, // 所属账户ID
+    //        "signature": "0x09fda37fcad4a9bb7b72f652323881a33847be3a44e4403c7a576b3383df30ec6e5e256f28fbdbac5eb0642f304f331783f36d70b5e6fd576818bc42e14624191c", // 用户私钥签名文本
+    //        "timestamp": 1641279989, // 签名时间戳,需要在1分钟内完成验签,否则本次签名失效
+    //        "sign": "794DA326066EAAC0F9E0D3E94E306B38" // 微信参数签名
+    //    }
+    //    {
+    //        "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 签名地址
+    //        "id": 1, // 所属账户ID
+    //        "signature": "0x09fda37fcad4a9bb7b72f652323881a33847be3a44e4403c7a576b3383df30ec6e5e256f28fbdbac5eb0642f304f331783f36d70b5e6fd576818bc42e14624191c", // 签名文本
+    //        "timestamp": 1641279989 // 签名时间戳,需要在1分钟内完成验签,否则本次签名失效
+    //    }
+    @PostMapping("loginTokenAndVerification")
+    public Result<Map> loginTokenAndVerification(
+            @RequestParam(value = "address")String address,
+            @RequestParam(value = "id")String id,
+            @RequestParam(value = "signature")String signature,
+            @RequestParam(value = "timestamp")Integer timestamp) {
+
+        log.info("loginTokenAndVerification active:"+active);
+        SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
+        parameters.put("address", address);
+        parameters.put("id", id);
+        parameters.put("signature", signature);
+        parameters.put("timestamp", timestamp);
+
+
+
+        String sign = createSign(parameters);
+        log.info("sign:"+sign);
+        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
+        form.add("address", address);
+        form.add("id", id);
+        form.add("signature", signature);
+        form.add("timestamp", timestamp);
+        form.add("sign",sign);
+        //验证地址
+        String DAppUrl = "https://yt.landownership.live/api/account/loginValidate";
+        RestTemplate restTemplate = new RestTemplate();
+        HttpHeaders headers = new HttpHeaders();
+        //设置content-type
+        MediaType type = MediaType.parseMediaType("multipart/form-data");
+        headers.setContentType(type);
+        // 以表单的方式提交
+        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+        //用HttpEntity封装整个请求报文
+        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
+
+        try {
+            String response = restTemplate.postForObject(DAppUrl,files, String.class);
+            log.info("response={}", response);
+        }catch (HttpClientErrorException e){
+            log.error(e.toString());
+        }
+
+        String userId ;
+        if(active.equals("dev")){
+            //if(loginId == null || !loginId.equals("1")){
+            //    return Result.of(null,false,"请输入参数 loginId = '1' ",ResultEnum.NO_PARAMETERS_CARRIED.getCode());
+            //}
+            //如果测试环境中,提供测试账号
+            userId = id;
+        }else{
+            // 正式环境
+            userId = id;
+        }
+
+        ComUsersVo comUsersVo = comUsersService.findByUserId(userId);
+
+        if(comUsersVo == null){
+            //如果没有用户信息
+            comUsersVo = new ComUsersVo();
+            comUsersVo.setUserId(userId);
+            comUsersVo.setGold(0);
+            comUsersVo.setDiamond(0);
+            comUsersVo.setCnt(0);
+            comUsersVo.setSnb(0);
+            comUsersService.save(comUsersVo);
+        }
+
+        //生成token
+        final String token = jwtTokenUtil.generateToken(comUsersVo.getUserId());
+        Map map = new HashMap();
+        map.put("active",active);
+        map.put("token",token);
+        return Result.of(map);
+    }
+
+    /**
+     * 微信支付签名算法sign
+     * @param parameters
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static String createSign(SortedMap<Object,Object> parameters){
+        StringBuffer sb = new StringBuffer();
+        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)
+        Iterator it = es.iterator();
+        while(it.hasNext()) {
+            Map.Entry entry = (Map.Entry)it.next();
+            String k = (String)entry.getKey();
+            Object v = entry.getValue();
+            if(null != v && !"".equals(v)
+                    && !"sign".equals(k) && !"key".equals(k)) {
+                sb.append(k + "=" + v + "&");
+            }
+        }
+        sb.append("key=" + Key);
+        String sign = MD5Util.getMD5(sb.toString()).toUpperCase();
+        return sign;
+    }
+
 
     /**
      * 获取用户信息