WXApiRequestHandler.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // WXApiManager.m
  3. // SDKSample
  4. //
  5. // Created by Jeason on 15/7/14.
  6. //
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "WXApi.h"
  10. #import "WXApiRequestHandler.h"
  11. #import "WXApiManager.h"
  12. #import "SendMessageToWXReq+requestWithTextOrMediaMessage.h"
  13. #import "WXMediaMessage+messageConstruct.h"
  14. @implementation WXApiRequestHandler
  15. #pragma mark - Public Methods
  16. + (BOOL)sendText:(NSString *)text
  17. InScene:(enum WXScene)scene {
  18. SendMessageToWXReq *req = [SendMessageToWXReq requestWithText:text
  19. OrMediaMessage:nil
  20. bText:YES
  21. InScene:scene];
  22. return [WXApi sendReq:req];
  23. }
  24. + (BOOL)sendImageData:(NSData *)imageData
  25. TagName:(NSString *)tagName
  26. MessageExt:(NSString *)messageExt
  27. Action:(NSString *)action
  28. ThumbImage:(UIImage *)thumbImage
  29. InScene:(enum WXScene)scene {
  30. WXImageObject *ext = [WXImageObject object];
  31. ext.imageData = imageData;
  32. WXMediaMessage *message = [WXMediaMessage messageWithTitle:nil
  33. Description:nil
  34. Object:ext
  35. MessageExt:messageExt
  36. MessageAction:action
  37. ThumbImage:thumbImage
  38. MediaTag:tagName];
  39. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  40. OrMediaMessage:message
  41. bText:NO
  42. InScene:scene];
  43. return [WXApi sendReq:req];
  44. }
  45. + (BOOL)sendLinkURL:(NSString *)urlString
  46. TagName:(NSString *)tagName
  47. Title:(NSString *)title
  48. Description:(NSString *)description
  49. ThumbImage:(UIImage *)thumbImage
  50. InScene:(enum WXScene)scene {
  51. WXWebpageObject *ext = [WXWebpageObject object];
  52. ext.webpageUrl = urlString;
  53. WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
  54. Description:description
  55. Object:ext
  56. MessageExt:nil
  57. MessageAction:nil
  58. ThumbImage:thumbImage
  59. MediaTag:tagName];
  60. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  61. OrMediaMessage:message
  62. bText:NO
  63. InScene:scene];
  64. return [WXApi sendReq:req];
  65. }
  66. + (BOOL)sendMusicURL:(NSString *)musicURL
  67. dataURL:(NSString *)dataURL
  68. Title:(NSString *)title
  69. Description:(NSString *)description
  70. ThumbImage:(UIImage *)thumbImage
  71. InScene:(enum WXScene)scene {
  72. WXMusicObject *ext = [WXMusicObject object];
  73. ext.musicUrl = musicURL;
  74. ext.musicDataUrl = dataURL;
  75. WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
  76. Description:description
  77. Object:ext
  78. MessageExt:nil
  79. MessageAction:nil
  80. ThumbImage:thumbImage
  81. MediaTag:nil];
  82. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  83. OrMediaMessage:message
  84. bText:NO
  85. InScene:scene];
  86. return [WXApi sendReq:req];
  87. }
  88. + (BOOL)sendVideoURL:(NSString *)videoURL
  89. Title:(NSString *)title
  90. Description:(NSString *)description
  91. ThumbImage:(UIImage *)thumbImage
  92. InScene:(enum WXScene)scene {
  93. WXMediaMessage *message = [WXMediaMessage message];
  94. message.title = title;
  95. message.description = description;
  96. [message setThumbImage:thumbImage];
  97. WXVideoObject *ext = [WXVideoObject object];
  98. ext.videoUrl = videoURL;
  99. message.mediaObject = ext;
  100. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  101. OrMediaMessage:message
  102. bText:NO
  103. InScene:scene];
  104. return [WXApi sendReq:req];
  105. }
  106. + (BOOL)sendEmotionData:(NSData *)emotionData
  107. ThumbImage:(UIImage *)thumbImage
  108. InScene:(enum WXScene)scene {
  109. WXMediaMessage *message = [WXMediaMessage message];
  110. [message setThumbImage:thumbImage];
  111. WXEmoticonObject *ext = [WXEmoticonObject object];
  112. ext.emoticonData = emotionData;
  113. message.mediaObject = ext;
  114. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  115. OrMediaMessage:message
  116. bText:NO
  117. InScene:scene];
  118. return [WXApi sendReq:req];
  119. }
  120. + (BOOL)sendFileData:(NSData *)fileData
  121. fileExtension:(NSString *)extension
  122. Title:(NSString *)title
  123. Description:(NSString *)description
  124. ThumbImage:(UIImage *)thumbImage
  125. InScene:(enum WXScene)scene {
  126. WXMediaMessage *message = [WXMediaMessage message];
  127. message.title = title;
  128. message.description = description;
  129. [message setThumbImage:thumbImage];
  130. WXFileObject *ext = [WXFileObject object];
  131. ext.fileExtension = @"pdf";
  132. ext.fileData = fileData;
  133. message.mediaObject = ext;
  134. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  135. OrMediaMessage:message
  136. bText:NO
  137. InScene:scene];
  138. return [WXApi sendReq:req];
  139. }
  140. + (BOOL)sendAppContentData:(NSData *)data
  141. ExtInfo:(NSString *)info
  142. ExtURL:(NSString *)url
  143. Title:(NSString *)title
  144. Description:(NSString *)description
  145. MessageExt:(NSString *)messageExt
  146. MessageAction:(NSString *)action
  147. ThumbImage:(UIImage *)thumbImage
  148. InScene:(enum WXScene)scene {
  149. WXAppExtendObject *ext = [WXAppExtendObject object];
  150. ext.extInfo = info;
  151. ext.url = url;
  152. ext.fileData = data;
  153. WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
  154. Description:description
  155. Object:ext
  156. MessageExt:messageExt
  157. MessageAction:action
  158. ThumbImage:thumbImage
  159. MediaTag:nil];
  160. SendMessageToWXReq* req = [SendMessageToWXReq requestWithText:nil
  161. OrMediaMessage:message
  162. bText:NO
  163. InScene:scene];
  164. return [WXApi sendReq:req];
  165. }
  166. + (BOOL)addCardsToCardPackage:(NSArray *)cardItems {
  167. AddCardToWXCardPackageReq *req = [[[AddCardToWXCardPackageReq alloc] init] autorelease];
  168. req.cardAry = cardItems;
  169. return [WXApi sendReq:req];
  170. }
  171. + (BOOL)sendAuthRequestScope:(NSString *)scope
  172. State:(NSString *)state
  173. OpenID:(NSString *)openID
  174. InViewController:(UIViewController *)viewController {
  175. SendAuthReq* req = [[[SendAuthReq alloc] init] autorelease];
  176. req.scope = scope; // @"post_timeline,sns"
  177. req.state = state;
  178. req.openID = openID;
  179. return [WXApi sendAuthReq:req
  180. viewController:viewController
  181. delegate:[WXApiManager sharedManager]];
  182. }
  183. + (BOOL)jumpToBizWebviewWithAppID:(NSString *)appID
  184. Description:(NSString *)description
  185. tousrname:(NSString *)tousrname
  186. ExtMsg:(NSString *)extMsg {
  187. [WXApi registerApp:appID withDescription:description];
  188. JumpToBizWebviewReq *req = [[[JumpToBizWebviewReq alloc]init]autorelease];
  189. req.tousrname = tousrname;
  190. req.extMsg = extMsg;
  191. req.webType = WXMPWebviewType_Ad;
  192. return [WXApi sendReq:req];
  193. }
  194. + (NSString *)jumpToBizPay {
  195. //============================================================
  196. // V3&V4支付流程实现
  197. // 注意:参数配置请查看服务器端Demo
  198. // 更新时间:2015年11月20日
  199. //============================================================
  200. NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
  201. //解析服务端返回json数据
  202. NSError *error;
  203. //加载一个NSURL对象
  204. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
  205. //将请求的url数据放到NSData对象中
  206. NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  207. if ( response != nil) {
  208. NSMutableDictionary *dict = NULL;
  209. //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
  210. dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
  211. NSLog(@"url:%@",urlString);
  212. if(dict != nil){
  213. NSMutableString *retcode = [dict objectForKey:@"retcode"];
  214. if (retcode.intValue == 0){
  215. NSMutableString *stamp = [dict objectForKey:@"timestamp"];
  216. //调起微信支付
  217. PayReq* req = [[[PayReq alloc] init]autorelease];
  218. req.partnerId = [dict objectForKey:@"partnerid"];
  219. req.prepayId = [dict objectForKey:@"prepayid"];
  220. req.nonceStr = [dict objectForKey:@"noncestr"];
  221. req.timeStamp = stamp.intValue;
  222. req.package = [dict objectForKey:@"package"];
  223. req.sign = [dict objectForKey:@"sign"];
  224. [WXApi sendReq:req];
  225. //日志输出
  226. 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 );
  227. return @"";
  228. }else{
  229. return [dict objectForKey:@"retmsg"];
  230. }
  231. }else{
  232. return @"服务器返回错误,未获取到json对象";
  233. }
  234. }else{
  235. return @"服务器返回错误";
  236. }
  237. }
  238. @end