eganwu 10 місяців тому
батько
коміт
73f8b2315d

+ 29 - 0
src/custom/restcontroller/common/usercenter/UserHelper.java

@@ -0,0 +1,29 @@
+package restcontroller.common.usercenter;
+
+import common.BaseClass;
+import common.Controller;
+import common.YosException;
+import common.data.Rows;
+import utility.wechat.miniprogram.WechatMiniProgram;
+import utility.wechat.miniprogram.WechatUserID;
+
+public class UserHelper extends BaseClass {
+
+
+    public static WechatUserID getWechatUserID(Controller controller) throws YosException {
+        WechatMiniProgram wechatMiniProgram = new WechatMiniProgram(controller.systemclient);
+        return wechatMiniProgram.getWechatUserID(controller.content.getString("wechat_code"));
+
+    }
+
+    public static Rows getOpenidsRows(Controller controller, long userid) throws YosException {
+        Rows rows = controller.dbConnect.runSqlQuery("select * from sys_wechatapp_openids where userid='" + userid + "' and systemclient='" + controller.systemclient + "'");
+        return rows;
+    }
+
+    public static Rows getUnionidsRows(Controller controller, long userid) throws YosException {
+        Rows rows = controller.dbConnect.runSqlQuery("select * from sys_wechatapp_unionids where userid='" + userid + "'");
+        return rows;
+    }
+
+}

+ 47 - 27
src/custom/restcontroller/common/usercenter/usercenter.java

