|
@@ -3,6 +3,8 @@ package com.ygj.yuemum.service.global;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.ygj.yuemum.dao.global.WeChatInfoDao;
|
|
import com.ygj.yuemum.dao.global.WeChatInfoDao;
|
|
import com.ygj.yuemum.domain.global.WeChatInfo;
|
|
import com.ygj.yuemum.domain.global.WeChatInfo;
|
|
|
|
+import com.ygj.yuemum.domain.wxmini.WXUser;
|
|
|
|
+import com.ygj.yuemum.service.wxmini.WXUserService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -19,19 +21,26 @@ import java.util.*;
|
|
public class WeChatInfoService {
|
|
public class WeChatInfoService {
|
|
@Autowired
|
|
@Autowired
|
|
private WeChatInfoDao weChatInfoDao;
|
|
private WeChatInfoDao weChatInfoDao;
|
|
|
|
+ @Autowired
|
|
|
|
+ private WXUserService wxUserService;
|
|
public static String APPID = "wx52852a3c47540fad";
|
|
public static String APPID = "wx52852a3c47540fad";
|
|
public static String APPSECRET = "31e4eef58038830327eb88661e3a681f";
|
|
public static String APPSECRET = "31e4eef58038830327eb88661e3a681f";
|
|
|
|
+ public static String MINIAPPID = "wxcf2db527c41184e8";
|
|
|
|
+ public static String MINIAPPSECRET = "6534f385c5a3a0985ea7bead4f5c545f";
|
|
public static String GRANT_TYPE = "client_credential";
|
|
public static String GRANT_TYPE = "client_credential";
|
|
public static String JSAPI_TICKET_TYPE = "jsapi";
|
|
public static String JSAPI_TICKET_TYPE = "jsapi";
|
|
public static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
|
|
public static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
|
|
public static String JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
|
|
public static String JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
|
|
- private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5','6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
|
|
|
|
+ public static String JSCODE2SESSION = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
|
+ private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
String access_Token;
|
|
String access_Token;
|
|
String expires_in;
|
|
String expires_in;
|
|
String jsapi_Ticket;
|
|
String jsapi_Ticket;
|
|
String noncestr;
|
|
String noncestr;
|
|
String timeStamp;
|
|
String timeStamp;
|
|
String signaTure;
|
|
String signaTure;
|
|
|
|
+ String session_key;
|
|
|
|
+ String openid;
|
|
|
|
|
|
public int SaveWeChatInfo() {
|
|
public int SaveWeChatInfo() {
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
@@ -51,33 +60,65 @@ public class WeChatInfoService {
|
|
weChatInfo.setJsapi_ticket(jsapi_Ticket);
|
|
weChatInfo.setJsapi_ticket(jsapi_Ticket);
|
|
weChatInfo.setUpdate_time(dateFormat.format(new Date()));
|
|
weChatInfo.setUpdate_time(dateFormat.format(new Date()));
|
|
weChatInfoDao.updateWeChatInfo(weChatInfo);
|
|
weChatInfoDao.updateWeChatInfo(weChatInfo);
|
|
- }catch (Exception ex){
|
|
|
|
|
|
+ } catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
ex.printStackTrace();
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
- public Map<String,String> getWeChatInfo(String url){
|
|
|
|
- WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
|
|
|
|
|
|
+
|
|
|
|
+ public Map<String, String> getWXOpenid(String js_code) {
|
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
|
+ String resultToken = sendGet(JSCODE2SESSION + "?grant_type=authorization_code" + "&appid=" + MINIAPPID + "&secret=" + MINIAPPSECRET + "&js_code=" + js_code);
|
|
|
|
+ Map mapToken = JSON.parseObject(resultToken);
|
|
|
|
+ session_key = String.valueOf(mapToken.get("session_key"));
|
|
|
|
+ openid = String.valueOf(mapToken.get("openid"));
|
|
|
|
+ //save database
|
|
|
|
+ //检查用户是否已经注册,注册返回手机号等信息,没注册返回openid作为用户名
|
|
|
|
+ //新用户 insert ,没有手机返回openid 有手机返回手机号等信息
|
|
|
|
+ WXUser ckeckwxUser = wxUserService.getWXUser(openid);
|
|
|
|
+ if(ckeckwxUser != null) {
|
|
|
|
+ result.put("openid", openid);
|
|
|
|
+ result.put("type","2");
|
|
|
|
+ result.put("nickName",ckeckwxUser.getXu_name());
|
|
|
|
+ } else {
|
|
|
|
+ WXUser wxUser = new WXUser();
|
|
|
|
+ wxUser.setXu_openid(openid);
|
|
|
|
+ wxUser.setXu_sessionkey(session_key);
|
|
|
|
+ UUID id=UUID.randomUUID();
|
|
|
|
+ String[] idd=id.toString().split("-");
|
|
|
|
+ String lsid = "YSLS"+idd[1];
|
|
|
|
+ wxUser.setXu_name(lsid);
|
|
|
|
+ wxUserService.insertWXUser(wxUser);
|
|
|
|
+ result.put("openid", openid);
|
|
|
|
+ result.put("type","1");
|
|
|
|
+ result.put("nickName",lsid);
|
|
|
|
+ }
|
|
|
|
+ result.put("session_key", session_key);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Map<String, String> getWeChatInfo(String url) {
|
|
|
|
+ WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
|
|
Map<String, String> result = new HashMap<>();
|
|
Map<String, String> result = new HashMap<>();
|
|
String timestamp = getTimestamp();
|
|
String timestamp = getTimestamp();
|
|
String noncestr = getNoncestr();
|
|
String noncestr = getNoncestr();
|
|
- String signature =SHA1(jointString(weChatInfo.getJsapi_ticket(),noncestr,timestamp,url));
|
|
|
|
- result.put("timestamp",timestamp);
|
|
|
|
- result.put("nonceStr",noncestr);
|
|
|
|
- result.put("signature",signature);
|
|
|
|
- result.put("appid",APPID);
|
|
|
|
- result.put("jsapi_ticket",weChatInfo.getJsapi_ticket());
|
|
|
|
|
|
+ String signature = SHA1(jointString(weChatInfo.getJsapi_ticket(), noncestr, timestamp, url));
|
|
|
|
+ result.put("timestamp", timestamp);
|
|
|
|
+ result.put("nonceStr", noncestr);
|
|
|
|
+ result.put("signature", signature);
|
|
|
|
+ result.put("appid", APPID);
|
|
|
|
+ result.put("jsapi_ticket", weChatInfo.getJsapi_ticket());
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
//-----------------------------------
|
|
//-----------------------------------
|
|
- public static String jointString(String jsapi_Ticket,String noncestr,String timestamp,String url){
|
|
|
|
- String sb = new String ();
|
|
|
|
- sb +="jsapi_ticket=" + jsapi_Ticket+"&";
|
|
|
|
- sb +="noncestr=" + noncestr+"&";
|
|
|
|
- sb +="timestamp=" + timestamp+"&";
|
|
|
|
- sb +="url=" + url;
|
|
|
|
|
|
+ public static String jointString(String jsapi_Ticket, String noncestr, String timestamp, String url) {
|
|
|
|
+ String sb = new String();
|
|
|
|
+ sb += "jsapi_ticket=" + jsapi_Ticket + "&";
|
|
|
|
+ sb += "noncestr=" + noncestr + "&";
|
|
|
|
+ sb += "timestamp=" + timestamp + "&";
|
|
|
|
+ sb += "url=" + url;
|
|
return sb;
|
|
return sb;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -105,10 +146,11 @@ public class WeChatInfoService {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public static String getTimestamp(){
|
|
|
|
- return Long.toString(System.currentTimeMillis()/1000);
|
|
|
|
|
|
+ public static String getTimestamp() {
|
|
|
|
+ return Long.toString(System.currentTimeMillis() / 1000);
|
|
}
|
|
}
|
|
- public static String getNoncestr(){
|
|
|
|
|
|
+
|
|
|
|
+ public static String getNoncestr() {
|
|
return UUID.randomUUID().toString();
|
|
return UUID.randomUUID().toString();
|
|
}
|
|
}
|
|
|
|
|