hermit 7 rokov pred
rodič
commit
161af3c89c

+ 0 - 41
src/main/java/com/wxmp/core/common/AppDefineInitService.java

@@ -1,41 +0,0 @@
-/*
- * FileName:AppDefineInitService.java 
- * <p>
- * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
- * <p>
- * Licensed under the GNU General Public License, Version 3 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * <p>
- * http://www.gnu.org/licenses/gpl-3.0.html
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * 
- */
-package com.wxmp.core.common;
-
-import com.wxmp.core.spring.SpringBeanDefineService;
-import com.wxmp.wxapi.process.WxMemoryCacheClient;
-import com.wxmp.wxcms.domain.Account;
-import com.wxmp.wxcms.mapper.AccountDao;
-
-import javax.annotation.Resource;
-
-/**
- * 系统启动时自动加载,把公众号信息加入到缓存中
- */
-public class AppDefineInitService implements SpringBeanDefineService {
-
-	@Resource
-	private AccountDao accountDao;
-	
-	public void initApplicationCacheData() {
-		Account account = accountDao.getSingleAccount();
-		WxMemoryCacheClient.addMpAccount(account);
-	}
-	
-}

+ 2 - 2
src/main/java/com/wxmp/core/listener/InitListener.java

@@ -45,8 +45,8 @@ public class InitListener implements ServletContextListener {
         try {
             //放入公众号
             AccountService accountService = SpringContextHolder.getBean("accountService");
-            Account account = accountService.getSingleAccount();
-            WxMemoryCacheClient.addMpAccount(account);
+            List<Account> accounts = accountService.listForPage(new Account());
+            WxMemoryCacheClient.addMpAccount(accounts);
             //读取所有缓存
             SysConfigService configService = SpringContextHolder.getBean("sysConfigService");
             List<SysConfig> configList = configService.getConfigList();

+ 9 - 76
src/main/java/com/wxmp/wxapi/process/WxMemoryCacheClient.java

@@ -18,8 +18,11 @@
  */
 package com.wxmp.wxapi.process;
 
+import java.util.List;
 import java.util.Map;
 
+import com.wxmp.wxcms.domain.Account;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
@@ -31,25 +34,24 @@ import com.wxmp.core.util.CacheUtils;
  * 目前使用 服务器内存的方式;
  *
  * 1、开发者可以根据自己的需求使用不同的缓存方式,比如memcached
- * 2、系统默认使用单个公众账号的缓存处理,如果有多个账号,请开发者自行处理
  *
  */
 public class WxMemoryCacheClient {
 
 
-	private static Logger log = LogManager.getLogger(WxMemoryCacheClient.class);
 	public static final String SESSION_ACCOUNT= "session_account";
 
-	public static void addMpAccount(MpAccount account){
+	public static void addMpAccount(List<Account> accounts){
 		Map<String,MpAccount> mpAccountMap = (Map<String, MpAccount>) CacheUtils.get("mpAccountMap");
 		if(mpAccountMap==null){
 			mpAccountMap=Maps.newHashMap();
 		}
-		if(account != null){
-			mpAccountMap.put(account.getAccount(), account);
-			CacheUtils.put("mpAccountMap", mpAccountMap);
-			setAccount(account.getAccount());
+		if (CollectionUtils.isNotEmpty(accounts)) {
+			for (MpAccount account:accounts) {
+				mpAccountMap.put(account.getAccount(), account);
+			}
 		}
+		CacheUtils.put("mpAccountMap", mpAccountMap);
 	}
 //
 	public static MpAccount getMpAccount(){
@@ -62,20 +64,6 @@ public class WxMemoryCacheClient {
 		return mpAccountMap.get(accout);
 	}
 
-//	//获取唯一的公众号,如果需要多账号,请自行处理
-//	public static MpAccount getSingleMpAccount(){
-//		Map<String,MpAccount> mpAccountMap = (Map<String, MpAccount>) CacheUtils.get("mpAccountMap");
-//		if(mpAccountMap==null){
-//			mpAccountMap=Maps.newHashMap();
-//		}
-//		MpAccount sigleAccount = null;
-//		for(String key : mpAccountMap.keySet()){
-//			sigleAccount = mpAccountMap.get(key);
-//			break;
-//		}
-//		return sigleAccount;
-//	}
-
 	public static AccessToken addAccessToken(String account ,AccessToken token){
 		Map<String,AccessToken> accountAccessTokenMap = (Map<String, AccessToken>) CacheUtils.get("accountAccessTokenMap");
 		if(accountAccessTokenMap==null){
@@ -103,24 +91,6 @@ public class WxMemoryCacheClient {
 		return accountAccessTokenMap.get(getAccount());
 	}
 
-//	/**
-//	 * 获取唯一的公众号的accessToken,如果需要多账号,请自行处理
-//	 * accessToken的获取,绝对不要从缓存中直接获取,请从WxApiClient中获取;
-//	 * @return
-//	 */
-//	public static AccessToken getSingleAccessToken(){
-//		Map<String,AccessToken> accountAccessTokenMap = (Map<String, AccessToken>) CacheUtils.get("accountAccessTokenMap");
-//		if(accountAccessTokenMap==null){
-//			accountAccessTokenMap=Maps.newHashMap();
-//		}
-//		AccessToken accessToken = null;
-//		for(String key : accountAccessTokenMap.keySet()){
-//			accessToken = accountAccessTokenMap.get(key);
-//			break;
-//		}
-//		return accessToken;
-//	}
-
 	/**
 	 * 添加JSTicket到缓存
 	 * @param account
@@ -151,25 +121,6 @@ public class WxMemoryCacheClient {
 		return accountJSTicketMap.get(getAccount());
 	}
 
-//	/**
-//	 * 获取唯一的公众号的JSTicket,如果需要多账号,请自行处理
-//	 * JSTicket的获取,绝对不要从缓存中直接获取,请从WxApiClient中获取;
-//	 * @return
-//	 */
-//	public static JSTicket getSingleJSTicket(){
-//		Map<String,JSTicket> accountJSTicketMap = (Map<String, JSTicket>) CacheUtils.get("accountJSTicketMap");
-//		if(null==accountJSTicketMap){
-//			accountJSTicketMap=Maps.newHashMap();
-//		}
-//		JSTicket jsTicket = null;
-//		for(String key : accountJSTicketMap.keySet()){
-//			jsTicket = accountJSTicketMap.get(key);
-//			break;
-//		}
-//		return jsTicket;
-//	}
-
-
 	//处理openid缓存
 	public static String getOpenid(String sessionid){
 		Map<String,String> sessionOpenIdMap = (Map<String,String>) CacheUtils.get("sessionOpenIdMap");
@@ -219,24 +170,6 @@ public class WxMemoryCacheClient {
 		return accountOAuthTokenMap.get(getAccount());
 	}
 
-//	/**
-//	 * 获取唯一的公众号的accessToken,如果需要多账号,请自行处理
-//	 * OAuthAccessToken的获取,绝对不要从缓存中直接获取,请从WxApiClient中获取;
-//	 * @return
-//	 */
-//	public static OAuthAccessToken getSingleOAuthAccessToken(){
-//		Map<String,OAuthAccessToken> accountOAuthTokenMap = (Map<String, OAuthAccessToken>) CacheUtils.get("accountOAuthTokenMap");
-//		if(null==accountOAuthTokenMap){
-//			accountOAuthTokenMap=Maps.newHashMap();
-//		}
-//		OAuthAccessToken token = null;
-//		for(String key : accountOAuthTokenMap.keySet()){
-//			token = accountOAuthTokenMap.get(key);
-//			break;
-//		}
-//		return token;
-//	}
-
 	/**
 	 * 设置公众号信息 缓存中
 	 * @param account

+ 2 - 1
src/main/java/com/wxmp/wxcms/ctrl/AccountCtrl.java

@@ -64,10 +64,11 @@ public class AccountCtrl extends BaseCtrl {
 		//account存入缓存中
 		if (null != searchEntity && null != searchEntity.getId()) {
 			account = entityService.getById(searchEntity.getId());
-			WxMemoryCacheClient.setAccount(account.getAccount());
+
 		} else {
 			account = entityService.getByAccount(WxMemoryCacheClient.getAccount());
 		}
+		WxMemoryCacheClient.setAccount(account.getAccount());
 		return AjaxResult.success(WxUtil.getAccount(list, account.getName()));
 	}
 }

+ 11 - 1
src/main/java/com/wxmp/wxcms/ctrl/SysUserCtrl.java

@@ -19,10 +19,14 @@
 package com.wxmp.wxcms.ctrl;
 
 import com.wxmp.core.common.BaseCtrl;
+import com.wxmp.core.common.Constants;
 import com.wxmp.core.util.AjaxResult;
 import com.wxmp.core.util.MD5Util;
 import com.wxmp.core.util.SessionUtil;
+import com.wxmp.wxapi.process.WxMemoryCacheClient;
+import com.wxmp.wxcms.domain.Account;
 import com.wxmp.wxcms.domain.SysUser;
+import com.wxmp.wxcms.service.AccountService;
 import com.wxmp.wxcms.service.SysUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -44,7 +48,8 @@ public class SysUserCtrl extends BaseCtrl {
 
 	@Autowired
 	private SysUserService sysUserService;
-
+	@Autowired
+	private AccountService accountService;
 	@ResponseBody
 	@RequestMapping(value = "/login", method = RequestMethod.POST)
 	public AjaxResult login(SysUser user) {
@@ -54,6 +59,11 @@ public class SysUserCtrl extends BaseCtrl {
 			return AjaxResult.failure("用户名或者密码错误");
 		}
 		SessionUtil.setUser(sysUser);
+		//设置登陆者默认公众号
+		Account account = accountService.getSingleAccount();
+		if (account != null) {
+			WxMemoryCacheClient.setAccount(account.getAccount());
+		}
 		return AjaxResult.success(sysUser.getTrueName());
 	}
 

+ 0 - 1
src/main/java/com/wxmp/wxcms/ctrl/WxCmsCtrl.java

@@ -131,7 +131,6 @@ public class WxCmsCtrl extends BaseCtrl {
 			tmpAccount.setName(account.getName());
 			accountDao.update(tmpAccount);
 		}
-		WxMemoryCacheClient.addMpAccount(account);
 		return AjaxResult.success(account);
 	}
 	

+ 0 - 5
src/main/resources/spring-config.xml

@@ -27,9 +27,4 @@
 	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
 		<property name="configLocation" value="classpath:j2cache/ehcache.xml" />
 	</bean>
-	<!--&lt;!&ndash; 服务器启动时自动加载,目前主要提取加载需要缓存的公众号信息 &ndash;&gt;-->
-	<!--<bean id="appServiceImpl" class="com.wxmp.core.common.AppDefineInitService"></bean>-->
-	<!--<bean id="beanDefineConfigue" class="com.wxmp.core.spring.SpringBeanDefineConfigue">-->
-		<!--<property name="appService" ref="appServiceImpl"></property>-->
-	<!--</bean>-->
 </beans>

+ 7 - 5
src/main/webapp/js/src/index.js

@@ -97,11 +97,13 @@ function getCountStatus(id) {
                 if(id){
                     location.reload();
                 }else{
-                    renderHtml({
-                        targetId: 'account_nav',
-                        template: "/views/common/template/account-nav.html",
-                        htmlData:result
-                    });
+                    if(result.data){
+                        renderHtml({
+                            targetId: 'account_nav',
+                            template: "/views/common/template/account-nav.html",
+                            htmlData:result
+                        });
+                    }
                 }
             }
         }

+ 1 - 1
src/main/webapp/views/index.html

@@ -20,7 +20,7 @@
             <p class="layui-nav-item ">当前公众号</p>
             <div class="layui-nav-item" id="account_nav">
                 <a href="javascript:">
-                    <span></span>
+                    <span>未获取到账号</span>
                 </a>
                 <dl class="layui-nav-child">
                     <dd><a href="/views/index.html#/index/addaccount/add"><i class="iconfont icon-add"></i>添加公众号</a></dd>