java实现公众号分享朋友或朋友圈的自定义分享功能

疑问

一般公众号自带的分享功能是如下图这样的

 下面是一个链接,上面是一个标题!


但是我们一般都会要求是有标题有内容,有图片的,这种!


究竟怎么实现呢?

我花了将近一天的时候,百度,然后才找到一个靠谱的代码

现在我要贴上完整的

设置js安全接口域名

1、登录公众号平台,打开公众号设置--功能设置

2、设置js安全接口域名

前提是你有备案的域名哦!

举个例子

比如你的域名是www.jianshu.com

你的项目名是demo;

那么你可以设置接口域名为www.jianshu.com/demo

注意:不能有http://存在!

设置好域名或目录以后,然后根据提示把对应的文件下载下来!

放在你服务器的根目录!

是不是很绕口呢?很难理解!

我是java项目,我把项目放在tomcat的webapp的项目文件夹里面

我的js设置域名是www.jianshu.com/gold

所以当然我把项目放在gold项目下面啦,如果你填的是www.jianshu.com  那么你应该把这个文件放在webapps下方;

然后再浏览器输入http://www.jianshuu.com/gold/文件名.txt  或者http://www.jianshuu.com/文件名.txt 

可以访问就没问题了呢?

这个很重要,如果没有这个文件,你支付或者分享功能都不可能实现的!

 


我告诉你,要向实现自定义分享功能,有两个部分,一个服务器设置,一个是js前端设置 

 

首先我们贴上服务器设置的后端代码

我是ssh框架,你要注意一些用法改动啊

需要一个jar包  Jedis包,这个是核心是灵魂;

CommonUtil.java

package cn.com.share;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.*;
import javax.servlet.http.HttpServletResponse;
import redis.clients.jedis.Jedis;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.junit.Test;
import com.alibaba.fastjson.JSONObject;
public class CommonUtil {
    private static Logger logger = Logger.getLogger(CommonUtil.class);
    private static String appId = "对应你的公众号appid";
    private static String secret = "对应你的公众号秘钥";
    public static JSONObject getUrlInfo(String requestURL,String method,String json) throws Exception {
        URL get_url = new URL(requestURL);
        // 将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接
        // (标识一个url所引用的远程对象连接)
        // 此时cnnection只是为一个连接对象,待连接中
        HttpURLConnection httpURLConnection = (HttpURLConnection) get_url
                .openConnection();
        // 设置请求方式为post
        httpURLConnection.setRequestMethod(method);
        // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
        httpURLConnection.setDoOutput(true);
        // 设置连接输入流为true
        httpURLConnection.setDoInput(true);
        // post请求缓存设为false
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestProperty("Content-type","application/json;charset=utf-8");
        // 建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
        httpURLConnection.connect();

        // 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
        OutputStreamWriter out = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"); 
        if(method.equals("POST")){
            out.append(json);
        }
        out.flush();  
        out.close();

        // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader)
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpURLConnection.getInputStream(), "UTF-8"));
        // 读取数据操作
        String str = null;  
        StringBuffer buffer = new StringBuffer();  
        while((str = reader.readLine())!= null){  
            buffer.append(str);  
        }        
        //转换成json
        JSONObject jsonObj = JSONObject.parseObject(buffer.toString());
        reader.close();
        return jsonObj;

    }
    
    
    public static String getAccessToken() throws Exception{
        Jedis jedis = new Jedis("你的服务器公网ip地址,不带端口号的");
        String access_token = null;
        Boolean conn_flag = true;
        try{
            logger.info("jedis "+jedis.ping());
            access_token = jedis.get("access_token_dg");
            if (access_token !=null && access_token.length() > 0 ){
                return access_token;
            }
        }catch(Exception e){
            logger.info("redis连接失败");
            conn_flag = false;
        }
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" + secret;
        JSONObject access_token_json = getUrlInfo(url,"GET",null);
        access_token = access_token_json.getString("access_token");
        if (conn_flag == true){
            jedis.set("access_token_dg", access_token);
            jedis.expire("access_token_dg", 7000);
        }
        return access_token;
    }
    
    
    
    
    public static String getFreight() throws Exception{
        Jedis jedis = new Jedis("localhost");
        String freight = null;
        try{
            logger.info("jedis "+jedis.ping());
            freight = jedis.get("freight");
            if (freight !=null && freight.length() > 0 ){
                return freight;
            }
        }catch(Exception e){
            logger.info("redis连接失败");
        }
        return null;
    }
    
    
    
    
    public static String getOpenId(String code) throws Exception{
        logger.debug("code "+code);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ appId +"&secret="+ secret +"&code="+ code +"&grant_type=authorization_code";
        JSONObject openid_json = getUrlInfo(url,"GET",null);
        return openid_json.getString("openid");
    }
    
    public static JSONObject getUserInfo(String code) throws Exception{
        String access_token = getAccessToken();
        logger.debug("access_token "+access_token);
        String openid = getOpenId(code);
        logger.debug("openid "+openid);
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
        return getUrlInfo(url,"GET",null);
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值