YBVideoAddGoodsVC.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. //
  2. // YBVideoAddGoodsVC.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/28.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBVideoAddGoodsVC.h"
  9. #import <Qiniu/QiniuSDK.h>
  10. #import "UIView+LBExtension.h"
  11. #import "YBStorageObj.h"
  12. @interface YBVideoAddGoodsVC ()<TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate>{
  13. UITextView *linkT;
  14. UITextView *nameT;
  15. UITextView *yuanjiaT;
  16. UITextView *xianjiaT;
  17. UITextView *contentT;
  18. UIButton *shopImgBtn;
  19. UIImage *selectImg;
  20. UIButton *addBtn;
  21. NSMutableArray *btnArray;
  22. UIView *moveLineView;
  23. UIScrollView *backScroll;
  24. UIScrollView *wxScroll;
  25. UIButton *wxAddBtn;
  26. UITextView *idT;
  27. UITextView *nameT2;
  28. UITextView *yuanjiaT2;
  29. UITextView *xianjiaT2;
  30. UITextView *contentT2;
  31. UIImageView *goodsThumbImgV;
  32. NSString *addType;
  33. NSDictionary *wxGoodsDic;
  34. }
  35. @end
  36. @implementation YBVideoAddGoodsVC
  37. - (void)ChangeBtnBackground{
  38. if (linkT.text.length > 0 && nameT.text.length > 0 && xianjiaT.text.length > 0 && contentT.text.length > 0 && (selectImg||![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"thumb"])])) {
  39. addBtn.alpha = 1;
  40. addBtn.userInteractionEnabled = YES;
  41. }else{
  42. addBtn.alpha = 0.5;
  43. addBtn.userInteractionEnabled = NO;
  44. }
  45. }
  46. - (void)topBtnClick:(UIButton *)sender{
  47. if (sender.selected) {
  48. return;
  49. }
  50. [UIView animateWithDuration:0.3 animations:^{
  51. moveLineView.centerX = sender.centerX;
  52. }];
  53. for (UIButton *btn in btnArray) {
  54. if (sender == btn) {
  55. btn.selected = YES;
  56. }else{
  57. btn.selected = NO;
  58. }
  59. }
  60. if (sender.tag == 1000) {
  61. addType = @"0";
  62. backScroll.hidden = NO;
  63. wxScroll.hidden = YES;
  64. }else{
  65. addType = @"1";
  66. backScroll.hidden = YES;
  67. wxScroll.hidden = NO;
  68. }
  69. }
  70. - (void)creatNaviIteam{
  71. btnArray = [NSMutableArray array];
  72. NSArray *array = @[YZMsg(@"添加商品")];//,@"商城小程序"
  73. for (int i = 0; i < array.count; i ++) {
  74. UIButton *btn = [UIButton buttonWithType:0];
  75. btn.frame = CGRectMake(_window_width/2-130/2 + i *130, 24+statusbarHeight, 130, 40);
  76. [btn setTitle:array[i] forState:0];
  77. [btn setTitleColor:RGB_COLOR(@"#777777", 1) forState:0];
  78. [btn setTitleColor:RGB_COLOR(@"#ffffff", 1) forState:UIControlStateSelected];
  79. btn.titleLabel.font = SYS_Font(16);
  80. [btn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  81. btn.tag = 1000 + i;
  82. [self.naviView addSubview:btn];
  83. [btnArray addObject:btn];
  84. if (i == 0) {
  85. btn.selected = YES;
  86. moveLineView = [[UIView alloc]initWithFrame:CGRectMake(btn.centerX-7.5, btn.bottom-5, 15, 3)];
  87. moveLineView.backgroundColor = [UIColor clearColor];
  88. moveLineView.layer.cornerRadius =1;
  89. moveLineView.layer.masksToBounds = YES;
  90. [self.naviView addSubview:moveLineView];
  91. }
  92. }
  93. }
  94. - (void)viewDidLoad {
  95. [super viewDidLoad];
  96. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selfTapClick)];
  97. [self.view addGestureRecognizer:tap];
  98. addType = @"0";
  99. [self creatNaviIteam];
  100. [self creatUI];
  101. [self creatWXScroll];
  102. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeBtnBackground) name:UITextViewTextDidChangeNotification object:nil];
  103. //增加监听,当键盘出现或改变时收出消息
  104. [[NSNotificationCenter defaultCenter] addObserver:self
  105. selector:@selector(keyboardWillShow:)
  106. name:UIKeyboardWillShowNotification
  107. object:nil];
  108. [[NSNotificationCenter defaultCenter] addObserver:self
  109. selector:@selector(keyboardWillHide:)
  110. name:UIKeyboardWillHideNotification
  111. object:nil];
  112. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:nil];
  113. if (_goodsInfo) {
  114. NSString *typestr = minstr([_goodsInfo valueForKey:@"type"]);
  115. if ([typestr isEqual:@"1"]) {
  116. UIButton *selBtn = (UIButton *)[self.naviView viewWithTag:1001];
  117. [self topBtnClick:selBtn];
  118. idT.text = @"239";//minstr([_goodsInfo valueForKey:@"id"]);
  119. [self getGoodsMsgBtnClick];
  120. }
  121. }
  122. [self ChangeBtnBackground];
  123. }
  124. - (void)dealloc{
  125. [[NSNotificationCenter defaultCenter]removeObserver:self];
  126. }
  127. - (void)creatUI{
  128. backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight)];
  129. [self.view addSubview:backScroll];
  130. NSArray *array = @[YZMsg(@"*商品链接(可粘贴淘宝店铺商品链接)"),YZMsg(@"*商品名称(最多可输入20个字)"),YZMsg(@"原价(元)"),YZMsg(@"*现价(元)"),YZMsg(@"*商品简介(最多可输入50个字)"),YZMsg(@"*商品图片")];
  131. CGFloat yyyy = 0.0;
  132. for (int i = 0; i < array.count; i ++) {
  133. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, yyyy, _window_width, 30)];
  134. [backScroll addSubview:view];
  135. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, view.width-20, view.height)];
  136. label.textColor = RGB_COLOR(@"#969696", 1);
  137. label.font = [UIFont systemFontOfSize:12];
  138. [view addSubview:label];
  139. label.attributedText = [self setAttStr:array[i]];
  140. UIView *textView = [[UIView alloc]initWithFrame:CGRectMake(0, view.bottom, _window_width, 40)];
  141. textView.backgroundColor = CellRow_Cor;//RGB_COLOR(@"#FAFAFA", 1);
  142. if (i == 4) {
  143. textView.height = 70;
  144. }
  145. if (i == 5) {
  146. textView.height = 120;
  147. }
  148. [backScroll addSubview:textView];
  149. if (i != 5) {
  150. UITextView *textT = [[UITextView alloc]initWithFrame:CGRectMake(20, 0, textView.width-40, 40)];
  151. textT.font = [UIFont systemFontOfSize:13];
  152. textT.textColor = RGB_COLOR(@"#969696", 1);
  153. textT.backgroundColor = [UIColor clearColor];
  154. textT.delegate = self;
  155. [textView addSubview:textT];
  156. switch (i) {
  157. case 0:
  158. linkT = textT;
  159. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"href"])]) {
  160. linkT.text = minstr([_goodsInfo valueForKey:@"href"]);
  161. }
  162. break;
  163. case 1:
  164. nameT = textT;
  165. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"name"])]) {
  166. nameT.text = minstr([_goodsInfo valueForKey:@"name"]);
  167. }
  168. break;
  169. case 2:
  170. yuanjiaT = textT;
  171. if ([_goodsInfo valueForKey:@"old_price"]) {
  172. yuanjiaT.text = minstr([_goodsInfo valueForKey:@"old_price"]);
  173. }
  174. break;
  175. case 3:
  176. xianjiaT = textT;
  177. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"price"])]) {
  178. xianjiaT.text = minstr([_goodsInfo valueForKey:@"price"]);
  179. }
  180. break;
  181. case 4:
  182. contentT = textT;
  183. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"des"])]) {
  184. contentT.text = minstr([_goodsInfo valueForKey:@"des"]);
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }else{
  191. textView.backgroundColor = [UIColor clearColor];
  192. shopImgBtn = [UIButton buttonWithType:0];
  193. shopImgBtn.frame = CGRectMake(15, 0, 90, 120);
  194. [shopImgBtn setImage:[UIImage imageNamed:@"video_shop_add"] forState:0];
  195. //[shopImgBtn setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.03]];
  196. [shopImgBtn addTarget:self action:@selector(shopImgBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  197. shopImgBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
  198. [textView addSubview:shopImgBtn];
  199. if ([_goodsInfo allKeys].count>0) {
  200. if ([[_goodsInfo valueForKey:@"thumb"] isKindOfClass:[UIImage class]]) {
  201. selectImg = [_goodsInfo valueForKey:@"thumb"];
  202. [shopImgBtn setImage:selectImg forState:0];
  203. }else {
  204. [shopImgBtn sd_setImageWithURL:[NSURL URLWithString:minstr([_goodsInfo valueForKey:@"thumb"])] forState:0];
  205. }
  206. }
  207. }
  208. yyyy = textView.bottom;
  209. }
  210. addBtn = [UIButton buttonWithType:0];
  211. addBtn.frame = CGRectMake(15, yyyy + 20, _window_width-30, 40);
  212. [addBtn setBackgroundColor:Pink_Cor];
  213. addBtn.alpha = 0.5;
  214. addBtn.userInteractionEnabled = NO;
  215. [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  216. [addBtn setTitle:YZMsg(@"确认商品信息") forState:0];
  217. addBtn.layer.cornerRadius = 5;
  218. addBtn.layer.masksToBounds = YES;
  219. addBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  220. [backScroll addSubview:addBtn];
  221. backScroll.contentSize = CGSizeMake(0, addBtn.bottom);
  222. }
  223. -(void)textViewEditChanged:(NSNotification *)noti {
  224. UITextView *changeTextView = (UITextView *)noti.object;
  225. NSString *toBeString = changeTextView.text;
  226. NSString *lang = [[[UITextInputMode activeInputModes]firstObject] primaryLanguage]; // 键盘输入模式
  227. if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
  228. UITextRange *selectedRange = [changeTextView markedTextRange];//获取高亮部分
  229. UITextPosition *position = [changeTextView positionFromPosition:selectedRange.start offset:0];
  230. //没有高亮选择的字,则对已输入的文字进行字数统计和限制
  231. if (!position) {
  232. if (changeTextView == nameT) {
  233. if (toBeString.length > 20) {
  234. changeTextView.text = [toBeString substringToIndex:20];
  235. [MBProgressHUD showPop:YZMsg(@"最多可输入20个字")];
  236. }
  237. }
  238. if (changeTextView == contentT) {
  239. if (toBeString.length > 50) {
  240. changeTextView.text = [toBeString substringToIndex:50];
  241. [MBProgressHUD showPop:YZMsg(@"最多可输入50个字")];
  242. }
  243. }
  244. }else{
  245. //有高亮选择的字符串,则暂不对文字进行统计和限制
  246. }
  247. }else{
  248. // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
  249. if (changeTextView == nameT) {
  250. if (toBeString.length > 20) {
  251. changeTextView.text = [toBeString substringToIndex:20];
  252. [MBProgressHUD showPop:YZMsg(@"最多可输入20个字")];
  253. }
  254. }
  255. if (changeTextView == contentT) {
  256. if (toBeString.length > 50) {
  257. changeTextView.text = [toBeString substringToIndex:50];
  258. [MBProgressHUD showPop:YZMsg(@"最多可输入50个字")];
  259. }
  260. }
  261. }
  262. }
  263. //- (void)textViewDidChange:(UITextView *)textView; {
  264. // if (textView == nameT) {
  265. // if (textView.text.length > 20) {
  266. // textView.text = [textView.text substringToIndex:20];
  267. // [MBProgressHUD showPop:YZMsg(@"最多可输入20个字")];
  268. // }
  269. // }
  270. // if (textView == contentT) {
  271. // if (textView.text.length > 50) {
  272. // textView.text = [textView.text substringToIndex:50];
  273. // [MBProgressHUD showPop:YZMsg(@"最多可输入50个字")];
  274. // }
  275. // }
  276. //}
  277. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;{
  278. if (textView == xianjiaT || textView == yuanjiaT) {
  279. //新输入的
  280. if (text.length == 0) {
  281. return YES;
  282. }
  283. NSString *checkStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
  284. //正则表达式(只支持两位小数)
  285. NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";;
  286. //判断新的文本内容是否符合要求
  287. return [self isValid:checkStr withRegex:regex];
  288. }
  289. return YES;
  290. }
  291. - (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex {
  292. NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
  293. return [predicte evaluateWithObject:checkStr];
  294. }
  295. - (void)creatWXScroll{
  296. wxScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight)];
  297. wxScroll.hidden = YES;
  298. [self.view addSubview:wxScroll];
  299. NSArray *array = @[YZMsg(@"商品ID(输入小程序中商品对应ID,将自动获取商品信息)"),YZMsg(@"商品名称"),YZMsg(@"原价"),YZMsg(@"现价"),YZMsg(@"商品简介"),YZMsg(@"商品图片")];
  300. CGFloat yyyy = 0.0;
  301. for (int i = 0; i < array.count; i ++) {
  302. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, yyyy, _window_width, 30)];
  303. [wxScroll addSubview:view];
  304. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, view.width-20, view.height)];
  305. label.textColor = RGB_COLOR(@"#969696", 1);
  306. label.font = [UIFont systemFontOfSize:12];
  307. [view addSubview:label];
  308. UIView *textView = [[UIView alloc]initWithFrame:CGRectMake(0, view.bottom, _window_width, 40)];
  309. if (i == 0) {
  310. textView.height = 60;
  311. }
  312. if (i == 4) {
  313. textView.height = 80;
  314. }
  315. if (i == 5) {
  316. textView.height = 90;
  317. }
  318. [wxScroll addSubview:textView];
  319. if (i == 0) {
  320. //label.attributedText = [self setWXAttStr:array[i]];
  321. label.text = array[i];
  322. textView.userInteractionEnabled = YES;
  323. textView.backgroundColor = CellRow_Cor;
  324. }else{
  325. label.text = array[i];
  326. textView.userInteractionEnabled = NO;
  327. textView.backgroundColor = CellRow_Cor;//RGB_COLOR(@"#FAFAFA", 1);
  328. }
  329. if (i != 5) {
  330. UITextView *textT = [[UITextView alloc]initWithFrame:CGRectMake(20, 0, textView.width-40, 40)];
  331. textT.font = [UIFont systemFontOfSize:13];
  332. textT.textColor = RGB_COLOR(@"#969696", 1);
  333. textT.backgroundColor = [UIColor clearColor];
  334. textT.delegate = self;
  335. [textView addSubview:textT];
  336. if (i == 0) {
  337. textT.top = 5;
  338. textT.backgroundColor = RGB_COLOR(@"#ffffff", 0.03);
  339. textT.width = textView.width-150;
  340. // textT.layer.borderWidth = 1;
  341. // textT.layer.borderColor = RGB_COLOR(@"#eeeeee", 1).CGColor;
  342. UIButton *getGoodsMsgBtn = [UIButton buttonWithType:0];
  343. getGoodsMsgBtn.frame = CGRectMake(textT.right + 10, textT.top+5, 100, 30);
  344. [getGoodsMsgBtn setBackgroundColor:Pink_Cor];
  345. [getGoodsMsgBtn addTarget:self action:@selector(getGoodsMsgBtnClick) forControlEvents:UIControlEventTouchUpInside];
  346. [getGoodsMsgBtn setTitle:YZMsg(@"获取商品信息") forState:0];
  347. getGoodsMsgBtn.layer.cornerRadius = 5;
  348. getGoodsMsgBtn.layer.masksToBounds = YES;
  349. getGoodsMsgBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  350. [textView addSubview:getGoodsMsgBtn];
  351. }
  352. switch (i) {
  353. case 0:
  354. idT = textT;
  355. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"href"])]) {
  356. linkT.text = minstr([_goodsInfo valueForKey:@"href"]);
  357. }
  358. break;
  359. case 1:
  360. nameT2 = textT;
  361. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"name"])]) {
  362. nameT.text = minstr([_goodsInfo valueForKey:@"name"]);
  363. }
  364. break;
  365. case 2:
  366. yuanjiaT2 = textT;
  367. if ([_goodsInfo valueForKey:@"old_price"] && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"old_price"])]) {
  368. yuanjiaT.text = minstr([_goodsInfo valueForKey:@"old_price"]);
  369. }
  370. break;
  371. case 3:
  372. xianjiaT2 = textT;
  373. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"price"])]) {
  374. xianjiaT.text = minstr([_goodsInfo valueForKey:@"price"]);
  375. }
  376. break;
  377. case 4:
  378. contentT2 = textT;
  379. if (_goodsInfo && ![PublicObj checkNull:minstr([_goodsInfo valueForKey:@"des"])]) {
  380. contentT.text = minstr([_goodsInfo valueForKey:@"des"]);
  381. }
  382. break;
  383. default:
  384. break;
  385. }
  386. }else{
  387. textView.backgroundColor = [UIColor clearColor];
  388. goodsThumbImgV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 90, 90)];
  389. goodsThumbImgV.backgroundColor = CellRow_Cor;//RGB_COLOR(@"#fafafa", 1);
  390. goodsThumbImgV.contentMode = UIViewContentModeScaleAspectFill;
  391. goodsThumbImgV.clipsToBounds = YES;
  392. [textView addSubview:goodsThumbImgV];
  393. }
  394. yyyy = textView.bottom;
  395. }
  396. wxAddBtn = [UIButton buttonWithType:0];
  397. wxAddBtn.frame = CGRectMake(15, yyyy + 20, _window_width-30, 40);
  398. [wxAddBtn setBackgroundColor:Pink_Cor];
  399. wxAddBtn.alpha = 0.5;
  400. wxAddBtn.userInteractionEnabled = NO;
  401. [wxAddBtn addTarget:self action:@selector(wxAddBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  402. [wxAddBtn setTitle:YZMsg(@"确认商品信息") forState:0];
  403. wxAddBtn.layer.cornerRadius = 5;
  404. wxAddBtn.layer.masksToBounds = YES;
  405. wxAddBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  406. [wxScroll addSubview:wxAddBtn];
  407. wxScroll.contentSize = CGSizeMake(0, wxAddBtn.bottom);
  408. }
  409. - (NSMutableAttributedString *)setAttStr:(NSString *)str{
  410. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
  411. if ([str hasPrefix:@"*"]) {
  412. [attStr addAttribute:NSForegroundColorAttributeName
  413. value:[UIColor redColor]
  414. range:NSMakeRange(0, 1)];
  415. }
  416. return attStr;
  417. }
  418. - (NSMutableAttributedString *)setWXAttStr:(NSString *)str{
  419. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
  420. NSRange range = [str rangeOfString:@"("];
  421. [attStr addAttribute:NSForegroundColorAttributeName
  422. value:RGB_COLOR(@"#323232", 1)
  423. range:NSMakeRange(0, range.location)];
  424. [attStr addAttribute:NSFontAttributeName
  425. value:SYS_Font(10)
  426. range:NSMakeRange(range.location, str.length - range.location)];
  427. return attStr;
  428. }
  429. - (void)shopImgBtnClick:(UIButton *)sender{
  430. [self.view endEditing:YES];
  431. YBWeakSelf;
  432. RKActionSheet *sheet = [[RKActionSheet alloc]initWithTitle:@""];
  433. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"相机") complete:^{
  434. [weakSelf takePhoto];
  435. }];
  436. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"相册") complete:^{
  437. [weakSelf selectPhotos];
  438. }];
  439. [sheet addActionWithType:RKSheet_Cancle andTitle:YZMsg(@"取消") complete:^{
  440. }];
  441. [sheet showSheet];
  442. }
  443. - (void)takePhoto{
  444. // 区分拍照-相册
  445. UIImagePickerController *imagePickerController = [UIImagePickerController new];
  446. imagePickerController.delegate = self;
  447. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  448. imagePickerController.allowsEditing = NO;
  449. imagePickerController.showsCameraControls = YES;
  450. imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
  451. [self presentViewController:imagePickerController animated:YES completion:nil];
  452. }
  453. -(void)selectPhotos {
  454. TZImagePickerController *imagePC = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:self];
  455. imagePC.preferredLanguage = [lagType isEqual:ZH_CN] ? @"zh-Hans":@"en";
  456. imagePC.showSelectBtn = YES;
  457. imagePC.allowCrop = NO;
  458. imagePC.allowPickingOriginalPhoto = NO;
  459. imagePC.oKButtonTitleColorNormal = Pink_Cor;
  460. imagePC.allowTakePicture = YES;
  461. imagePC.allowTakeVideo = NO;
  462. imagePC.allowPickingVideo = NO;
  463. imagePC.allowPickingMultipleVideo = NO;
  464. imagePC.modalPresentationStyle = 0;
  465. [[[YBBaseAppDelegate sharedAppDelegate] topViewController]presentViewController:imagePC animated:YES completion:nil];
  466. }
  467. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
  468. selectImg = photos[0];
  469. [shopImgBtn setImage:selectImg forState:UIControlStateNormal];
  470. [self ChangeBtnBackground];
  471. }
  472. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  473. NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
  474. if ([type isEqualToString:@"public.image"])
  475. {
  476. //先把图片转成NSData
  477. UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  478. UIImageOrientation imageOrientation = image.imageOrientation;
  479. if(imageOrientation!=UIImageOrientationUp)
  480. {
  481. // 原始图片可以根据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。
  482. // 以下为调整图片角度的部分
  483. UIGraphicsBeginImageContext(image.size);
  484. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  485. image = UIGraphicsGetImageFromCurrentImageContext();
  486. UIGraphicsEndImageContext();
  487. // 调整图片角度完毕
  488. }
  489. selectImg = image;
  490. [shopImgBtn setImage:image forState:UIControlStateNormal];
  491. [self ChangeBtnBackground];
  492. }
  493. [picker dismissViewControllerAnimated:YES completion:nil];
  494. }
  495. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  496. [picker dismissViewControllerAnimated:YES completion:nil];
  497. }
  498. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  499. if ([UIDevice currentDevice].systemVersion.floatValue < 11) {
  500. return;
  501. }
  502. if ([viewController isKindOfClass:NSClassFromString(@"PUPhotoPickerHostViewController")]) {
  503. [viewController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  504. if (obj.frame.size.width < 42) {
  505. [viewController.view sendSubviewToBack:obj];
  506. *stop = YES;
  507. }
  508. }];
  509. }
  510. }
  511. - (void)addBtnClick:(UIButton *)sender{
  512. if ([PublicObj checkNull:linkT.text]) {
  513. [MBProgressHUD showError:YZMsg(@"请输入商品链接")];
  514. return;
  515. }
  516. if ([PublicObj checkNull:nameT.text]) {
  517. [MBProgressHUD showError:YZMsg(@"请输入商品名称")];
  518. return;
  519. }
  520. if ([PublicObj checkNull:xianjiaT.text]) {
  521. [MBProgressHUD showError:YZMsg(@"请输入商品价格")];
  522. return;
  523. }
  524. if ([PublicObj checkNull:contentT.text]) {
  525. [MBProgressHUD showError:YZMsg(@"请输入商品简介")];
  526. return;
  527. }
  528. if (contentT.text.length > 50) {
  529. [MBProgressHUD showError:YZMsg(@"商品简介最多50字")];
  530. return;
  531. }
  532. if (!selectImg && [PublicObj checkNull:minstr([_goodsInfo valueForKey:@"thumb"])]) {
  533. [MBProgressHUD showError:YZMsg(@"请上传商品封面")];
  534. return;
  535. }
  536. if (selectImg) {
  537. YBWeakSelf;
  538. [[YBStorageObj storageManeger]getCOSType:^(int code) {
  539. if (code == 0) {
  540. [weakSelf uploadImg];
  541. }
  542. }];
  543. }else {
  544. [self uploadFinish:minstr([_goodsInfo valueForKey:@"thumb"])];
  545. }
  546. }
  547. -(void)uploadImg {
  548. [MBProgressHUD showMessage:@""];
  549. NSString *imageName = [PublicObj getNameBaseCurrentTime:@"_goods.png"];
  550. YBWeakSelf;
  551. [[YBStorageObj storageManeger]yb_storageImg:selectImg andName:imageName progress:^(CGFloat percent) {
  552. } complete:^(int code, NSString *key) {
  553. dispatch_async(dispatch_get_main_queue(), ^{
  554. [MBProgressHUD hideHUD];
  555. });
  556. if (code == 0) {
  557. dispatch_async(dispatch_get_main_queue(), ^{
  558. [weakSelf uploadFinish:key];
  559. });
  560. }else {
  561. dispatch_async(dispatch_get_main_queue(), ^{
  562. [MBProgressHUD showPop:YZMsg(@"添加失败")];
  563. });
  564. }
  565. }];
  566. }
  567. -(void)uploadFinish:(NSString *)finishPath {
  568. /*
  569. NSDictionary *originDic = @{
  570. @"id":@"0",
  571. @"href":linkT.text,
  572. @"name":nameT.text,
  573. @"price":xianjiaT.text,
  574. @"old_price":yuanjiaT.text,
  575. @"des":contentT.text,
  576. @"thumb":finishPath
  577. };
  578. NSMutableDictionary *o_dic = @{@"type":@"0"}.mutableCopy;
  579. [o_dic addEntriesFromDictionary:originDic];
  580. NSLog(@"<<><><><>:%@",o_dic);
  581. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:o_dic options:NSJSONWritingPrettyPrinted error:nil];
  582. NSString *goodsinfo = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  583. NSLog(@"====:%@==json%@",o_dic,goodsinfo);
  584. if (self.addGoodsEvent) {
  585. self.addGoodsEvent(o_dic, goodsinfo);
  586. }
  587. [self.navigationController popViewControllerAnimated:YES];
  588. */
  589. NSDictionary *postDic = @{@"name":nameT.text,
  590. @"href":linkT.text,
  591. @"thumb":finishPath,
  592. @"old_price":yuanjiaT.text,
  593. @"price":xianjiaT.text,
  594. @"des":contentT.text,
  595. @"type":@"0",
  596. };
  597. NSString *postUrl = @"Shop.setGoods";
  598. if (_isUpdateGoods) {
  599. postDic = @{@"name":nameT.text,
  600. @"href":linkT.text,
  601. @"thumb":finishPath,
  602. @"old_price":yuanjiaT.text,
  603. @"price":xianjiaT.text,
  604. @"des":contentT.text,
  605. @"type":@"0",
  606. @"goodsid":minstr([_goodsInfo valueForKey:@"id"]),
  607. };
  608. postUrl = @"Shop.updateGoods";
  609. }
  610. YBWeakSelf;
  611. [MBProgressHUD showMessage:@""];
  612. [YBNetworking postWithUrl:postUrl Dic:postDic Suc:^(int code, id info, NSString *msg) {
  613. [MBProgressHUD hideHUD];
  614. [MBProgressHUD showPop:msg];
  615. if (code == 0) {
  616. if (weakSelf.addRefreshEvnet) {
  617. weakSelf.addRefreshEvnet();
  618. }
  619. [weakSelf.navigationController popViewControllerAnimated:YES];
  620. }
  621. } Fail:^(id fail) {
  622. [MBProgressHUD hideHUD];
  623. }];
  624. }
  625. - (void)selfTapClick{
  626. [self.view endEditing:YES];
  627. }
  628. - (void)getGoodsMsgBtnClick{
  629. if ([PublicObj checkNull:idT.text]) {
  630. [MBProgressHUD showError:YZMsg(@"请输入商品ID")];
  631. return;
  632. }
  633. [idT resignFirstResponder];
  634. [MBProgressHUD showMessage:@""];
  635. [YBNetworking postWithUrl:@"Shop.GetApplets" Dic:@{@"id":idT.text} Suc:^(int code, id info, NSString *msg) {
  636. [MBProgressHUD hideHUD];
  637. if (code == 0) {
  638. wxGoodsDic = info;
  639. nameT2.text = minstr([wxGoodsDic valueForKey:@"name"]);
  640. yuanjiaT2.text = minstr([wxGoodsDic valueForKey:@"old_price"]);
  641. xianjiaT2.text = minstr([wxGoodsDic valueForKey:@"price"]);
  642. contentT2.text = minstr([wxGoodsDic valueForKey:@"des"]);
  643. [goodsThumbImgV sd_setImageWithURL:[NSURL URLWithString:minstr([wxGoodsDic valueForKey:@"thumb"])]];
  644. wxAddBtn.alpha = 1;
  645. wxAddBtn.userInteractionEnabled = YES;
  646. }else{
  647. [MBProgressHUD showError:msg];
  648. }
  649. } Fail:^(id fail) {
  650. [MBProgressHUD hideHUD];
  651. }];
  652. }
  653. - (void)wxAddBtnClick:(UIButton *)sender{
  654. /*
  655. NSMutableDictionary *dic = @{@"type":@"1"}.mutableCopy;
  656. [dic addEntriesFromDictionary:wxGoodsDic];
  657. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
  658. NSString *goodsinfo = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  659. NSLog(@"====:%@==json%@",dic,goodsinfo);
  660. if (self.addGoodsEvent) {
  661. self.addGoodsEvent(dic, goodsinfo);
  662. }
  663. [self.navigationController popViewControllerAnimated:YES];
  664. */
  665. }
  666. #pragma mark -- 获取键盘高度
  667. - (void)keyboardWillShow:(NSNotification *)aNotification
  668. {
  669. if (contentT.isFirstResponder) {
  670. NSDictionary *userInfo = [aNotification userInfo];
  671. NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  672. CGRect keyboardRect = [aValue CGRectValue];
  673. CGFloat height = keyboardRect.origin.y;
  674. CGFloat heightw = keyboardRect.size.height;
  675. int newHeight = _window_height - height -44;
  676. [UIView animateWithDuration:0.3 animations:^{
  677. [backScroll setContentOffset:CGPointMake(0, heightw)];
  678. }];
  679. }
  680. //获取键盘的高度
  681. }
  682. - (void)keyboardWillHide:(NSNotification *)aNotification {
  683. [UIView animateWithDuration:0.1 animations:^{
  684. [UIView animateWithDuration:0.3 animations:^{
  685. [backScroll setContentOffset:CGPointMake(0, 0)];
  686. }];
  687. }];
  688. }
  689. @end