Browse Source

解决企业首次开通应用时无法agentConfig的问题。 主要是企业微信平台本身需要等待3秒左右才能config成功,客户端做了刷新页面的重试机制来解决此问题。

seyason 2 years ago
parent
commit
b178f103fc

+ 3 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/AuthRedirectController.java

@@ -74,16 +74,18 @@ public class AuthRedirectController {
         String url = WeiXinCorpController.GET_CORP_USERINFO_URL.replace("SUITE_ACCESS_TOKEN", getSuiteAccessToken()).replace("CODE", code);
         String forObject = this.restTemplate.getForObject(url, String.class);
         JSONObject obj = JSONObject.parseObject(forObject);
+        System.out.println(obj.toString());
         String wxUserId = obj.getString("UserId");
         String openUserId = obj.getString("open_userid");
         String corpId = obj.getString("CorpId");
 
-        System.out.println("wxUserId="+wxUserId+", openUserId="+openUserId);
+        System.out.println("登录 wxUserId="+wxUserId+", openUserId="+openUserId);
         List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", openUserId));
         Integer companyId = 0;
         if (userList.size() > 0) {
             //该用户已存在
             User curUser = userList.get(0);
+            System.out.println("找到用户corpwxUserid=="+curUser.getCorpwxUserid());
             companyId = curUser.getCompanyId();
             if (curUser.getIsActive() == 1) {
                 reqParam.put("userId", curUser.getId());

+ 57 - 42
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/WeiXinCorpController.java

@@ -393,7 +393,7 @@ public class WeiXinCorpController {
                 getTicketUrl = getTicketUrl.replace("ACCESS_TOKEN", corpToken);
                 String forObject = this.restTemplate.getForObject(getTicketUrl, String.class);
                 JSONObject json = JSONObject.parseObject(forObject);
-                System.out.println("返回:"+json.toJSONString());
+                System.out.println("getCorpWXAgentConfig返回:"+json.toJSONString());
                 if (json.getIntValue("errcode") == 0) {
                     String ticket = json.getString("ticket");
                     AgentItem it = new AgentItem();
@@ -408,7 +408,7 @@ public class WeiXinCorpController {
                 msg.setError(MessageUtils.message("wx.TicketError"));
                 return msg;
             }
-            System.out.println("jsTicket = " + agentItem.jsTicket);
+            System.out.println("getCorpWXAgentConfig jsTicket = " + agentItem.jsTicket);
             // 对以下字段进行签名
             SortedMap<String, String> packageParams = new TreeMap<String, String>();
             SortedMap<String, String> packageParam = new TreeMap<String, String>();
@@ -517,42 +517,47 @@ public class WeiXinCorpController {
                     if (wxCorpInfo != null && wxCorpInfo.getSaasSyncContact() == 1) {
                         //企业存在,生成对应的用户
                         Integer companyId = wxCorpInfo.getCompanyId();
-                        User user = wxCorpInfoService.generateUserInfo(companyId, corpWxUserId);
-
-                        new Thread(() -> {
-                            //查询详情进行更新
-                            JSONObject userObj = null;
-                            try {
-                                userObj = getUserInfo(getCorpAccessToken(wxCorpInfo), corpWxUserId);
-                            } catch (Exception exception) {
-                                exception.printStackTrace();
-                            }
-                            System.out.println("关注时获取详情userObj=="+userObj);
-                            //成功获取到通讯录的个人详情
-                            String userId = userObj.getString("userid");
-                            String openUserId = userObj.getString("open_userid");
-                            User userUpdate = new User();
-                            userUpdate.setId(user.getId());
-                            userUpdate.setName(userObj.getString("name"))
-                                    .setPhone(userObj.getString("mobile"))
-                                    .setCorpwxUserid(openUserId)
-                                    .setCorpwxRealUserid(userId)
-                                    .setJobNumber(openUserId.equals(userId)?null:userId);
+                        User find = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", companyId).eq("corpwx_userid", corpWxUserId));
+                        if (find == null) {
+                            User user = wxCorpInfoService.generateUserInfo(companyId, corpWxUserId);
 
-                            //通过getUserInfo接口获取到的json key是小写的
-                            JSONArray department = userObj.getJSONArray("department");
-                            Integer curUserWXDeptid = getMaxDeptIdFromArray(department);
-                            Department sysDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", curUserWXDeptid));
+                            new Thread(() -> {
+                                //查询详情进行更新
+                                JSONObject userObj = null;
+                                try {
+                                    userObj = getUserInfo(getCorpAccessToken(wxCorpInfo), corpWxUserId);
+                                } catch (Exception exception) {
+                                    exception.printStackTrace();
+                                }
+                                System.out.println("关注时获取详情userObj=="+userObj);
+                                //成功获取到通讯录的个人详情
+                                String userId = userObj.getString("userid");
+                                String openUserId = userObj.getString("open_userid");
+                                User userUpdate = new User();
+                                userUpdate.setId(user.getId());
+                                userUpdate.setName(userObj.getString("name"))
+                                        .setPhone(userObj.getString("mobile"))
+                                        .setCorpwxUserid(openUserId)
+                                        .setCorpwxRealUserid(userId)
+                                        .setJobNumber(openUserId.equals(userId)?null:userId);
 
-                            if (sysDept != null) {
-                                userUpdate.setDepartmentId(sysDept.getDepartmentId());
-                                List<Department> allDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
-                                userUpdate.setDepartmentCascade(convertDepartmentIdToCascade(user.getDepartmentId(), allDeptList));
-                            }
-                            userMapper.updateById(userUpdate);
-                            System.out.println("======关注时更新人员======");
-                            sendCardMsg(corpWxUserId);
-                        }).start();
+                                //通过getUserInfo接口获取到的json key是小写的
+                                JSONArray department = userObj.getJSONArray("department");
+                                Integer curUserWXDeptid = getMaxDeptIdFromArray(department);
+                                Department sysDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", curUserWXDeptid));
+
+                                if (sysDept != null) {
+                                    userUpdate.setDepartmentId(sysDept.getDepartmentId());
+                                    List<Department> allDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
+                                    userUpdate.setDepartmentCascade(convertDepartmentIdToCascade(user.getDepartmentId(), allDeptList));
+                                }
+                                userMapper.updateById(userUpdate);
+                                System.out.println("======关注时更新人员======");
+                                sendCardMsg(corpWxUserId);
+                            }).start();
+                        } else {
+                            log.info("当前用户已存在");
+                        }
                     }
                 } else if ("enter_agent".equals(event)) {
 
@@ -738,12 +743,12 @@ public class WeiXinCorpController {
             jsonObject = jsonObject.getJSONObject("xml");
             //订单回调。存储更新订单、更新公司续费情况
             if (jsonObject.has("InfoType")){
-                System.err.println("+++++++++++++++++++订单回调开始++++++++++++++++++++++");
-                System.err.println(jsonObject);
                 String infoType = jsonObject.getString("InfoType");
                 String orderId = "";
                 if (jsonObject.has("OrderId")){
                     orderId = jsonObject.getString("OrderId");
+                    System.err.println("+++++++++++++++++++订单回调开始++++++++++++++++++++++");
+                    System.err.println(jsonObject);
                 }
                 WxOrder wxOrder = null;
                 switch (infoType){
@@ -810,8 +815,16 @@ public class WeiXinCorpController {
                         break;
                 }
             }
-
-            if (jsonObject.has("AuthCode")) {
+            if ("cancel_auth".equals(jsonObject.get("InfoType"))) {
+                //企业取消授权通知,更新ticket缓存
+                String corpId = jsonObject.getString("AuthCorpId");
+                if (corpTicketMap.containsKey(corpId)) {
+                    System.out.println("企业取消授权,清除缓存ticket=="+corpId);
+                    agentCorpTicketMap.remove(corpId);
+                    corpTicketMap.remove(corpId);
+                }
+            }
+            else if (jsonObject.has("AuthCode")) {
                 //企业授权通知
                 String authCode = jsonObject.getString("AuthCode");
                 new Thread(){
@@ -1231,7 +1244,7 @@ public class WeiXinCorpController {
         headers.setContentType(MediaType.APPLICATION_JSON);
         JSONObject reqParam = new JSONObject();
         reqParam.put("auth_code",  authCode);
-
+        log.info("处理企业授权开通==========");
         HttpEntity<String> requestEntity = new HttpEntity<String>(reqParam.toJSONString(), headers);
         ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
                 HttpMethod.POST, requestEntity, String.class);
@@ -1395,6 +1408,7 @@ public class WeiXinCorpController {
                                 .setJobNumber(openUserId.equals(userId)?null:userId)
                                 .setCorpwxRealUserid(userId);
                         userMapper.insert(user);
+                        System.out.println("生成超级管理员:"+userId);
                         //todo: 生成初始测试项目及任务
                         Project project=new Project();
                         project.setCompanyId(company.getId());
@@ -1548,7 +1562,7 @@ public class WeiXinCorpController {
                     String userId = authUserInfo.getString("userid");//授权人的userid
                     String openUserId = authUserInfo.getString("open_userid");
                     //检查该授权人是否已经存在
-                    if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", userId)) == 0) {
+                    if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", openUserId)) == 0) {
                         JSONObject userDetail = getUserInfo(curCorpAccessToken, userId);
                         //检查是否有超级管理员
                         SysRole smanager = sysRoleMapper.selectOne(
@@ -2263,6 +2277,7 @@ public class WeiXinCorpController {
                             changeUser.setDepartmentId(0);
                             changeUser.setDepartmentCascade("0");
                         }
+                        changeUser.setCorpwxDeptid(userItem.getCorpwxDeptid());
                     }
                     if (oldUser.getCorpwxRealUserid() == null || !userItem.getCorpwxRealUserid().equals(oldUser.getCorpwxRealUserid())) {
                         changeUser.setId(oldUser.getId());

+ 1 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/LeaveSheetServiceImpl.java

@@ -252,7 +252,7 @@ public class LeaveSheetServiceImpl extends ServiceImpl<LeaveSheetMapper, LeaveSh
                 .eq(status != null, "status", status)
                 .eq(leaveType != null, "leave_type", leaveType)
                 .le(endTime != "" && endTime != null, "start_date", endTime)
-                .ge(startTime != "" && startTime != null, "end_date", startTime);
+                .ge(startTime != "" && startTime != null, "end_date", startTime).orderByDesc("id");
         List<LeaveSheet> leaveSheets = leaveSheetMapper.selectList(qw);
         if (leaveSheets.size()==0){
             return httpRespMsg;

+ 5 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ProjectPercentageServiceImpl.java

@@ -17,6 +17,7 @@ import com.management.platform.service.ProjectPercentageService;
 import com.management.platform.util.ExcelUtil;
 import com.management.platform.util.HttpRespMsg;
 import com.management.platform.util.MessageUtils;
+import com.taobao.api.internal.util.StringUtils;
 import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.ss.usermodel.*;
@@ -239,6 +240,10 @@ public class ProjectPercentageServiceImpl extends ServiceImpl<ProjectPercentageM
                             continue;
                         }
                         String stringCellValue = row.getCell(i).getStringCellValue();
+                        //对于excel中的空白,设置比例为0
+                        if (StringUtils.isEmpty(stringCellValue)) {
+                            stringCellValue = "0";
+                        }
                         s+=Integer.parseInt(stringCellValue);
                         String pid = projectList.get(i-1);
                         jsonObject.put(pid,stringCellValue);

+ 2 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/WxCorpInfoServiceImpl.java

@@ -281,7 +281,7 @@ public class WxCorpInfoServiceImpl extends ServiceImpl<WxCorpInfoMapper, WxCorpI
     @Override
     public void sendWXCorpTemplateCardMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg) {
         try {
-//            if (isDev) return;
+            if (isDev) return;
             String accessToken = getCorpAccessToken(corpInfo);
             String url=URL_SEND_WXCORP_MSG.replaceAll("ACCESS_TOKEN", accessToken);
             HttpHeaders headers = new HttpHeaders();
@@ -300,7 +300,7 @@ public class WxCorpInfoServiceImpl extends ServiceImpl<WxCorpInfoMapper, WxCorpI
                 JSONObject json = JSONObject.parseObject(resp);
                 if (json.getIntValue("errcode") == 0) {
                     //发送成功
-
+                    System.out.println("发送企业微信模板消息成功");
                 } else {
                     System.out.println("发送企业微信模板消息发生错误==" + resp);
                     throw new Exception(json.toJSONString());

+ 3 - 0
fhKeeper/formulahousekeeper/timesheet/src/views/project/finance.vue

@@ -1038,6 +1038,9 @@ import { error } from 'dingtalk-jsapi';
                 const a = document.createElement('a'); // 创建a标签
                 a.setAttribute('download', row.name);// download属性
                 // console.log(row.url);
+                if (row.url.indexOf('upload/') == -1) {
+                    row.url = '/upload/' +row.url;
+                }
                 a.setAttribute('href', row.url);// href链接
                 a.click();// 自执行点击事件
                 a.remove();

+ 36 - 22
fhKeeper/formulahousekeeper/timesheet_h5/src/views/index/index.vue

@@ -30,7 +30,6 @@
             return {
                 isCorpWX:false,
                 isWX:false,
-                // user: JSON.parse(localStorage.userInfo) || '',
                 user: '',
                 unreadNum:0,
                 images: [
@@ -39,7 +38,7 @@
                     // require('../../assets/img/index/banner_3.png'),
                 ],
                 routers: [],
-                key: 0
+                key: 0,
             };
         },
         created() {
@@ -57,23 +56,38 @@
             //     }
             //     document.head.appendChild(consoleScript);
             // }
+
+            //有错误信息,优先跳转到登录页面去
+            if(window.location.href.indexOf('errorMsg') != '-1') {
+                this.$router.push("/login");
+            }
+            
             var ua = navigator.userAgent.toLowerCase();
             if (ua.indexOf("wxwork") > 0) {
                 this.isCorpWX = true;
             } else if (ua.indexOf("micromessenger") > 0) {
                 this.isWX = true;
             }
-            let that = this
-            if (localStorage.userInfo != null) {
-                that.getAccountInfo();
-            } else {
+            let that = this;
+            //企业微信是从后台授权后跳转过来的
+            if (this.isCorpWX) {
                 if(window.location.href.indexOf('userId') != '-1') {
-                    that.getAccountInfo()
+                    let href = window.location.href;
+                    var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
+                    if (loginUserId.includes('#/')) {
+                        loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
+                    }
+                    that.getAccountInfo(loginUserId)
                 } else {
                     this.$router.push("/login");
                 }
+            } else {
+                //其他情况,刷新用户信息
+                if (localStorage.userInfo != null) {
+                    that.user = JSON.parse(localStorage.userInfo);
+                    that.getAccountInfo(that.user.id);
+                }
             }
-           
         },
         components: {
             Footer
@@ -198,6 +212,8 @@
                                             // console.log('查看错误信息', res)
                                             if (res.errMsg.indexOf('function not exist') > -1) {
                                                 alert('版本过低请升级')
+                                            } else {
+                                                // alert('agentConfig出错:'+res.errMsg);
                                             }
                                         },
                                     })
@@ -205,29 +221,27 @@
                             }).catch(err => {
                                 if (err.errMsg.indexOf('function not exist') > -1) {
                                     alert('版本过低请升级')
+                                }else {
+                                    // alert(res.errMsg);
                                 }
                             })
                         });
+                        wx.error(function(res){
+                            // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
+                            // alert('wxConfig发生异常:'+JSON.stringify(res));
+                            // 企业第一次授权安装进入后会报not in reliable domain的错误,刷新后正常
+                            location.reload();
+                        });
+                    } else {
+                        // alert(''+res);
                     }
                 }).catch(err => {
-
+                    alert(err);
                 })
             },
 
             //获取账户信息
-            getAccountInfo() {
-                let userId = this.user.id
-                if(window.location.href.indexOf('userId') != '-1') {
-                    let href = window.location.href;
-                    var loginUserId = href.substring(href.indexOf("userId=")+"userId=".length);
-                    if (loginUserId.includes('#/')) {
-                        loginUserId = loginUserId.substring(0, loginUserId.indexOf('#/'));
-                    }
-                    userId = loginUserId
-                }
-                if(window.location.href.indexOf('errorMsg') != '-1') {
-                    this.$router.push("/login");
-                }
+            getAccountInfo(userId) {
                 this.$axios.get('/user/loginByUserId', {params:{userId: userId}})
                 .then(res => {
                     // console.log(res);