This commit is contained in:
v7lin 2021-07-31 20:09:24 +08:00
parent d15a204c62
commit d12244f1d3
9 changed files with 144 additions and 148 deletions

View File

@ -177,9 +177,9 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
@Override @Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (METHOD_REGISTERAPP.equals(call.method)) { if (METHOD_REGISTERAPP.equals(call.method)) {
String appKey = call.argument(ARGUMENT_KEY_APPKEY); final String appKey = call.argument(ARGUMENT_KEY_APPKEY);
String scope = call.argument(ARGUMENT_KEY_SCOPE); final String scope = call.argument(ARGUMENT_KEY_SCOPE);
String redirectUrl = call.argument(ARGUMENT_KEY_REDIRECTURL); final String redirectUrl = call.argument(ARGUMENT_KEY_REDIRECTURL);
iwbapi = WBAPIFactory.createWBAPI(activityPluginBinding.getActivity()); iwbapi = WBAPIFactory.createWBAPI(activityPluginBinding.getActivity());
iwbapi.registerApp(applicationContext, new AuthInfo(applicationContext, appKey, redirectUrl, scope)); iwbapi.registerApp(applicationContext, new AuthInfo(applicationContext, appKey, redirectUrl, scope));
result.success(null); result.success(null);
@ -202,13 +202,13 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
iwbapi.authorize(new WbAuthListener() { iwbapi.authorize(new WbAuthListener() {
@Override @Override
public void onComplete(Oauth2AccessToken token) { public void onComplete(Oauth2AccessToken token) {
Map<String, Object> map = new HashMap<>(); final Map<String, Object> map = new HashMap<>();
if (token.isSessionValid()) { if (token.isSessionValid()) {
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.SUCCESS); map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.SUCCESS);
map.put(ARGUMENT_KEY_RESULT_USERID, token.getUid()); map.put(ARGUMENT_KEY_RESULT_USERID, token.getUid());
map.put(ARGUMENT_KEY_RESULT_ACCESSTOKEN, token.getAccessToken()); map.put(ARGUMENT_KEY_RESULT_ACCESSTOKEN, token.getAccessToken());
map.put(ARGUMENT_KEY_RESULT_REFRESHTOKEN, token.getRefreshToken()); map.put(ARGUMENT_KEY_RESULT_REFRESHTOKEN, token.getRefreshToken());
long expiresIn = (long) Math.ceil((token.getExpiresTime() - System.currentTimeMillis()) / 1000.0); final long expiresIn = (long) Math.ceil((token.getExpiresTime() - System.currentTimeMillis()) / 1000.0);
map.put(ARGUMENT_KEY_RESULT_EXPIRESIN, expiresIn);// map.put(ARGUMENT_KEY_RESULT_EXPIRESIN, expiresIn);//
} else { } else {
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN); map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN);
@ -220,7 +220,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
@Override @Override
public void onError(UiError uiError) { public void onError(UiError uiError) {
Map<String, Object> map = new HashMap<>(); final Map<String, Object> map = new HashMap<>();
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN); map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN);
if (channel != null) { if (channel != null) {
channel.invokeMethod(METHOD_ONAUTHRESP, map); channel.invokeMethod(METHOD_ONAUTHRESP, map);
@ -229,7 +229,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
@Override @Override
public void onCancel() { public void onCancel() {
Map<String, Object> map = new HashMap<>(); final Map<String, Object> map = new HashMap<>();
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.USERCANCEL); map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.USERCANCEL);
if (channel != null) { if (channel != null) {
channel.invokeMethod(METHOD_ONAUTHRESP, map); channel.invokeMethod(METHOD_ONAUTHRESP, map);
@ -241,9 +241,9 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
} }
private void handleShareTextCall(@NonNull MethodCall call, @NonNull Result result) { private void handleShareTextCall(@NonNull MethodCall call, @NonNull Result result) {
WeiboMultiMessage message = new WeiboMultiMessage(); final WeiboMultiMessage message = new WeiboMultiMessage();
TextObject object = new TextObject(); final TextObject object = new TextObject();
object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024 object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024
message.textObject = object; message.textObject = object;
@ -255,17 +255,17 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
} }
private void handleShareMediaCall(@NonNull MethodCall call, @NonNull Result result) { private void handleShareMediaCall(@NonNull MethodCall call, @NonNull Result result) {
WeiboMultiMessage message = new WeiboMultiMessage(); final WeiboMultiMessage message = new WeiboMultiMessage();
if (METHOD_SHAREIMAGE.equals(call.method)) { if (METHOD_SHAREIMAGE.equals(call.method)) {
if (call.hasArgument(ARGUMENT_KEY_TEXT)) { if (call.hasArgument(ARGUMENT_KEY_TEXT)) {
TextObject object = new TextObject(); final TextObject object = new TextObject();
object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024 object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024
message.textObject = object; message.textObject = object;
} }
ImageObject object = new ImageObject(); final ImageObject object = new ImageObject();
if (call.hasArgument(ARGUMENT_KEY_IMAGEDATA)) { if (call.hasArgument(ARGUMENT_KEY_IMAGEDATA)) {
object.imageData = call.argument(ARGUMENT_KEY_IMAGEDATA);// 2 * 1024 * 1024 object.imageData = call.argument(ARGUMENT_KEY_IMAGEDATA);// 2 * 1024 * 1024
} else if (call.hasArgument(ARGUMENT_KEY_IMAGEURI)) { } else if (call.hasArgument(ARGUMENT_KEY_IMAGEURI)) {
@ -275,7 +275,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
message.mediaObject = object; message.mediaObject = object;
} else if (METHOD_SHAREWEBPAGE.equals(call.method)) { } else if (METHOD_SHAREWEBPAGE.equals(call.method)) {
WebpageObject object = new WebpageObject(); final WebpageObject object = new WebpageObject();
object.identify = UUID.randomUUID().toString(); object.identify = UUID.randomUUID().toString();
object.title = call.argument(ARGUMENT_KEY_TITLE);// 512 object.title = call.argument(ARGUMENT_KEY_TITLE);// 512
object.description = call.argument(ARGUMENT_KEY_DESCRIPTION);// 1024 object.description = call.argument(ARGUMENT_KEY_DESCRIPTION);// 1024

95
lib/src/model/resp.dart Normal file
View File

@ -0,0 +1,95 @@
import 'dart:convert';
import 'package:json_annotation/json_annotation.dart';
part 'resp.g.dart';
abstract class BaseResp {
const BaseResp({
required this.errorCode,
this.errorMessage,
});
///
static const int SUCCESS = 0;
///
static const int USERCANCEL = -1;
///
static const int SENT_FAIL = -2;
///
static const int AUTH_DENY = -3;
///
static const int USERCANCEL_INSTALL = -4;
///
static const int PAY_FAIL = -5;
/// response UserInfo
static const int SHARE_IN_SDK_FAILED = -8;
///
static const int UNSUPPORT = -99;
///
static const int UNKNOWN = -100;
@JsonKey(
defaultValue: SUCCESS,
)
final int errorCode;
final String? errorMessage;
bool get isSuccessful => errorCode == SUCCESS;
bool get isCancelled => errorCode == USERCANCEL;
Map<String, dynamic> toJson();
@override
String toString() => const JsonEncoder.withIndent(' ').convert(toJson());
}
@JsonSerializable(
explicitToJson: true,
)
class AuthResp extends BaseResp {
AuthResp({
required int errorCode,
String? errorMessage,
this.userId,
this.accessToken,
this.refreshToken,
this.expiresIn,
}) : super(errorCode: errorCode, errorMessage: errorMessage);
factory AuthResp.fromJson(Map<String, dynamic> json) =>
_$AuthRespFromJson(json);
final String? userId;
final String? accessToken;
final String? refreshToken;
final int? expiresIn;
@override
Map<String, dynamic> toJson() => _$AuthRespToJson(this);
}
@JsonSerializable(
explicitToJson: true,
)
class ShareMsgResp extends BaseResp {
const ShareMsgResp({
required int errorCode,
String? errorMessage,
}) : super(errorCode: errorCode, errorMessage: errorMessage);
factory ShareMsgResp.fromJson(Map<String, dynamic> json) =>
_$ShareMsgRespFromJson(json);
@override
Map<String, dynamic> toJson() => _$ShareMsgRespToJson(this);
}

View File

@ -1,13 +1,13 @@
// GENERATED CODE - DO NOT MODIFY BY HAND // GENERATED CODE - DO NOT MODIFY BY HAND
part of 'weibo_auth_resp.dart'; part of 'resp.dart';
// ************************************************************************** // **************************************************************************
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
WeiboAuthResp _$WeiboAuthRespFromJson(Map<String, dynamic> json) { AuthResp _$AuthRespFromJson(Map<String, dynamic> json) {
return WeiboAuthResp( return AuthResp(
errorCode: json['errorCode'] as int? ?? 0, errorCode: json['errorCode'] as int? ?? 0,
errorMessage: json['errorMessage'] as String?, errorMessage: json['errorMessage'] as String?,
userId: json['userId'] as String?, userId: json['userId'] as String?,
@ -17,8 +17,7 @@ WeiboAuthResp _$WeiboAuthRespFromJson(Map<String, dynamic> json) {
); );
} }
Map<String, dynamic> _$WeiboAuthRespToJson(WeiboAuthResp instance) => Map<String, dynamic> _$AuthRespToJson(AuthResp instance) => <String, dynamic>{
<String, dynamic>{
'errorCode': instance.errorCode, 'errorCode': instance.errorCode,
'errorMessage': instance.errorMessage, 'errorMessage': instance.errorMessage,
'userId': instance.userId, 'userId': instance.userId,
@ -26,3 +25,16 @@ Map<String, dynamic> _$WeiboAuthRespToJson(WeiboAuthResp instance) =>
'refreshToken': instance.refreshToken, 'refreshToken': instance.refreshToken,
'expiresIn': instance.expiresIn, 'expiresIn': instance.expiresIn,
}; };
ShareMsgResp _$ShareMsgRespFromJson(Map<String, dynamic> json) {
return ShareMsgResp(
errorCode: json['errorCode'] as int? ?? 0,
errorMessage: json['errorMessage'] as String?,
);
}
Map<String, dynamic> _$ShareMsgRespToJson(ShareMsgResp instance) =>
<String, dynamic>{
'errorCode': instance.errorCode,
'errorMessage': instance.errorMessage,
};

View File

@ -1,29 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:weibo_kit/src/model/weibo_sdk_resp.dart';
part 'weibo_auth_resp.g.dart';
@JsonSerializable(
explicitToJson: true,
)
class WeiboAuthResp extends WeiboSdkResp {
WeiboAuthResp({
required int errorCode,
String? errorMessage,
this.userId,
this.accessToken,
this.refreshToken,
this.expiresIn,
}) : super(errorCode: errorCode, errorMessage: errorMessage);
factory WeiboAuthResp.fromJson(Map<String, dynamic> json) =>
_$WeiboAuthRespFromJson(json);
final String? userId;
final String? accessToken;
final String? refreshToken;
final int? expiresIn;
@override
Map<String, dynamic> toJson() => _$WeiboAuthRespToJson(this);
}

View File

@ -1,55 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
part 'weibo_sdk_resp.g.dart';
@JsonSerializable(
explicitToJson: true,
)
class WeiboSdkResp {
const WeiboSdkResp({
required this.errorCode,
this.errorMessage,
});
factory WeiboSdkResp.fromJson(Map<String, dynamic> json) =>
_$WeiboSdkRespFromJson(json);
///
static const int SUCCESS = 0;
///
static const int USERCANCEL = -1;
///
static const int SENT_FAIL = -2;
///
static const int AUTH_DENY = -3;
///
static const int USERCANCEL_INSTALL = -4;
///
static const int PAY_FAIL = -5;
/// response UserInfo
static const int SHARE_IN_SDK_FAILED = -8;
///
static const int UNSUPPORT = -99;
///
static const int UNKNOWN = -100;
@JsonKey(
defaultValue: SUCCESS,
)
final int errorCode;
final String? errorMessage;
bool get isSuccessful => errorCode == SUCCESS;
bool get isCancelled => errorCode == USERCANCEL;
Map<String, dynamic> toJson() => _$WeiboSdkRespToJson(this);
}

View File

@ -1,20 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'weibo_sdk_resp.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
WeiboSdkResp _$WeiboSdkRespFromJson(Map<String, dynamic> json) {
return WeiboSdkResp(
errorCode: json['errorCode'] as int? ?? 0,
errorMessage: json['errorMessage'] as String?,
);
}
Map<String, dynamic> _$WeiboSdkRespToJson(WeiboSdkResp instance) =>
<String, dynamic>{
'errorCode': instance.errorCode,
'errorMessage': instance.errorMessage,
};

View File

@ -3,8 +3,7 @@ import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:weibo_kit/src/model/weibo_auth_resp.dart'; import 'package:weibo_kit/src/model/resp.dart';
import 'package:weibo_kit/src/model/weibo_sdk_resp.dart';
class Weibo { class Weibo {
/// ///
@ -45,11 +44,8 @@ class Weibo {
const MethodChannel('v7lin.github.io/weibo_kit') const MethodChannel('v7lin.github.io/weibo_kit')
..setMethodCallHandler(_handleMethod); ..setMethodCallHandler(_handleMethod);
final StreamController<WeiboAuthResp> _authRespStreamController = final StreamController<BaseResp> _respStreamController =
StreamController<WeiboAuthResp>.broadcast(); StreamController<BaseResp>.broadcast();
final StreamController<WeiboSdkResp> _shareMsgRespStreamController =
StreamController<WeiboSdkResp>.broadcast();
Future<void> registerApp({ Future<void> registerApp({
required String appKey, required String appKey,
@ -73,24 +69,19 @@ class Weibo {
Future<dynamic> _handleMethod(MethodCall call) async { Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) { switch (call.method) {
case _METHOD_ONAUTHRESP: case _METHOD_ONAUTHRESP:
_authRespStreamController.add(WeiboAuthResp.fromJson( _respStreamController.add(AuthResp.fromJson(
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>())); (call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break; break;
case _METHOD_ONSHAREMSGRESP: case _METHOD_ONSHAREMSGRESP:
_shareMsgRespStreamController.add(WeiboSdkResp.fromJson( _respStreamController.add(ShareMsgResp.fromJson(
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>())); (call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break; break;
} }
} }
/// ///
Stream<WeiboAuthResp> authResp() { Stream<BaseResp> respStream() {
return _authRespStreamController.stream; return _respStreamController.stream;
}
///
Stream<WeiboSdkResp> shareMsgResp() {
return _shareMsgRespStreamController.stream;
} }
Future<bool> isInstalled() async { Future<bool> isInstalled() async {

View File

@ -1,6 +1,5 @@
library weibo_kit; library weibo_kit;
export 'src/model/weibo_auth_resp.dart'; export 'src/model/resp.dart';
export 'src/model/weibo_sdk_resp.dart';
export 'src/weibo.dart'; export 'src/weibo.dart';
export 'src/weibo_constant.dart'; export 'src/weibo_constant.dart';

View File

@ -54,21 +54,24 @@ void main() {
}); });
test('auth', () async { test('auth', () async {
final StreamSubscription<WeiboAuthResp> sub = final StreamSubscription<BaseResp> subs =
Weibo.instance.authResp().listen((WeiboAuthResp resp) { Weibo.instance.respStream().listen((BaseResp resp) {
expect(resp.errorCode, WeiboSdkResp.USERCANCEL); expect(resp.runtimeType, AuthResp);
expect(resp.errorCode, BaseResp.USERCANCEL);
}); });
await Weibo.instance.auth( await Weibo.instance.auth(
appKey: 'your weibo app key', appKey: 'your weibo app key',
scope: <String>[WeiboScope.ALL], scope: <String>[WeiboScope.ALL],
); );
await sub.cancel(); await Future<void>.delayed(const Duration(seconds: 1));
await subs.cancel();
}); });
test('share', () async { test('share', () async {
final StreamSubscription<WeiboSdkResp> sub = final StreamSubscription<BaseResp> sub =
Weibo.instance.shareMsgResp().listen((WeiboSdkResp resp) { Weibo.instance.respStream().listen((BaseResp resp) {
expect(resp.errorCode, WeiboSdkResp.USERCANCEL); expect(resp.runtimeType, ShareMsgResp);
expect(resp.errorCode, BaseResp.USERCANCEL);
}); });
await Weibo.instance.shareText( await Weibo.instance.shareText(
text: 'share text', text: 'share text',