dataModel.m 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. //
  2. // dateModel.m
  3. // YiZanService
  4. //
  5. // Created by zzl on 15/3/19.
  6. // Copyright (c) 2015年 zywl. All rights reserved.
  7. //
  8. #import "dataModel.h"
  9. #import "NSObject+myobj.h"
  10. #import "Util.h"
  11. #import "AFURLSessionManager.h"
  12. #import <QMapKit/QMapKit.h>
  13. #import <objc/message.h>
  14. #import "Mwxpay.h"
  15. #import "FMDB.h"
  16. #import <ImSDK/TIMManager.h>
  17. #import "IMAMsg.h"
  18. #import "AVIMAble.h"
  19. #import <StoreKit/StoreKit.h>
  20. #import "TPAACAudioConverter.h"
  21. @implementation dataModel
  22. @end
  23. @interface SAutoEx()<NSCoding,SKPaymentTransactionObserver,SKProductsRequestDelegate>
  24. @property (nonatomic, copy) NSString *pro_id;
  25. @property (nonatomic, strong) NetHttpsManager *httpManager;
  26. @property (nonatomic, copy) void (^block) (SResBase *resb);
  27. @end
  28. static FMDatabaseQueue * g_db_queue = nil;
  29. @implementation SAutoEx
  30. - (id)initWithObj:(NSDictionary*)obj
  31. {
  32. self = [super init];
  33. if( self && obj )
  34. {
  35. [self fetchIt:obj];
  36. }
  37. return self;
  38. }
  39. - (void)fetchIt:(NSDictionary*)obj
  40. {
  41. if( obj == nil ) return;
  42. NSMutableDictionary* nameMapProp = NSMutableDictionary.new;
  43. id leaderClass = [self class];
  44. unsigned int outCount, i;
  45. objc_property_t *properties = class_copyPropertyList(leaderClass, &outCount);
  46. for (i = 0; i < outCount; i++)
  47. {
  48. objc_property_t property = properties[i];
  49. NSString *propName = [NSString stringWithUTF8String:property_getName(property)];
  50. [nameMapProp setObject:[NSString stringWithFormat:@"%s",property_getAttributes(property)] forKey:propName];
  51. }
  52. if( properties )
  53. {
  54. free( properties );
  55. }
  56. if( nameMapProp.count == 0 ) return;
  57. NSArray* allnames = [nameMapProp allKeys];
  58. for ( NSString* oneName in allnames )
  59. {
  60. if( ![oneName hasPrefix:@"m"] ) continue;
  61. //mId....like this
  62. NSString* jsonkey = [oneName stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:[[oneName substringWithRange:NSMakeRange(1, 1)] lowercaseString] ];
  63. //mId ==> mid;
  64. jsonkey = [jsonkey substringFromIndex:1];
  65. //mid ==> id;
  66. id itobj = [obj objectForKeyMy:jsonkey];
  67. if( itobj == nil ) continue;
  68. [self setValue:itobj forKey:oneName];
  69. }
  70. }
  71. -(NSArray*)getClassAllPropNameForCoder
  72. {
  73. NSMutableDictionary* nameMapProp = NSMutableDictionary.new;
  74. id leaderClass = [self class];
  75. unsigned int outCount, i;
  76. objc_property_t *properties = class_copyPropertyList(leaderClass, &outCount);
  77. for (i = 0; i < outCount; i++)
  78. {
  79. objc_property_t property = properties[i];
  80. NSString *propName = [NSString stringWithUTF8String:property_getName(property)];
  81. if( [propName hasPrefix:@"mm"] ) continue;//这种字段不需要保存
  82. [nameMapProp setObject:[NSString stringWithFormat:@"%s",property_getAttributes(property)] forKey:propName];
  83. }
  84. if(properties)
  85. {
  86. free( properties );
  87. }
  88. return [nameMapProp allKeys];
  89. }
  90. - (void)encodeWithCoder:(NSCoder *)aCoder
  91. {
  92. NSArray* allprop = [self getClassAllPropNameForCoder];
  93. for ( NSString* one in allprop )
  94. {
  95. [aCoder encodeObject: [self valueForKey:one] forKey:one];
  96. }
  97. }
  98. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
  99. {
  100. self = [super init];
  101. NSArray* allprop = [self getClassAllPropNameForCoder];
  102. for ( NSString* one in allprop )
  103. {
  104. id k = [aDecoder decodeObjectForKey: one];
  105. if( k )
  106. [self setValue:k forKey:one];
  107. }
  108. return self;
  109. }
  110. + (BOOL)saveData:(id)data forkey:(NSString*)key
  111. {
  112. if( data == nil || key == nil) return NO;
  113. NSString*ss = [NSString stringWithFormat:@"%@_%@",key,[IMAPlatform sharedInstance].host.imUserId];
  114. NSUserDefaults* st = [NSUserDefaults standardUserDefaults];
  115. NSData* dat = [NSKeyedArchiver archivedDataWithRootObject:data];
  116. [st setObject:dat forKey:ss];
  117. return [st synchronize];
  118. }
  119. + (id)loadDataWithKey:(NSString*)key
  120. {
  121. if( key == nil ) return nil;
  122. NSString*ss = [NSString stringWithFormat:@"%@_%@",key,[IMAPlatform sharedInstance].host.imUserId ];
  123. NSUserDefaults* st = [NSUserDefaults standardUserDefaults];
  124. NSData* dat = [st objectForKey:ss];
  125. if( dat == nil ) return nil;
  126. return [NSKeyedUnarchiver unarchiveObjectWithData:dat];
  127. }
  128. + (void)initialize
  129. {
  130. if( g_db_queue == nil )
  131. {
  132. NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  133. NSString *dbPath = [docsPath stringByAppendingPathComponent:@"zbdata.db"];
  134. g_db_queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
  135. }
  136. }
  137. //addListData 添加all的数据到存储
  138. + (void)addListData:(NSArray*)all idname:(NSString*)idname forKey:(NSString*)key block:(void(^)(SResBase* resb))block
  139. {
  140. [g_db_queue inTransaction:^(FMDatabase *db, BOOL *rollback)
  141. {
  142. NSString* t_name = [NSString stringWithFormat:@"t_%@",key];
  143. BOOL create = [db executeUpdate: [NSString stringWithFormat:@"create table if not exists %@( userid text , id interger , data blob ) " ,t_name]];
  144. if( !create )
  145. {
  146. NSLog(@"open db failed");
  147. *rollback = YES;
  148. block( [SResBase infoWithString: ASLocalizedString(@"存储数据失败")]) ;
  149. return ;
  150. }
  151. NSString* userid = [IMAPlatform sharedInstance].host.imUserId ;
  152. NSString* sql = [NSString stringWithFormat:@"INSERT INTO %@ ( userid, id,data ) VALUES ( ? , ? ,?)", t_name];
  153. for (int k = 0 ; k < all.count;k++)
  154. {
  155. BOOL b;
  156. int itid = k;
  157. NSData* dat;
  158. itid = [[all[k] valueForKey:idname] intValue];
  159. dat = [NSKeyedArchiver archivedDataWithRootObject:all[k]];
  160. b = [db executeUpdate:sql,userid,@(itid),dat];
  161. if( !b )
  162. {
  163. NSLog(@"insert err:%@ at index:%d user:%@ key:%@",dat,k,userid,t_name);
  164. *rollback = YES;
  165. block([SResBase infoWithString: ASLocalizedString(@"存储数据失败")]) ;
  166. return ;
  167. }
  168. }
  169. block( [SResBase infoWithOKString:ASLocalizedString(@"存储数据成功")] );
  170. }];
  171. }
  172. + (void)getListData:(int)page forKey:(NSString*)key block:(void(^)(SResBase* resb ,NSArray* all))block
  173. {
  174. [g_db_queue inDatabase:^(FMDatabase *db) {
  175. NSString* userid = [IMAPlatform sharedInstance].host.imUserId ;
  176. NSString* t_name = [NSString stringWithFormat:@"t_%@",key];
  177. NSString* sql = [NSString stringWithFormat:@"select * from %@ where userid = %@ order by id asc limit 20 offset %d",t_name,userid,page*20];
  178. FMResultSet *rsl = [db executeQuery:sql];
  179. NSMutableArray* t = NSMutableArray.new;
  180. while ( [rsl next] )
  181. {
  182. NSData* dat = [rsl dataForColumn:@"data"];
  183. if( dat != nil && ![dat isKindOfClass:[NSNull class]] )
  184. {
  185. id obj = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
  186. [t addObject:obj];
  187. }
  188. }
  189. block( [SResBase infoWithOKString:ASLocalizedString(@"获取数据成功")], t );
  190. }];
  191. }
  192. + (void)removeListData:(NSArray*)all idname:(NSString*)idname forKey:(NSString*)key block:(void(^)(SResBase* resb))block
  193. {
  194. [g_db_queue inTransaction:^(FMDatabase *db ,BOOL *rollback) {
  195. NSString* userid = [IMAPlatform sharedInstance].host.imUserId;
  196. NSString* t_name = [NSString stringWithFormat:@"t_%@",key];
  197. for ( id oneobj in all ) {
  198. int mid = [[oneobj valueForKey:idname] intValue];
  199. NSString* sql = [NSString stringWithFormat:@"delete from %@ where userid = %@ and id=%d ",t_name,userid,mid];
  200. BOOL b = [db executeUpdate:sql];
  201. if( !b )
  202. {
  203. NSLog(@"remove list dataerr");
  204. *rollback = YES;
  205. block( [SResBase infoWithString:ASLocalizedString(@"删除数据失败")] );
  206. return;
  207. }
  208. }
  209. block( [SResBase infoWithOKString:ASLocalizedString(@"获取数据成功")] );
  210. }];
  211. }
  212. + (void)clearDataForKey:(NSString*)key block:(void(^)(SResBase* resb))block
  213. {
  214. [g_db_queue inDatabase:^(FMDatabase *db) {
  215. NSString* t_name = [NSString stringWithFormat:@"t_%@",key];
  216. NSString* sql = [NSString stringWithFormat:@"drop table %@ ",t_name];
  217. BOOL b = [db executeUpdate:sql];
  218. if( block )
  219. {
  220. if( b )
  221. {
  222. block( [SResBase infoWithOKString:ASLocalizedString(@"删除表成功")]);
  223. }
  224. else
  225. {
  226. block( [SResBase infoWithString:ASLocalizedString(@"删除表失败")]);
  227. }
  228. }
  229. }];
  230. }
  231. + (id)findObjForKey:(NSString*)key idvalue:(int)idvalue
  232. {
  233. MYNSCondition* itlock = [[MYNSCondition alloc] init];//搞个事件来同步下
  234. __block id findlocobj = nil;
  235. [g_db_queue inDatabase:^(FMDatabase *db) {
  236. NSString* userid = [IMAPlatform sharedInstance].host.imUserId;
  237. NSString* t_name = [NSString stringWithFormat:@"t_%@",key];
  238. NSString* sql = [NSString stringWithFormat:@"select * from %@ where userid = %@ and id = %d limit 0,1",t_name,userid,idvalue];
  239. FMResultSet *rsl = [db executeQuery:sql];
  240. while ( [rsl next] ) {
  241. NSData* dat = [rsl dataForColumn:@"data"];
  242. if( dat != nil && ![dat isKindOfClass:[NSNull class]] )
  243. {
  244. findlocobj = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
  245. }
  246. }
  247. [itlock lock];
  248. [itlock signal];
  249. [itlock unlock];
  250. }];
  251. [itlock lock];
  252. [itlock wait];
  253. [itlock unlock];
  254. return findlocobj;
  255. }
  256. //这个对象是否存储 有存储到 这key下面的
  257. - (id)haveThisObjInLocalForKey:(NSString*)key idname:(NSString*)idname
  258. {
  259. MYNSCondition* itlock = [[MYNSCondition alloc] init];//搞个事件来同步下
  260. __block id findlocobj = nil;
  261. [g_db_queue inDatabase:^(FMDatabase *db) {
  262. NSString* userid = [IMAPlatform sharedInstance].host.imUserId;
  263. NSString* t_name = [NSString stringWithFormat:@"t_%@",key];
  264. NSString* sql = [NSString stringWithFormat:@"select * from %@ where userid = %@ and id = %d limit 0,1",t_name,userid,[[self valueForKey:idname] intValue]];
  265. //查询数据
  266. FMResultSet *rsl = [db executeQuery:sql];
  267. //遍历结果集
  268. while ( [rsl next] ) {
  269. NSData* dat = [rsl dataForColumn:@"data"];
  270. if( dat != nil && ![dat isKindOfClass:[NSNull class]] )
  271. {
  272. findlocobj = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
  273. }
  274. }
  275. [itlock lock];
  276. [itlock signal];
  277. [itlock unlock];
  278. }];
  279. [itlock lock];
  280. [itlock wait];
  281. [itlock unlock];
  282. return findlocobj;
  283. }
  284. @end
  285. @interface SResBase()
  286. @end
  287. SResBase* g_forShareClient = nil;
  288. @implementation SResBase
  289. + (SResBase*)shareClient
  290. {
  291. static dispatch_once_t predicate;
  292. dispatch_once(&predicate, ^{
  293. g_forShareClient = SResBase.new;
  294. });
  295. return g_forShareClient;
  296. }
  297. - (id)initWithObj:(NSDictionary *)obj
  298. {
  299. self = [super init];
  300. if( self )
  301. {
  302. if( obj == nil )
  303. {
  304. self.msuccess = NO;
  305. self.mmsg = ASLocalizedString(@"无效的数据");
  306. }
  307. else
  308. [self fetchIt:obj];
  309. }
  310. return self;
  311. }
  312. - (void)fetchIt:(NSDictionary *)obj
  313. {
  314. _mcode = [[obj objectForKeyMy:@"status"] intValue];
  315. _msuccess = _mcode == 1;
  316. self.mmsg = [obj objectForKeyMy:@"error"];
  317. if( self.mmsg == nil )
  318. {
  319. if( _msuccess )
  320. self.mmsg = ASLocalizedString(@"操作成功");
  321. else
  322. self.mmsg = ASLocalizedString(@"操作失败");
  323. }
  324. self.mdata = obj;
  325. }
  326. +(SResBase*)infoWithString:(NSString*)str
  327. {
  328. SResBase* retobj = SResBase.new;
  329. retobj.mcode = 0;
  330. retobj.msuccess = NO;
  331. retobj.mmsg = str;
  332. return retobj;
  333. }
  334. +(SResBase*)infoWithOKString:(NSString*)msg
  335. {
  336. SResBase* retobj = SResBase.new;
  337. retobj.mcode = 1;
  338. retobj.msuccess = YES;
  339. retobj.mmsg = msg;
  340. return retobj;
  341. }
  342. + (void)postReq:(NSString*)method ctl:(NSString*)ctl parm:(NSDictionary*)param block:(void(^)(SResBase* resb))block
  343. {
  344. [[NetHttpsManager manager] postMethod:method ctl:ctl param:param successBlock:^(NSDictionary *jsonData) {
  345. if( block )
  346. block( [[SResBase alloc]initWithObj:jsonData] );
  347. } failBlock:^(NSError *error) {
  348. NSLog(@"req failed:%@", error);
  349. block([SResBase infoWithString:ASLocalizedString(@"网络请求失败")]);
  350. }];
  351. }
  352. + (NSDictionary*)postReqSync:(NSString *)method ctl:(NSString *)ctl parm:(NSDictionary *)param
  353. {
  354. NSDictionary *tmpDict = [[NetHttpsManager manager] postSynchMehtod:method ctl:ctl param:param];
  355. return tmpDict;
  356. }
  357. @end
  358. @implementation czModel
  359. + (void)getCZInfo:(void (^)(SResBase* resb,int yue,int rate, NSArray* czItems, NSArray* payItmes))block
  360. {
  361. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  362. [parmDict setObject:VersionNum forKey:@"sdk_version_name"];
  363. [SResBase postReq:@"recharge" ctl:@"pay" parm:parmDict block:^(SResBase *resb) {
  364. if( resb.msuccess )
  365. {
  366. NSArray* c = [resb.mdata objectForKeyMy:@"rule_list"];
  367. NSMutableArray* t = NSMutableArray.new;
  368. for ( NSDictionary* one in c )
  369. {
  370. [t addObject: [[czModel alloc]initWithObj:one]];
  371. }
  372. int k = [[resb.mdata objectForKeyMy:@"show_other"] intValue];
  373. if( k == 1 )
  374. {
  375. czModel * input = czModel.new;
  376. input.mBI = YES;
  377. [t addObject:input];
  378. }
  379. NSArray* p = [resb.mdata objectForKeyMy:@"pay_list"];
  380. NSMutableArray* tt = NSMutableArray.new;
  381. for ( NSDictionary* one in p ) {
  382. [tt addObject: [[payMethodModel alloc]initWithObj:one]];
  383. }
  384. //余额
  385. int yue = [[resb.mdata objectForKeyMy:@"diamonds"] intValue];
  386. //充值金额与钻石的换算比率
  387. int rate = [[resb.mdata objectForKeyMy:@"rate"] intValue];
  388. block( resb, yue,rate,t,tt);
  389. }
  390. else
  391. {
  392. block( resb ,0,0,nil,nil);
  393. }
  394. }];
  395. }
  396. @end
  397. @implementation payMethodModel
  398. - (void)payIt:(czModel*)mode block:(void(^)(SResBase* resb))block
  399. {
  400. NSDictionary* param;
  401. if( mode.mBI )
  402. {
  403. param = @{@"pay_id":@(self.mId),@"money":@(mode.mMoney)};
  404. }
  405. else
  406. {
  407. param = @{@"pay_id":@(self.mId),@"rule_id":@(mode.mId)};
  408. }
  409. [SResBase postReq:@"pay" ctl:@"pay" parm:param block:^(SResBase *resb) {
  410. if( resb.msuccess )
  411. {
  412. NSString* paycode = [[[resb.mdata objectForKeyMy:@"pay"] objectForKeyMy:@"sdk_code"] objectForKeyMy:@"pay_sdk_type"];
  413. if( [paycode isEqualToString:@"alipay"] )
  414. {
  415. NSString* payinfo = [[[[resb.mdata objectForKeyMy:@"pay"] objectForKeyMy:@"sdk_code"] objectForKeyMy:@"config"] objectForKeyMy:@"order_spec"];
  416. NSString* sign = [[[[resb.mdata objectForKeyMy:@"pay"] objectForKeyMy:@"sdk_code"] objectForKeyMy:@"config"] objectForKeyMy:@"sign"];
  417. NSString* sign_type = [[[[resb.mdata objectForKeyMy:@"pay"] objectForKeyMy:@"sdk_code"] objectForKeyMy:@"config"] objectForKeyMy:@"sign_type"];
  418. NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
  419. payinfo, sign, sign_type];
  420. [self alipay:orderString block:block];
  421. }
  422. else if ([paycode isEqualToString:@"wxpay"])
  423. {
  424. NSDictionary* diccfg = [[[resb.mdata objectForKeyMy:@"pay"] objectForKeyMy:@"config"] objectForKeyMy:@"ios"];
  425. // [self wxpay:diccfg block:block];
  426. }
  427. else if ([paycode isEqualToString:@"iappay"])
  428. {
  429. self.httpManager = [NetHttpsManager manager];
  430. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  431. NSDictionary* diccfg = [[resb.mdata objectForKeyMy:@"pay"] objectForKeyMy:@"sdk_code"];
  432. NSDictionary *config = diccfg[@"config"];
  433. self.pro_id = config[@"product_id"];
  434. if ([SKPaymentQueue canMakePayments]) {
  435. [self getProductInfowithprotectId:self.pro_id block:block];
  436. } else {
  437. [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
  438. }
  439. }
  440. else
  441. {
  442. block( [SResBase infoWithString:ASLocalizedString(@"服务器返回不支持的支付方式")]);
  443. }
  444. }
  445. else
  446. {
  447. block( resb );
  448. }
  449. }];
  450. }
  451. #pragma mark -- 苹果内购服务
  452. /**
  453. 苹果内购服务
  454. @param proId ProductId事先在itunesConnect中添加好的,已存在的付费项目,否则查询会失败。
  455. @param block 回调
  456. */
  457. - (void)getProductInfowithprotectId:(NSString *)proId block:(void(^)(SResBase* resb))block
  458. {
  459. // [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在请求Itunes Store请等待")];
  460. self.block = block;
  461. NSMutableArray *proArr = [NSMutableArray new];
  462. [proArr addObject:proId];
  463. NSSet * set = [NSSet setWithArray:proArr];
  464. SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  465. request.delegate = self;
  466. [request start];
  467. NSLog(@"%@",set);
  468. NSLog(ASLocalizedString(@"请求开始请等待..."));
  469. }
  470. #pragma mark - 以上查询的回调函数-------
  471. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  472. {
  473. NSArray *myProduct = response.products;
  474. NSLog(@"%@",myProduct);
  475. if (myProduct.count == 0)
  476. {
  477. SResBase* retobj = nil;
  478. retobj.msuccess = YES;
  479. retobj = [[SResBase alloc]init];
  480. retobj.msuccess = YES;
  481. retobj.mmsg = ASLocalizedString(@"无法获取产品信息,购买失败");
  482. retobj.mcode = 1;
  483. self.block(retobj);
  484. //[FanweMessage alert:ASLocalizedString(@"无法获取产品信息,购买失败。")];
  485. return;
  486. }
  487. else
  488. {
  489. NSLog(@"productID:%@", response.invalidProductIdentifiers);
  490. NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
  491. SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
  492. [[SKPaymentQueue defaultQueue] addPayment:payment];
  493. }
  494. }
  495. #pragma mark - others SKPaymentTransactionObserver
  496. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
  497. {
  498. for (SKPaymentTransaction *transaction in transactions)
  499. {
  500. switch (transaction.transactionState)
  501. {
  502. case SKPaymentTransactionStatePurchased://交易完成
  503. NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
  504. [self completeTransaction:transaction];
  505. break;
  506. case SKPaymentTransactionStateFailed://交易失败
  507. [self failedTransaction:transaction];
  508. break;
  509. case SKPaymentTransactionStateRestored://已经购买过该商品
  510. NSLog(ASLocalizedString(@"已经购买过该产品"));
  511. [self restoreTransaction:transaction];
  512. break;
  513. case SKPaymentTransactionStatePurchasing: //商品添加进列表
  514. NSLog(ASLocalizedString(@"商品添加进列表"));
  515. break;
  516. default:
  517. break;
  518. }
  519. }
  520. }
  521. - (void)completeTransaction:(SKPaymentTransaction *)transaction
  522. {
  523. NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
  524. NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
  525. NSString *base64String = [data base64EncodedStringWithOptions:0];
  526. if ([productIdentifier length] > 0)
  527. {
  528. [self shoppingValidation:base64String block: self.block];
  529. }
  530. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  531. }
  532. #pragma mark -- 向自己的服务器验证购买凭证01
  533. - (void)shoppingValidation : (NSString *)base64Str block:(void(^)(SResBase* resb))block
  534. {
  535. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
  536. [dict setObject:@"pay" forKey:@"ctl"];
  537. [dict setObject:@"iappay" forKey:@"act"];
  538. NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
  539. [dict setObject:userid forKey:@"user_id"];
  540. [dict setObject:base64Str forKey:@"receipt-data"];
  541. [self.httpManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  542. SResBase* retobj = nil;
  543. retobj.msuccess = YES;
  544. retobj = [[SResBase alloc]init];
  545. retobj.msuccess = YES;
  546. retobj.mmsg = ASLocalizedString(@"支付成功");
  547. retobj.mcode = 1;
  548. block(retobj);
  549. } FailureBlock:^(NSError *error) {
  550. }];
  551. }
  552. - (void)failedTransaction:(SKPaymentTransaction *)transaction
  553. {
  554. if(transaction.error.code != SKErrorPaymentCancelled)
  555. {
  556. NSLog(ASLocalizedString(@"购买失败"));
  557. }
  558. else
  559. {
  560. SResBase* retobj = nil;
  561. retobj.msuccess = YES;
  562. retobj = [[SResBase alloc]init];
  563. retobj.msuccess = YES;
  564. retobj.mmsg = ASLocalizedString(@"您已取消交易");
  565. self.block(retobj);
  566. }
  567. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  568. }
  569. - (void)restoreTransaction:(SKPaymentTransaction *)transaction
  570. {
  571. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  572. }
  573. //- (void)wxpay:(NSDictionary*)wxpaycfg block:(void(^)(SResBase* resb))block
  574. //{
  575. // [SResBase shareClient].mg_pay_block = ^(SResBase* resb){
  576. //
  577. // block( resb );
  578. //
  579. // [SResBase shareClient].mg_pay_block = nil;
  580. //
  581. // };
  582. //
  583. // //微信支付
  584. // NSDictionary *configDic = wxpaycfg;
  585. // Mwxpay * wxmodel =[Mwxpay mj_objectWithKeyValues: configDic];
  586. // PayReq* req = [[PayReq alloc] init];
  587. // req.openID = wxmodel.appid;
  588. // req.partnerId = wxmodel.partnerid;
  589. // req.prepayId = wxmodel.prepayid;
  590. // req.nonceStr = wxmodel.noncestr;
  591. // req.timeStamp = [wxmodel.timestamp intValue];
  592. // req.package = wxmodel.package;
  593. // req.sign = wxmodel.sign;
  594. //
  595. // [WXApi sendReq:req completion:^(BOOL success) {
  596. // if (!success) {
  597. // [SResBase shareClient].mg_pay_block = nil;
  598. // }
  599. // }];
  600. //
  601. //}
  602. - (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
  603. {
  604. }
  605. @end
  606. NSMutableArray* g_historysearch = nil;
  607. @implementation musiceModel
  608. {
  609. id _context;
  610. BOOL _downloading;//0 普通,1下载中..
  611. NSProgress* _progress;
  612. }
  613. #define DIR_MUSIC @"music"
  614. #define T_MUSIC_NAME @"music_list"
  615. - (void)dealloc
  616. {
  617. if( _downloading )
  618. {
  619. NSLog(@"music cancel ...");
  620. [_progress cancel];
  621. }
  622. else
  623. {
  624. }
  625. }
  626. /**
  627. 完整的音乐文件地址
  628. @return 返回值
  629. */
  630. - (NSString *)getFullFilePath
  631. {
  632. // NSString *str = [NSString stringWithFormat:@"/Documents/%@/%@",DIR_MUSIC,self.mFilePath];
  633. // if (str && ![str isEqualToString:@""]) {
  634. // return [NSHomeDirectory() stringByAppendingString:str];
  635. // }
  636. return @"";
  637. }
  638. - (NSString*)getTimeLongStr
  639. {
  640. return @"";
  641. // if( _mTime_len == 0 ) return @"";
  642. // return [NSString stringWithFormat:@"%02d:%02d", _mTime_len/60,_mTime_len%60];
  643. }
  644. // 从我的歌曲列表里面删除这个
  645. - (void)delThis:(void(^)(SResBase* resb))block
  646. {
  647. [SResBase postReq:@"del_music" ctl:@"music" parm:@{@"audio_id":_id} block:^(SResBase *resb) {
  648. if( resb.msuccess )
  649. {
  650. if( _downloading )
  651. {//如果正在下载就停止
  652. self.mmDelegate = nil;
  653. _context = nil;
  654. [_progress cancel];
  655. }
  656. [self removeMuiscFile:block];
  657. }
  658. else
  659. block( resb );
  660. }];
  661. }
  662. // 清除本地歌曲缓存,不会删除列表,情理缓存的时候使用
  663. + (BOOL)clearLocalSave
  664. {
  665. //删除数据库
  666. [SAutoEx clearDataForKey:T_MUSIC_NAME block:nil];
  667. //删除本地文件
  668. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  669. documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:DIR_MUSIC isDirectory:YES];
  670. return [[NSFileManager defaultManager]removeItemAtURL:documentsDirectoryURL error:nil];
  671. }
  672. //返回本地文件的总大小,,in byte
  673. + (int)getLocatDataSize
  674. {
  675. NSFileManager* manager = [NSFileManager defaultManager];
  676. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  677. documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:DIR_MUSIC isDirectory:YES];
  678. if (![manager fileExistsAtPath:[documentsDirectoryURL path]]) return 0;
  679. NSArray* all = [manager contentsOfDirectoryAtPath:[documentsDirectoryURL path] error:nil];
  680. int filesize = 0;
  681. for ( NSString* one in all )
  682. {
  683. filesize += [[[manager attributesOfItemAtPath:[[documentsDirectoryURL URLByAppendingPathComponent:one] path] error:nil] objectForKey:NSFileSize] intValue];
  684. }
  685. return filesize;
  686. }
  687. - (void)fetchBeforDownload:(void(^)(SResBase* resb))block
  688. {
  689. // [SResBase postReq:@"downurl" ctl:@"music" parm:@{@"audio_id":self.mAudio_id} block:^(SResBase *resb) {
  690. //
  691. // if( resb.msuccess )
  692. // {
  693. // if( self.mAudio_link.length == 0 )
  694. // self.mAudio_link = [[resb.mdata objectForKeyMy:@"audio"] objectForKeyMy:@"audio_link"];
  695. // if( self.mLrc_content.length == 0 )
  696. // self.mLrc_content = [[resb.mdata objectForKeyMy:@"audio"] objectForKeyMy:@"lrc_content"];
  697. // }
  698. // block( resb );
  699. //
  700. // }];
  701. }
  702. - (void)startDonwLoad:(id)context
  703. {
  704. if( _downloading ) return;
  705. _downloading = YES;
  706. _context = context;
  707. self.mmDownloadInfo = ASLocalizedString(@"下载中");
  708. self.mmFileStatus = 2;
  709. [self callback];
  710. [self fetchBeforDownload:^(SResBase *resb) {
  711. [self realDownLoadFile];
  712. }];
  713. }
  714. - (void)realDownLoadFile
  715. {
  716. #define test_down 0
  717. #if test_down
  718. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  719. _progress = [NSProgress progressWithTotalUnitCount:100];
  720. [_progress addObserver:self
  721. forKeyPath:@"fractionCompleted"
  722. options:NSKeyValueObservingOptionNew
  723. context:NULL];
  724. for ( int j = 0; j < 100; j++) {
  725. _progress.completedUnitCount = j;
  726. [NSThread sleepForTimeInterval:1];
  727. }
  728. self.mFilePath = @"test";
  729. [self saveMuiscFile];
  730. });
  731. #else
  732. // if( self.mAudio_link.length == 0 )
  733. // {
  734. // [self stopDownLoad:ASLocalizedString(@"下载地址错误")bsuccess:NO];
  735. // return;
  736. // }
  737. //
  738. // //默认会话模式
  739. // NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  740. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  741. //
  742. // manager.completionQueue = dispatch_get_global_queue(0, 0);//回掉不要在主线程执行,方便继续下载歌词
  743. //
  744. // NSURL *URL = [NSURL URLWithString:self.mAudio_link];
  745. // NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  746. //
  747. // NSLog(ASLocalizedString(@"~~~~~~~~~~~~~~音乐下载地址:%@"),self.mAudio_link);
  748. // NSLog(@"eqew=============id ========%@",self.mAudio_id)
  749. //
  750. // NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
  751. //
  752. // //下载进度 监听
  753. // if( _progress == nil )
  754. // {
  755. // _progress = downloadProgress;
  756. //
  757. // [_progress addObserver:self
  758. // forKeyPath:@"fractionCompleted"
  759. // options:NSKeyValueObservingOptionNew
  760. // context:NULL];
  761. // }
  762. //
  763. // }destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  764. //
  765. // NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
  766. // //DIR_MUSIC @"music" 下载文件存 的路径
  767. // documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:DIR_MUSIC isDirectory:YES];
  768. //
  769. // NSError* errror = nil;
  770. //
  771. // //目录不存在,并且又创建失败...
  772. // if(![[NSFileManager defaultManager]fileExistsAtPath:[documentsDirectoryURL path]] &&
  773. // ![[NSFileManager defaultManager] createDirectoryAtURL:documentsDirectoryURL withIntermediateDirectories:YES attributes:nil error:&errror]
  774. // )
  775. // {
  776. //
  777. // return nil;
  778. // }
  779. // else
  780. // {
  781. // NSString *musicIDStr;
  782. // self.mAudio_id = [NSString stringWithFormat:@"%@",self.mAudio_id];
  783. // if ([self.mAudio_id containsString:@"&"]) {
  784. // musicIDStr = [[self.mAudio_id componentsSeparatedByString:@"&"] firstObject];
  785. // }else{
  786. // musicIDStr = self.mAudio_id ;
  787. // }
  788. // NSString* sss = [NSString stringWithFormat:@"music_%@_%@.mp3",[IMAPlatform sharedInstance].host.imUserId,musicIDStr];
  789. // return [documentsDirectoryURL URLByAppendingPathComponent:sss];
  790. // }
  791. //
  792. // }completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  793. //
  794. // if(filePath)
  795. // {
  796. // // lastPathComponent 获取文件路径
  797. // self.mFilePath = [filePath lastPathComponent];
  798. // //保存
  799. // [self saveMuiscFile];
  800. // }
  801. // else
  802. // {
  803. // [self delThis:^(SResBase *resb)
  804. // {
  805. // }];
  806. // // [self stopDownLoad:ASLocalizedString(@"下载文件歌曲失败")bsuccess:NO];
  807. // dispatch_async(dispatch_get_main_queue(), ^{
  808. // kNotifPost(@"musicDownFileFailure", @{@"musicName":self.mAudio_name});
  809. // });
  810. // self.mFilePath = @"";
  811. // }
  812. //
  813. // [_progress removeObserver:self forKeyPath:@"fractionCompleted"];
  814. // _progress = nil;
  815. // }];
  816. //
  817. // [downloadTask resume];
  818. #endif
  819. }
  820. //完整的音乐文件地址
  821. - (NSString*)getFullFilePath:(NSString *)filePathStr
  822. {
  823. NSString *str = [NSString stringWithFormat:@"/Documents/%@/%@",@"music",filePathStr];
  824. if (str && ![str isEqualToString:@""])
  825. {
  826. return [NSHomeDirectory() stringByAppendingString:str];
  827. }
  828. return NSHomeDirectory();
  829. }
  830. - (NSInteger)fileSizeAtPath:(NSString*)filePath
  831. {
  832. NSFileManager* manager = [NSFileManager defaultManager];
  833. if ([manager fileExistsAtPath:filePath])
  834. {
  835. return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
  836. }
  837. return 0;
  838. }
  839. - (void)removeMuiscFile:(void(^)(SResBase* resb))block
  840. {
  841. //删除文件
  842. [[NSFileManager defaultManager]removeItemAtPath:[self getFullFilePath] error:nil];
  843. //删除数据库记录
  844. [SResBase removeListData:@[self] idname:@"mAudio_id" forKey:T_MUSIC_NAME block:block];
  845. }
  846. - (void)addThisToMyList:(void(^)(SResBase* resb))block
  847. {
  848. // NSDictionary* param = @{
  849. // @"audio_link":self.mAudio_link?self.mAudio_link:@"",
  850. // @"lrc_content":self.mLrc_content?self.mLrc_content:@"",
  851. // @"audio_name":self.mAudio_name?self.mAudio_name:@"",
  852. // @"artist_name":self.mArtist_name?self.mArtist_name:@"",
  853. // @"time_len":@(self.mTime_len),
  854. // @"audio_id":self.mAudio_id };
  855. // [SResBase postReq:@"add_music" ctl:@"music" parm:param block:block];
  856. }
  857. - (void)saveMuiscFile
  858. {
  859. #if test_down
  860. [self stopDownLoad:ASLocalizedString(@"下载完成")bsuccess:YES];
  861. #else
  862. self.mmFileStatus = 1;//1 已经下载
  863. [SResBase addListData:@[self] idname:@"mAudio_id" forKey:T_MUSIC_NAME block:^(SResBase *resb) {
  864. if( !resb.msuccess )
  865. {
  866. [self stopDownLoad:ASLocalizedString(@"保存歌曲失败")bsuccess:NO];
  867. }
  868. else
  869. [self stopDownLoad:ASLocalizedString(@"下载完成")bsuccess:YES];
  870. }];
  871. #endif
  872. }
  873. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context
  874. {
  875. if( object == _progress )
  876. {
  877. _mmPecent = _progress.fractionCompleted * 100;
  878. [self callback];
  879. return;
  880. }
  881. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  882. }
  883. - (void)callback
  884. {
  885. dispatch_async(dispatch_get_main_queue(), ^{
  886. if( self.mmDelegate && [self.mmDelegate respondsToSelector:@selector(musicDownloading:context:needstop:)] )
  887. {
  888. BOOL needstop = NO;
  889. [self.mmDelegate musicDownloading:self context:_context needstop:&needstop];
  890. if( needstop && _downloading )
  891. {
  892. [_progress cancel];
  893. }
  894. }
  895. });
  896. }
  897. - (void)stopDownLoad:(NSString*)str bsuccess:(BOOL)bsuccess
  898. {
  899. if( bsuccess )
  900. [self addThisToMyList:nil];//下载完成就添加一次
  901. self.mmDownloadInfo = str;
  902. self.mmFileStatus = bsuccess?1:0;
  903. [self callback];
  904. _downloading = NO;
  905. }
  906. #define KEY_HIS @"music_search"
  907. #define MAX_KEYS 20
  908. + (NSArray*)getSearchHistory
  909. {
  910. if( g_historysearch == nil )
  911. {
  912. g_historysearch = NSMutableArray.new;
  913. NSArray* t = [SAutoEx loadDataWithKey:KEY_HIS];
  914. if( t )
  915. [g_historysearch addObjectsFromArray: t];
  916. }
  917. return g_historysearch;
  918. }
  919. + (void)addHistory:(NSString*)key
  920. {
  921. if( key == nil ) return;
  922. NSMutableArray* ta = (NSMutableArray*) [self getSearchHistory];
  923. NSInteger i = [ta indexOfObject:key];
  924. if( i != NSNotFound )
  925. {//如果有搜索过这个,就提前,表明最近搜索
  926. [ta removeObjectAtIndex:i];
  927. [ta insertObject:key atIndex:0];
  928. }
  929. else
  930. {//如果没有,就加入
  931. if( ta.count > MAX_KEYS )
  932. [ta removeLastObject];
  933. [ta insertObject:key atIndex:0];
  934. }
  935. [SAutoEx saveData:ta forkey:KEY_HIS];
  936. }
  937. + (void)cleanHistory
  938. {
  939. [g_historysearch removeAllObjects];
  940. [SAutoEx saveData:g_historysearch forkey:KEY_HIS];
  941. }
  942. + (void)deleteHistory:(NSInteger )indexPath
  943. {
  944. [g_historysearch removeObjectAtIndex:indexPath ];
  945. [SAutoEx saveData:g_historysearch forkey:KEY_HIS];
  946. }
  947. //获取我的音乐列表
  948. + (void)getMyMusicList:(int)page search:(NSString*)search block:(void(^)(SResBase* resb, NSArray* all ))block
  949. {
  950. #if test_down
  951. NSMutableArray* t = NSMutableArray.new;
  952. musiceModel*bbbb = musiceModel.new;
  953. bbbb.mAudio_name = ASLocalizedString(@"什么歌曲");
  954. bbbb.mArtist_name = ASLocalizedString(@"什么作者");
  955. bbbb.mTime_len = 75;
  956. bbbb.mmFileStatus = 1;
  957. [t addObject:bbbb];
  958. musiceModel*aaaa = musiceModel.new;
  959. aaaa.mAudio_name = ASLocalizedString(@"什么歌曲2");
  960. aaaa.mArtist_name = ASLocalizedString(@"什么作者2");
  961. aaaa.mTime_len = 45;
  962. aaaa.mmFileStatus = 0;
  963. [t addObject:aaaa];
  964. block( [SResBase infoWithOKString:ASLocalizedString(@"加载成功")] , t);
  965. #else
  966. [SResBase postReq:@"get_music_list" ctl:@"app" parm:@{@"page":@(page),@"title":SafeStr(search)} block:^(SResBase *resb) {
  967. if( resb.msuccess )
  968. {
  969. NSMutableArray* t = NSMutableArray.new;
  970. NSArray* ta = [resb.mdata objectForKeyMy:@"list"];
  971. for (NSDictionary* one in ta ) {
  972. [t addObject: [musiceModel modelWithJSON:one]];
  973. }
  974. block( resb , t );
  975. // [musiceModel fetchFuckDownloadURL:t surehave:NO block:block];
  976. }
  977. else block( resb , nil );
  978. }];
  979. #endif
  980. }
  981. //搜索歌曲
  982. + (void)searchMuisc:(NSString*)keywords page:(int)page block:(void(^)(SResBase* resb, NSArray* all ))block
  983. {
  984. if ([BGUtils isBlankString:keywords]) {
  985. keywords = @"";
  986. }
  987. [self addHistory:keywords];
  988. [SResBase postReq:@"search" ctl:@"music" parm:@{ @"keyword":keywords,@"p":@(page)} block:^(SResBase *resb) {
  989. if( resb.msuccess )
  990. {
  991. NSArray* a = [resb.mdata objectForKeyMy:@"list"];
  992. NSMutableArray* t = NSMutableArray.new;
  993. for ( NSDictionary* one in a ) {
  994. musiceModel *model = [[musiceModel alloc]initWithObj:one];
  995. // model.mAudio_link = @"http://blog.anbig.com/wp-content/uploads/2016/01/6f0d95443616e1f77983da81afacd781.mp3";
  996. [t addObject: model];
  997. }
  998. [musiceModel fetchFuckDownloadURL:t surehave:NO block:block];
  999. }
  1000. else
  1001. {
  1002. block( resb, nil);
  1003. }
  1004. }];
  1005. }
  1006. //所有返回的列表数据,都经过这个函数,做些处理,,比如下载她的地址,匹配是否本地有,等等,
  1007. //surehave 表示是否确定本地有,如果不确定,讲会进行本地搜索匹配
  1008. + (void)fetchFuckDownloadURL:(NSArray*)allmodes surehave:(BOOL)surehave block:(void(^)(SResBase* resb, NSArray* all ))block
  1009. {
  1010. // dispatch_async( dispatch_get_global_queue(0, 0), ^{
  1011. //
  1012. // for ( int j = 0 ; j < allmodes.count; j ++ )
  1013. // {
  1014. // musiceModel* one = allmodes[j];
  1015. //
  1016. // //如果每次访问网络,非常卡,这个只有下载歌曲的时候再做吧,,,
  1017. // if((NO) && (one.mAudio_link.length == 0 || one.mLrc_content.length == 0) )
  1018. // {
  1019. // //@"downurl"
  1020. // NSDictionary* ret = [SResBase postReqSync:@"downurl" ctl:@"music" parm:@{@"audio_id":one.mAudio_id}];
  1021. //
  1022. // if( ret )
  1023. // {
  1024. // SResBase* ttt = [[SResBase alloc]initWithObj:ret];
  1025. // if( ttt .msuccess )
  1026. // {
  1027. //
  1028. // if( one.mAudio_link.length == 0 )
  1029. // one.mAudio_link = [[ttt.mdata objectForKeyMy:@"audio"] objectForKeyMy:@"audio_link"];
  1030. // NSLog(@">>>>>>>>>>>>>>>%@",one.mAudio_link);
  1031. // if( one.mLrc_content.length == 0 )
  1032. // one.mLrc_content = [[ttt.mdata objectForKeyMy:@"audio"] objectForKeyMy:@"lrc_content"];
  1033. // }
  1034. // else
  1035. // {
  1036. // dispatch_async(dispatch_get_main_queue(), ^{
  1037. //
  1038. // block( ttt , nil );
  1039. //
  1040. // });
  1041. // return ;
  1042. // }
  1043. // }
  1044. // else
  1045. // {
  1046. // dispatch_async(dispatch_get_main_queue(), ^{
  1047. //
  1048. // block( [SResBase infoWithString:ASLocalizedString(@"获取歌曲地址失败")] , nil );
  1049. // });
  1050. // return ;
  1051. // }
  1052. // }
  1053. //
  1054. // ///匹配本地是否有
  1055. // if( surehave )
  1056. // one.mmFileStatus = 1;
  1057. // else
  1058. // {//查找数据库,是否有这个歌曲
  1059. // musiceModel* locobj = [one haveThisObjInLocalForKey:T_MUSIC_NAME idname:@"mAudio_id"];
  1060. // if( locobj )
  1061. // {
  1062. // one.mmFileStatus = 1;
  1063. // one.mFilePath = locobj.mFilePath;
  1064. // if( one.mLrc_content.length == 0 )
  1065. // one.mLrc_content = locobj.mLrc_content;
  1066. // }
  1067. // else
  1068. // {
  1069. // one.mmFileStatus = 0;
  1070. // }
  1071. // }
  1072. // }
  1073. //
  1074. // dispatch_async(dispatch_get_main_queue(), ^{
  1075. //
  1076. // block( [SResBase infoWithOKString:ASLocalizedString(@"获取歌曲成功")] , allmodes);
  1077. // });
  1078. // });
  1079. }
  1080. // 获取本地音乐
  1081. + (void)getLocalMusic:(int)page block:(void(^)(SResBase* resb, NSArray* all ))block
  1082. {
  1083. [SResBase getListData:page forKey:T_MUSIC_NAME block:^(SResBase *resb, NSArray *all) {
  1084. if( resb.msuccess )
  1085. {
  1086. [musiceModel fetchFuckDownloadURL:all surehave:YES block:block];
  1087. }
  1088. else
  1089. block( resb ,all );
  1090. }];
  1091. }
  1092. @end