123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- //
- // WXApiManager.m
- // SDKSample
- //
- // Created by Jeason on 15/7/14.
- //
- //
- #import "WXApi.h"
- #import "WXApiRequestHandler.h"
- #import "WXApiManager.h"
- #import "SendMessageToWXReq+requestWithTextOrMediaMessage.h"
- #import "WXMediaMessage+messageConstruct.h"
- @implementation WXApiRequestHandler
- #pragma mark - Public Methods
- + (BOOL)sendText:(NSString *)text
- InScene:(enum WXScene)scene {
- SendMessageToWXReq *req = [SendMessageToWXReq requestWithText:text
- OrMediaMessage:nil
- bText:YES
- InScene:scene];
- return [WXApi sendReq:req];
- }
- + (BOOL)sendImageData:(NSData *)imageData
- TagName:(NSString *)tagName
- MessageExt:(NSString *)messageExt
- Action:(NSString *)action
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXImageObject *ext = [WXImageObject object];
- ext.imageData = imageData;
-
- WXMediaMessage *message = [WXMediaMessage messageWithTitle:nil
- Description:nil
- Object:ext
- MessageExt:messageExt
- MessageAction:action
- ThumbImage:thumbImage
- MediaTag:tagName];
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
-
- return [WXApi sendReq:req];
- }
- + (BOOL)sendLinkURL:(NSString *)urlString
- TagName:(NSString *)tagName
- Title:(NSString *)title
- Description:(NSString *)description
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXWebpageObject *ext = [WXWebpageObject object];
- ext.webpageUrl = urlString;
- WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
- Description:description
- Object:ext
- MessageExt:nil
- MessageAction:nil
- ThumbImage:thumbImage
- MediaTag:tagName];
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
- return [WXApi sendReq:req];
- }
- + (BOOL)sendMusicURL:(NSString *)musicURL
- dataURL:(NSString *)dataURL
- Title:(NSString *)title
- Description:(NSString *)description
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXMusicObject *ext = [WXMusicObject object];
- ext.musicUrl = musicURL;
- ext.musicDataUrl = dataURL;
- WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
- Description:description
- Object:ext
- MessageExt:nil
- MessageAction:nil
- ThumbImage:thumbImage
- MediaTag:nil];
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
-
- return [WXApi sendReq:req];
- }
- + (BOOL)sendVideoURL:(NSString *)videoURL
- Title:(NSString *)title
- Description:(NSString *)description
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = title;
- message.description = description;
- [message setThumbImage:thumbImage];
-
- WXVideoObject *ext = [WXVideoObject object];
- ext.videoUrl = videoURL;
-
- message.mediaObject = ext;
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
- return [WXApi sendReq:req];
- }
- + (BOOL)sendEmotionData:(NSData *)emotionData
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXMediaMessage *message = [WXMediaMessage message];
- [message setThumbImage:thumbImage];
-
- WXEmoticonObject *ext = [WXEmoticonObject object];
- ext.emoticonData = emotionData;
-
- message.mediaObject = ext;
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
- return [WXApi sendReq:req];
- }
- + (BOOL)sendFileData:(NSData *)fileData
- fileExtension:(NSString *)extension
- Title:(NSString *)title
- Description:(NSString *)description
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = title;
- message.description = description;
- [message setThumbImage:thumbImage];
-
- WXFileObject *ext = [WXFileObject object];
- ext.fileExtension = @"pdf";
- ext.fileData = fileData;
-
- message.mediaObject = ext;
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
- return [WXApi sendReq:req];
- }
- + (BOOL)sendAppContentData:(NSData *)data
- ExtInfo:(NSString *)info
- ExtURL:(NSString *)url
- Title:(NSString *)title
- Description:(NSString *)description
- MessageExt:(NSString *)messageExt
- MessageAction:(NSString *)action
- ThumbImage:(UIImage *)thumbImage
- InScene:(enum WXScene)scene {
- WXAppExtendObject *ext = [WXAppExtendObject object];
- ext.extInfo = info;
- ext.url = url;
- ext.fileData = data;
- WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
- Description:description
- Object:ext
- MessageExt:messageExt
- MessageAction:action
- ThumbImage:thumbImage
- MediaTag:nil];
-
- SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
- OrMediaMessage:message
- bText:NO
- InScene:scene];
- return [WXApi sendReq:req];
- }
- + (BOOL)addCardsToCardPackage:(NSArray *)cardItems {
- AddCardToWXCardPackageReq *req = [[AddCardToWXCardPackageReq alloc] init];
- req.cardAry = cardItems;
- return [WXApi sendReq:req];
- }
- + (BOOL)sendAuthRequestScope:(NSString *)scope
- State:(NSString *)state
- OpenID:(NSString *)openID
- InViewController:(UIViewController *)viewController {
- SendAuthReq* req = [[SendAuthReq alloc] init];
- req.scope = scope; // @"post_timeline,sns"
- req.state = state;
- req.openID = openID;
-
- return [WXApi sendAuthReq:req
- viewController:viewController
- delegate:[WXApiManager sharedManager]];
- }
- + (BOOL)jumpToBizWebviewWithAppID:(NSString *)appID
- Description:(NSString *)description
- tousrname:(NSString *)tousrname
- ExtMsg:(NSString *)extMsg {
- [WXApi registerApp:appID];
- JumpToBizWebviewReq *req = [[JumpToBizWebviewReq alloc]init];
- req.tousrname = tousrname;
- req.extMsg = extMsg;
- req.webType = WXMPWebviewType_Ad;
- return [WXApi sendReq:req];
- }
- + (NSString *)jumpToBizPay {
- //============================================================
- // V3&V4支付流程实现
- // 注意:参数配置请查看服务器端Demo
- // 更新时间:2015年11月20日
- //============================================================
- NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
- //解析服务端返回json数据
- NSError *error;
- //加载一个NSURL对象
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
- //将请求的url数据放到NSData对象中
- NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- if ( response != nil) {
- NSMutableDictionary *dict = NULL;
- //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
- dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
-
- NSLog(@"url:%@",urlString);
- if(dict != nil){
- NSMutableString *retcode = [dict objectForKey:@"retcode"];
- if (retcode.intValue == 0){
- NSMutableString *stamp = [dict objectForKey:@"timestamp"];
- #if 0
- //调起微信支付
- PayReq* req = [[PayReq alloc] init];
- req.partnerId = [dict objectForKey:@"partnerid"];
- req.prepayId = [dict objectForKey:@"prepayid"];
- req.nonceStr = [dict objectForKey:@"noncestr"];
- req.timeStamp = stamp.intValue;
- req.package = [dict objectForKey:@"package"];
- req.sign = [dict objectForKey:@"sign"];
- [WXApi sendReq:req];
- //日志输出
- NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
- #endif
- return @"";
- }else{
- return [dict objectForKey:@"retmsg"];
- }
- }else{
- return @"服务器返回错误,未获取到json对象";
- }
- }else{
- return @"服务器返回错误";
- }
- }
- @end
|