WechatApi.java 4.64 KB
package com.polysoft.nafmii.wechat;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.whaty.nafmii.BuildConfig;
import com.whaty.nafmii.R;

public class WechatApi {

    private static WechatApi INSTANCE = null;

    private final Context ctx;
    private final IWXAPI api;

    private WechatApi(Context ctx, boolean isDebug) {
        this.ctx = ctx;
        this.api = WXAPIFactory.createWXAPI(ctx, BuildConfig.WX_APPID, isDebug);
        this.api.registerApp(BuildConfig.WX_APPID);
    }

    public static WechatApi getInstance(Context ctx) {
        return getInstance(ctx, false);
    }

    public static WechatApi getInstance(Context ctx, boolean isDebug) {
        if (null == WechatApi.INSTANCE) {
            WechatApi.INSTANCE = new WechatApi(ctx, isDebug);
        }
        return WechatApi.INSTANCE;
    }

    public void sendRequest(BaseReq req) {
        this.api.sendReq(req);
    }

    public boolean isWXAppInstalled() {
        return this.api.isWXAppInstalled();
    }

    public void handleIntent(Intent intent, IWXAPIEventHandler handler) {
        this.api.handleIntent(intent, handler);
    }

    public void pay(String payInfo) {
        this.sendRequest(JSON.parseObject(payInfo, PayReq.class));
    }

    public void share(Context ctx, final ShareBean bean) {
        String webImg = bean.getWebImg();
        String shareType = bean.getShareType();
        if ("0".equals(shareType)) { // 分享文字
            this.shareText(bean);
        } else if ("1".equals(shareType)) { // 分享图片
            this.shareImage(bean);
        } else if ("2".equals(shareType)) { // 分享url地址
            this.loadImageUrl(ctx, webImg, new Callback<Bitmap>() {
                @Override
                public void onBack(Bitmap data) {
                    shareWebPage(bean, data);
                }
            });
        }
    }

    private void shareText(ShareBean bean) {

    }

    private void shareImage(ShareBean bean) {
    }


    private void shareWebPage(ShareBean bean, Bitmap bitmap) {
        WXWebpageObject web = new WXWebpageObject();
        web.webpageUrl = bean.getWebUrl();
        WXMediaMessage msg = new WXMediaMessage(web);
        // 填写网页标题、描述、位图
        msg.title = bean.getTitle();
        //网页描述
        msg.description = bean.getContent();

        msg.setThumbImage(bitmap);
        bitmap.recycle();

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        // transaction用于唯一标识一个请求(可自定义)
        req.transaction = "webpage";
        // 上文的WXMediaMessage对象
        req.message = msg;
        req.scene = this.getShareScene(bean);
        // 向微信发送请求
        this.sendRequest(req);
    }


    private int getShareScene(ShareBean bean) {
        String scene = bean.getScene();
        if ("0".equals(scene)) { // 朋友圈
            return SendMessageToWX.Req.WXSceneTimeline;
        } else if ("1".equals(scene)) { // 分享到好友
            return SendMessageToWX.Req.WXSceneSession;
        } else if ("2".equals(scene)) { // 分享到收藏夹
            return SendMessageToWX.Req.WXSceneFavorite;
        }
        // 默认分享到好友
        return SendMessageToWX.Req.WXSceneSession;
    }


    private void loadImageUrl(Context ctx, String url, final Callback<Bitmap> callback) {
        SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                callback.onBack(resource);
            }
        };

        if (TextUtils.isEmpty(url)) {
            Glide.with(ctx).asBitmap().load(R.mipmap.ic_launcher).override(120, 120).into(target);
        } else {
            Glide.with(ctx).asBitmap().load(url).override(120, 120).into(target);
        }
    }

    private static interface Callback<T> {
        public void onBack(T data);
    }
}