Explorar el Código

绑定企业接口

ximinghao hace 3 meses
padre
commit
885efb2735

+ 42 - 6
src/main/java/com/skyversation/xjcy/controller/OtherController.java

@@ -1,6 +1,9 @@
 package com.skyversation.xjcy.controller;
 
-import com.skyversation.xjcy.service.SerialNumberGenerator;
+import com.alibaba.fastjson.JSONObject;
+import com.skyversation.xjcy.dms.DMSColumn;
+import com.skyversation.xjcy.dms.DMSService;
+import com.skyversation.xjcy.service.AuthService;
 import com.skyversation.xjcy.service.WeChatService;
 import com.skyversation.xjcy.util.MessageManage;
 import lombok.extern.slf4j.Slf4j;
@@ -11,8 +14,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import javax.servlet.http.HttpServletRequest;
-import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static com.skyversation.xjcy.service.AuthService.RESPONSE_FIELD_USERNAME;
 
 @Slf4j
 @Validated
@@ -21,14 +26,45 @@ import java.util.Arrays;
 public class OtherController {
 
     private final WeChatService weChatService;
+    private final AuthService authService;
+    private final DMSService dMSService;
 
-    public OtherController(WeChatService weChatService) {
+    public OtherController(WeChatService weChatService, AuthService authService, DMSService dMSService) {
         this.weChatService = weChatService;
+        this.authService = authService;
+        this.dMSService = dMSService;
     }
 
     @RequestMapping(value = "/wxacode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public ResponseEntity<byte[]> serialNumber(@RequestParam(required = false)String path,
-                                       @RequestParam(required = false)String scene) {
+    public ResponseEntity<byte[]> wxacode(@RequestParam(required = false) String path,
+                                          @RequestParam(required = false) String scene) {
         return weChatService.createWxacode(path, scene);
     }
+
+    @RequestMapping(value = "/bindEnterprise")
+    public String bindEnterprise(@RequestParam(required = false) String enterprise, @RequestParam(required = false) String token) {
+        if (enterprise != null && enterprise.isEmpty()) enterprise = null;
+        if (!authService.checkToken(token)) {
+            return MessageManage.error(-1, "请提供有效token");
+        }
+        JSONObject userContent = authService.getUserContentByToken(token);
+        String usercode = userContent.getString(RESPONSE_FIELD_USERNAME);
+
+        //更新dms记录
+        List<JSONObject> userJsons = dMSService.getUserJSONByCode(usercode, authService.getTokenOfServiceAccount());
+        if (!userJsons.isEmpty()) {
+            JSONObject userJSON = userJsons.get(0);
+            if (userJSON != null) {
+                userJSON.put("c_gl_gs", enterprise);
+                dMSService.updateToDms(Collections.singletonList(userJSON), authService.getTokenOfServiceAccount(), DMSColumn.USER);
+            }
+        }
+        if (enterprise == null) {
+            authService.deUpgradeRoleEnterprise(userContent);
+            return MessageManage.successWithNoData("成功解除此用户的相关绑定");
+        } else {
+            authService.upgradeRoleEnterprise(userContent);
+            return MessageManage.successWithNoData("成功绑定到对应企业");
+        }
+    }
 }

+ 4 - 3
src/main/java/com/skyversation/xjcy/dms/DMSService.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.serializer.SerializerFeature;
 import com.skyversation.xjcy.bean.*;
+import com.skyversation.xjcy.service.AuthService;
 import com.skyversation.xjcy.util.HttpUtil;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.Resource;
@@ -236,11 +237,11 @@ public class DMSService {
         return queryDmsList(request, token, DMSColumn.CLUE.getId());
     }
 
-    public List<JSONObject> getUserJSONByCode(String userCode) {
+    public List<JSONObject> getUserJSONByCode(String userCode,String token) {
         DMSRequest request = new DMSRequest();
         request.type = DMSRequest.DMSRequestType.List;
-        request.addWhere("user_code", "1", userCode);
-        List<JSONObject> result = queryDmsList(request, DMSColumn.USER.getId(), DMSColumn.USER.getId());
+        request.addWhere("c_usercode", "1", userCode);
+        List<JSONObject> result = queryDmsList(request, token, DMSColumn.USER.getId());
         return result;
     }
 

+ 37 - 20
src/main/java/com/skyversation/xjcy/service/AuthService.java

@@ -67,6 +67,10 @@ public class AuthService {
      * 删除用户路径
      */
     private static final String DELETE_USER = "/user/deleteUser";
+    /**
+     * 按TOKEN获取用户详情接口
+     */
+    private static final String USER_CONTENT_BY_TOKEN = "/user/getUserByToken";
 
     /**
      * 微信账号用户名前缀
@@ -112,7 +116,7 @@ public class AuthService {
     /**
      * 响应字段:用户名
      */
-    private static final String RESPONSE_FIELD_USERNAME = "username";
+    public static final String RESPONSE_FIELD_USERNAME = "username";
     /**
      * 相应字段:用户角色Id
      */
@@ -314,10 +318,40 @@ public class AuthService {
         return phoneService.sendCode(phone);
     }
 
-    public void bindEnterprise(String token, String enterpriseId) {
+    /**
+     * 升级用户为企业用户权限组
+     */
+    public void upgradeRoleEnterprise(JSONObject userContent) {
+        updateRole(userContent, Collections.singletonList("40"), Collections.emptyList(), "11");
+        updateRole(userContent, Collections.singletonList("44"), Collections.emptyList(), "12");
+    }
 
+    /**
+     * 降级用户使其失去企业用户权限组
+     */
+    public void deUpgradeRoleEnterprise(JSONObject userContent) {
+        updateRole(userContent, Collections.emptyList(), Collections.singletonList("40"), "11");
+        updateRole(userContent, Collections.emptyList(), Collections.singletonList("44"), "12");
     }
 
+    /**
+     * 通过token获取用户auth信息的content部分
+     * @param token 需要获取信息的用户的token
+     * @return 用户auth接口返回的content部分
+     */
+    public JSONObject getUserContentByToken(String token) {
+        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
+        params.add("token", token);
+        params.add("serviceId",0);
+        Map<String, String> header = new HashMap<>();
+        header.put("token", token);
+        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.requestPost(oauthPath + USER_CONTENT_BY_TOKEN, params, header));
+        if (isSuccess(jsonObject)) {
+            return jsonObject.getJSONObject(RESPONSE_FIELD_CONTENT);
+        }else {
+            throw new RuntimeException(jsonObject.getString(RESPONSE_FIELD_MESSAGE));
+        }
+    }
     // ============================ 私有方法 ============================
 
     /**
@@ -386,7 +420,6 @@ public class AuthService {
         HttpUtil.requestPost(oauthPath + DELETE_USER, params, header);
     }
 
-
     /**
      * 初始化用户数据
      */
@@ -444,29 +477,13 @@ public class AuthService {
      * 检查用户账户DMS数据存在性
      */
     private JSONObject getUserDmsData(String userCode) {
-        List<JSONObject> js = dMSService.getUserJSONByCode(userCode);
+        List<JSONObject> js = dMSService.getUserJSONByCode(userCode,getTokenOfServiceAccount());
         if (!js.isEmpty()) {
             return js.get(0);
         }
         return null;
     }
 
-    /**
-     * 升级用户为企业用户权限组
-     */
-    private void upgradeRoleEnterprise(JSONObject userContent) {
-        updateRole(userContent, Collections.singletonList("40"), Collections.emptyList(), "11");
-        updateRole(userContent, Collections.singletonList("44"), Collections.emptyList(), "12");
-    }
-
-    /**
-     * 降级用户使其失去企业用户权限组
-     */
-    private void deUpgradeRoleEnterprise(JSONObject userContent) {
-        updateRole(userContent, Collections.emptyList(), Collections.singletonList("40"), "11");
-        updateRole(userContent, Collections.emptyList(), Collections.singletonList("44"), "12");
-    }
-
     /**
      * 尝试注册并初始化用户
      */