|  |  |  | 
|---|
|  |  |  | package com.matrix.system.shopXcx.api; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.matrix.core.tools.LogUtil; | 
|---|
|  |  |  | import com.matrix.core.tools.PropertiesUtil; | 
|---|
|  |  |  | import com.matrix.core.tools.StringUtils; | 
|---|
|  |  |  | import com.matrix.component.tools.HttpRequest; | 
|---|
|  |  |  | import com.matrix.component.tools.HttpResponse; | 
|---|
|  |  |  | import com.matrix.core.tools.LogUtil; | 
|---|
|  |  |  | import com.matrix.core.tools.PropertiesUtil; | 
|---|
|  |  |  | import com.matrix.system.common.bean.BusParameterSettings; | 
|---|
|  |  |  | import com.matrix.system.common.constance.AppConstance; | 
|---|
|  |  |  | import com.matrix.system.common.dao.BusParameterSettingsDao; | 
|---|
|  |  |  | import net.sf.json.JSONObject; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class WeChatApiTools { | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 小程序秘钥 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private static final String XCX_SECRET = "xcx_secret"; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 小程序appid | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private static final String XCX_APPID = "xcx_appid"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BusParameterSettingsDao busParameterSettingsDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 微信登录url | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private static String accessToken = ""; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static String appid = ""; | 
|---|
|  |  |  | private static String secret = ""; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param code | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String getXcxLoginUrl(String code) { | 
|---|
|  |  |  | public  String getXcxLoginUrl(String code,Long companyId) { | 
|---|
|  |  |  | String wechatLoginUrl = PropertiesUtil.getString(WECHAT_LOGIN_URL); | 
|---|
|  |  |  | return String.format(wechatLoginUrl, getAppid(), getSecret(), code); | 
|---|
|  |  |  | return String.format(wechatLoginUrl, getAppid(companyId), getSecret(companyId), code); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取小程序APPId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String  getAppid(){ | 
|---|
|  |  |  | if(StringUtils.isBlank(appid)){ | 
|---|
|  |  |  | appid = PropertiesUtil.getString(XCX_APPID); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  appid; | 
|---|
|  |  |  | public  String  getAppid(Long companyId){ | 
|---|
|  |  |  | BusParameterSettings appId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId); | 
|---|
|  |  |  | return  appId.getParamValue(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取小程序秘钥 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String  getSecret(){ | 
|---|
|  |  |  | if(StringUtils.isBlank(secret)){ | 
|---|
|  |  |  | secret = PropertiesUtil.getString(XCX_SECRET); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  secret; | 
|---|
|  |  |  | public  String  getSecret(Long companyId){ | 
|---|
|  |  |  | BusParameterSettings secret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_SECRET, companyId); | 
|---|
|  |  |  | return  secret.getParamValue(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | accessToken = ""; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String getAccessToken() throws IOException { | 
|---|
|  |  |  | public  String getAccessToken(Long companyId) throws IOException { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (isTokenInvalid()) { | 
|---|
|  |  |  | synchronized (accessToken) { | 
|---|
|  |  |  | 
|---|
|  |  |  | HttpResponse result = null; | 
|---|
|  |  |  | result = reqObj | 
|---|
|  |  |  | .sendHttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" | 
|---|
|  |  |  | + getAppid() + "&secret=" + getSecret(), null); | 
|---|
|  |  |  | + getAppid(companyId) + "&secret=" + getSecret(companyId), null); | 
|---|
|  |  |  | JSONObject json = JSONObject.fromObject(result.getDataString()); | 
|---|
|  |  |  | String access_token = json.getString("access_token"); | 
|---|
|  |  |  | accessToken = access_token; | 
|---|