整理
This commit is contained in:
parent
d15a204c62
commit
d12244f1d3
@ -177,9 +177,9 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
|
||||
@Override
|
||||
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
|
||||
if (METHOD_REGISTERAPP.equals(call.method)) {
|
||||
String appKey = call.argument(ARGUMENT_KEY_APPKEY);
|
||||
String scope = call.argument(ARGUMENT_KEY_SCOPE);
|
||||
String redirectUrl = call.argument(ARGUMENT_KEY_REDIRECTURL);
|
||||
final String appKey = call.argument(ARGUMENT_KEY_APPKEY);
|
||||
final String scope = call.argument(ARGUMENT_KEY_SCOPE);
|
||||
final String redirectUrl = call.argument(ARGUMENT_KEY_REDIRECTURL);
|
||||
iwbapi = WBAPIFactory.createWBAPI(activityPluginBinding.getActivity());
|
||||
iwbapi.registerApp(applicationContext, new AuthInfo(applicationContext, appKey, redirectUrl, scope));
|
||||
result.success(null);
|
||||
@ -202,13 +202,13 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
|
||||
iwbapi.authorize(new WbAuthListener() {
|
||||
@Override
|
||||
public void onComplete(Oauth2AccessToken token) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
if (token.isSessionValid()) {
|
||||
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.SUCCESS);
|
||||
map.put(ARGUMENT_KEY_RESULT_USERID, token.getUid());
|
||||
map.put(ARGUMENT_KEY_RESULT_ACCESSTOKEN, token.getAccessToken());
|
||||
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);// 向上取整
|
||||
} else {
|
||||
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN);
|
||||
@ -220,7 +220,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
|
||||
|
||||
@Override
|
||||
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);
|
||||
if (channel != null) {
|
||||
channel.invokeMethod(METHOD_ONAUTHRESP, map);
|
||||
@ -229,7 +229,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.USERCANCEL);
|
||||
if (channel != null) {
|
||||
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) {
|
||||
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
|
||||
|
||||
message.textObject = object;
|
||||
@ -255,17 +255,17 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
|
||||
}
|
||||
|
||||
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 (call.hasArgument(ARGUMENT_KEY_TEXT)) {
|
||||
TextObject object = new TextObject();
|
||||
final TextObject object = new TextObject();
|
||||
object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024
|
||||
|
||||
message.textObject = object;
|
||||
}
|
||||
|
||||
ImageObject object = new ImageObject();
|
||||
final ImageObject object = new ImageObject();
|
||||
if (call.hasArgument(ARGUMENT_KEY_IMAGEDATA)) {
|
||||
object.imageData = call.argument(ARGUMENT_KEY_IMAGEDATA);// 2 * 1024 * 1024
|
||||
} else if (call.hasArgument(ARGUMENT_KEY_IMAGEURI)) {
|
||||
@ -275,7 +275,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
|
||||
|
||||
message.mediaObject = object;
|
||||
} else if (METHOD_SHAREWEBPAGE.equals(call.method)) {
|
||||
WebpageObject object = new WebpageObject();
|
||||
final WebpageObject object = new WebpageObject();
|
||||
object.identify = UUID.randomUUID().toString();
|
||||
object.title = call.argument(ARGUMENT_KEY_TITLE);// 512
|
||||
object.description = call.argument(ARGUMENT_KEY_DESCRIPTION);// 1024
|
||||
|
95
lib/src/model/resp.dart
Normal file
95
lib/src/model/resp.dart
Normal 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);
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'weibo_auth_resp.dart';
|
||||
part of 'resp.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WeiboAuthResp _$WeiboAuthRespFromJson(Map<String, dynamic> json) {
|
||||
return WeiboAuthResp(
|
||||
AuthResp _$AuthRespFromJson(Map<String, dynamic> json) {
|
||||
return AuthResp(
|
||||
errorCode: json['errorCode'] as int? ?? 0,
|
||||
errorMessage: json['errorMessage'] as String?,
|
||||
userId: json['userId'] as String?,
|
||||
@ -17,8 +17,7 @@ WeiboAuthResp _$WeiboAuthRespFromJson(Map<String, dynamic> json) {
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$WeiboAuthRespToJson(WeiboAuthResp instance) =>
|
||||
<String, dynamic>{
|
||||
Map<String, dynamic> _$AuthRespToJson(AuthResp instance) => <String, dynamic>{
|
||||
'errorCode': instance.errorCode,
|
||||
'errorMessage': instance.errorMessage,
|
||||
'userId': instance.userId,
|
||||
@ -26,3 +25,16 @@ Map<String, dynamic> _$WeiboAuthRespToJson(WeiboAuthResp instance) =>
|
||||
'refreshToken': instance.refreshToken,
|
||||
'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,
|
||||
};
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
@ -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,
|
||||
};
|
@ -3,8 +3,7 @@ import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:weibo_kit/src/model/weibo_auth_resp.dart';
|
||||
import 'package:weibo_kit/src/model/weibo_sdk_resp.dart';
|
||||
import 'package:weibo_kit/src/model/resp.dart';
|
||||
|
||||
class Weibo {
|
||||
///
|
||||
@ -45,11 +44,8 @@ class Weibo {
|
||||
const MethodChannel('v7lin.github.io/weibo_kit')
|
||||
..setMethodCallHandler(_handleMethod);
|
||||
|
||||
final StreamController<WeiboAuthResp> _authRespStreamController =
|
||||
StreamController<WeiboAuthResp>.broadcast();
|
||||
|
||||
final StreamController<WeiboSdkResp> _shareMsgRespStreamController =
|
||||
StreamController<WeiboSdkResp>.broadcast();
|
||||
final StreamController<BaseResp> _respStreamController =
|
||||
StreamController<BaseResp>.broadcast();
|
||||
|
||||
Future<void> registerApp({
|
||||
required String appKey,
|
||||
@ -73,24 +69,19 @@ class Weibo {
|
||||
Future<dynamic> _handleMethod(MethodCall call) async {
|
||||
switch (call.method) {
|
||||
case _METHOD_ONAUTHRESP:
|
||||
_authRespStreamController.add(WeiboAuthResp.fromJson(
|
||||
_respStreamController.add(AuthResp.fromJson(
|
||||
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
|
||||
break;
|
||||
case _METHOD_ONSHAREMSGRESP:
|
||||
_shareMsgRespStreamController.add(WeiboSdkResp.fromJson(
|
||||
_respStreamController.add(ShareMsgResp.fromJson(
|
||||
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// 登录
|
||||
Stream<WeiboAuthResp> authResp() {
|
||||
return _authRespStreamController.stream;
|
||||
}
|
||||
|
||||
/// 分享
|
||||
Stream<WeiboSdkResp> shareMsgResp() {
|
||||
return _shareMsgRespStreamController.stream;
|
||||
///
|
||||
Stream<BaseResp> respStream() {
|
||||
return _respStreamController.stream;
|
||||
}
|
||||
|
||||
Future<bool> isInstalled() async {
|
||||
|
@ -1,6 +1,5 @@
|
||||
library weibo_kit;
|
||||
|
||||
export 'src/model/weibo_auth_resp.dart';
|
||||
export 'src/model/weibo_sdk_resp.dart';
|
||||
export 'src/model/resp.dart';
|
||||
export 'src/weibo.dart';
|
||||
export 'src/weibo_constant.dart';
|
||||
|
@ -54,21 +54,24 @@ void main() {
|
||||
});
|
||||
|
||||
test('auth', () async {
|
||||
final StreamSubscription<WeiboAuthResp> sub =
|
||||
Weibo.instance.authResp().listen((WeiboAuthResp resp) {
|
||||
expect(resp.errorCode, WeiboSdkResp.USERCANCEL);
|
||||
final StreamSubscription<BaseResp> subs =
|
||||
Weibo.instance.respStream().listen((BaseResp resp) {
|
||||
expect(resp.runtimeType, AuthResp);
|
||||
expect(resp.errorCode, BaseResp.USERCANCEL);
|
||||
});
|
||||
await Weibo.instance.auth(
|
||||
appKey: 'your weibo app key',
|
||||
scope: <String>[WeiboScope.ALL],
|
||||
);
|
||||
await sub.cancel();
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
await subs.cancel();
|
||||
});
|
||||
|
||||
test('share', () async {
|
||||
final StreamSubscription<WeiboSdkResp> sub =
|
||||
Weibo.instance.shareMsgResp().listen((WeiboSdkResp resp) {
|
||||
expect(resp.errorCode, WeiboSdkResp.USERCANCEL);
|
||||
final StreamSubscription<BaseResp> sub =
|
||||
Weibo.instance.respStream().listen((BaseResp resp) {
|
||||
expect(resp.runtimeType, ShareMsgResp);
|
||||
expect(resp.errorCode, BaseResp.USERCANCEL);
|
||||
});
|
||||
await Weibo.instance.shareText(
|
||||
text: 'share text',
|
||||
|
Loading…
Reference in New Issue
Block a user