add.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. {extend name="common/base"/}
  2. {block name="style"}
  3. <style>
  4. .upload-img {
  5. width: 120px;
  6. height: 90px;
  7. overflow: hidden;
  8. position: relative;
  9. border: 1px solid #eee;
  10. padding: 1px;
  11. margin: 5px;
  12. float: left;
  13. }
  14. .upload-close {
  15. position: absolute;
  16. top: 1px;
  17. right: 1px;
  18. }
  19. </style>
  20. {/block}
  21. <!-- 主体 -->
  22. {block name="body"}
  23. <form class="layui-form p-4">
  24. <h3 class="pb-3">新建商品</h3>
  25. <table class="layui-table layui-table-form">
  26. <tr>
  27. <td class="layui-td-gray">商品标题<font>*</font></td>
  28. <td colspan="5"><input type="text" name="title" lay-verify="required" lay-reqText="请输入商品标题" placeholder="请输入商品标题" class="layui-input"></td>
  29. <td class="layui-td-gray">商品分类<font>*</font></td>
  30. <td>
  31. <select name="cate_id" lay-verify="required" lay-reqText="请选择商品分类">
  32. <option value="">请选择商品分类</option>
  33. {volist name=":set_recursion(get_goods_cate())" id="v"}
  34. <option value="{$v.id}">{$v.title}</option>
  35. {/volist}
  36. </select>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td class="layui-td-gray">关键字<font>*</font></td>
  41. <td colspan="5">
  42. <input type="text" id="keyword_name" name="keyword_names" lay-verify="required" lay-reqText="请选择关键字" placeholder="请选择关键字" class="layui-input" readonly>
  43. <input type="hidden" id="keyword_id" name="keyword_ids">
  44. </td>
  45. <td class="layui-td-gray" rowspan="3">缩略图<font>*</font></td>
  46. <td rowspan="3" style="vertical-align:top">
  47. <div class="layui-upload" style="text-align:center;">
  48. <button type="button" class="layui-btn layui-btn-sm" id="upload_btn_thumb">上传商品封面图(尺寸:750x560)</button>
  49. <div class="layui-upload-list" id="upload_box_thumb">
  50. <img src="" width="100" style="width:200px;max-width:200px" />
  51. <input type="hidden" name="thumb" value="" lay-verify="required" lay-reqText="请上传缩略图">
  52. </div>
  53. </div>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td class="layui-td-gray">商品卖点<font>*</font></td>
  58. <td colspan="5">
  59. <input type="text" name="tips" lay-verify="required" lay-reqText="请输入商品卖点" placeholder="一句话描述商品卖点,30字以内" class="layui-input">
  60. </td>
  61. </tr>
  62. <tr>
  63. <td class="layui-td-gray" style="vertical-align:top;">商品简介</td>
  64. <td colspan="5">
  65. <textarea name="desc" placeholder="请输入商品简介,200字以内" class="layui-textarea"></textarea>
  66. </td>
  67. </tr>
  68. <tr>
  69. <td class="layui-td-gray">商品标签<font>*</font></td>
  70. <td colspan="7">
  71. <input type="checkbox" name="tag_values[]" title="正品保证" lay-skin="primary" value="1" checked />
  72. <input type="checkbox" name="tag_values[]" title="一年保修" lay-skin="primary" value="2" checked />
  73. <input type="checkbox" name="tag_values[]" title="七天退换(拆封后不支持)" lay-skin="primary" value="3" />
  74. <input type="checkbox" name="tag_values[]" title="赠运费险" lay-skin="primary" value="4" />
  75. <input type="checkbox" name="tag_values[]" title="闪电发货" lay-skin="primary" value="5" />
  76. <input type="checkbox" name="tag_values[]" title="售后无忧" lay-skin="primary" value="6" />
  77. </td>
  78. </tr>
  79. <tr>
  80. <td class="layui-td-gray">市场价格<font>*</font></td>
  81. <td>
  82. <input type="text" name="base_price" lay-verify="required|number" lay-reqText="请输入市场价格" placeholder="请输入市场价格" class="layui-input">
  83. </td>
  84. <td class="layui-td-gray">实际价格<font>*</font></td>
  85. <td>
  86. <input type="text" name="price" lay-verify="required|number" lay-reqText="请输入实际价格" placeholder="请输入实际价格" class="layui-input">
  87. </td>
  88. <td class="layui-td-gray">商品库存<font>*</font></td>
  89. <td>
  90. <input type="text" name="stocks" lay-verify="required|number" lay-reqText="请输入商品库存" placeholder="请输入商品库存" class="layui-input">
  91. </td>
  92. <td class="layui-td-gray">是否包邮<font>*</font></td>
  93. <td>
  94. <input type="radio" name="is_mail" value="1" title="是" checked lay-verify="otherReq" lay-reqText="请选择是否包邮">
  95. <input type="radio" name="is_mail" value="0" title="否" lay-verify="otherReq" lay-reqText="请选择是否包邮">
  96. </td>
  97. </tr>
  98. <tr>
  99. <td class="layui-td-gray">商品属性</td>
  100. <td>
  101. <select name="type">
  102. <option value="">请选择属性</option>
  103. <option value="1">精华</option>
  104. <option value="2">热门</option>
  105. <option value="3">推荐</option>
  106. </select>
  107. </td>
  108. <td class="layui-td-gray">排序</td>
  109. <td>
  110. <input type="text" name="sort" value="0" lay-verify="number" placeholder="请输入排序,数字" class="layui-input">
  111. </td>
  112. <td class="layui-td-gray">首页显示</td>
  113. <td>
  114. <input type="radio" name="is_home" value="1" title="是" checked>
  115. <input type="radio" name="is_home" value="0" title="否">
  116. </td>
  117. <td class="layui-td-gray">状态<font>*</font></td>
  118. <td>
  119. <input type="radio" name="status" value="1" title="正常" checked>
  120. <input type="radio" name="status" value="0" title="下架">
  121. </td>
  122. </tr>
  123. <tr>
  124. <td class="layui-td-gray">商品图集</td>
  125. <td colspan="7">
  126. <div class="layui-upload">
  127. <button type="button" class="layui-btn layui-btn-sm" id="uploadBtn2">上传商品图</button>
  128. <div class="layui-upload-list clearfix" id="demo2">
  129. <input type="hidden" name="banner" value="">
  130. </div>
  131. </div>
  132. </td>
  133. </tr>
  134. <tr>
  135. <td colspan="8" class="layui-td-gray" style="text-align:left">商品介绍<font>*</font></td>
  136. </tr>
  137. <tr>
  138. <td colspan="8">
  139. <textarea class="layui-textarea" id="container_content"></textarea>
  140. </td>
  141. </tr>
  142. </table>
  143. <div class="pt-3">
  144. <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
  145. <button type="reset" class="layui-btn layui-btn-primary">重置</button>
  146. </div>
  147. </form>
  148. {/block}
  149. <!-- /主体 -->
  150. <!-- 脚本 -->
  151. {block name="script"}
  152. <script>
  153. // 查找指定的元素在数组中的位置
  154. Array.prototype.indexOf = function (val) {
  155. for (var i = 0; i < this.length; i++) {
  156. if (this[i] == val) {
  157. return i;
  158. }
  159. }
  160. return -1;
  161. };
  162. // 通过索引删除数组元素
  163. Array.prototype.remove = function (val) {
  164. var index = this.indexOf(val);
  165. if (index > -1) {
  166. this.splice(index, 1);
  167. }
  168. };
  169. var moduleInit = ['tool','tagpicker','tinymce'];
  170. function gouguInit() {
  171. var form = layui.form, tool = layui.tool,tagpicker = layui.tagpicker;
  172. //上传缩略图
  173. var upload_thumb = layui.upload.render({
  174. elem: '#upload_btn_thumb',
  175. url: '/admin/api/upload',
  176. done: function (res) {
  177. //如果上传失败
  178. if (res.code == 1) {
  179. return layer.msg('上传失败');
  180. }
  181. //上传成功
  182. $('#upload_box_thumb input').attr('value', res.data.id);
  183. $('#upload_box_thumb img').attr('src', res.data.filepath);
  184. }
  185. });
  186. //banner图上传
  187. var uploadInst2 = layui.upload.render({
  188. elem: '#uploadBtn2'
  189. , url: '/admin/api/upload'
  190. , done: function (res) {
  191. //如果上传失败
  192. if (res.code == 1) {
  193. return layer.msg('上传失败');
  194. }
  195. //上传成功
  196. var idsStr = $('#demo2 input').val();
  197. var idsArray = [];
  198. if (idsStr != '') {
  199. idsArray = idsStr.split(",");
  200. }
  201. idsArray.push(res.data.id);
  202. $('#demo2 input').attr('value', idsArray.join(','));
  203. $('#demo2').append('<div class="upload-img img-cover" id="uploadImg' + res.data.id + '"><div class="gg-img-cover cover-4-3"><img src="' + res.data.filepath + '"><div class="upload-close"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delimg" data-id="' + res.data.id + '">删除</a></div></div>');
  204. }
  205. });
  206. $('#demo2').on('click', '[lay-event="delimg"]', function () {
  207. var _id = $(this).data('id');
  208. var idsStr = $('#demo2 input').val();
  209. var idsArray = [];
  210. if (idsStr != '') {
  211. idsArray = idsStr.split(",");
  212. }
  213. idsArray.remove(_id);
  214. $('#demo2 input').attr('value', idsArray.join(','));
  215. $('#uploadImg' + _id).remove();
  216. })
  217. var tags = new tagpicker({
  218. 'url': '/admin/api/get_keyword_cate',
  219. 'target': 'keyword_name',
  220. 'tag_ids': 'keyword_id',
  221. 'tag_tags': 'keyword_name',
  222. 'height': 500,
  223. 'isDiy': 1
  224. });
  225. //商品描述富文本编辑器
  226. var edit = layui.tinymce.render({
  227. selector: '#container_content',
  228. height: 500
  229. });
  230. //自定义验证规则
  231. form.verify({
  232. otherReq: function (value, item) {
  233. var verifyName = $(item).attr('name')
  234. , verifyType = $(item).attr('type')
  235. , formElem = $(item).parents('.layui-form')//获取当前所在的form元素,如果存在的话
  236. , verifyElem = formElem.find('input[name=' + verifyName + ']')//获取需要校验的元素
  237. , isTrue = verifyElem.is(':checked')//是否命中校验
  238. , focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
  239. if (!isTrue || !value) {
  240. //定位焦点
  241. focusElem.css(verifyType == 'radio' ? { "color": "#FF5722" } : { "border-color": "#FF5722" });
  242. //对非输入框设置焦点
  243. focusElem.first().attr("tabIndex", "1").css("outline", "0").blur(function () {
  244. focusElem.css(verifyType == 'radio' ? { "color": "" } : { "border-color": "" });
  245. }).focus();
  246. var reqText = verifyElem.attr('lay-reqText');
  247. if (reqText && reqText != '') {
  248. return reqText;
  249. }
  250. else {
  251. return '必填项不能为空';
  252. }
  253. }
  254. }
  255. });
  256. //监听提交
  257. form.on('submit(webform)', function (data) {
  258. data.field.content = tinyMCE.editors['container_content'].getContent();
  259. if (data.field.content == '') {
  260. layer.msg('请先完善商品描述内容');
  261. return false;
  262. }
  263. let callback = function (e) {
  264. layer.msg(e.msg);
  265. if (e.code == 0) {
  266. tool.sideClose(1000);
  267. }
  268. }
  269. tool.post("/admin/goods/add", data.field, callback);
  270. return false;
  271. });
  272. }
  273. </script>
  274. {/block}
  275. <!-- /脚本 -->