mirror of
https://github.com/DKJone/DKWechatHelper.git
synced 2026-07-29 06:12:07 +08:00
完整的项目源码
This commit is contained in:
@@ -108,3 +108,186 @@
|
||||
@end
|
||||
|
||||
|
||||
|
||||
//MARK: - WeChatRedEnvelop From:https://github.com/buginux/WeChatRedEnvelop
|
||||
|
||||
@implementation WeChatRedEnvelopParam
|
||||
|
||||
- (NSDictionary *)toParams {
|
||||
return @{
|
||||
@"msgType": self.msgType,
|
||||
@"sendId": self.sendId,
|
||||
@"channelId": self.channelId,
|
||||
@"nickName": self.nickName,
|
||||
@"headImg": self.headImg,
|
||||
@"nativeUrl": self.nativeUrl,
|
||||
@"sessionUserName": self.sessionUserName,
|
||||
@"timingIdentifier": self.timingIdentifier
|
||||
};
|
||||
}
|
||||
@end
|
||||
|
||||
@interface WBRedEnvelopParamQueue ()
|
||||
@property (strong, nonatomic) NSMutableArray *queue;
|
||||
@end
|
||||
|
||||
@implementation WBRedEnvelopParamQueue
|
||||
+ (instancetype)sharedQueue {
|
||||
static WBRedEnvelopParamQueue *queue = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
queue = [[WBRedEnvelopParamQueue alloc] init];
|
||||
});
|
||||
return queue;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_queue = [[NSMutableArray alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)enqueue:(WeChatRedEnvelopParam *)param {
|
||||
[self.queue addObject:param];
|
||||
}
|
||||
|
||||
- (WeChatRedEnvelopParam *)dequeue {
|
||||
if (self.queue.count == 0 && !self.queue.firstObject) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
WeChatRedEnvelopParam *first = self.queue.firstObject;
|
||||
|
||||
[self.queue removeObjectAtIndex:0];
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
- (WeChatRedEnvelopParam *)peek {
|
||||
if (self.queue.count == 0) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
return self.queue.firstObject;
|
||||
}
|
||||
|
||||
- (BOOL)isEmpty {
|
||||
return self.queue.count == 0;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface WBReceiveRedEnvelopOperation ()
|
||||
|
||||
@property (assign, nonatomic, getter=isExecuting) BOOL executing;
|
||||
@property (assign, nonatomic, getter=isFinished) BOOL finished;
|
||||
|
||||
@property (strong, nonatomic) WeChatRedEnvelopParam *redEnvelopParam;
|
||||
@property (assign, nonatomic) unsigned int delaySeconds;
|
||||
|
||||
@end
|
||||
|
||||
@implementation WBReceiveRedEnvelopOperation
|
||||
|
||||
@synthesize executing = _executing;
|
||||
@synthesize finished = _finished;
|
||||
|
||||
- (instancetype)initWithRedEnvelopParam:(WeChatRedEnvelopParam *)param delay:(unsigned int)delaySeconds {
|
||||
if (self = [super init]) {
|
||||
_redEnvelopParam = param;
|
||||
_delaySeconds = delaySeconds;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)start {
|
||||
if (self.isCancelled) {
|
||||
self.finished = YES;
|
||||
self.executing = NO;
|
||||
return;
|
||||
}
|
||||
|
||||
[self main];
|
||||
|
||||
self.executing = YES;
|
||||
self.finished = NO;
|
||||
}
|
||||
|
||||
- (void)main {
|
||||
|
||||
[NSThread sleepForTimeInterval:self.delaySeconds/1000.0];
|
||||
WCRedEnvelopesLogicMgr *logicMgr = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("WCRedEnvelopesLogicMgr") class]];
|
||||
[logicMgr OpenRedEnvelopesRequest:[self.redEnvelopParam toParams]];
|
||||
|
||||
self.finished = YES;
|
||||
self.executing = NO;
|
||||
}
|
||||
|
||||
- (void)cancel {
|
||||
self.finished = YES;
|
||||
self.executing = NO;
|
||||
}
|
||||
|
||||
- (void)setFinished:(BOOL)finished {
|
||||
[self willChangeValueForKey:@"isFinished"];
|
||||
_finished = finished;
|
||||
[self didChangeValueForKey:@"isFinished"];
|
||||
}
|
||||
|
||||
- (void)setExecuting:(BOOL)executing {
|
||||
[self willChangeValueForKey:@"isExecuting"];
|
||||
_executing = executing;
|
||||
[self didChangeValueForKey:@"isExecuting"];
|
||||
}
|
||||
|
||||
- (BOOL)isAsynchronous {
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@interface WBRedEnvelopTaskManager ()
|
||||
|
||||
@property (strong, nonatomic) NSOperationQueue *normalTaskQueue;
|
||||
@property (strong, nonatomic) NSOperationQueue *serialTaskQueue;
|
||||
|
||||
@end
|
||||
|
||||
@implementation WBRedEnvelopTaskManager
|
||||
|
||||
+ (instancetype)sharedManager {
|
||||
static WBRedEnvelopTaskManager *taskManager = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
taskManager = [WBRedEnvelopTaskManager new];
|
||||
});
|
||||
return taskManager;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_serialTaskQueue = [[NSOperationQueue alloc] init];
|
||||
_serialTaskQueue.maxConcurrentOperationCount = 1;
|
||||
|
||||
_normalTaskQueue = [[NSOperationQueue alloc] init];
|
||||
_normalTaskQueue.maxConcurrentOperationCount = 5;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)addNormalTask:(WBReceiveRedEnvelopOperation *)task {
|
||||
[self.normalTaskQueue addOperation:task];
|
||||
}
|
||||
|
||||
- (void)addSerialTask:(WBReceiveRedEnvelopOperation *)task {
|
||||
[self.serialTaskQueue addOperation:task];
|
||||
}
|
||||
|
||||
- (BOOL)serialQueueIsEmpty {
|
||||
return [self.serialTaskQueue operations].count == 0;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user