一、WxShareUtils.kt
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.support.annotation.Keep
import com.xxx.base.application.tinker.BaseApplicationLike
import com.xxx.base.util.BitmapUtil
import com.xxx.xxx.common.application.tinker.MyApplicationLike
import com.xxx.xxx.BuildConfig
import com.xxx.xxx.xxx.R
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.modelmsg.*
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
@Keep
object WxShareUtils {
private fun textData(model: WxShareInfoModel): WXMediaMessage {
// 初始化一个 WXTextObject 对象,填写分享的文本内容
val textObj = WXTextObject()
textObj.text = model.shareText
// 用 WXTextObject 对象初始化一个 WXMediaMessage 对象
val msg = WXMediaMessage(textObj)
msg.description = model.shareText
return msg
}
private fun imageData(model: WxShareInfoModel): WXMediaMessage {
val bmp: Bitmap = base642Bitmap(model.imageBase64, R.mipmap.ic_launcher)
val tBmp = base642Bitmap(model.thumbBase64, R.mipmap.ic_launcher)
// 初始化 WXImageObject 和 WXMediaMessage 对象
val imgObj = WXImageObject(bmp)
val msg = WXMediaMessage(imgObj)
// 设置缩略图
val thumbBmp = Bitmap.createScaledBitmap(tBmp, tBmp.width /10, tBmp.height /10, true)
bmp.recycle()
msg.title = model.title
msg.description = model.description
msg.thumbData = BitmapUtil.bitmap2Bytes(thumbBmp)
return msg
}
private fun base642Bitmap(imgBase64: String?, res: Int): Bitmap {
if (imgBase64.isNullOrEmpty()) {
return BitmapFactory.decodeResource(BaseApplicationLike.mApplication.resources, res)
}
return BitmapUtil.Base64ToBitMap(imgBase64)
}
private fun musicData(model: WxShareInfoModel): WXMediaMessage {
// 注意:musicUrl 和 musicLowBandUrl 不能同时为空
// 初始化一个WXMusicObject,填写url
val music = WXMusicObject()
music.musicUrl = model.musicUrl
music.songAlbumUrl = model.songAlbumUrl
music.musicDataUrl = model.musicDataUrl
// 用 WXMusicObject 对象初始化一个 WXMediaMessage 对象
val msg = WXMediaMessage(music)
msg.title = model.title
msg.description = model.description
val thumbBmp: Bitmap = base642Bitmap(model.thumbBase64, R.mipmap.ic_launcher)
// 设置音乐缩略图
msg.thumbData = BitmapUtil.bitmap2Bytes(thumbBmp)
return msg
}
private fun videoData(model: WxShareInfoModel): WXMediaMessage {
//初始化一个WXVideoObject,填写url
val videoObj = WXVideoObject()
videoObj.videoUrl = model.videoUrl
//用 WXVideoObject 对象初始化一个 WXMediaMessage 对象
val msg = WXMediaMessage(videoObj)
msg.title = model.title
msg.description = model.description
val thumbBmp: Bitmap = base642Bitmap(model.thumbBase64, R.mipmap.ic_launcher)
// 设置视频缩略图
msg.thumbData = BitmapUtil.bitmap2Bytes(thumbBmp)
return msg
}
private fun webpageData(model: WxShareInfoModel): WXMediaMessage {
// 初始化一个WXWebpageObject,填写url
val webpage = WXWebpageObject()
webpage.webpageUrl = model.webpageUrl
// 用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
val msg = WXMediaMessage(webpage)
msg.title = model.title
msg.description = model.description
val thumbBmp: Bitmap = base642Bitmap(model.thumbBase64, R.mipmap.ic_launcher)
// 设置网页缩略图
msg.thumbData = BitmapUtil.bitmap2Bytes(thumbBmp)
return msg
}
private fun miniProgramData(model: WxShareInfoModel): WXMediaMessage {
// 初始化一个WXWebpageObject,填写url
val miniProgramObj = WXMiniProgramObject()
miniProgramObj.webpageUrl = model.webpageUrl // 兼容低版本的网页链接
miniProgramObj.miniprogramType = model.miniProgramType // 正式版:0,测试版:1,体验版:2
miniProgramObj.userName = model.userName // 小程序原始id"gh_d43f693ca31f"
miniProgramObj.withShareTicket = model.withShareTicket // 是否使用带 shareTicket 的转发
miniProgramObj.path = model.path//小程序页面路径;对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"#### pages/summary/summary
// 用 WXMiniProgramObject 对象初始化一个 WXMiniProgramObject 对象
val msg = WXMediaMessage(miniProgramObj)
msg.title = model.title
msg.description = model.description
val thumbBmp: Bitmap = base642Bitmap(model.thumbBase64, R.mipmap.ic_launcher)
// 设置网页缩略图
msg.thumbData = BitmapUtil.bitmap2Bytes(thumbBmp)
return msg
}
fun sendMessage2Wx(model: WxShareInfoModel, wechatResult: ToWechatResult?) {
val message: WXMediaMessage =when (model.shareWxMsgType) {
1 -> {
textData(model)
}
2 -> {
imageData(model)
}
3 -> {
musicData(model)
}
4 -> {
videoData(model)
}
5 -> {
webpageData(model)
}
6 -> {
miniProgramData(model)
}
else ->null
} ?:return
val req = reqData(model.scene, message)
val api = apiData()
wechatResult?.let {
it.wxExecuteResult(api.sendReq(req))
}
}
/**
* 移动应用(APP)拉起小程序
*/
fun jump2MiniProgram(model: WxShareInfoModel, wechatResult: ToWechatResult?) {
// 构造一个Req
val req = WXLaunchMiniProgram.Req()
req.userName = model.userName // 填小程序原始id
req.path = model.path // 拉起小程序页面的可带参路径,不填默认拉起小程序首页,对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"。
req.miniprogramType = model.miniProgramType // 可选打开 开发版,体验版和正式版
val api = apiData()
api.sendReq(req)
wechatResult?.let {
it.wxExecuteResult(api.sendReq(req))
}
}
private fun apiData(): IWXAPI {
//调用api接口,发送数据到微信
var api: IWXAPI?
if (MyApplicationLike.getApi() !=null) {
api = MyApplicationLike.getApi()
}else {
api = WXAPIFactory.createWXAPI(BaseApplicationLike.mApplication, BuildConfig.weixin_app_id, true)
api.registerApp(BuildConfig.weixin_app_id)
}
return api
}
private fun reqData(targetScene: Int, message: WXMediaMessage): SendMessageToWX.Req {
// 构造一个Req
val req = SendMessageToWX.Req()
req.transaction = buildTransaction(message.type)
req.message = message
req.scene = targetScene
return req
}
private fun buildTransaction(type: Int): String? {
return "$type${System.currentTimeMillis()}"
}
fun checkWeixin(): Boolean {
return if (MyApplicationLike.getApi() !=null) {
MyApplicationLike.getApi().isWXAppInstalled
}else {
false
}
}
@Keep
data class WxShareInfoModel(var shareWxMsgType: Int =0,//分享文字、图片、音乐、视频、网页
var scene: Int =0,//可分享至好友会话或者朋友圈
var shareText: String? ="",//分享文本内容
var title: String? ="",//分享内容标题
var description: String? ="",//分享内容描述
var thumbBase64: String? ="",//图片、音乐、视频、网页缩略图base64数据
var imageBase64: String? ="",//图片base64数据
var musicUrl: String? ="",//音乐网站地址
var musicDataUrl: String? ="",//音乐数据地址
var songAlbumUrl: String? ="",//专辑图片
var videoUrl: String? ="",//视频地址
var webpageUrl: String? ="",//分享网页地址
var userName: String? ="",//小程序原始id
var miniProgramType: Int =0,//分享小程序的版本
var hdImageBase64: String? ="",//小程序新版本的预览图,大小不能超过128k
var withShareTicket: Boolean =false,//是否使用带 shareTicket 的转发
var path: String? =""//小程序跳转页面
)
interface ToWechatResult {
/**
* 调用微信成功/失败
*/
fun wxExecuteResult(boolean: Boolean) {}
}
}
网友评论