先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。如果你使用了支付类接口,请确保支付目录在该安全域名下,否则将无法完成支付。
备注:登录后可在“开发者中心”查看对应的接口权限。(注意:一个公众号只能设置三个域名,设置好了每个月只有三次修改的机会,而且还需要审核,所以慎重。重点是微信接口只能在这三个域名下测试,本地无法测试)
在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.0.0.js
请注意,如果你的页面启用了https,务必引入https://res.wx.qq.com/open/js/jweixin-1.0.0.js ,否则将无法在iOS9.0以上系统中成功使用JSSDK
如需使用摇一摇周边功能,请引入 jweixin-1.1.0.js
备注:支持使用 AMD/CMD 标准模块加载方法加载
#超级合伙人联盟服务号
WEIXIN_APPID=xxxxxx
WEIXIN_APP_SECRET=xxxxxx
OSS_ACCESS_ID=xxxxx
OSS_ACCESS_KEY=xxxxx
OSS_URL=http://hhr360oss.oss-cn-hangzhou.aliyuncs.com/
package com.hongwei.futures.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import javax.imageio.stream.FileImageInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import com.opensymphony.xwork2.ActionContext;
public class WeiXinUtil {
private static final Log logger = LogFactory.getLog(WeiXinUtil.class);
/**
*
* @param httpSession
* @param request
* @param actionContext
* @param from 第一次分享出去后 后面微信会自己拼接 from 和 isappinstalled(这是个坑人的点)
* @param isappinstalled
* @throws Exception
*/
public static void getToken(HttpSession httpSession, HttpServletRequest request, ActionContext actionContext, String from,
String isappinstalled) throws Exception {
String access_token = null;
if (null == httpSession.getAttribute("access_token")) {
// 第一步获取token存入全局缓存,
String result1 = HttpClientUtil.getHTTP("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Property.getProperty("WEIXIN_APPID") + "&secret=" + Property.getProperty("WEIXIN_APP_SECRET"));
org.json.JSONObject obj1 = new JSONObject(result1);
access_token = obj1.get("access_token").toString();
httpSession.setAttribute("access_token", access_token);
} else {
access_token = httpSession.getAttribute("access_token").toString();
}
String jsapi_ticket = null;
if (null == httpSession.getAttribute("jsapi_ticket")) {
// 第二步根据token得到jsapi_ticket存入全局缓存
String result2 = HttpClientUtil.getHTTP("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi");
JSONObject obj2 = new JSONObject(result2);
jsapi_ticket = obj2.get("ticket").toString();
httpSession.setAttribute("jsapi_ticket", jsapi_ticket);
} else {
jsapi_ticket = httpSession.getAttribute("jsapi_ticket").toString();
}
// 获取请求的地址
StringBuffer url = request.getRequestURL();
String contextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
String httpUrl = contextUrl + request.getRequestURI();
if (from != null && isappinstalled != null) {
httpUrl = httpUrl + "?from=" + from + "&isappinstalled=" + isappinstalled;
}
// 签名算法
Map map = Sign.sign(jsapi_ticket, httpUrl);
actionContext.put("appId", Property.getProperty("WEIXIN_APPID"));
actionContext.put("timestamp", map.get("timestamp"));
actionContext.put("nonceStr", map.get("nonceStr"));
actionContext.put("signature", map.get("signature"));
actionContext.put("newDate", new Date().getTime());
}
public static String getOnlyToken(HttpSession httpSession) throws Exception {
String access_token = null;
if (null == httpSession.getAttribute("access_token")) {
// 第一步获取token存入全局缓存,
String result1 = HttpClientUtil.getHTTP("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Property.getProperty("WEIXIN_APPID") + "&secret=" + Property.getProperty("WEIXIN_APP_SECRET"));
org.json.JSONObject obj1 = new JSONObject(result1);
access_token = obj1.get("access_token").toString();
httpSession.setAttribute("access_token", access_token);
} else {
access_token = httpSession.getAttribute("access_token").toString();
}
return access_token;
}
/**
* 从微信上获取图片并且上传到OSS服务器上
* @param httpSession
* @param serverId
* @return
* @throws Exception
*/
public static String downloadPicAndUploadOSS(HttpSession httpSession, String serverId) throws Exception {
String token = getOnlyToken(httpSession);
String downloadUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+token+"&media_id="+serverId;
byte[] buffer = null;
BufferedInputStream in = new BufferedInputStream(new URL(downloadUrl).openStream());
String url = WeiXinUtil.class.getResource("/").getPath();
String filePath = url+"cc.jpg";
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
buffer = new byte[2048];
int length = in.read(buffer);
while (length != -1) {
out.write(buffer, 0, length);
length = in.read(buffer);
}
in.close();
out.close();
byte[] imageToArray = imageToArray(filePath);
String stockImage = OSSUploadUtil.imageFileUpload(imageToArray, "cc.jpg", "_stock");
logger.info("stock_image_url====>" + stockImage);
return stockImage;
}
public static byte[] imageToArray(String filePath) throws FileNotFoundException, IOException {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(filePath));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
}
package com.hongwei.futures.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HttpClientUtil {
private static final Log logger = LogFactory.getLog(HttpClientUtil.class);
/**
* http get 方式交互
*/
public static String getHTTP(String URL) {
String responseMsg = "";
// 构造HTTPClient的实例
HttpClient httpClient = new HttpClient();
GetMethod getmethod = new GetMethod(URL);
// 使用系统系统的默认的恢复策略
getmethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// ִ执行 HTTPClient方法,调用HTTP接口
httpClient.executeMethod(getmethod);
// 读取返回的内容
byte[] responseBody = getmethod.getResponseBody();
// 处理返回内容
responseMsg = new String(responseBody);
// 返回结果显示
// System.out.println("HTTP GET 方式执行结果:"+responseMsg);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放操作
getmethod.releaseConnection();
}
return responseMsg;
}
public static String getHTTP_New(String URL) {
String responseMsg = "";
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(URL);
// 此处可以在getMethod上添加请求参数
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getMethod.getResponseBody();
// 处理内容
responseMsg = new String(responseBody);
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
return responseMsg;
}
// HTTP 通过POST方式交互
/**
* http post 方式交互
*/
public static String postHTTP(String URL, String uid, String pwd, String tos, String content, String otime) {
String ResultStrMsg = "";
// 1.构造HttpClient的实例
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("GB2312");
PostMethod method = new PostMethod(URL);
// 把参数值放入到PostMethod对象中
// 方式一
NameValuePair[] dataparam = { new NameValuePair("id", uid), new NameValuePair("pwd", pwd),
new NameValuePair("to", tos), new NameValuePair("content", content), new NameValuePair("time", otime) };
method.addParameters(dataparam);
// 方式二
// method.addParameter("", "");
// method.addParameter("", "");
try {
// 执行接口方法,调用接口方法
httpClient.executeMethod(method);
// 读取返回的值ֵ
ResultStrMsg = method.getResponseBodyAsString().trim();
// System.out.println("HTTP POST 方式执行结果:"+ResultStrMsg);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
method.releaseConnection();
}
return ResultStrMsg;
}
public static String postHTTP_new(String URL, String uid, String pwd, String tos, String content, String otime) {
String ResultStrMsg = "";
// 1.构造HttpClient的实例
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("GB2312");
PostMethod method = new PostMethod(URL);
// 把参数值放入到PostMethod对象中
// 方式一
NameValuePair[] dataparam = { new NameValuePair("account", uid), new NameValuePair("pswd", pwd),
new NameValuePair("mobile", tos), new NameValuePair("msg", content), new NameValuePair("needstatus", otime) };
method.addParameters(dataparam);
// 方式二
// method.addParameter("", "");
// method.addParameter("", "");
try {
// 执行接口方法,调用接口方法
httpClient.executeMethod(method);
// 读取返回的值ֵ
ResultStrMsg = method.getResponseBodyAsString().trim();
// System.out.println("HTTP POST 方式执行结果:"+ResultStrMsg);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
method.releaseConnection();
}
return ResultStrMsg;
}
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
logger.error("Weixin server connection timed out.");
} catch (Exception e) {
logger.error("https request error:{}", e);
}
return jsonObject;
}
}
package com.hongwei.futures.util;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;
import java.util.UUID;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
/**
* 微信分享接口的SHA1签名
* @author Administrator
*
*/
public class Sign {
public static void main(String[] args) {
String jsapi_ticket = "jsapi_ticket";
// 注意 URL 一定要动态获取,不能 hardcode
String url = "http://example.com";
Map ret = sign(jsapi_ticket, url);
for (Map.Entry entry : ret.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
};
public static Map sign(String jsapi_ticket, String url) {
Map ret = new HashMap();
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String string1;
String signature = "";
//注意这里参数名必须全部小写,且必须有序
string1 = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + nonce_str +
"×tamp=" + timestamp +
"&url=" + url;
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
ret.put("url", url);
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", nonce_str);
ret.put("timestamp", timestamp);
ret.put("signature", signature);
return ret;
}
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
private static String create_nonce_str() {
return UUID.randomUUID().toString();
}
private static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000);
}
}
package com.hongwei.futures.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Random;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ServiceException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSErrorCode;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.CannedAccessControlList;
import com.aliyun.openservices.oss.model.ObjectMetadata;
public class OSSUploadUtil {
private static String[] types = new String[] { ".bmp", ".png", ".gif", ".jpeg", ".pjpeg", ".jpg" };
public static String imageFileUpload(byte[] fileByte, String user_avatar, String suffix) throws Exception {
String fileType = ".jpg";
for (int i = 0; i < types.length; i++) {
if (types[i].equalsIgnoreCase(user_avatar.substring(user_avatar.lastIndexOf(".")))) {
if (types[i].endsWith(".gif"))
fileType = ".gif";
if (types[i].endsWith(".png"))
fileType = ".png";
}
}
String fileName = (System.currentTimeMillis() + (new Random(999999).nextLong())) + suffix + fileType;
try {
InputStream input = new ByteArrayInputStream(fileByte);
String bucketName = "whkzdd";
// 使用默认的OSS服务器地址创建OSSClient对象。
OSSClient client = new OSSClient(Property.getProperty("OSS_ACCESS_ID"), Property.getProperty("OSS_ACCESS_KEY"));
ensureBucket(client, bucketName);
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentLength(fileByte.length);
client.putObject(bucketName, fileName, input, objectMeta);
String saveUrl = Property.getProperty("OSS_URL") + fileName;
return saveUrl;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void ensureBucket(OSSClient client, String bucketName)throws OSSException, ClientException {
try {
// 创建bucket
client.createBucket(bucketName);
client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
} catch (ServiceException e) {
if (!OSSErrorCode.BUCKES_ALREADY_EXISTS.equals(e.getErrorCode())) {
// 如果Bucket已经存在,则忽略
throw e;
}
}
}
}
// 个人信息
@Action("userInfo")
public String userInfo() {
FuUser fuUser = (FuUser) this.getHttpServletRequest().getSession().getAttribute("fuUser");
this.getActionContext().put("fuUser", fuUser);
try {
WeiXinUtil.getToken(this.getHttpSession(), this.getHttpServletRequest(), this.getActionContext(),
from, isappinstalled);
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}