SPSBShareViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // SPSBShareViewController.m
  3. // 我的社保
  4. //
  5. // Created by jiaxian_he on 2021/5/31.
  6. //
  7. #import "SPSBShareViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "WXApi.h"
  10. #import <TencentOpenAPI/TencentOAuth.h>
  11. #import <JXHGeneralTools.h>
  12. #import "SPSBUMEventAnalysis.h"
  13. #import "UIView+SPSBLoadingTipsView.h"
  14. NSString *const SPSBShareCircleOfFriends = @"SPSBShareCircleOfFriends";
  15. NSString *const SPSBShareWechat = @"SPSBShareWechat";
  16. NSString *const SPSBShareQQ = @"SPSBShareQQ";
  17. NSString *const SPSBShareQzone = @"SPSBShareQzone";
  18. NSString *const SPSBShareMiniProgram = @"SPSBShareMiniProgram";
  19. NSString *const SPSBShareSinaWeibo = @"SPSBShareSinaWeibo";
  20. @implementation SPSBShareModel
  21. - (instancetype)initWithTypeArray:(NSArray *)typeArray content:(NSString *)content image:(nullable id)image title:(NSString *)title url:(NSString *)url mediaType:(SSDKContentType)mediaType eventId:(nullable NSString *)eventId finish:(void(^_Nullable)(void))finish {
  22. self = [super init];
  23. if (!self) return nil;
  24. _spsb_url = url;
  25. _spsb_image = image;
  26. _spsb_title = title;
  27. _spsb_mediaType = mediaType;
  28. _spsb_eventId = eventId;
  29. _spsb_content = content;
  30. _spsb_typeArray = typeArray;
  31. _spsb_finishAction = finish;
  32. return self;
  33. }
  34. @end
  35. @interface SPSBShareViewController () {
  36. NSMutableArray *_tagArray;
  37. }
  38. @end
  39. @implementation SPSBShareViewController
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. [self setupUI];
  43. }
  44. #pragma mark - Action
  45. - (void)shareAction:(UIButton *)sender {
  46. if (_spsb_data.spsb_eventId) {
  47. [SPSBUMEventAnalysis analyseEventWithId:_spsb_data.spsb_eventId];
  48. }
  49. id image;
  50. if ([_spsb_data.spsb_image isKindOfClass:[UIImage class]]) {
  51. image = _spsb_data.spsb_image;
  52. } else if ([_spsb_data.spsb_image isKindOfClass:[NSString class]]) {
  53. image = [[SSDKImage alloc] initWithURL:[NSURL URLWithString:_spsb_data.spsb_image]];
  54. } else {
  55. image = [UIImage imageNamed:SPSBLogo];
  56. }
  57. NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
  58. NSInteger tag = [_tagArray[sender.tag - 3000] integerValue];
  59. switch (tag) {
  60. case 2000: {
  61. if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi]) {
  62. [self.view showToastWithTitle:@"未安装微信"];
  63. break;
  64. }
  65. [self dismissViewWithCompletion:^{
  66. [shareParams SSDKSetupWeChatParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] thumbImage:nil image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
  67. [ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  68. }];
  69. }];
  70. }
  71. break;
  72. case 2001: {
  73. if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi]) {
  74. [self.view showToastWithTitle:@"未安装微信"];
  75. break;
  76. }
  77. [self dismissViewWithCompletion:^{
  78. [shareParams SSDKSetupWeChatParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] thumbImage:nil image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeWechatSession];
  79. [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  80. }];
  81. }];
  82. }
  83. break;
  84. case 2002: {
  85. if (![TencentOAuth iphoneQQInstalled]) {
  86. [self.view showToastWithTitle:@"未安装QQ"];
  87. break;
  88. }
  89. [self dismissViewWithCompletion:^{
  90. [shareParams SSDKSetupQQParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] audioFlashURL:nil videoFlashURL:nil thumbImage:nil images:image type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeQQFriend];
  91. [ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  92. }];
  93. }];
  94. }
  95. break;
  96. case 2003: {
  97. if (![TencentOAuth iphoneQQInstalled]) {
  98. [self.view showToastWithTitle:@"未安装QQ"];
  99. break;
  100. }
  101. [self dismissViewWithCompletion:^{
  102. [shareParams SSDKSetupQQParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title url:[NSURL URLWithString:self->_spsb_data.spsb_url] audioFlashURL:nil videoFlashURL:nil thumbImage:nil images:image type:self->_spsb_data.spsb_mediaType forPlatformSubType:SSDKPlatformSubTypeQZone];
  103. [ShareSDK share:SSDKPlatformSubTypeQZone parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  104. }];
  105. }];
  106. }
  107. break;
  108. case 2004: {
  109. if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi]) {
  110. [self.view showToastWithTitle:@"未安装微信"];
  111. break;
  112. }
  113. [self dismissViewWithCompletion:^{
  114. [shareParams SSDKSetupCopyParamsByText:self->_spsb_data.spsb_content images:image url:[NSURL URLWithString:self->_spsb_data.spsb_url] type:self->_spsb_data.spsb_mediaType];
  115. [shareParams SSDKSetupWeChatMiniProgramShareParamsByTitle:self->_spsb_data.spsb_title description:self->_spsb_data.spsb_content webpageUrl:[NSURL URLWithString:self->_spsb_data.spsb_url] path:@"page/home/index" thumbImage:jxh_getImage(share_img) hdThumbImage:jxh_getImage(share_img) userName:@"gh_2d56467457de" withShareTicket:false miniProgramType:0 forPlatformSubType:SSDKPlatformSubTypeWechatSession];
  116. [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  117. }];
  118. }];
  119. }
  120. break;
  121. case 2005: {
  122. [self dismissViewWithCompletion:^{
  123. [shareParams SSDKSetupSMSParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title images:image attachments:nil recipients:@[] type:self->_spsb_data.spsb_mediaType];
  124. [ShareSDK share:SSDKPlatformTypeSMS parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  125. }];
  126. }];
  127. }
  128. break;
  129. case 2006: {
  130. [self dismissViewWithCompletion:^{
  131. [shareParams SSDKSetupSinaWeiboShareParamsByText:self->_spsb_data.spsb_content title:self->_spsb_data.spsb_title images:image video:nil url:[NSURL URLWithString:self->_spsb_data.spsb_url] latitude:0 longitude:0 objectID:nil isShareToStory:false type:self->_spsb_data.spsb_mediaType];
  132. [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
  133. }];
  134. }];
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. - (void)shareCloseAction {
  142. [self dismissViewWithCompletion:^{
  143. }];
  144. }
  145. - (void)dismissViewWithCompletion:(void (^)(void))completion {
  146. jxh_windowUserInteractionEnabled(false);
  147. [self dismissAnimationWithIndex:_spsb_data.spsb_typeArray.count - 1 completion:completion];
  148. }
  149. - (void)dismissAnimationWithIndex:(NSInteger)index completion:(void (^)(void))completion {
  150. if (index < 0) return;
  151. UIButton *button = [self.view viewWithTag:3000 + index];
  152. NSInteger c = index / 3;
  153. [button makeConstraints:^(JXHConstraintMaker *make) {
  154. make.top.update(jxh_screenHeight() + c * 118);
  155. }];
  156. [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionLayoutSubviews animations:^{
  157. [self.view layoutSubviews];
  158. } completion:^(BOOL finished) {
  159. }];
  160. if (index == 0) {
  161. [UIView animateWithDuration:0.5 animations:^{
  162. self.view.alpha = 0;
  163. } completion:^(BOOL finished) {
  164. [self dismissViewControllerAnimated:false completion:^{
  165. jxh_windowUserInteractionEnabled(true);
  166. completion();
  167. if (self->_spsb_data.spsb_finishAction) {
  168. self->_spsb_data.spsb_finishAction();
  169. }
  170. }];
  171. }];
  172. }
  173. jxh_delayAction(0.05, dispatch_get_main_queue(), ^{
  174. [self dismissAnimationWithIndex:index - 1 completion:completion];
  175. });
  176. }
  177. - (void)showAnimationWithIndex:(NSInteger)index max:(NSInteger)max {
  178. NSInteger top = jxh_screenHeight() - 346 - jxh_safeInsets(spsb_keyWindow()).bottom;
  179. if (index + 1 > max) {
  180. jxh_windowUserInteractionEnabled(true);
  181. return;
  182. }
  183. UIButton *button = [self.view viewWithTag:3000 + index];
  184. NSInteger c = index / 3;
  185. [button makeConstraints:^(JXHConstraintMaker *make) {
  186. make.top.update(top + c * 118);
  187. }];
  188. [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionLayoutSubviews animations:^{
  189. [self.view layoutSubviews];
  190. } completion:nil];
  191. jxh_delayAction(0.05, dispatch_get_main_queue(), ^{
  192. [self showAnimationWithIndex:index + 1 max:max];
  193. });
  194. }
  195. #pragma mark - NetworkAction
  196. #pragma mark - Overwrite
  197. - (void)setupAnimation {
  198. }
  199. - (void)showAnimation {
  200. jxh_windowUserInteractionEnabled(false);
  201. [self showAnimationWithIndex:0 max:_spsb_data.spsb_typeArray.count];
  202. }
  203. #pragma mark - UI
  204. - (void)setupUI {
  205. self.view.backgroundColor = spsb_clear_color();
  206. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
  207. UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
  208. [self.view addSubview:effectView];
  209. [effectView makeConstraints:^(JXHConstraintMaker *make) {
  210. make.edges.equalTo(0);
  211. }];
  212. [self addViews];
  213. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareCloseAction)]];
  214. }
  215. - (void)addViews {
  216. CGFloat left = (jxh_screenWidth() - 70 * 3 - 46 * 2) / 2;
  217. NSDictionary *dataDic = @{
  218. SPSBShareWechat : @[@"微信好友", @"share_wechat", @2001],
  219. SPSBShareCircleOfFriends : @[@"朋友圈", @"share_friends", @2000],
  220. SPSBShareQQ : @[@"QQ好友", @"share_qq", @2002],
  221. SPSBShareQzone : @[@"QQ空间", @"share_qzone", @2003],
  222. SPSBShareMiniProgram : @[@"微信小程序", @"share_miniprograms", @2004],
  223. SPSBShareSinaWeibo : @[@"新浪微博", @"share_weibo", @2006],
  224. };
  225. _tagArray = [@[] mutableCopy];
  226. UIButton *tempButton = nil;
  227. for (NSInteger i = 0; i < _spsb_data.spsb_typeArray.count; i ++) {
  228. [_tagArray addObject:dataDic[_spsb_data.spsb_typeArray[i]][2]];
  229. UIButton *button = [UIButton convenienceWithTarget:self action:@selector(shareAction:)];
  230. button.tag = 3000 + i;
  231. [self.view addSubview:button];
  232. NSInteger c = i / 3;
  233. [button makeConstraints:^(JXHConstraintMaker *make) {
  234. if (tempButton) {
  235. if (i % 3 == 0) {
  236. make.leading.equalTo(left);
  237. } else {
  238. make.leading.equalTo(tempButton.trailing).offset(46);
  239. }
  240. } else {
  241. make.leading.equalTo(left);
  242. }
  243. make.top.equalTo(jxh_screenHeight() + c * 118);
  244. make.width.equalTo(70);
  245. }];
  246. UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dataDic[_spsb_data.spsb_typeArray[i]][1]]];
  247. [button addSubview:icon];
  248. [icon makeConstraints:^(JXHConstraintMaker *make) {
  249. make.top.equalTo(0);
  250. make.centerX.equalTo(button);
  251. make.size.equalTo(CGSizeMake(60, 60));
  252. }];
  253. UILabel *label = [UILabel convenienceWithFont:spsb_font(14) text:dataDic[_spsb_data.spsb_typeArray[i]][0] textColor:spsb_000000_color(0.8) textAlignment:NSTextAlignmentCenter];
  254. [button addSubview:label];
  255. [label makeConstraints:^(JXHConstraintMaker *make) {
  256. make.top.equalTo(icon.bottom).offset(10);
  257. make.centerX.equalTo(button);
  258. make.bottom.equalTo(0);
  259. }];
  260. tempButton = button;
  261. }
  262. UIButton *close = [UIButton convenienceWithTarget:self action:@selector(shareCloseAction)];
  263. [close setImage:jxh_getImage(share_close) state:JXHButtonControlStateNormal];
  264. [self.view addSubview:close];
  265. [close makeConstraints:^(JXHConstraintMaker *make) {
  266. make.top.equalTo(tempButton.bottom).offset(60);
  267. make.centerX.equalTo(self.view);
  268. }];
  269. }
  270. @end