@@ -309,45 +309,50 @@ public class usercenter extends Controller {
     @API(title = "微信账号绑定", apiversion = R.ID20240516090402.v1.class, accesstoken = false)
     @CACHEING_CLEAN(cms = {@cm(clazz = usercenter.class, method = {"queryUserMsg"})})
     public String WechatBinding() throws YosException {
-        String wechat_code = content.getString("wechat_code");
+
+        String openid = UserHelper.getWechatUserID(this).getOpenid();
+        String unionid = UserHelper.getWechatUserID(this).getUnionid();
         boolean isbinging = content.getBooleanValue("isbinging");
-        Long sys_enterprise_hrid = 0L;
-        if (content.containsKey("sys_enterprise_hrid") && content.getLongValue("sys_enterprise_hrid") > 0) {
-            sys_enterprise_hrid = content.getLongValue("sys_enterprise_hrid");
+        long sys_enterprise_hrid = content.getLongValue("sys_enterprise_hrid");
+        long logoutUserid = userid;
+        boolean isleader = false;
+
+        if (isbinging && sys_enterprise_hrid > 0) {
             Rows rows = dbConnect.runSqlQuery("SELECT * from sys_enterprise_hr WHERE sys_enterprise_hrid='" + sys_enterprise_hrid + "' and siteid ='" + siteid + "'");
-            if (rows.isNotEmpty()) {
-                if (rows.get(0).getLong("userid") > 0) {
-                    return getErrReturnObject().setErrMsg("团队成员已被绑定").toString();
-                }
+            if (rows.isEmpty()) {
+                return getErrReturnObject().setErrMsg("绑定的主账号或子账号不存在").toString();
             }
+            long tempuserid = rows.get(0).getLong("userid");
+            if (UserHelper.getOpenidsRows(this, tempuserid).isNotEmpty()) {
+                return getErrReturnObject().setErrMsg("主账号或子账号已绑定微信!").toString();
+            }
+            if (UserHelper.getUnionidsRows(this, tempuserid).isNotEmpty()) {
+                return getErrReturnObject().setErrMsg("主账号或子账号已绑定微信!").toJSONString();
+            }
+
+            isleader = rows.get(0).getBoolean("isleader");
+
             rows = dbConnect.runSqlQuery("SELECT * from sys_enterprise_hr WHERE userid='" + userid + "' and siteid ='" + siteid + "'");
             if (rows.isNotEmpty()) {
-                if (rows.get(0).getLong("userid") > 0) {
-                    return getErrReturnObject().setErrMsg("当前账号已绑定微信!").toString();
-                }
+                return getErrReturnObject().setErrMsg("当前账号已绑定主账号或子账号!").toString();
             }
+            //绑定主账号微信
+            if (isleader) {
+                userid = tempuserid;
+            }
+
         }
-        WechatMiniProgram wechatMiniProgram = new WechatMiniProgram(systemclient);
-        WechatUserID wechatUser = wechatMiniProgram.getWechatUserID(wechat_code);
-        String openid = wechatUser.getOpenid();
-        String unionid = wechatUser.getUnionid();
-        if (isbinging) {
-            Rows openidsRows = dbConnect.runSqlQuery("select * from sys_wechatapp_openids where userid='" + userid + "' and systemclient='" + systemclient + "'");
+        if (isbinging && sys_enterprise_hrid == 0) {
+            Rows openidsRows = UserHelper.getOpenidsRows(this, userid);
             if (openidsRows.isNotEmpty() && !openidsRows.get(0).getString("openid").equals(openid)) {
                 return getErrReturnObject().setErrMsg("当前账号已绑定微信!").toJSONString();
             }
-
-            Rows unionidsRows = dbConnect.runSqlQuery("select * from sys_wechatapp_unionids where userid='" + userid + "'");
+            Rows unionidsRows = UserHelper.getUnionidsRows(this, userid);
             if (unionidsRows.isNotEmpty() && !unionidsRows.get(0).getString("unionid").equals(unionid)) {
                 return getErrReturnObject().setErrMsg("当前账号已绑定微信!").toJSONString();
             }
         }
 
-
-        String wechatuserinfo = "";
-        if (content.containsKey("wechatuserinfo")) {
-            wechatuserinfo = content.getString("wechatuserinfo", true);
-        }
         ArrayList<String> sqllist = new ArrayList<>();
         if (openid != null && !openid.equals("")) {
             if (isbinging) {
@@ -358,8 +363,13 @@ public class usercenter extends Controller {
                     sqlFactory.addParameter("systemclient", systemclient);
                     sqlFactory.addParameter("openid", openid);
                     sqlFactory.addParameter("userid", userid);
-                    sqlFactory.addParameter("userinfo", wechatuserinfo);
+                    sqlFactory.addParameter("userinfo", "");
                     sqllist.add(sqlFactory.getSQL());
+                } else {
+                    UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sys_wechatapp_openids");
+                    updateSQL.setUniqueid(openidsRows.get(0).getLong("wechatapp_openidsid"));
+                    updateSQL.setValue("userid", userid);
+                    sqllist.add(updateSQL.getSQL());
                 }
             } else {
                 if (dbConnect.runSqlQuery("select 1 from sys_wechatapp_openids where openid='" + openid + "' and userid='" + userid + "' and systemclient='" + systemclient + "'").isEmpty()) {
@@ -377,8 +387,13 @@ public class usercenter extends Controller {
                     sqlFactory.addParameter("wechatapp_unionidsid", createTableID("sys_wechatapp_unionids"));
                     sqlFactory.addParameter("unionid", unionid);
                     sqlFactory.addParameter("userid", userid);
-                    sqlFactory.addParameter("userinfo", wechatuserinfo);
+                    sqlFactory.addParameter("userinfo", "");
                     sqllist.add(sqlFactory.getSQL());
+                } else {
+                    UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sys_wechatapp_unionids");
+                    updateSQL.setUniqueid(openidsRows.get(0).getLong("wechatapp_unionidsid"));
+                    updateSQL.setValue("userid", userid);
+                    sqllist.add(updateSQL.getSQL());
                 }
             } else {
                 if (dbConnect.runSqlQuery("select 1 from sys_wechatapp_unionids where unionid='" + unionid + "' and userid='" + userid + "'").isEmpty()) {
@@ -395,8 +410,13 @@ public class usercenter extends Controller {
             updateSQL.setValue("userid", userid);
             updateSQL.update();
             if (usertype == 99) {
+                if (isleader) {
+                    new StoreHelper().updateUserType21(this, userid);
+                    loguserout(logoutUserid);
+                } else {
+                    new StoreHelper().updateUserType22(this, userid);
+                }
 
-                new StoreHelper().updateUserType22(this, userid);
             }
 
         }

+ 19 - 0
src/custom/restcontroller/webmanage/saletool/store/StoreHelper.java

@@ -69,6 +69,25 @@ public class StoreHelper extends BaseClass {
         return insertSQL;
     }
 
+    public void updateUserType21(Controller controller,Long userid) throws YosException {
+        updateUserType(controller, userid,21);
+
+        ArrayList<Long> deleteroles = dbConnect.runSqlQuery("SELECT visitor_roleid from sys_wechatapp WHERE (systemclient='"
+                + controller.systemclient + "' or systemclient_auth='" + controller.systemclient + "' ) and  siteid='" + controller.siteid + "'  and isvisitor=1").toArrayList("visitor_roleid", new ArrayList<>());
+
+        if (deleteroles.size() > 0) {
+            deleteUserRole(controller, userid,deleteroles);
+        }
+
+        Rows rows = dbConnect.runSqlQuery("select * from sys_role where rolename='" + Parameter.get("storerolename") + "' and siteid='" + controller.siteid + "'");
+        if (rows.isNotEmpty()) {
+            insertUserRole(controller,userid, rows.get(0).getLong("roleid"));
+
+            rows = dbConnect.runSqlQuery("select * from sys_role where usertype=21 and issystem=1 and siteid='" + controller.siteid + "'");
+            insertUserRole(controller,userid, rows.get(0).getLong("roleid"));
+        }
+    }
+
     //改为经销商员工类型
     public void updateUserType22(Controller controller,Long userid) throws YosException {
         updateUserType(controller, userid,22);