Jelajahi Sumber

新增营销宝数据同步接口

shenjingwei 2 bulan lalu
induk
melakukan
29aa451f29

+ 143 - 0
src/custom/common/crm/sso.java

@@ -1,5 +1,6 @@
 package common.crm;
 
+import beans.parameter.Parameter;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import common.AccessToken;
@@ -19,8 +20,10 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 import restcontroller.ClientUserInfo;
+import utility.tools.HttpRequest;
 import utility.tools.WebRequest;
 
+import java.net.http.HttpResponse;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.UUID;
@@ -162,6 +165,146 @@ public class sso extends Basic {
         }
     }
 
+    @RequestMapping("/yxb/logincheck")
+    @PostMapping
+    @ResponseBody
+    public JSONObject ssologincheck(HttpServletRequest request, HttpServletResponse response, @RequestBody String RequestContent) throws YosException {
+        JSONObject requestcontent = null;
+        try {
+            requestcontent = JSONObject.parseObject(JSONObject.parseObject(RequestContent).toJSONString());
+        } catch (Exception e) {
+            return new ReturnObject_Err().setErrMsg("请求正文格式错误,必须为JSONObject格式,请求内容:" + RequestContent);
+        }
+        String[] mustkeys = {"accesstoken", "accountno"};
+        for (String mustkey : mustkeys) {
+            if (!requestcontent.containsKey(mustkey)) {
+                return new ReturnObject_Err().setErrMsg("json缺少KEY" + mustkey);
+            }
+        }
+        String accesstoken = requestcontent.getString("accesstoken");
+        String accountno = requestcontent.getString("accountno");
+
+        JSONObject object = new JSONObject();
+        object.put("code", 0);
+        object.put("status", 0);
+        object.put("msg", "无效的token");
+        if (AccessToken.exists(accesstoken)) {
+            Row row = AccessToken.get(accesstoken).getUserRow();
+            if (accountno.equals(row.getString("accountno"))) {
+                object.put("code", 1);
+                object.put("status", 1);
+                object.put("msg", "token有效");
+            }
+        }
+        return object;
+    }
+
+
+    @RequestMapping("/yxb")
+    @PostMapping
+    @ResponseBody
+    public JSONObject loginFromYXB(HttpServletRequest request, HttpServletResponse response, @RequestBody String RequestContent) throws YosException {
+        HttpSession session = request.getSession(true);
+        String sessionID = session.getId();
+        DBConnect connect = new DBConnect();
+        /**
+         * 验证请求正文是否为规范的SONObject格式
+         */
+        JSONObject requestcontent = null;
+        try {
+            requestcontent = JSONObject.parseObject(JSONObject.parseObject(RequestContent).toJSONString());
+        } catch (Exception e) {
+            this.createLoginLog(RequestContent, request, false, new JSONArray(), "请求格式不正确!");
+            return new ReturnObject_Err().setErrMsg("请求正文格式错误,必须为JSONObject格式");
+        }
+
+        /**
+         * 验证请求正文中是否包含必填的键值
+         */
+        String[] mustkeys = {"accountno", "password", "systemclient"};
+        for (String mustkey : mustkeys) {
+            if (!requestcontent.containsKey(mustkey)) {
+                this.createLoginLog(RequestContent, request, false, new JSONArray(), "json缺少KEY" + mustkey);
+                return new ReturnObject_Err().setErrMsg("json缺少KEY" + mustkey);
+            }
+        }
+        String accountno = requestcontent.getString("accountno");
+        String inputpassword = requestcontent.getString("password");//即营销宝的accesstoken
+
+        String systemclient = requestcontent.getString("systemclient");//登陆操作端
+        if (connect.runSqlQuery("select systemclient from sys_systemclient where systemclient='" + systemclient + "'").isEmpty()) {
+            this.createLoginLog(RequestContent, request, false, new JSONArray(), "不存在的操作端");
+            return new ReturnObject_Err().setErrMsg("不存在的操作端" + systemclient);
+        }
+
+        boolean passwordVerificationPassed = false;//密码校验是否通过,默认不通过
+        JSONObject yxbtokencheck = new JSONObject();
+        yxbtokencheck.put("accesstoken", inputpassword);
+        yxbtokencheck.put("accountno", accountno);
+
+        HttpResponse<String> post = new HttpRequest().POST(yxbtokencheck.toString(), Parameter.getString("yxb_sso_login_url") + "/logincheck");
+        String body = post.body();
+        JSONObject jsonObject = JSONObject.parseObject(body);
+        if (jsonObject.getIntValue("code") == 1) {//token验证有效
+            Rows users = connect.runSqlQuery("select userid,status,failedlogins from sys_users where accountno='" + accountno + "'");
+            if (users.isNotEmpty()) {
+                Row user = users.get(0);
+                String status = user.getString("status");
+                if (status.equalsIgnoreCase("ACTIVE")) {
+                    passwordVerificationPassed = true;
+                }
+            }
+        }
+        if (passwordVerificationPassed) {
+            QuerySQL userQuery = SQLFactory.createQuerySQL(connect, "sys_users", "userid", "name", "createdate", "phonenumber", "status", "accountno", "passwordchangedate", "isthirddeveloper").setTableAlias("t1");
+            userQuery.addJoinTable(BaseClass.JOINTYPE.inner, "sys_usersite", "t2", "t1.userid=t2.userid", "usersiteid", "siteid", "usertype");
+            userQuery.addJoinTable(BaseClass.JOINTYPE.inner, "sys_site", "t3", "t2.siteid=t3.siteid", "sitename");
+            userQuery.addJoinTable(BaseClass.JOINTYPE.left, "sys_hr", "t4", "t1.userid=t4.userid and t2.siteid=t4.siteid", "departmentid", "hrid");
+            userQuery.addJoinTable(BaseClass.JOINTYPE.inner, "sys_site_parameter", "t5", "t2.siteid=t5.siteid and t5.loginmode_account=1");
+            userQuery.setWhere("t1.status", "ACTIVE");
+            userQuery.setWhere("t1.accountno", accountno);
+
+            Rows userrows = userQuery.query();
+            if (userrows.isEmpty()) {
+                JSONObject object = new JSONObject();
+                object.put("code", 0);
+                object.put("status", 0);
+                object.put("msg", "没有为当前账号配分配有效的角色");
+                this.createLoginLog(RequestContent, request, false, new JSONArray(), "没有为当前账号配分配有效的角色");
+                return object;
+            }
+
+            long userid = 0;
+            for (Row userrow : userrows) {
+                userid = userrow.getLong("userid");//用户id
+                long usersiteid = userrow.getLong("usersiteid");//用户角色id
+
+                userrow.put("logintime", BaseClass.getDateTime_Str());
+                userrow.put("systemclient", systemclient);
+                userrow.put("ip", WebRequest.getRequestIP(request));
+                userrow.put("useragent", WebRequest.getUserAgent(request));
+                userrow.putAll(ClientUserInfo.setLoginReturn(usersiteid, userrow));
+                userrow.put("token", AccessToken.create(systemclient, sessionID, userrow).getToken());
+            }
+            JSONObject object = new JSONObject();
+            object.put("code", 1);
+            object.put("status", 1);
+            object.put("msg", "成功");
+            object.put("account_list", userrows.toJsonArray());
+            String remind = remindChangePassword(userid, inputpassword, userrows.toArray("siteid"));
+            object.put("remindchangepassword", remind.isEmpty() ? 0 : 1);
+            object.put("remindchangepassword_str", remind);
+            this.createLoginLog(RequestContent, request, true, userrows.toJsonArray("userid"), "null");
+            return object;
+        } else {
+            JSONObject object = new JSONObject();
+            object.put("code", 0);
+            object.put("status", 0);
+            object.put("msg", "登录验证已失效,请重新登录CRM!");
+            this.createLoginLog(RequestContent, request, false, new JSONArray(), "登录验证已失效,请重新登录CRM!");
+            return object;
+        }
+    }
 
     private boolean createAgentUser(DBConnect dbConnect, long sys_enterpriseid, String accountno, String name, String phonenumber) {
         try {

+ 10 - 0
src/custom/restcontroller/R.java

@@ -7055,6 +7055,16 @@ public class R {
         public static class v1 {
         }
     }
+
+    public static class ID2026030512342901 {
+        public static class v1 {
+        }
+    }
+
+    public static class ID2026030513573201 {
+        public static class v1 {
+        }
+    }
 }
 
 

+ 79 - 0
src/custom/restcontroller/yxb/yxb.java

@@ -0,0 +1,79 @@
+package restcontroller.yxb;
+
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import common.Controller;
+import common.YosException;
+import common.annotation.API;
+import common.data.*;
+import lombok.Getter;
+import lombok.Setter;
+import restcontroller.R;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+public class yxb extends Controller {
+    public yxb(JSONObject content) throws YosException {
+        super(content);
+    }
+
+    @API(title = "营销宝数据同步查询", apiversion = R.ID2026030512342901.v1.class)
+    public String getdata() throws YosException {
+        String lastrequestdata = content.getStringValue("lastrequestdata");//上次请求的时间
+        HashMap<String, YxbResult> resultData = new HashMap<>();//数据更新对象
+
+        Rows yxb_datasyncRows;
+        if (lastrequestdata.isEmpty()) {
+            yxb_datasyncRows = dbConnect.runSqlQuery("select ownertable,ownerid from yxb_datasync");
+        } else {
+            yxb_datasyncRows = dbConnect.runSqlQuery("select ownertable,ownerid from yxb_datasync where changedate>'" + lastrequestdata + "'");
+        }
+        RowsMap rowsMap = yxb_datasyncRows.toRowsMap("ownertable");
+        for (String tablename : rowsMap.keySet()) {
+            String uniquecolumnname = getuniquecolumnname(tablename);
+            QuerySQL querySQL;
+            ArrayList<String> idlist = rowsMap.get(tablename).toArrayList("ownerid");//当前表的所有待更新数据ID
+            if (tablename.equalsIgnoreCase("sys_userrole")) {
+                //如果是账号角色授权表,则单独处理
+                querySQL = SQLFactory.createQuerySQL(this, tablename, "userid").setTableAlias("t1");
+                querySQL.addJoinTable(JOINTYPE.inner, "sys_role", "t2", "t1.roleid=t2.roleid", "rolename", "remarks");
+                querySQL.setWhere(uniquecolumnname, idlist);
+            } else {
+                querySQL = SQLFactory.createQuerySQL(this, tablename);
+                querySQL.setWhere(uniquecolumnname, idlist);
+            }
+            Rows datarows = querySQL.query();//查询出所有的待同步的数据
+            idlist.removeAll(datarows.toArrayList(uniquecolumnname));//排除存在的数据id,即为已经删除的数据的数据ID
+            resultData.put(tablename, new YxbResult(datarows, idlist));
+        }
+        return getSucReturnObject().setData(JSONObject.from(resultData)).toString();
+    }
+
+    @API(title = "营销宝数据同步确认", apiversion = R.ID2026030513573201.v1.class)
+    public String getdatareback() throws YosException {
+        String lastrequestdata = content.getStringValue("lastrequestdata");//上次请求的时间
+        String tablename = content.getStringValue("tablename");
+        JSONArray ids = content.getJSONArray("ids");
+        if (!ids.isEmpty()) {
+            DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "yxb_datasync");
+            deleteSQL.setWhere("ownertable", tablename);
+            deleteSQL.setWhere("ownerid", ids);
+            deleteSQL.setWhere("changedate", Op.LTE, lastrequestdata);//将该时间之前的记录全部删除
+            deleteSQL.delete();
+        }
+        return getSucReturnObject().toString();
+    }
+
+    @Getter
+    @Setter
+    class YxbResult {
+        public Rows dataRows;
+        public ArrayList<String> deleteIDs;
+
+        public YxbResult(Rows dataRows, ArrayList<String> deleteIDs) {
+            this.dataRows = dataRows;
+            this.deleteIDs = deleteIDs;
+        }
+    }
+}