小程序在线客服功能.rar

  • I1_464568
    了解作者
  • 5.4KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-17 03:57
    上传日期
微信小程序在线客服功能,主要实现了微信小程序不能长按识别二维码的问题,在客服功能中,用户回复0,客服自动发送二维码
小程序在线客服功能.rar
  • 小程序在线客服功能
  • WxUtil.java
    5.5KB
  • PushMessagesAction.java
    5.3KB
  • HttpRequestUtil.java
    2.4KB
内容介绍
package com.mx.travel.file.util; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.security.MessageDigest; import net.sf.json.JSONObject; public class WxUtil { // 获取AccessToken public final static String GetPageAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET"; // 获取JsapiTicket public final static String GetJsapiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi"; // 永久素材的地址 private static final String UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"; public static String httpurl(String url) { String message = ""; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); System.setProperty("sun.net.client.defaultReadTimeout", "30000"); http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); message = new String(jsonBytes, "UTF-8"); is.close(); } catch (Exception e) { // TODO: handle exception } return message; } public static String getAccessToken(String appid, String appsecret) { String url = GetPageAccessTokenUrl.replace("APPID", appid).replace("SECRET", appsecret); String message = httpurl(url); JSONObject demoJson = JSONObject.fromObject(message); System.out.println("AccessToken---" + demoJson.toString()); String access_token = demoJson.getString("access_token"); return access_token; } public static String getJsapiTicket(String accessToken) { String url = GetJsapiTicketUrl.replace("ACCESS_TOKEN", accessToken); String message = httpurl(url); JSONObject demoJson = JSONObject.fromObject(message); String ticket = demoJson.getString("ticket"); return ticket; } public static String SHA1(String decript) { try { MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (Exception e) { // TODO: handle exception } return ""; } public static String uploadFile(File file, String accessToken, String type) throws Exception { // File file = new File(filePath); if (!file.exists() || !file.isFile()) { throw new IOException("文件不存在!"); } String url = UPLOAD_URL.replace("ACCESS_TOKEN", accessToken).replace("TYPE", type); URL urlObj = new URL(url); // 连接 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); // 请求头 conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); // conn.setRequestProperty("Content-Type","multipart/form-data;"); // 设置边界 String BOUNDARY = "----------" + System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY); StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition:form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] head = sb.toString().getBytes("utf-8"); // 输出流 OutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(head); // 文件正文部分 DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); // 结尾 byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8"); out.write(foot); out.flush(); out.close(); // 获取响应 StringBuffer buffer = new StringBuffer(); BufferedReader reader = null; String result = null; reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } if (result == null) { result = buffer.toString(); } reader.close(); // 需要添加json-lib jar包 JSONObject json = JSONObject.fromObject(result); String mediaId = json.getString("media_id"); return mediaId; } public static void main(String[] args) throws Exception { String access_token = WxUtil.getAccessToken("appid", "skey"); File file = new File("D:/weixin/gong.jpg"); String type = "image"; String aa = uploadFile(file, access_token, type); System.out.println(aa); } }
评论
    相关推荐
    • 微信小程序源码
      微信小程序源码微信小程序源码微信小程序源码微信小程序源码微信小程序源码
    • 微信小程序
      微信小程序微信小程序微信小程序微信小程序微信小程序微信小程序微信小程序
    • 微信小程序
      PartyTogether 微信小程序 This is a small DEMO for Class 10-year anniversary. I will play around with 微信小程序,and at the same time make it work for our class.
    • 微信小程序
      零基础学习微信小程序开发,精选5个案例详细讲解。零基础学习微信小程序开发,精选5个案例详细讲解。
    • 微信小程序
      微信小程序食品商城示例,微信小程序食品商城示例,微信小程序食品商城示例,
    • 微信小程序
      微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报微信小程序天气预报
    • 微信小程序微信小应用微信开发
      微信小程序,微信小应用,微信开发
    • 微信小程序微信小程序游戏纪念碑古游戏源码
      现在微信小程序游戏比较火,跳一跳风靡,这里给大家分享一个小程序游戏:纪念碑谷的源码,希望能帮助到大家
    • 微信小程序
      微信小程序源码仿幸运转盘抽奖微信小程序源码仿幸运转盘抽奖微信小程序源码仿幸运转盘抽奖
    • 微信小程序微信公众号微信支付golang
      微信小程序微信公众号微信支付golang-源码