egg(72,73)--egg之商品curd的编辑
生活随笔
收集整理的這篇文章主要介紹了
egg(72,73)--egg之商品curd的编辑
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
controller
app/controller/admin/goods.jsedit
async edit() {//獲取修改數(shù)據(jù)的idvar id=this.ctx.request.query.id;//獲取所有的顏色值var colorResult=await this.ctx.model.GoodsColor.find({});//獲取所有的商品類(lèi)型var goodsType=await this.ctx.model.GoodsType.find({});//獲取商品分類(lèi)var goodsCate=await this.ctx.model.GoodsCate.aggregate([{$lookup:{from:'goods_cate',localField:'_id',foreignField:'pid',as:'items' } },{$match:{"pid":'0'}}])//獲取修改的商品var goodsResult=await this.ctx.model.Goods.find({'_id':id});//獲取當(dāng)前商品的顏色// 5bbb68dcfe498e2346af9e4a,5bbb68effe498e2346af9e4b,5bc067d92e5f889dc864aa96var colorArrTemp=goodsResult[0].goods_color.split(',');// console.log(colorArrTemp);var goodsColorArr=[];colorArrTemp.forEach((value)=>{goodsColorArr.push({"_id":value})})var goodsColorReulst=await this.ctx.model.GoodsColor.find({$or:goodsColorArr})// console.log(colorReulst);//獲取規(guī)格信息 var goodsAttsResult=await this.ctx.model.GoodsAttr.find({"goods_id":goodsResult[0]._id}); var goodsAttsStr='';goodsAttsResult.forEach(async (val)=>{if(val.attribute_type==1){ goodsAttsStr+=`<li><span>${val.attribute_title}: </span><input type="hidden" name="attr_id_list" value="${val.attribute_id}" /> <input type="text" name="attr_value_list" value="${val.attribute_value}" /></li>`;}else if(val.attribute_type==2){goodsAttsStr+=`<li><span>${val.attribute_title}: </span><input type="hidden" name="attr_id_list" value="${val.attribute_id}" /> <textarea cols="50" rows="3" name="attr_value_list">${val.attribute_value}</textarea></li>`;}else{//獲取 attr_value 獲取可選值列表var oneGoodsTypeAttributeResult=await this.ctx.model.GoodsTypeAttribute.find({_id:val.attribute_id})var arr=oneGoodsTypeAttributeResult[0].attr_value.split('\n'); goodsAttsStr+=`<li><span>${val.attribute_title}: </span><input type="hidden" name="attr_id_list" value="${val.attribute_id}" />`;goodsAttsStr+=`<select name="attr_value_list">`;for(var j=0;j<arr.length;j++){if(arr[j]==val.attribute_value){goodsAttsStr+=`<option value="${arr[j]}" selected >${arr[j]}</option>`;}else{goodsAttsStr+=`<option value="${arr[j]}" >${arr[j]}</option>`;} }goodsAttsStr+=`</select>`;goodsAttsStr+=`</li>`;}})//商品的圖庫(kù)信息var goodsImageResult=await this.ctx.model.GoodsImage.find({"goods_id":goodsResult[0]._id}); console.log(goodsImageResult);await this.ctx.render('admin/goods/edit',{colorResult:colorResult,goodsType:goodsType,goodsCate:goodsCate,goods:goodsResult[0],goodsAtts:goodsAttsStr,goodsImage:goodsImageResult,goodsColor:goodsColorReulst});}doEdit
async doEdit() {let parts = this.ctx.multipart({ autoFields: true });let files = {}; let stream;while ((stream = await parts()) != null) {if (!stream.filename) { break;} let fieldname = stream.fieldname; //file表單的名字//上傳圖片的目錄let dir=await this.service.tools.getUploadFile(stream.filename);let target = dir.uploadDir;let writeStream = fs.createWriteStream(target);await pump(stream, writeStream); files=Object.assign(files,{[fieldname]:dir.saveDir })} var formFields=Object.assign(files,parts.field);//修改商品的idvar goods_id=parts.field.id; //修改商品信息await this.ctx.model.Goods.updateOne({"_id":goods_id},formFields);//修改圖庫(kù)信息 (增加)var goods_image_list=formFields.goods_image_list;if(goods_id && goods_image_list){ if(typeof(goods_image_list)=='string'){goods_image_list=new Array(goods_image_list);}for(var i=0;i<goods_image_list.length;i++){ let goodsImageRes =new this.ctx.model.GoodsImage({goods_id:goods_id,img_url:goods_image_list[i]});await goodsImageRes.save();}}//修改商品類(lèi)型數(shù)據(jù) 1、刪除以前的類(lèi)型數(shù)據(jù) 2、重新增加新的商品類(lèi)型數(shù)據(jù)//1、刪除以前的類(lèi)型數(shù)據(jù)await this.ctx.model.GoodsAttr.deleteOne({"goods_id":goods_id}); //2、重新增加新的商品類(lèi)型數(shù)據(jù)var attr_value_list=formFields.attr_value_list;var attr_id_list=formFields.attr_id_list;if(goods_id && attr_id_list && attr_value_list){ //解決只有一個(gè)屬性的時(shí)候存在的bugif(typeof(attr_id_list)=='string'){attr_id_list=new Array(attr_id_list);attr_value_list=new Array(attr_value_list);}for(var i=0;i<attr_value_list.length;i++){//查詢goods_type_attributeif(attr_value_list[i]){ var goodsTypeAttributeResutl=await this.ctx.model.GoodsTypeAttribute.find({"_id":attr_id_list[i]})let goodsAttrRes =new this.ctx.model.GoodsAttr({goods_id:goods_id, cate_id:formFields.cate_id,attribute_id:attr_id_list[i],attribute_type:goodsTypeAttributeResutl[0].attr_type,attribute_title:goodsTypeAttributeResutl[0].title,attribute_value:attr_value_list[i]});await goodsAttrRes.save();}}}await this.success('/admin/goods','修改商品數(shù)據(jù)成功');}view
app/view/admin/goods/edit.html <%- include ../public/page_header.html %><!-- 富文本編輯器 --><link href="/public/admin/wysiwyg-editor/css/font-awesome.min.css" rel="stylesheet" type="text/css" /><!-- Include Editor style. --><link href="/public/admin/wysiwyg-editor/css/froala_editor.pkgd.min.css" rel="stylesheet" type="text/css" /><link href="/public/admin/wysiwyg-editor/css/froala_style.min.css" rel="stylesheet" type="text/css" /><!-- 引入jquery --><!-- Include Editor JS files. --><script type="text/javascript" src="/public/admin/wysiwyg-editor/js/froala_editor.pkgd.min.js"></script><script type="text/javascript" src="/public/admin/wysiwyg-editor/js/zh_cn.js"></script><!-- 批量上傳圖片插件 --><link rel="stylesheet" type="text/css" href="/public/admin/webuploader/css/webuploader.css"><link rel="stylesheet" type="text/css" href="/public/admin/webuploader/css/diyUpload.css"><script type="text/javascript" src="/public/admin/webuploader/js/webuploader.html5only.min.js"></script><script type="text/javascript" src="/public/admin/webuploader/js/diyUpload.js"></script><div class="panel panel-default"> <div class="panel-body"><div class="table-responsive input-form"><form action="/admin/goods/doEdit?_csrf=<%=csrf%>" method="post" class="goods_content" enctype="multipart/form-data"> <!-- Nav tabs --><ul class="nav nav-tabs" role="tablist"><li role="presentation" class="active"><a href="#general" role="tab" data-toggle="tab">通用信息</a></li><li role="presentation"><a href="#detail" role="tab" data-toggle="tab">詳細(xì)描述</a></li><li role="presentation"><a href="#mix" role="tab" data-toggle="tab">其他信息</a></li><li role="presentation"><a href="#attribute" role="tab" data-toggle="tab">規(guī)格與包裝</a></li><li role="presentation"><a href="#photo" role="tab" data-toggle="tab">商品相冊(cè)</a></li> </ul><!-- Tab panes --><div class="tab-content"><div role="tabpanel" class="tab-pane active" id="general"><input type="hidden" name="id" value="<%=goods._id%>" /><ul class="form_input"><li> <span> 商品標(biāo)題:</span> <input type="text" name="title" class="input" value="<%=goods.title%>" /></li> <li> <span> 附屬標(biāo)題:</span> <input type="text" name="sub_title" class="input" value="<%=goods.sub_title%>"/></li><li> <span>商品版本:</span> <input type="text" name="goods_version" class="input"value="<%=goods.goods_version%>" /></li> <li> <span>所屬分類(lèi):</span> <select name="cate_id" id="cate_id"><%for(var i=0;i<goodsCate.length;i++){%><option value="<%=goodsCate[i]._id%>" <%if(goods.cate_id.toString()==goodsCate[i]._id.toString()){%>selected<%}%> ><%=goodsCate[i].title%></option><%for(var j=0;j<goodsCate[i].items.length;j++){%><option value="<%=goodsCate[i].items[j]._id%>" <%if(goods.cate_id.toString()==goodsCate[i].items[j]._id.toString()){%>selected<%}%>>----<%=goodsCate[i].items[j].title%></option> <%}%><%}%></select><input type="hidden" name="cname" id="cname" /> </li><li> <span> 商品圖片:</span> <input type="file" name="goods_img"/> <span> </span> <img class="pic" src="<%=goods.goods_img%>" /></li> <li> <span>商品價(jià)格:</span> <input type="text" name="shop_price" value="<%=goods.shop_price%>"/></li><li> <span>商品原價(jià):</span> <input type="text" name="market_price" value="<%=goods.market_price%>"/></li><li> <span>商品狀態(tài):</span> <input type="radio" name="status" <%if(goods.status==1){%> checked <%}%> value="1" id="a"/> <label for="a">顯示</label> <input type="radio" name="status" <%if(goods.status==0){%> checked <%}%> value="0" id="b"/><label for="b">隱藏</label></li><li> <span>加入推薦:</span> <input type="checkbox" value="1" <%if(goods.is_best==1){%> checked <%}%> name="is_best"/> 精品<input type="checkbox" value="1" name="is_hot" <%if(goods.is_hot==1){%> checked <%}%> /> 熱銷(xiāo)<input type="checkbox" value="1" name="is_new" <%if(goods.is_new==1){%> checked <%}%> /> 新品</li></ul></div><div role="tabpanel" class="tab-pane" id="detail"><textarea name="goods_content" id="content" cols="100" rows="8"><%=goods.goods_content%></textarea></div><div role="tabpanel" class="tab-pane" id="mix"><ul class="form_input"><li> <span>商品顏色:</span><%for(var i=0;i<colorResult.length;i++){%><%if(goods.goods_color.indexOf(colorResult[i]._id.toString())!=-1){%><input type="checkbox" checked name="goods_color" value="<%=colorResult[i]._id%>" id="color_<%=colorResult[i]._id%>" /> <label for="color_<%=colorResult[i]._id%>"><%=colorResult[i].color_name%></label><%}else{%><input type="checkbox" name="goods_color" value="<%=colorResult[i]._id%>" id="color_<%=colorResult[i]._id%>" /> <label for="color_<%=colorResult[i]._id%>"><%=colorResult[i].color_name%></label><%}%><%}%></li><li> <span>關(guān)聯(lián)商品:</span><input type="text" name="relation_goods" class="relation_goods" value="<%=goods.relation_goods%>"/> <i>填寫(xiě)關(guān)聯(lián)商品的id 多個(gè)以逗號(hào)隔開(kāi) 格式:23,24,39</i></li> <li> <span>關(guān)聯(lián)贈(zèng)品:</span><input type="text" name="goods_gift" class="goods_gift" value="<%=goods.goods_gift%>"/> <i>可為空 格式:23-2,39-5 說(shuō)明:例如23-2 中的23表示商品id,2表示商品數(shù)量</i></li> <li> <span>關(guān)聯(lián)配件:</span><input type="text" name="goods_fitting" class="goods_fitting" value="<%=goods.goods_fitting%>"/> <i>可為空 格式:23-2,39-5 說(shuō)明:例如23-2 中的23表示商品id,2表示商品數(shù)量</i></li> <li> <span>更多屬性:</span><input type="text" name="goods_attr" class="goods_attr" value="<%=goods.goods_attr%>"/> <i> 格式: 顏色:紅色,白色,黃色 | 尺寸:41,42,43</i></li><li> <span>Seo關(guān)鍵詞:</span><input type="text" name="goods_keywords" class="goods_keywords" value="<%=goods.goods_keywords%>"/> </li><li> <span>Seo描述:</span> <textarea name="goods_desc" id="goods_desc" cols="100" rows="2"><%=goods.goods_desc%></textarea></li></ul></div><div role="tabpanel" class="tab-pane" id="attribute"><ul class="form_input"><li> <span>商品類(lèi)型: </span><select name="goods_type_id" id="goods_type_id"><option value="0">--請(qǐng)選擇商品類(lèi)型--</option><%for(var i=0;i<goodsType.length;i++){%><option value="<%=goodsType[i]._id%>" <%if(goods.goods_type_id.toString()==goodsType[i]._id.toString()){%>selected<%}%> ><%=goodsType[i].title%></option><%}%></select></li></ul><ul class="form_input" id="goods_type_attribute"><%-goodsAtts%></ul></div><div role="tabpanel" class="tab-pane" id="photo"><div id="goods_image"> <ul id="goods_image_list" class="goods_image_list clear"><%for(var i=0;i<goodsImage.length;i++){%><li><img class="pic" src="<%=goodsImage[i].img_url%>" /><div class="color_list"><select class="relation_goods_color" goods_image_id="<%=goodsImage[i]._id%>"><option value="0">關(guān)聯(lián)顏色</option><%for(var j=0;j<goodsColor.length;j++){%><option value="<%=goodsColor[j]._id%>" <%if(goodsImage[i].color_id.toString()==goodsColor[j]._id){%>selected<%}%> ><%=goodsColor[j].color_name%></option><%}%></select> </div><div class="goods_image_delete" goods_image_id="<%=goodsImage[i]._id%>"></div></li><%}%></ul></div><div id="photoLib" class="photoLib"></div><div id="photoList"></div></div></div><button type="submit" class="btn btn-success goods_content_btn">提交</button></form></div></div></div><!-- 緩存當(dāng)前的屬性信息 --><div id="goods_attr_value" style="display:none"><%-goodsAtts%></div><script>$(function(){//關(guān)聯(lián)商品類(lèi)型$('#goods_type_id').change(function(){// alert($(this).val());var cate_id=$(this).val();if(cate_id=='<%=goods.goods_type_id.toString()%>'){$('#goods_type_attribute').html($('#goods_attr_value').html()); }else{ var data='';$.get('/admin/goods/goodsTypeAttribute?cate_id='+cate_id,function(response){data=response.result; var str="";for(var i=0;i<data.length;i++){if(data[i].attr_type==1){ str+='<li><span>'+data[i].title+': </span><input type="hidden" name="attr_id_list" value="'+data[i]._id+'" /> <input type="text" name="attr_value_list" /></li>' }else if(data[i].attr_type==2){str+='<li><span>'+data[i].title+': </span> <input type="hidden" name="attr_id_list" value="'+data[i]._id+'"> <textarea cols="50" rows="3" name="attr_value_list"></textarea></li>' }else{var arr=data[i].attr_value.split('\n'); str+='<li><span>'+data[i].title+': </span><input type="hidden" name="attr_id_list" value="'+data[i]._id+'">';str+='<select name="attr_value_list">';for(var j=0;j<arr.length;j++){str+='<option value="'+arr[j]+'">'+arr[j]+'</option>';}str+='</select>';str+='</li>';}} $('#goods_type_attribute').html(str);})}})})//富文本編輯器$(function() {$('#content').froalaEditor({height: 300, //給編輯器設(shè)置默認(rèn)的高度language: 'zh_cn', imageUploadURL: '/admin/goods/goodsUploadImage',//根據(jù)不同的分辨率加載不同的配置toolbarButtons: ['fullscreen', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '|', 'fontFamily', 'fontSize', 'color', 'inlineStyle', 'paragraphStyle', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', '-', 'insertLink', 'insertImage', 'insertVideo', 'embedly', 'insertFile', 'insertTable', '|', 'emoticons', 'specialCharacters', 'insertHR', 'selectAll', 'clearFormatting', '|', 'print', 'spellChecker', 'help', 'html', '|', 'undo', 'redo'],toolbarButtonsMD: ['fullscreen', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '|', 'fontFamily', 'fontSize', 'color', 'inlineStyle', 'paragraphStyle', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', '-', 'insertLink', 'insertImage', 'insertVideo', 'embedly', 'insertFile', 'insertTable', '|', 'emoticons', 'specialCharacters', 'insertHR', 'selectAll', 'clearFormatting', '|', 'print', 'spellChecker', 'help', 'html', '|', 'undo', 'redo'],toolbarButtonsSM: ['fullscreen', 'bold', 'italic', 'underline', 'strikeThrough', 'subscript', 'superscript', '|', 'fontFamily', 'fontSize', 'color', 'inlineStyle', 'paragraphStyle', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', '-', 'insertLink', 'insertImage', 'insertVideo', 'embedly', 'insertFile', 'insertTable', '|', 'emoticons', 'specialCharacters', 'insertHR', 'selectAll', 'clearFormatting', '|', 'print', 'spellChecker', 'help', 'html', '|', 'undo', 'redo']});});//批量上傳圖片$(function(){var photoStr=''; $('#photoLib').diyUpload({url:'/admin/goods/goodsUploadPhoto', success:function( response ) {// console.info( data );photoStr='<input type="hidden" name="goods_image_list" value='+response.link+' />';$('#photoList').append(photoStr);},error:function( err ) {console.info( err ); }});})$(function(){//改變顏色$('.relation_goods_color').change(function(){var color_id=$(this).val();var goods_image_id=$(this).attr('goods_image_id');console.log(color_id,goods_image_id);$.post('/admin/goods/changeGoodsImageColor?_csrf=<%=csrf%>',{color_id:color_id,goods_image_id:goods_image_id},function(response){console.log(response);})})//刪除圖片$('.goods_image_delete').click(function(){var _that=this;var flag=confirm('您確定要?jiǎng)h除嗎?');if(flag){var goods_image_id=$(this).attr('goods_image_id');$.post('/admin/goods/goodsImageRemove?_csrf=<%=csrf%>',{goods_image_id:goods_image_id},function(response){console.log(response);if(response.success){$(_that).parent().remove();}})}})})</script> </body> </html>效果
總結(jié)
以上是生活随笔為你收集整理的egg(72,73)--egg之商品curd的编辑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 05《基于深度卷积神经网络的车型识别研究
- 下一篇: 音阶频率对照表_各个音阶的对应频率