生成帶參數二維碼的兩種方法
- 需求介紹
- 實現思路
- 方法一:通過微信生成帶參數的二維碼圖片
- 方法二:通過ZXing生成帶參數的二維碼圖片
需求介紹
業務需求,用户微信掃描二維碼進入公眾號中的某一個業務頁面並可以完成相應的業務操作。
例如我的需求就是:用户掃描二維碼進入公眾號的居民滿意度調查頁面,用户在調查頁面進行打分操作。
實現思路
- 生成包含小區ID和需要跳轉的也公眾號授權頁面服務器路徑參數的二維碼圖片;
- 將帶參數的二維碼圖片保存至對應的圖片服務器中。
- 生成對應的小區滿意度調查通知(通知包含公眾號二維碼)。
- 打印通知張貼,掃描通知上的二維碼進入調查打分頁面。
關聯公共號ID,獲取token等基礎操作在此省略,掃描二維碼用户後續操作,不知道的可以查看上一篇微信公共號獲取用户發送信息的前段內容,掃描二維碼動作即是用户掃碼推事件,設置事件類型值為scan。
方法一:通過微信生成帶參數的二維碼圖片
package com.chachaba.app;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.chachaba.constant.result.CommonResult;
import com.chachaba.files.service.FilesService;
import com.chachaba.shiro.exception.ServiceException;
import com.chachaba.util.UrlEncode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 微信授權登錄
*
* @author xingyu
* @date 2020年9月23日14:51:16
**/
@RestController
@RequestMapping("/app/wemp")
@Api(value = "wemp", description = "公眾號專用接口,開發給前端")
public class APPWxMPUserController {
/**
* 微信公眾號APPID
*/
private static final String APP_ID = "你的微信公眾號APPID";
/**
* 微信公眾號APPSECRET
*/
private static final String APPSECRET = "你的微信公眾號APPSECRET";
/**
* 用户授權, 獲取code參數的url地址
*/
private static final String LOGIN_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
/**
* 通過code獲取access_token的url地址
*/
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
/**
* 通過access_token獲取到微信用户信息的地址
*/
private static final String USER_INFO_URL = "https://api.weixin.qq.com/sns/userinfo";
@Autowired
private FilesService filesService;
@ApiOperation("獲取微信授權的code並跳轉到對應的url")
@ApiImplicitParam(name = "scope", value = "授權方式,值為snsapi_base或者snsapi_userinfo", paramType = "query", required = true, dataType = "String")
@RequestMapping("/getLoginUrl")
public CommonResult getLoginUrl(@ApiParam(name = "scope", value = "值有兩個: snsapi_base 默認授權,獲取不到WeChat用户詳細信息 snsapi_userinfo彈出方式授權,可以獲取到WeChat用户信息 ") String scope,
@ApiParam(name = "redirectUri", value = "用户同意授權後跳轉的頁面") String redirectUri,
@ApiParam(name = "state", value = "重定向後的參數") String state) throws Exception {
//獲得url後面的參數
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("appid", APP_ID);
paramsMap.put("redirect_uri", URLEncoder.encode(redirectUri, "utf-8"));
paramsMap.put("response_type", "code");
paramsMap.put("scope", scope);
//重定向後帶過去的參數
paramsMap.put("state", state);
String urlParams = UrlEncode.getUrlParams(paramsMap, true);
//微信官方要求必須帶上#wechat_redirect
urlParams = "?" + urlParams + "#wechat_redirect";
Map<String, String> returnMap = new HashMap<>();
returnMap.put("url", LOGIN_URL + urlParams);
return new CommonResult(CommonResult.SUCCESS_CODE, "success", returnMap);
}
@ApiOperation("通過code獲取到ACCESS_TOKEN")
@RequestMapping("/getAccessToken")
public CommonResult getAccessToken(@ApiParam(name = "code", value = "用户同意授權後獲取的code值") String code) throws Exception {
if (StringUtils.isBlank(code)) {
throw new ServiceException("code參數為null,請檢查");
}
//獲得url後面的參數
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("appid", APP_ID);
paramsMap.put("secret", APPSECRET);
paramsMap.put("code", code);
paramsMap.put("grant_type", "authorization_code");
String urlParams = UrlEncode.getUrlParams(paramsMap, true);
urlParams = "?" + urlParams;
//生成新的訪問的URL地址
String url = ACCESS_TOKEN_URL + urlParams;
String getResult = HttpUtil
.createGet(url)
.execute()
.charset("gbk")
.body();
//獲得到access_token
Map<String, Object> returnMap = JSONObject.parseObject(getResult);
//獲取用户access_token
String accessToken = (String) returnMap.get("access_token");
//獲取到用户唯一標識openid
String openid = (String) returnMap.get("openid");
Map<String, Object> userInfo = getUserInfo(accessToken, openid, "zh_CN");
return new CommonResult(CommonResult.SUCCESS_CODE, "success", userInfo);
}
/**
* 通過 accessToken換區 微信授權用户詳細信息
*
* @param accessToken 上一步獲取的access_token
* @param openid 用户唯一標識
* @param lang 返回國家地區語言版本,zh_CN 簡體,zh_TW 繁體,en 英語
*/
private Map<String, Object> getUserInfo(String accessToken, String openid, String lang) {
//獲得url後面的參數
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("access_token", accessToken);
paramsMap.put("openid", openid);
paramsMap.put("lang", lang);
String urlParams = UrlEncode.getUrlParams(paramsMap, true);
urlParams = "?" + urlParams;
//生成新的訪問的URL地址
String url = USER_INFO_URL + urlParams;
String getResult = HttpUtil
.createGet(url)
.execute()
.charset("utf-8")
.body();
//獲取的到微信用户信息
return JSONObject.parseObject(getResult);
}
/**
* 臨時生成字符二維碼url
*/
private static final String GET_CODE_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";
/**
* 二維碼有效時間
*/
private static final String QR_ORDER_EXPIRE_TIME ="2592000";
/**
* 返回二維碼鏈接url
*/
public final static String BACK_QR_CODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";
/**
* 獲取AccessToken
*/
public final static String ACCESS_TOKEN_URL1 = "https://api.weixin.qq.com/cgi-bin/token";
public static String getToken(){
//獲得url後面的參數
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("appid", APP_ID);
paramsMap.put("secret", APPSECRET);
paramsMap.put("grant_type", "client_credential");
String urlParams = UrlEncode.getUrlParams(paramsMap, true);
urlParams = "?" + urlParams;
//生成新的訪問的URL地址
String url = ACCESS_TOKEN_URL1 + urlParams;
String getResult = HttpUtil
.createGet(url)
.execute()
.charset("gbk")
.body();
//獲得到access_token
Map<String, Object> returnMap = JSONObject.parseObject(getResult);
//獲取用户access_token
String accessToken = (String) returnMap.get("access_token");
return accessToken;
}
//永久二維碼請求説明
//http請求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
// POST數據格式:json POST數據例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
// 或者也可以使用以下POST數據創建字符串形式的二維碼參數: {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
/**
* @description: 獲取二維碼ticket後,開發者可用ticket換取二維碼圖片。
* @author: gxy
* @time: 2020/10/28 16:42
* @version 1.0
*
*/
public static String getTicketData(String str) {
String token = getToken();
if (StringUtils.isEmpty(str) || token == null) {
return null;
}
// if (str.length() > 64) {
// return null;
// }
//臨時字符二維碼url
String url = (GET_CODE_TICKET_URL).replace("TOKEN", token);
//二維碼參數,以及過期時間
String data = "{\"expire_seconds\": " + QR_ORDER_EXPIRE_TIME + ", \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"test\"}}}";
data = data.replace("test", str);
String result = WxHttpUtil.sendHttpByPost(url, data);
if (!StringUtils.isEmpty(result)) {
JSONObject jsonObject = JSONObject.parseObject(result);
str = jsonObject.getString("ticket");
System.out.println("ticket = {} " + str);
return str;
}
return null;
}
/**
* @description: 用户返回二維碼鏈接url
* @time: 2020/10/28 16:42
* @version 1.0
*/
public static String getTicket(String data) {
String url = BACK_QR_CODE_URL + data;
return url;
}
/**
* 居民調查問卷地址
*/
public final static String CUSTOMER_SURVEY_URL = "你的業務頁面服務器地址";
/**
* 圖片服務上傳地址
*
*/
public final static String FTP_SERVER_URL = "你的圖片服務器地址";
/**
*@description: 生成二維碼頁面
* @time: 2020/10/28 16:42
* @version 1.0
* @param modelMap
* @param dId 小區ID
* @param dName 小區名稱
* @return
*/
@RequestMapping(value = "/con/qrPage")
public String getCustomerMsg1(ModelMap modelMap,String dId,String dName) {
String url =CUSTOMER_SURVEY_URL+"?dId="+dId+"&dName="+dName;
String ticket= getTicketData(url);
if(StringUtils.isEmpty(ticket)){
//換區ticktUrl失敗 返回403頁面
return "403";
}
String ticketUrl = getTicket(ticket);
System.out.println("返回的二維碼 url = {} "+ticketUrl);
//將返回的二維碼下載成圖片 本地地址
// downloadPicture(ticketUrl,dId,dName);
//直接寫入FTP服務器 圖片地址
String viewPath = filesService.qrCodeWirte2Ftp(ticketUrl,dId,dName);
//返回服務顯示地址
return viewPath;
}
private static void downloadPicture(String urlList,String dId,String dName) {
URL url = null;
//int imageNumber = 0;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
// 本地地址及名稱
String imageName = "F:/" + dId+ ".jpg";
FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context = output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*@description: 掃描二維碼頁面
* 用户掃描帶場景值二維碼時,可能推送以下兩種事件:
* 如果用户還未關注公眾號,則用户可以關注公眾號,關注後微信會將帶場景值關注事件推送給開發者。
* 如果用户已經關注公眾號,則微信會將帶場景值掃描事件推送給開發者。
*/
// scancode_push:掃碼推事件的事件推送
// 推送XML數據包示例:
//
// <xml><ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
// <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
// <CreateTime>1408090502</CreateTime>
// <MsgType><![CDATA[event]]></MsgType>
// <Event><![CDATA[scancode_push]]></Event>
// <EventKey><![CDATA[6]]></EventKey>
// <ScanCodeInfo><ScanType><![CDATA[qrcode]]></ScanType>
// <ScanResult><![CDATA[1]]></ScanResult>
// </ScanCodeInfo>
// </xml>
}
工具類WxHttpUtil.java
package com.chachaba.app;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WxHttpUtil {
/**
* @param url
* @param data 生成字符二維碼的數據包
* @return
*
*/
public static String sendHttpByPost(String url ,String data ){
try {
URL urlPost = new URL(url);
URLConnection urlConnection = urlPost.openConnection();
//要發送數據出去,必須設置為可發送狀態
urlConnection.setDoOutput(true);
//獲取輸出流
OutputStream outputStream = urlConnection.getOutputStream();
//寫出數據
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();
//獲取輸入流
InputStream is = urlConnection.getInputStream();
int size ;
byte[] jsonBytes = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
while ((size=is.read(jsonBytes))!=-1){
stringBuilder.append(new String(jsonBytes,0,size));
}
is.close();
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param url
* @param url 換取二維碼的數據包
* @return
*
*/
public static String sendHttpByGet(String url){
try {
URL urlGet = new URL(url);
URLConnection urlConnection = urlGet.openConnection();
InputStream is = urlConnection.getInputStream();
int size ;
byte[] jsonBytes = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
while ((size=is.read(jsonBytes))!=-1){
stringBuilder.append(new String(jsonBytes,0,size));
}
is.close();
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解析xml 獲取key-value
*
* @param inputStream
* @return
*/
public static Map<String, String> getXmlData(InputStream inputStream) {
Map<String, String> map = new HashMap<>(12);
//截取xml
SAXReader reader = new SAXReader();
try {
Document document = reader.read(inputStream);
//獲取根節點
Element rootElement = document.getRootElement();
// h獲取所有的節點
List<Element> elements = rootElement.elements();
for (Element e : elements) {
map.put(e.getName(), e.getStringValue());
}
} catch (DocumentException e) {
e.printStackTrace();
}
return map;
}
/**
* * @description 將xml字符串轉換成map
* * @param xml
* * @return Map
*
*/
public static Map<String, String> readStringXmlOut(String xml) {
Map<String, String> map = new HashMap<>(12);
Document doc = null;
try {
doc = DocumentHelper.parseText(xml); // 將字符串轉為XML
Element rootElt = doc.getRootElement(); // 獲取根節點
@SuppressWarnings("unchecked")
List<Element> list = rootElt.elements();// 獲取根節點下所有節點
for (Element element : list) { // 遍歷節點
map.put(element.getName(), element.getText()); // 節點的name為map的key,text為map的value
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
方法二:通過ZXing生成帶參數的二維碼圖片
/**
* 微信端授權滿意度問卷地址
*/
private static String QR_CODE_CUSTOMER_SURVEY_URL = " http://服務地址/wx_jump_satisScore.html?dId=DID";
/**
* 圖片服務上傳地址
*http://183.61.244.2:9995/file/lhwy
*/
private String FTP_SERVER_URL = "你的圖片服務器地址";
/**
* 生成小區的二維碼圖片(本地項目upload路徑) 並上傳至FTP服務器
* (方法需要修改 用多進程或者直接上傳)
* @param request
* @param dId
*/
public void createQRCode(HttpServletRequest request, String dId) {
// 存放在二維碼中的內容 存跳轉授權頁面
String text =(QR_CODE_CUSTOMER_SURVEY_URL).replace("DID",dId);
//設置圖片上傳路徑
String url = request.getSession().getServletContext().getRealPath("/upload");
System.out.println(url);
// 嵌入二維碼的圖片路徑
String imgPath = url + "/logo.jpg";
// 生成的二維碼的路徑及名稱
String destPath = url+"/Community_QRcode/"+dId+".jpg";
try {
//生成二維碼
QRCodeUtil.encode(text, imgPath, destPath, true);
//上傳至.2服務器中
String destPath1 = "file:///"+destPath;
String viewPath = filesService.qrCodeWirte2Ftp(destPath1,dId,"");
// 解析二維碼
// String str = QRCodeUtil.decode(destPath);
// // 打印出解析出的內容
// System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
工具類 QRCodeUtil.java
package com.chachaba.assess.controller;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
/**
* @Date: 2020/10/23 5:20 下午
* @Description: 二維碼工具類
*/
public class QRCodeUtil {
//編碼
private static final String CHARSET = "utf-8";
//文件格式
private static final String FORMAT_NAME = "JPG";
// 二維碼尺寸
private static final int QRCODE_SIZE = 300;
// LOGO寬度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
/**
* 生成二維碼
* @param content 內容
* @param imgPath logo
* @param needCompress 是否需要壓縮
* @return java.awt.image.BufferedImage
* @throws Exception
*/
public static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入圖片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 插入logo
* @param source 二維碼圖片
* @param imgPath logo路徑
* @param needCompress 是否壓縮
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + " 該文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 壓縮LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 繪製縮小後的圖
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
public static void mkdirs(String destPath) {
File file = new File(destPath);
// 當文件夾不存在時,mkdirs會自動創建多層目錄,區別於mkdir.(mkdir如果父目錄不存在則會拋出異常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* 生成二維碼,獲得到輸入流 log內嵌
* @param content 內容
* @param imgPath logo路徑
* @param output 輸入流
* @param needCompress 是否壓縮
* @throws Exception
*/
public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
ImageIO.write(image, FORMAT_NAME, output);
}
/**
* 獲取指定的logo文件輸入流
* @param logoPath logo路徑
* @return java.io.InputStream
*/
public static InputStream getResourceAsStream(String logoPath) {
return QRCodeUtil.class.getResourceAsStream(logoPath);
}
/**
* 將要解析的二維碼的存放路徑
* @param file 要解析的二維碼
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
/**
* 解析二維碼
* @param path 要解析的二維碼路徑
* @return
* @throws Exception
*/
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
/**
* 生成二維碼
* @param content
* @param imgPath
* @param destPath
* @param needCompress
* @throws Exception
*/
public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
mkdirs(destPath);
// String file = new Random().nextInt(99999999)+".jpg";
// ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
ImageIO.write(image, FORMAT_NAME, new File(destPath));
}
}
fileService.java 圖片上傳FTP 部分代碼
/**
* @descript 將公共號返回的二維碼轉成圖片穿到服務器上
* @date 2020-10-30 下午1:57:24
* @vision 1.0
* @param urlList
* @param dId
* @param dName
* @return
*/
@Override
public String qrCodeWirte2Ftp(String urlList,String dId,String dName){
URL url = null;
String viewPath ="";
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
//ftp配置
FtpUtils instance = FtpUtils.getInstance();
String DeString = AESEncryption.getInstance().decrypt(this.userPwd);
instance.doInitConfig(this.hostName, this.port, this.userName, DeString);
instance.open();
String fileSavePath = "/upload/Community_QRcode";
String fileName = dId +".jpg";
boolean isSuccess = instance.upload(this.defaultDir + fileSavePath, fileName, dataInputStream);
//看看地址
log.info("看看地址"+fileSavePath + "/" + fileName);
if (!isSuccess) {
throw new ServiceException("上傳失敗");
}
viewPath = "你的圖片查看服務地址" +fileSavePath + "/" + fileName;
//-------------關流-----------------
dataInputStream.close();
instance.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return viewPath;
}
wx_jump_satisScore.html 微信授權跳轉業務頁面相關代碼
<script type="text/javascript" charset="utf-8">
$(function () {
var dId = getQueryVariable("dId");
// var dName = getQueryVariable("dName");
getUrl(dId);
});
/**
* 此頁面起到一個連接的作用, 即為要跳轉的頁面生成code參數
*
* 跳轉到投訴頁面 tscl_list.html
*/
function getUrl(dId) {
var url = "你的物業頁面(我的滿意度調查頁面)";
$.ajax({
//url :url1,
url: pre_server_url + "/app/wemp/getLoginUrl",
type: "GET",
data: {scope: "snsapi_userinfo", redirectUri: url,state :dId},
success: function (result) {
//跳轉到投訴頁面 [帶code參數]
window.location.href = result.data.url;
}
})
}
/**
* 獲取URL參數
* @param variable
* @returns {*}
*/
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return (false);
}
業務頁面部分代碼
//獲取token
function gettoken(code) {
var getopenidurl = pre_server_url + 'app/wemp/getAccessToken?code='
var getoken = pre_server_url + 'wechatUser/addOrGetWechatUser'
$.ajax({
type: "get",
url: getopenidurl + code,
crossDomain: true,
async: false,
success: function (result) {
var openid = result.data.openid;
var country = result.data.country;
var province = result.data.province;
var city = result.data.city;
var sex = result.data.sex;
var nickname = result.data.nickname;
var headimgurl = result.data.headimgurl;
if (result.data.errcode == 41001) {
weixinid = window.localStorage.getItem('touristId');
return;
}
$.ajax({
type: "post",
url: getoken,
dataType: 'json',
data: { openid, country, province, city, sex, nickname, headimgurl },
crossDomain: true,
async: false,
success: function (result) {
//localStorage.setItem("uId", result.data.id);
window.localStorage.setItem("touristId", result.data.id);
weixinid = result.data.id;
}
});
}
});
}
本文章為轉載內容,我們尊重原作者對文章享有的著作權。如有內容錯誤或侵權問題,歡迎原作者聯繫我們進行內容更正或刪除文章。