From b8561face5214df533e036b3868c05b15a80e085 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Tue, 12 Jan 2021 17:53:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=88=86=E4=BA=AB=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/constants/api.dart | 9 + lib/third_party/wechat/wechat_utils.dart | 278 +++++++++++++---------- lib/utils/share_tool.dart | 21 +- lib/widgets/share_page/post_all.dart | 4 +- 4 files changed, 182 insertions(+), 130 deletions(-) diff --git a/lib/constants/api.dart b/lib/constants/api.dart index a63a927..9d28e97 100644 --- a/lib/constants/api.dart +++ b/lib/constants/api.dart @@ -308,6 +308,15 @@ class WebApi { static const String testGoodsDetail = "http://testh5.reecook.cn/#/goods/detail/"; + + ///小程序二维码测试服 + static const String testMiniQrcodeGoodsDetail = + 'http://testh5.reecook.cn/goods/detail'; + + ///小程序二维码 + static const String miniQrcodeGoodsDetail = + 'https://h5.reecook.cn/goods/detail'; + static const String testInviteRegist = "http://testh5.reecook.cn/#/user/appRegister/"; static const String testDiamondsInviteRegist = diff --git a/lib/third_party/wechat/wechat_utils.dart b/lib/third_party/wechat/wechat_utils.dart index f2698e6..e131828 100644 --- a/lib/third_party/wechat/wechat_utils.dart +++ b/lib/third_party/wechat/wechat_utils.dart @@ -10,6 +10,7 @@ import 'dart:core'; import 'package:flutter/material.dart'; import 'package:fluwx/fluwx.dart' as fluwx; +import 'package:recook/constants/api.dart'; import 'package:recook/constants/config.dart'; import 'package:recook/constants/header.dart'; import 'package:recook/manager/user_manager.dart'; @@ -31,17 +32,18 @@ class WXLoginResult { final String state; final String androidTransaction; - WXLoginResult({this.errStr, - this.type, - this.errCode, - this.androidOpenId, - this.iOSDescription, - this.country, - this.lang, - this.code, - this.androidUrl, - this.state, - this.androidTransaction}); + WXLoginResult( + {this.errStr, + this.type, + this.errCode, + this.androidOpenId, + this.iOSDescription, + this.country, + this.lang, + this.code, + this.androidUrl, + this.state, + this.androidTransaction}); } class WXPayResult { @@ -55,99 +57,131 @@ class WXPayResult { final String androidTransaction; WXPayResult( - {this.errStr, this.type, this.errCode, this.androidOpenId, this.iOSDescription, this.androidPrepayId, this.extData, this.androidTransaction}); - + {this.errStr, + this.type, + this.errCode, + this.androidOpenId, + this.iOSDescription, + this.androidPrepayId, + this.extData, + this.androidTransaction}); } class WeChatUtils { - static bool isInstall = false; static initial() async { - await fluwx.registerWxApi( - appId: AppConfig.WX_APP_ID, - doOnAndroid: true, - doOnIOS: true, - universalLink: "https://reecook.cn/" - ).then((onValue){ + await fluwx + .registerWxApi( + appId: AppConfig.WX_APP_ID, + doOnAndroid: true, + doOnIOS: true, + universalLink: "https://reecook.cn/") + .then((onValue) { DPrint.printf("微信registr $onValue"); }); var result = await fluwx.isWeChatInstalled; isInstall = result; DPrint.printf("wechat install $result"); - } static shareUrl({ - String url, - String title, + String url, + String title, String assetsThumbnail, - String netWorkThumbnail, + String netWorkThumbnail, String description, - fluwx.WeChatScene scene = fluwx.WeChatScene.SESSION,}){ + fluwx.WeChatScene scene = fluwx.WeChatScene.SESSION, + }) { //2.0 - var model = fluwx.WeChatShareWebPageModel( - url, - thumbnail: - !TextUtils.isEmpty(netWorkThumbnail)?fluwx.WeChatImage.network(netWorkThumbnail): - !TextUtils.isEmpty(assetsThumbnail)?fluwx.WeChatImage.asset(assetsThumbnail): null, - title: title, - scene: scene, - description: description, - ); - fluwx.shareToWeChat(model); + var model = fluwx.WeChatShareWebPageModel( + url, + thumbnail: !TextUtils.isEmpty(netWorkThumbnail) + ? fluwx.WeChatImage.network(netWorkThumbnail) + : !TextUtils.isEmpty(assetsThumbnail) + ? fluwx.WeChatImage.asset(assetsThumbnail) + : null, + title: title, + scene: scene, + description: description, + ); + fluwx.shareToWeChat(model); } - static shareGoodsForMiniProgram ({ + static miniProgramShare({ + String userName, + String id, + String netWorkThumbnail, + String des, + }) { + String qrCode = + "${AppConfig.debug ? WebApi.testGoodsDetail : WebApi.goodsDetail}$id/${UserManager.instance.user.info.invitationNo}"; + + var model = fluwx.WeChatShareMiniProgramModel( + userName: 'gh_530bd0866836', + webPageUrl: 'https://h5.reecook.cn/', + path: 'pages/goodsDetail/goodsDetail?type=share&id=$id', + thumbnail: fluwx.WeChatImage.network(netWorkThumbnail), + title: des, + ); + fluwx.shareToWeChat(model); + } + + static shareGoodsForMiniProgram({ int goodsId, String title, - String thumbnail,}){ - if (UserAuthorityUtil().showNeedLoginToast()) {return;} - - var invitationNo = UserManager.instance.user.info.invitationNo??''; - fluwx.WXMiniProgramType _wxType = fluwx.WXMiniProgramType.PREVIEW; - if (!AppConfig.debug) { - _wxType = fluwx.WXMiniProgramType.RELEASE; - } - var userId = UserManager.instance.user.info.id; - var model = fluwx.WeChatShareMiniProgramModel( - path: "pages/goods/detail?goods_id=$goodsId&code=$invitationNo&userId=$userId", - webPageUrl: "reecook.cn", - miniProgramType: _wxType, - userName: AppConfig.WX_APP_MINIPRO_USERNAME, - // scene: fluWX.WeChatScene.SESSION, - title: title, - description: "", - thumbnail: fluwx.WeChatImage.network(thumbnail), - // thumbnail: thumbnail, - ); - fluwx.shareToWeChat(model); - // fluWX.share(model); + String thumbnail, + }) { + if (UserAuthorityUtil().showNeedLoginToast()) { + return; + } + + var invitationNo = UserManager.instance.user.info.invitationNo ?? ''; + fluwx.WXMiniProgramType _wxType = fluwx.WXMiniProgramType.PREVIEW; + if (!AppConfig.debug) { + _wxType = fluwx.WXMiniProgramType.RELEASE; + } + var userId = UserManager.instance.user.info.id; + var model = fluwx.WeChatShareMiniProgramModel( + path: + "pages/goods/detail?goods_id=$goodsId&code=$invitationNo&userId=$userId", + webPageUrl: "reecook.cn", + miniProgramType: _wxType, + userName: AppConfig.WX_APP_MINIPRO_USERNAME, + // scene: fluWX.WeChatScene.SESSION, + title: title, + description: "", + thumbnail: fluwx.WeChatImage.network(thumbnail), + // thumbnail: thumbnail, + ); + fluwx.shareToWeChat(model); + // fluWX.share(model); } - - static shareMiniProgram ({ + + static shareMiniProgram({ String path, String webPageUrl, String title, String description, - String thumbnail,}){ - fluwx.WXMiniProgramType _wxType = fluwx.WXMiniProgramType.PREVIEW; - if (!AppConfig.debug) { - _wxType = fluwx.WXMiniProgramType.RELEASE; - } - var model = fluwx.WeChatShareMiniProgramModel( - path: path, - webPageUrl: webPageUrl, - miniProgramType: _wxType, - userName: AppConfig.WX_APP_MINIPRO_USERNAME, - // scene: fluWX.WeChatScene.SESSION, - title: title, - description: description, - thumbnail: fluwx.WeChatImage.network(thumbnail), - // thumbnail: thumbnail, - ); - // fluWX.share(model); - fluwx.shareToWeChat(model); + String thumbnail, + }) { + fluwx.WXMiniProgramType _wxType = fluwx.WXMiniProgramType.PREVIEW; + if (!AppConfig.debug) { + _wxType = fluwx.WXMiniProgramType.RELEASE; + } + var model = fluwx.WeChatShareMiniProgramModel( + path: path, + webPageUrl: webPageUrl, + miniProgramType: _wxType, + userName: AppConfig.WX_APP_MINIPRO_USERNAME, + // scene: fluWX.WeChatScene.SESSION, + title: title, + description: description, + thumbnail: fluwx.WeChatImage.network(thumbnail), + // thumbnail: thumbnail, + ); + // fluWX.share(model); + fluwx.shareToWeChat(model); } static wxLogin(Function(WXLoginResult result) listener) { @@ -155,7 +189,7 @@ class WeChatUtils { fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((data) { if (data is fluwx.WeChatAuthResponse) { DPrint.printf( - "微信登录 errCode------------- ${data.errCode} - ${data.errStr}"); + "微信登录 errCode------------- ${data.errCode} - ${data.errStr}"); WXLoginResult result = WXLoginResult( type: data.type, errCode: data.errCode, @@ -172,55 +206,57 @@ class WeChatUtils { listener(result); } }); - fluwx.sendWeChatAuth( - scope: "snsapi_userinfo", state: "wechat_sdk_demo_reecook") - .then((data) {}); + fluwx + .sendWeChatAuth( + scope: "snsapi_userinfo", state: "wechat_sdk_demo_reecook") + .then((data) {}); } - static pay({@required String appId, - @required String partnerId, - @required String prepayId, - @required String packageValue, - @required String nonceStr, - @required int timeStamp, - @required String sign, - String signType, - String extData, - Function(WXPayResult result) listener}) { - print("appId-------- $appId"); - print("-partnerId------- $partnerId"); - print("prepayId-------- $prepayId"); - print("packageValue-------- $packageValue"); - print("nonceStr-------- $nonceStr"); - print("timeStamp-------- $timeStamp"); - print("signType-------- $signType"); - print("extData-------- $extData"); - print("sign-------- $sign"); - - fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((data) { - if (data is fluwx.WeChatPaymentResponse) { - if (listener == null) return; + static pay( + {@required String appId, + @required String partnerId, + @required String prepayId, + @required String packageValue, + @required String nonceStr, + @required int timeStamp, + @required String sign, + String signType, + String extData, + Function(WXPayResult result) listener}) { + print("appId-------- $appId"); + print("-partnerId------- $partnerId"); + print("prepayId-------- $prepayId"); + print("packageValue-------- $packageValue"); + print("nonceStr-------- $nonceStr"); + print("timeStamp-------- $timeStamp"); + print("signType-------- $signType"); + print("extData-------- $extData"); + print("sign-------- $sign"); + + fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((data) { + if (data is fluwx.WeChatPaymentResponse) { + if (listener == null) return; listener(WXPayResult( errCode: data.errCode, errStr: data.errStr, type: data.type, )); - } - }); - fluwx.isWeChatInstalled.then((install){ - if (!install) { - Toast.showInfo("未安装微信"); - return; - } + } + }); + fluwx.isWeChatInstalled.then((install) { + if (!install) { + Toast.showInfo("未安装微信"); + return; + } fluwx.payWithWeChat( - appId: appId, - partnerId: partnerId, - prepayId: prepayId, - packageValue: packageValue, - nonceStr: nonceStr, - timeStamp: timeStamp, - signType: "MD5", - sign: sign); - }); + appId: appId, + partnerId: partnerId, + prepayId: prepayId, + packageValue: packageValue, + nonceStr: nonceStr, + timeStamp: timeStamp, + signType: "MD5", + sign: sign); + }); } } diff --git a/lib/utils/share_tool.dart b/lib/utils/share_tool.dart index 84754db..0a7334b 100644 --- a/lib/utils/share_tool.dart +++ b/lib/utils/share_tool.dart @@ -135,19 +135,24 @@ class ShareTool { PlatformItem wechatItem = PlatformItem( "微信", Image.asset( - ShareToolIcon.wechat, + ShareToolIcon.wechatmini, width: 36, height: 36, ), itemClick: () { Navigator.maybePop(context); WeChatScene scene = WeChatScene.SESSION; - WeChatUtils.shareUrl( - url: goodsUrl, - netWorkThumbnail: Api.getImgUrl(miniPicurl), - // netWorkThumbnail: AppImageName.web_app_icon, - title: "仅$goodsPrice元 | $goodsName", - description: goodsDescription, - scene: scene); + // WeChatUtils.shareUrl( + // url: goodsUrl, + // netWorkThumbnail: Api.getImgUrl(miniPicurl), + // // netWorkThumbnail: AppImageName.web_app_icon, + // title: "仅$goodsPrice元 | $goodsName", + // description: goodsDescription, + // scene: scene); + WeChatUtils.miniProgramShare( + id: goodsId, + netWorkThumbnail: Api.getImgUrl(miniPicurl), + des: miniTitle, + ); }); PlatformItem weiboItem = PlatformItem( "微博", diff --git a/lib/widgets/share_page/post_all.dart b/lib/widgets/share_page/post_all.dart index 6afef03..37f4812 100644 --- a/lib/widgets/share_page/post_all.dart +++ b/lib/widgets/share_page/post_all.dart @@ -339,7 +339,9 @@ class PostBottomWidget extends StatelessWidget { Widget build(BuildContext context) { String qrCode = "${AppConfig.debug ? WebApi.testGoodsDetail : WebApi.goodsDetail}${goodsDetailModel.data.id}/${UserManager.instance.user.info.invitationNo}"; - ; + // String qrCode = + // "${AppConfig.debug ? WebApi.testMiniQrcodeGoodsDetail : WebApi.miniQrcodeGoodsDetail}?type=share&id=${goodsDetailModel.data.id}"; + String info = goodsDetailModel.data.goodsName; String crossedPrice = goodsDetailModel.data.price.max.originalPrice.toStringAsFixed(2);