Commit 3156574d by zhengxj

生成小程序二维码

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@24133 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent a912c033
...@@ -11,14 +11,20 @@ ...@@ -11,14 +11,20 @@
package com.glsx.common.utils.wehcat; package com.glsx.common.utils.wehcat;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
...@@ -29,7 +35,13 @@ import org.apache.http.message.BasicHeader; ...@@ -29,7 +35,13 @@ import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
...@@ -59,6 +71,8 @@ public class WechatUtils { ...@@ -59,6 +71,8 @@ public class WechatUtils {
public String GET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$ACCESS_TOKEN$&openid=$OPENID$&lang=zh_CN"; public String GET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$ACCESS_TOKEN$&openid=$OPENID$&lang=zh_CN";
public String GET_WECHAT_PUBLIC_CODE = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$APPID$&redirect_uri=$REDIRECT_URI$&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; public String GET_WECHAT_PUBLIC_CODE = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$APPID$&redirect_uri=$REDIRECT_URI$&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
public String GET_WXACODE_UNLIMIT = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$ACCESS_TOKEN$";
@Resource @Resource
RedisCacheUtil redisCacheUtil; RedisCacheUtil redisCacheUtil;
...@@ -107,6 +121,7 @@ public class WechatUtils { ...@@ -107,6 +121,7 @@ public class WechatUtils {
* @throws Exception * @throws Exception
*/ */
private String getAndCacheWechatToken(String appid, String secret) throws Exception { private String getAndCacheWechatToken(String appid, String secret) throws Exception {
String access_token = ""; String access_token = "";
String url = GET_TOKEN_URL.replace("$APPID$", appid).replace("$SECRET$", secret); String url = GET_TOKEN_URL.replace("$APPID$", appid).replace("$SECRET$", secret);
HttpUtils httpUtils = new HttpUtils(); HttpUtils httpUtils = new HttpUtils();
...@@ -305,6 +320,78 @@ public class WechatUtils { ...@@ -305,6 +320,78 @@ public class WechatUtils {
return result; return result;
} }
@SuppressWarnings({ "rawtypes", "unchecked", })
public Map<String, Object> getwxacodeunlimit(String sceneStr) throws Exception {
// {
// "scene": "a=1"
// }
String appid = PropertiesUtil.getProperty(Constants.WECHAT_MINI_SERVICE_APPID);
String secret = PropertiesUtil.getProperty(Constants.WECHAT_MINI_SERVICE_SECRET);
String page = "pages/index";
String accessToken = this.getWechatToken(appid, secret);
// 二维码存放路径
// String filePath = "C:/Users/wangqiulin/Desktop/1.png";
String filePath = "F:/test/" + System.currentTimeMillis() + ".png";
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String url = this.GET_WXACODE_UNLIMIT.replace("$ACCESS_TOKEN$", accessToken);
Map<String, Object> param = new HashMap<>();
param.put("scene", sceneStr);
param.put("page", page);
param.put("width", 430);
param.put("auto_color", false);
Map<String, Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
logger.info("调用生成微信URL接口传参:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class,
new Object[0]);
logger.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
byte[] result = entity.getBody();
String base64String = new String(result).replaceAll("\n", "");
logger.info(base64String);
logger.info(Base64.encodeBase64String(result));
inputStream = new ByteArrayInputStream(result);
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
logger.error("调用小程序生成微信永久小程序码URL接口异常", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -40,9 +40,11 @@ import com.glsx.common.utils.DateUtil; ...@@ -40,9 +40,11 @@ import com.glsx.common.utils.DateUtil;
import com.glsx.common.utils.Page; import com.glsx.common.utils.Page;
import com.glsx.common.utils.StringUtils; import com.glsx.common.utils.StringUtils;
import com.glsx.common.utils.wehcat.WechatMiniUtils; import com.glsx.common.utils.wehcat.WechatMiniUtils;
import com.glsx.common.utils.wehcat.WechatUtils;
import com.glsx.usercenter.constants.LoginType; import com.glsx.usercenter.constants.LoginType;
import com.glsx.usercenter.constants.ResultCodeEnum; import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.dto.MerchantInfo; import com.glsx.usercenter.dto.MerchantInfo;
import com.glsx.usercenter.exception.BusinessException;
import com.glsx.usercenter.mapper.SysUserImageMapper; import com.glsx.usercenter.mapper.SysUserImageMapper;
import com.glsx.usercenter.mapper.SysUserMapper; import com.glsx.usercenter.mapper.SysUserMapper;
import com.glsx.usercenter.pojo.user.SysManager; import com.glsx.usercenter.pojo.user.SysManager;
...@@ -77,8 +79,6 @@ public class UserController extends BaseController { ...@@ -77,8 +79,6 @@ public class UserController extends BaseController {
@Autowired @Autowired
private CommonClient commonClient; private CommonClient commonClient;
@Autowired
private SysUserImageMapper sysUserImageMapper;
@Autowired @Autowired
private IAreaService iAreaService; private IAreaService iAreaService;
...@@ -98,6 +98,9 @@ public class UserController extends BaseController { ...@@ -98,6 +98,9 @@ public class UserController extends BaseController {
@Autowired @Autowired
private ShiroUtil shiroUtil; private ShiroUtil shiroUtil;
@Autowired
private WechatUtils wechatUtils;
/** /**
* @param userQueryVo * @param userQueryVo
* @return ResultObject * @return ResultObject
...@@ -1248,4 +1251,28 @@ public class UserController extends BaseController { ...@@ -1248,4 +1251,28 @@ public class UserController extends BaseController {
sysUserService.sendBiddingOrderNoBidRemindMsg(orderList); sysUserService.sendBiddingOrderNoBidRemindMsg(orderList);
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg()); return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
} }
/**
*
*
* @author Zheng Xiajun
* @Title: getWxacodeUnlimit
* @Description: 生产服务商二维码
* @param @param
* orderList
* @param @return
* 设定文件
* @return ResultObject 返回类型
*
*/
@RequestMapping(value = "/getWxacodeUnlimit", method = { RequestMethod.POST })
public ResultObject getWxacodeUnlimit(String spId, String spName) {
String sceneStr = "13456";
try {
wechatUtils.getwxacodeunlimit(sceneStr);
} catch (Exception e) {
throw BusinessException.create("服务商二维码生成出错!");
}
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!