在我們的App中是很經(jīng)常的會(huì)用到微信的兩個(gè)功能荸镊,1就是分享咽斧,2就是支付(通常會(huì)和支付寶、銀聯(lián)支付放在一起使用),通常接入這種三方的東西躬存,對(duì)于移動(dòng)端來(lái)說(shuō)张惹,過(guò)程都比較簡(jiǎn)單,但是對(duì)于后臺(tái)來(lái)說(shuō)岭洲,一般也不是說(shuō)難宛逗,就是步驟多,配置的東西比移動(dòng)端多(像比如支付過(guò)程), 廢話不多說(shuō)盾剩,由于我這里的分享只需要做微信的分享雷激,所以過(guò)程比較簡(jiǎn)單,配置什么的就不多說(shuō)了告私,自己去看微信支付的UML流程圖和微信SDK的配置流程屎暇,對(duì)于iOS端的代碼如下:
首先在工程里面配置白名單:
AppDelegate里面初始化微信SDK:
[WXApi registerApp:kWeChatID];
支付or分享代碼:
[ShareView shareViewWithCompleteHandel:^(NSInteger index) {
WXMediaMessage * message = [WXMediaMessage message];
message.title = [NSString stringWithFormat:@"%@[%@k-%@k]",rel[@"job_name"],rel[@"salary_min"],rel[@"salary_max"]];
message.description = [NSString stringWithFormat:@"入職快,加薪快,還能賺外快\n%@",rel[@"com_name"]];
[message setThumbImage:image];
WXWebpageObject * webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = urlString;
message.mediaObject = webpageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = index == 0 ? WXSceneSession :index == 1 ? WXSceneTimeline:WXSceneFavorite;
[WXApi sendReq:req];
}];
// 支付,note:這里的repsonse是后臺(tái)生成的支付參數(shù)驻粟,下訂單是請(qǐng)求自己的服務(wù)端拿到這些參數(shù):
if ([responese isKindOfClass:[NSDictionary class]]) {
NSDictionary *payParams = (NSDictionary *)responese;
PayReq * req = [[PayReq alloc] init];
req.partnerId = payParams[@"partnerid"];
req.prepayId = payParams[@"prepayid"];
req.nonceStr = payParams[@"noncestr"];
req.timeStamp = [payParams[@"timestamp"] intValue];
req.package = payParams[@"package"];
req.sign = payParams[@"sign"];
[WXApi sendReq:req];
}
支付or分享結(jié)果的回調(diào):
// MARK: - 處理三方回調(diào)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options {
//判斷是微信還是支付寶操作
if ([url.absoluteString rangeOfString:kWeChatID].location != NSNotFound){
return [WXApi handleOpenURL:url delegate:self];
} else {
if ([url.host isEqualToString:@"safepay"]) {
//支付寶支付回調(diào)自己App的結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([url.absoluteString rangeOfString:kWeChatID].location != NSNotFound) {
return [WXApi handleOpenURL:url delegate:self];
} else if ([url.host isEqualToString:@"safepay"]) {
[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
// MARK: - WXApiDelegate
- (void)onResp:(BaseResp *)resp {
if ([resp isKindOfClass:[PayResp class]]){
//支付
PayResp *response = (PayResp*)resp;
switch(response.errCode){
case 0:
//服務(wù)器端查詢支付通知或查詢API返回的結(jié)果再提示成功
[[NSNotificationCenter defaultCenter] postNotificationName:kWeChatPaySucessNotificationName object:self userInfo:@{@"PayResp":resp}];
[_window.rootViewController alertWithTitle:@"支付成功!" complete:nil];
break;
case -1:
case -2: {
[_window.rootViewController alertWithTitle:@"支付失敗!" complete:nil];
}
break;
default:
NSLog(@"支付失敗根悼,retcode=%d",resp.errCode);
break;
}
} else if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
//分享
SendMessageToWXResp *sendMessageRes = (SendMessageToWXResp *)resp;
if (sendMessageRes.errCode == 0 ) {
[_window.rootViewController alertWithTitle:@"分享成功!" complete:nil];
} else if (sendMessageRes.errCode == WXErrCodeUserCancel) {
[_window.rootViewController alertWithTitle:@"您取消了分享!" complete:nil];
} else {
[_window.rootViewController alertWithTitle:@"分享失敗!" complete:nil];
}
}
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([url.host isEqualToString:@"safepay"]) {
//跳轉(zhuǎn)支付寶錢包進(jìn)行支付,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
集成起來(lái)比較簡(jiǎn)單蜀撑,由于這里的支付請(qǐng)求加密參數(shù)是放在后臺(tái)進(jìn)行的(也理論上是后臺(tái)做比較安全挤巡,為了防止反編譯,一般是后臺(tái)做)App端做的事情相對(duì)來(lái)來(lái)說(shuō)還是比較少的酷麦。