edit.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <div class="page-title">
  2. <div class="title-env">
  3. <h1 class="title">修改商品</h1>
  4. </div>
  5. <div class="breadcrumb-env">
  6. <ol class="breadcrumb bc-1">
  7. <li>
  8. <a href="/"><i class="fa-home"></i>Home</a>
  9. </li>
  10. <li>
  11. <a href="javascript:void(0)">商城管理</a>
  12. </li>
  13. <li class="active">
  14. <strong>商品管理</strong>
  15. </li>
  16. </ol>
  17. </div>
  18. </div>
  19. <div class="row">
  20. <div class="col-sm-12">
  21. <div class="panel panel-default">
  22. <div class="panel-body">
  23. <form role="form" class="form-horizontal" id="goods-data-form">
  24. <input type="hidden" name="_csrf" value="<?= Yii::$app->request->csrfToken ?>" />
  25. <input type="hidden" name="id" value="<?= $goods['id'] ?>" />
  26. <div class="form-group">
  27. <label class="col-sm-2 control-label">*名称:</label>
  28. <div class="col-sm-10">
  29. <input type="text" class="form-control" name="goodsName" value="<?= $goods['goodsName'] ?>" placeholder="">
  30. <p></p>
  31. </div>
  32. </div>
  33. <div class="form-group">
  34. <label class="col-sm-2 control-label">编号:</label>
  35. <div class="col-sm-10">
  36. <span style="display:block;padding-top:6px;"><?= $goods['itemNumber'] ?></span>
  37. </div>
  38. </div>
  39. <div class="form-group">
  40. <label class="col-sm-2 control-label">*已售出:</label>
  41. <div class="col-sm-10">
  42. <input type="tel" class="form-control" name="sold" value="<?= $goods['sold'] ?>" placeholder="">
  43. <p></p>
  44. </div>
  45. </div>
  46. <div class="form-group">
  47. <label class="col-sm-2 control-label" for="field-1">花材:</label>
  48. <div class="col-sm-10">
  49. <input type="text" class="form-control" value="<?= $goods['material'] ?>" name="material" placeholder="">
  50. <p></p>
  51. </div>
  52. </div>
  53. <div class="form-group">
  54. <label class="col-sm-2 control-label" for="field-1">花语:</label>
  55. <div class="col-sm-10">
  56. <input type="text" class="form-control" name="representative" value="<?= $goods['representative'] ?>" placeholder="">
  57. <p></p>
  58. </div>
  59. </div>
  60. <div class="form-group-separator"></div>
  61. <div class="form-group">
  62. <label class="col-sm-2 control-label">*图片:</label>
  63. <div>
  64. <div style="margin-left:16px;width:113px;height:174px;float:left;display:inline-block">
  65. 封面[550x550]:
  66. <div style="width:97px;height:110px;display:inline-block;margin-bottom:3px;">
  67. <img src="<?= Yii::$app->params['imgUrl'].$goods['cover'] ?>" name="cover" style="width:97px;height:110px;" onerror="javascript:this.src='/images/nopicture.png';" class="img-thumbnail" />
  68. </div>
  69. <a href="javascript:;" class="a-upload">
  70. <input type="file" accept="image/*" id="cover" value="" />选择图片
  71. </a>
  72. <input type="hidden" name="cover" value="<?= $goods['cover'] ?>" />
  73. </div>
  74. <?php $shopImg = json_decode($goods['shopImg'],true); ?>
  75. <div style="margin-left:16px;width:157px;height:174px;float:left;display:inline-block;float:left;">
  76. 图Ⅰ[800x800]:
  77. <div style="width:141px;height:110px;display:inline-block;margin-bottom:3px;">
  78. <img src="<?php if(isset($shopImg[0])){ echo Yii::$app->params['imgUrl'].$shopImg[0]; }else{ echo '/images/nopicture.png'; } ?>" name="shopImg1" style="width:128px;height:110px;" onerror="javascript:this.src='/images/nopicture.png';" class="img-thumbnail" />
  79. </div>
  80. <a href="javascript:;" class="a-upload">
  81. <input type="file" accept="image/*" id="shopImg1" value="" />选择图片
  82. </a>
  83. <input type="hidden" name="shopImg1" value="<?php echo isset($shopImg[0]) ? $shopImg[0] : ''; ?>" />
  84. </div>
  85. <div style="margin-left:16px;width:157px;height:174px;float:left;display:inline-block;float:left;">
  86. 图Ⅱ[800x800]:
  87. <div style="width:141px;height:110px;display:inline-block;margin-bottom:3px;">
  88. <img src="<?php if(isset($shopImg[1])){ echo Yii::$app->params['imgUrl'].$shopImg[1]; }else{ echo '/images/nopicture.png'; } ?>" name="shopImg2" style="width:128px;height:110px;" class="img-thumbnail" />
  89. </div>
  90. <a href="javascript:;" class="a-upload">
  91. <input type="file" accept="image/*" id="shopImg2" value="" />选择图片
  92. </a>
  93. <input type="hidden" name="shopImg2" value="<?php echo isset($shopImg[1]) ? $shopImg[1] : ''; ?>" />
  94. </div>
  95. <div style="margin-left:16px;width:157px;height:174px;float:left;display:inline-block;float:left;">
  96. 图Ⅲ[800x800]:
  97. <div style="width:141px;height:110px;display:inline-block;margin-bottom:3px;">
  98. <img src="<?php if(isset($shopImg[2])){ echo Yii::$app->params['imgUrl'].$shopImg[2]; }else{ echo '/images/nopicture.png'; } ?>" name="shopImg3" style="width:128px;height:110px;" onerror="javascript:this.src='/images/nopicture.png';" class="img-thumbnail" />
  99. </div>
  100. <a href="javascript:;" class="a-upload">
  101. <input type="file" accept="image/*" id="shopImg3" value="" />选择图片
  102. </a>
  103. <input type="hidden" name="shopImg3" value="<?php echo isset($shopImg[2]) ? $shopImg[2] : ''; ?>" />
  104. </div>
  105. <div style="clear:both;margin-left:228px;"><p></p></div>
  106. </div>
  107. </div>
  108. <div class="form-group-separator"></div>
  109. <div class="form-group price-area">
  110. <label class="col-sm-2 control-label">价格选项:</label>
  111. <div class="col-sm-10">
  112. <div class="col-sm-2">
  113. <input type="radio" name="priceType" data-type='single' value='single' <?php if($goods['multiPrice'] ==''){ echo 'checked="checked"';}?> > 单种价格
  114. </div>
  115. <div class="col-sm-2">
  116. <input type="radio" name="priceType" data-type='multi' value='multi' <?php if($goods['multiPrice'] !=''){ echo 'checked="checked"';}?> > 多种价格
  117. </div>
  118. </div>
  119. </div>
  120. <div class="form-group">
  121. <label class="col-sm-2 control-label">价格:</label>
  122. <div class="single-price-area" <?php if($goods['multiPrice'] != ''){ echo 'style="display:none;"'; }?> >
  123. <div class="col-sm-10" style="color:#555">
  124. <div class="form-group">
  125. <div class="col-sm-10">
  126. <input type="text" name="price" class="form-control" value="<?= $goods['price'] ?>" >
  127. <p></p>
  128. </div>
  129. </div>
  130. </div>
  131. </div>
  132. <div class="multi-price-area" <?php if(!isset($goodsPriceArr)){ echo 'style="display:none;"'; }?> >
  133. <div class="col-sm-10" style="color:#555;">
  134. <?php
  135. if(isset($goodsPriceArr)){
  136. foreach($goodsPriceArr as $key => $item) {
  137. ?>
  138. <div class="form-group">
  139. <div class="col-sm-10 price-area">
  140. 名称:<input type="text" name="goodTitle[]" value="<?=$item['title']?>" class="form-control" style="width:300px;display:inline-block;">
  141. &nbsp;&nbsp;&nbsp;&nbsp;价格:<input type="text" name="multiPrice[]" value="<?=$item['price']?>" class="form-control" style="width:100px;display:inline-block;" placeholder="">
  142. &nbsp;&nbsp;&nbsp;&nbsp;<img src="" alt="">
  143. &nbsp;&nbsp;<button type="button" class="btn btn-sm btn-white" style="display: none;margin-top:5px;">上传图片</button>
  144. &nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-pink rm-price-btn" onclick="removePrice($(this))" style="display:none; margin-top:5px;"><li class="fa-remove">删除</li></button>
  145. <input type="hidden" name="priceId[]" value="<?=$item['id']?>"/>
  146. </div>
  147. </div>
  148. <?php
  149. }
  150. }else{
  151. ?>
  152. <div class="form-group">
  153. <div class="col-sm-10">
  154. 名称:<input type="text" name="goodTitle[]" class="form-control" style="width:300px;display:inline-block;">
  155. &nbsp;&nbsp;&nbsp;&nbsp;价格:<input type="text" name="multiPrice[]" class="form-control" style="width:100px;display:inline-block;" placeholder="">
  156. &nbsp;&nbsp;&nbsp;&nbsp;<img src="" alt="">
  157. <input type="hidden" name="priceId[]" value=""/>
  158. &nbsp;&nbsp;<button type="button" class="btn btn-sm btn-white" style="display: none;margin-top:5px;">上传图片</button>
  159. </div>
  160. </div>
  161. <div class="form-group">
  162. <div class="col-sm-10">
  163. 名称:<input type="text" name="goodTitle[]" class="form-control" style="width:300px;display:inline-block;">
  164. &nbsp;&nbsp;&nbsp;&nbsp;价格:<input type="text" name="multiPrice[]" class="form-control" style="width:100px;display:inline-block;" placeholder="">
  165. &nbsp;&nbsp;&nbsp;&nbsp;<img src="" alt="">
  166. <input type="hidden" name="priceId[]" value=""/>
  167. &nbsp;&nbsp;<button type="button" class="btn btn-sm btn-white" style="display: none;margin-top:5px;">上传图片</button>
  168. </div>
  169. </div>
  170. <?php
  171. }
  172. ?>
  173. </div>
  174. <div class="col-sm-10">
  175. <label class="col-sm-2 control-label"></label>
  176. <div class="col-sm-8" style="margin-left: 35px;">
  177. <button type="button" class="btn btn-turquoise add-price-btn"><li class="fa-plus"></li> 增加价格</button>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. <div class="form-group price-area">
  183. <label class="col-sm-2 control-label">运费:</label>
  184. <div class="col-sm-10">
  185. <div class="col-sm-2">
  186. <input type="radio" name="isCharge" value='1' <?php if($goods['isCharge'] == 1){?>checked="checked"<?php }?> > 按距离收费
  187. </div>
  188. <div class="col-sm-2">
  189. <input type="radio" name="isCharge" value='0' <?php if($goods['isCharge'] == 0){?>checked="checked"<?php }?> > 免运费
  190. </div>
  191. </div>
  192. </div>
  193. <div class="form-group-separator"></div>
  194. <div class="form-group">
  195. <label class="col-sm-2 control-label" for="field-3">*商品简介:</label>
  196. <div class="col-sm-10">
  197. <?php use common\widgets\UeditorWidget; ?>
  198. <?= UeditorWidget::widget(['name' => 'content','content' => $goods['content']]); ?>
  199. <p></p>
  200. </div>
  201. </div>
  202. <div class="form-group-separator"></div>
  203. <div class="form-group">
  204. <label class="col-sm-2 control-label">*所属分类</label>
  205. <div class="col-sm-10 category-area">
  206. <div style="border:1px solid #CCC;padding-left:8px;border-radius:4px;">
  207. <?php
  208. if(!empty($categoryList)){
  209. foreach($categoryList as $clKey => $clVal){
  210. if(!empty($clVal['child'])){
  211. echo '<label class="checkbox-inline" style="padding-left:0px;">'.$clVal['categoryName'].'</label><p>';
  212. foreach($clVal['child'] as $ccKey => $ccVal)
  213. {
  214. echo '&nbsp;&nbsp;&nbsp;|-- <label class="checkbox-inline"><input name="categoryId[]"';
  215. if(in_array($ccVal['id'],$goodsCategory)) echo 'checked ';
  216. echo 'value="'.$ccVal['id'].'" type="checkbox">'.$ccVal['categoryName'].'</label>';
  217. }
  218. echo '</p>';
  219. }else{
  220. echo '<label class="checkbox-inline"><input name="categoryId[]"';
  221. if(in_array($clVal['id'],$goodsCategory)) echo 'checked ';
  222. echo 'value="'.$clVal['id'].'" type="checkbox">'.$clVal['categoryName'].'</label><p></p>';
  223. }
  224. }
  225. }
  226. ?>
  227. </div>
  228. <p name="categoryIdMention"></p>
  229. </div>
  230. </div>
  231. <div class="form-group">
  232. <label class="col-sm-2 control-label" for="field-4">*商品状态</label>
  233. <div class="col-sm-10">
  234. <div class="radio-inline">
  235. <label>
  236. <input type="radio" name="status" <?php if($goods['status'] == 0){ ?>checked<?php } ?> checked value="0">
  237. 上架
  238. </label>
  239. </div>
  240. <div class="radio-inline">
  241. <label>
  242. <input type="radio" name="status" <?php if($goods['status'] == 1){ ?>checked<?php } ?> value="1">
  243. 下架
  244. </label>
  245. </div>
  246. </div>
  247. </div>
  248. <div class="form-group-separator"></div>
  249. <div class="form-group" style="text-align:center;">
  250. <button type="button" onclick="editSubmit('click', this)" class="btn btn-info btn-single">确认</button>
  251. <button type="button" onclick="history.go(-1);" class="btn btn-info btn-single">返回</button>
  252. </div>
  253. </form>
  254. </div>
  255. </div>
  256. </div>
  257. </div>
  258. <!-- footer -->
  259. <?php
  260. use backend\widgets\FooterWidget;
  261. ?>
  262. <?= FooterWidget::widget()?>
  263. <!-- footer -->
  264. <script src="/js/lrz/dist/lrz.bundle.js"></script>
  265. <script>
  266. var goodsCategoryString = <?= $goodsCategoryString ?>;
  267. //添加商品验证流程
  268. var goodsId = $("input[name=id]");
  269. var goodsName = $("input[name=goodsName]");
  270. var price = $("input[name=price]");
  271. var sold = $("input[name=sold]");
  272. var material = $("input[name=material]");
  273. var representative = $(":input[name=representative]");
  274. var cover = $(":input[name=cover]");
  275. var shopImg1 = $(":input[name=shopImg1]");
  276. var shopImg2 = $(":input[name=shopImg2]");
  277. var shopImg3 = $(":input[name=shopImg3]");
  278. var content = $("textarea[name=content]");
  279. var needValidate = [goodsName,price,sold,material,representative];
  280. //添加商品验证流程
  281. editSubmit();
  282. function editSubmit()
  283. {
  284. var verifyStatus = true;
  285. setStats(goodsName, '请填写商品名称', function(val, mess){
  286. var info=[false, mess];
  287. if(val.length <= 0){
  288. info[1] = '请填写商品名称';
  289. }else if(getLength(val) >=0 && getLength(val) <=40){
  290. info[0] = true;
  291. info[1] = '填写正确';
  292. }else{
  293. info[0] = false;
  294. }
  295. return info;
  296. });
  297. var priceType = $('.price-area input:radio[name=priceType]:checked').attr('data-type');
  298. if(priceType == 'single'){
  299. setStats(price, '请填写价格', function(val, mess){
  300. var info=[false, mess];
  301. if(isNotNull(val) == false || isNaN(val)){
  302. info[1] = '请填写价格';
  303. }else{
  304. info[0] = true;
  305. }
  306. return info;
  307. });
  308. }else{
  309. var units = $('.multi-price-area .col-sm-10').find('.form-group');
  310. units.each(function(e){
  311. var title = $(this).find('.form-control').eq(0).val();
  312. var price = $(this).find('.form-control').eq(1).val();
  313. if($.trim(title) == ''){
  314. xhPopMsg('价格项: 名称不为空', 2500);
  315. verifyStatus = false;
  316. return;
  317. }
  318. if($.trim(price) == ''){
  319. xhPopMsg('价格项: 有价格未填写', 2500);
  320. verifyStatus = false;
  321. return;
  322. }
  323. if(isNaN(price)){
  324. xhPopMsg('价格项: 价格必须为数字', 2500);
  325. verifyStatus = false;
  326. return;
  327. }
  328. });
  329. var singlePriceObj = $('.single-price-area input[name=price]');
  330. singlePriceObj.attr('addStatus', 'yes');
  331. singlePriceObj.siblings('p').removeClass('help-error-mention').html('');
  332. }
  333. setStats(sold, '请填写售量', function(val, mess){
  334. var info=[false, mess];
  335. if(isNotNull(val) == false || isNaN(val)){
  336. info[1] = '请填写售量';
  337. }else{
  338. info[0] = true;
  339. }
  340. return info;
  341. });
  342. setStats(material, '请填写花材', function(val, mess){
  343. var info=[false, mess];
  344. if(getLength(val) >=0){
  345. info[0] = true;
  346. info[1] = '填写正确';
  347. }else{
  348. info[1] = '请填写花材';
  349. }
  350. return info;
  351. });
  352. setStats(representative, '请填写花语', function(val, mess){
  353. var info=[false, mess];
  354. if(getLength(val) >=0){
  355. info[0] = true;
  356. info[1] = '填写正确';
  357. }else{
  358. info[1] = '请填写花语';
  359. }
  360. return info;
  361. });
  362. setStats(shopImg1, '请填写上传图片', function(val, mess){
  363. var info=[false, mess];
  364. if(getLength(val) >=0){
  365. info[0] = true;
  366. info[1] = '填写正确';
  367. }else{
  368. info[1] = '至少需要上传一张图片';
  369. }
  370. return info;
  371. });
  372. setStats(content, '请填写简介', function(val, mess){
  373. var info=[false, mess];
  374. if(val.length > 0){
  375. info[0] = true;
  376. info[1] = '填写正确';
  377. }else{
  378. info[1] = '请填写简介';
  379. }
  380. return info;
  381. });
  382. if(arguments[0] == 'click'){//用户点击提交
  383. var submitButton = arguments[1];//获取提交按钮对象
  384. var submitButtonObj = $(submitButton);
  385. if(submitButtonObj.attr('submitStatus') == 'yes'){
  386. alert('表单已经提交成功,不允许重复提交');
  387. return;//表单已经提交成功,不允许重复提交
  388. }
  389. for(var m=0;m<needValidate.length;m++){
  390. var singleObj = needValidate[m];
  391. if(singleObj.attr('addStatus') == undefined){
  392. singleObj.blur();
  393. }
  394. if(singleObj.attr('addStatus') == 'no'){
  395. verifyStatus = false;
  396. continue;
  397. }
  398. }
  399. //封面 图片1判断
  400. if(isNotNull(shopImg1.val()) == false || isNotNull(cover.val()) == false){
  401. shopImg1.parent().parent().find('p').addClass('help-error-mention').html('封面和图Ⅰ必须上传');
  402. verifyStatus = false;
  403. }else{
  404. shopImg1.parent().parent().find('p').removeClass('help-error-mention').html('');
  405. }
  406. //确认有没选择分类
  407. var checkNum = $(".form-group .category-area :checkbox:checked").length;
  408. if(parseInt(checkNum) < 1){
  409. $("p[name=categoryIdMention]").addClass('help-error-mention').html('请选择分类');
  410. verifyStatus = false;
  411. }else{
  412. $("p[name=categoryIdMention]").removeClass('help-error-mention').html('');
  413. }
  414. /*var statusValue = $('input:radio[name=status]:checked').val();//商品状态
  415. var fullCategoryId = [];
  416. var newGoodsCategory = [];
  417. $(":input[name=categoryId]:checked").each(function(){
  418. var thisCId = $(this).val();
  419. newGoodsCategory.push(thisCId);
  420. });
  421. var addCategoryId = newGoodsCategory.minus(goodsCategoryString);//并集,需要添加的
  422. var delCategoryId = goodsCategoryString.minus(newGoodsCategory);//并集,需要删除的*/
  423. if(verifyStatus){
  424. /*var data = "categoryId="+newGoodsCategory.join(',')+"&addCategoryId="+addCategoryId.join(',')+"&delCategoryId="+delCategoryId.join(',');
  425. data += "&goodsName="+goodsName.val()+"&price="+price.val()+"&sold="+sold.val()+"&representative="+representative.val();
  426. data += "&cover="+cover.val()+"&shopImg1="+shopImg1.val()+"&shopImg2="+shopImg2.val()+"&material="+material.val();
  427. data += "&content="+content.val()+"&shopImg3="+shopImg3.val();*/
  428. //data += "&_csrf=<?= Yii::$app->request->csrfToken ?>&status="+statusValue+'&id='+goodsId.val();
  429. var data = $('#goods-data-form').serialize();
  430. $.ajax({
  431. type:"POST",
  432. url:'/goods/save-edit',
  433. data:data,
  434. dataType:'json',
  435. async:false,
  436. success:function(data){
  437. if(data.code == 'A0001'){
  438. submitButtonObj.attr('submitStatus','yes');
  439. xhPopMsg(data.msg,1300);//调用定时浮层 XXX毫秒后消失
  440. setTimeout(function(){
  441. window.location.href='/goods/list';
  442. },1400);
  443. }else{
  444. xhPopMsg(data.msg,1500);
  445. }
  446. }
  447. });
  448. }
  449. }
  450. }
  451. function setStats(obj,mess,check)
  452. {
  453. obj.focus(function(){
  454. $(this).removeAttr('addStatus');
  455. $(this).siblings('p').removeClass('help-error-mention').html('');
  456. $(this).parent().parent().removeClass("has-success").removeClass('has-error');
  457. });
  458. obj.blur(function(){
  459. var objVal = obj.val();
  460. var info = check(objVal,mess);
  461. if(info[0]){
  462. $(this).attr('addStatus','yes');
  463. $(this).siblings('p').removeClass('help-error-mention').html('');
  464. $(this).parent().parent().addClass("has-success").removeClass("has-error");
  465. }else{
  466. $(this).attr('addStatus','no');
  467. $(this).siblings('p').addClass('help-error-mention').html(info[1]);
  468. $(this).parent().parent().addClass("has-error").removeClass("has-success");
  469. }
  470. });
  471. }
  472. document.querySelector('#cover').addEventListener('change', function () {
  473. //参数说明 https://github.com/think2011/localResizeIMG/wiki/2.-%E5%8F%82%E6%95%B0%E6%96%87%E6%A1%A3
  474. lrz(this.files[0],{
  475. width: 800,// 图片最大不超过的宽度,默认为原图宽度,高度不设时会适应宽度。
  476. fieldName:'Filedata',//文件名
  477. quality:1,//图片质量 0.1 - 1
  478. })
  479. .then(function (rst) {
  480. // 处理成功会执行
  481. rst.formData.append('fileLen', rst.fileLen);
  482. rst.formData.append('_csrf', '<?= Yii::$app->request->csrfToken?>');
  483. $.ajax({
  484. //url: '/main/img', // 这个地址做了跨域处理,可以用于实际调试
  485. url: '/upload/upload-goods-img', // 这个地址做了跨域处理,可以用于实际调试
  486. data: rst.formData,
  487. processData: false,
  488. contentType: false,
  489. type: 'POST',
  490. dataType:'json',
  491. success: function (data) {
  492. $("img[name=cover]").attr('src','<?= Yii::$app->params['imgUrl']; ?>'+data.url);
  493. $("input[name='cover']").val(data.url);
  494. }
  495. });
  496. })
  497. .catch(function (err) {
  498. // 处理失败会执行
  499. })
  500. .always(function () {
  501. // 不管是成功失败,都会执行
  502. });
  503. });
  504. document.querySelector('#shopImg1').addEventListener('change', function () {
  505. //参数说明 https://github.com/think2011/localResizeIMG/wiki/2.-%E5%8F%82%E6%95%B0%E6%96%87%E6%A1%A3
  506. lrz(this.files[0],{
  507. width: 800,// 图片最大不超过的宽度,默认为原图宽度,高度不设时会适应宽度。
  508. fieldName:'Filedata',//文件名
  509. quality:1,//图片质量 0.1 - 1
  510. })
  511. .then(function (rst) {
  512. // 处理成功会执行
  513. rst.formData.append('fileLen', rst.fileLen);
  514. rst.formData.append('_csrf', '<?= Yii::$app->request->csrfToken?>');
  515. $.ajax({
  516. //url: '/main/img', // 这个地址做了跨域处理,可以用于实际调试
  517. url: '/upload/upload-goods-img', // 这个地址做了跨域处理,可以用于实际调试
  518. data: rst.formData,
  519. processData: false,
  520. contentType: false,
  521. type: 'POST',
  522. dataType:'json',
  523. success: function (data) {
  524. $("img[name=shopImg1]").attr('src','<?= Yii::$app->params['imgUrl']; ?>'+data.url);
  525. $("input[name='shopImg1']").val(data.url);
  526. }
  527. });
  528. })
  529. .catch(function (err) {
  530. // 处理失败会执行
  531. })
  532. .always(function () {
  533. // 不管是成功失败,都会执行
  534. });
  535. });
  536. document.querySelector('#shopImg2').addEventListener('change', function () {
  537. //参数说明 https://github.com/think2011/localResizeIMG/wiki/2.-%E5%8F%82%E6%95%B0%E6%96%87%E6%A1%A3
  538. lrz(this.files[0],{
  539. width: 800,// 图片最大不超过的宽度,默认为原图宽度,高度不设时会适应宽度。
  540. fieldName:'Filedata',//文件名
  541. quality:1,//图片质量 0.1 - 1
  542. })
  543. .then(function (rst) {
  544. // 处理成功会执行
  545. rst.formData.append('fileLen', rst.fileLen);
  546. rst.formData.append('_csrf', '<?= Yii::$app->request->csrfToken?>');
  547. $.ajax({
  548. //url: '/main/img', // 这个地址做了跨域处理,可以用于实际调试
  549. url: '/upload/upload-goods-img', // 这个地址做了跨域处理,可以用于实际调试
  550. data: rst.formData,
  551. processData: false,
  552. contentType: false,
  553. type: 'POST',
  554. dataType:'json',
  555. success: function (data) {
  556. $("img[name=shopImg2]").attr('src','<?= Yii::$app->params['imgUrl']; ?>'+data.url);
  557. $("input[name='shopImg2']").val(data.url);
  558. }
  559. });
  560. })
  561. .catch(function (err) {
  562. // 处理失败会执行
  563. })
  564. .always(function () {
  565. // 不管是成功失败,都会执行
  566. });
  567. });
  568. document.querySelector('#shopImg3').addEventListener('change', function () {
  569. //参数说明 https://github.com/think2011/localResizeIMG/wiki/2.-%E5%8F%82%E6%95%B0%E6%96%87%E6%A1%A3
  570. lrz(this.files[0],{
  571. width: 800,// 图片最大不超过的宽度,默认为原图宽度,高度不设时会适应宽度。
  572. fieldName:'Filedata',//文件名
  573. quality:1,//图片质量 0.1 - 1
  574. })
  575. .then(function (rst) {
  576. // 处理成功会执行
  577. rst.formData.append('fileLen', rst.fileLen);
  578. rst.formData.append('_csrf', '<?= Yii::$app->request->csrfToken?>');
  579. $.ajax({
  580. //url: '/main/img', // 这个地址做了跨域处理,可以用于实际调试
  581. url: '/upload/upload-goods-img', // 这个地址做了跨域处理,可以用于实际调试
  582. data: rst.formData,
  583. processData: false,
  584. contentType: false,
  585. type: 'POST',
  586. dataType:'json',
  587. success: function (data) {
  588. $("img[name=shopImg3]").attr('src','<?= Yii::$app->params['imgUrl']; ?>'+data.url);
  589. $("input[name='shopImg3']").val(data.url);
  590. }
  591. });
  592. })
  593. .catch(function (err) {
  594. // 处理失败会执行
  595. })
  596. .always(function () {
  597. // 不管是成功失败,都会执行
  598. });
  599. });
  600. function delPrice(obj){
  601. obj.closest('.form-group').remove();
  602. }
  603. function removePrice(obj){
  604. var formGroupObj = obj.closest('.form-group').siblings();
  605. var length = formGroupObj.length;
  606. if(length < 2){
  607. var content = '<div class="form-group"><div class="col-sm-10">名称:<input type="text" name="goodTitle[]" class="form-control" style="width:300px;display:inline-block;" >&nbsp;&nbsp;&nbsp;&nbsp;价格:<input type="text" name="multiPrice[]" class="form-control" style="width:100px;display:inline-block;" placeholder="">&nbsp;&nbsp;&nbsp;&nbsp;<img src="" alt="">&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-white" style="display: none;margin-top:5px;">上传图片</button></div></div>';
  608. formGroupObj.last().after(content);
  609. }
  610. obj.closest('.form-group').remove();
  611. }
  612. $(function(){
  613. //增加价格按钮事件
  614. $('.add-price-btn').on('click', function(){
  615. var unitNum = $(this).closest('.form-group').find('.multi-price-area .col-sm-10').eq(0).find('.form-group').length;
  616. if(unitNum > 9){
  617. xhPopMsg('价格最多只可设置10个', 2500);
  618. return;
  619. }
  620. var content = '<div class="form-group"><div class="col-sm-10">名称:<input type="text" name="goodTitle[]" class="form-control" style="width:300px;display:inline-block;" >&nbsp;&nbsp;&nbsp;&nbsp;价格:<input type="text" name="multiPrice[]" class="form-control" style="width:100px;display:inline-block;" placeholder="">&nbsp;&nbsp;&nbsp;&nbsp;<img src="" alt="">&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-white" style="display: none;margin-top:5px;">上传图片</button>&nbsp;&nbsp;&nbsp;&nbsp;<button class="btn btn-sm btn-pink" onclick="delPrice(this)" style="margin-top:4px;"><li class="fa-remove">删除</li></button></div></div>';
  621. $(this).closest('.form-group').find('.multi-price-area .col-sm-10').eq(0).find('.form-group').last().after(content);
  622. });
  623. //价格选项事件
  624. $('.price-area input:radio[name=priceType]').on('change', function(){
  625. var type = $(this).attr('data-type');
  626. if(type == 'single'){
  627. $('.single-price-area').show();
  628. $('.multi-price-area').hide();
  629. }else{
  630. $('.multi-price-area').show();
  631. $('.single-price-area').hide();
  632. var singlePriceObj = $('.single-price-area input[name=price]');
  633. singlePriceObj.attr('addStatus', 'yes');
  634. singlePriceObj.siblings('p').removeClass('help-error-mention').html('');
  635. }
  636. });
  637. //显示删除按钮
  638. $('.form-group .price-area').hover(function(){
  639. $(this).find('.rm-price-btn').show();
  640. },function(){
  641. $(this).find('.rm-price-btn').hide();
  642. });
  643. });
  644. </script>