attr.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. {include file="public/head"}
  5. <title>{$title|default=''}</title>
  6. <style>
  7. .demo-upload{
  8. display: block;
  9. height: 33px;
  10. text-align: center;
  11. border: 1px solid transparent;
  12. border-radius: 4px;
  13. overflow: hidden;
  14. background: #fff;
  15. position: relative;
  16. box-shadow: 0 1px 1px rgba(0,0,0,.2);
  17. margin-right: 4px;
  18. }
  19. .demo-upload img{
  20. width: 100%;
  21. height: 100%;
  22. display: block;
  23. }
  24. .demo-upload-cover{
  25. display: none;
  26. position: absolute;
  27. top: 0;
  28. bottom: 0;
  29. left: 0;
  30. right: 0;
  31. background: rgba(0,0,0,.6);
  32. }
  33. .demo-upload:hover .demo-upload-cover{
  34. display: block;
  35. }
  36. .demo-upload-cover i{
  37. color: #fff;
  38. font-size: 20px;
  39. cursor: pointer;
  40. margin: 0 2px;
  41. }
  42. .check{color: #f00}
  43. </style>
  44. </head>
  45. <body>
  46. <div id="store-attr" class="mp-form" v-cloak="">
  47. <i-Form :label-width="80" style="width: 100%" v-show="hidden == false">
  48. <Form-Item>
  49. <Row>
  50. <i-Col span="5">
  51. <i-Button type="dashed" long @click="hiddenBool" icon="plus-round">添加新规则</i-Button>
  52. </i-Col>
  53. </Row>
  54. </Form-Item>
  55. </i-Form>
  56. <i-Form :label-width="80" style="width: 100%" v-show="hidden == true">
  57. <Form-Item
  58. :label="'规则名称:'">
  59. <Row>
  60. <i-Col style="position: relative;margin-right: 6px" span="5"
  61. v-for="(item, index) in items"
  62. :key="index">
  63. <i-Input type="text" v-model="item.value" placeholder="设置名称"></i-Input>
  64. <i-Button style="position: absolute;top:0;right:0;margin-top:1px;border: none;font-size: 8px;line-height: 1.8" type="ghost" @click="handleRemove(index)" v-show="item.attrHidden == true"><Icon type="close-round" /></i-Button>
  65. <i-Button style="position: absolute;top:0;right:0;margin-top:1px;border: none;font-size: 8px;line-height: 1.8" type="ghost" @click="attrHiddenBool(item)" v-show="item.attrHidden == false"><Icon type="checkmark-round"></Icon></i-Button>
  66. </i-Col>
  67. <i-Col span="5">
  68. <i-Button type="dashed" long @click="handleAdd" icon="plus-round">添加新规则</i-Button>
  69. </i-Col>
  70. </Row>
  71. </Form-Item>
  72. <Form-Item v-show="item.attrHidden == true"
  73. v-for="(item, index) in items"
  74. :key="index"
  75. :label="''+item.value+':'" >
  76. <Row>
  77. <i-Col span="3"
  78. v-for="(attr,k) in item.detail"
  79. :key="attr"
  80. :name="attr">
  81. <Tag type="border" closable color="blue" @on-close="attrRemove(item,k)">{{ attr }}</Tag>
  82. </i-Col>
  83. <i-Col span="5">
  84. <i-Input type="text" v-model="item.detailValue" placeholder="设置属性"></i-Input>
  85. </i-Col>
  86. <i-Col span="5">
  87. <i-Button type="primary" style="margin-left: 6px" @click="attrAdd(item)">添加</i-Button>
  88. </i-Col>
  89. </Row>
  90. </Form-Item>
  91. <Form-Item v-show="hidden == true" style="width: 100%;">
  92. <Row style="margin: 0 88px 0 20px">
  93. <i-Col span="24">
  94. <i-Button type="primary" long @click="addGoods(true)">生成</i-Button>
  95. </i-Col>
  96. </Row>
  97. </Form-Item>
  98. <template v-if="items[0].value!='' && items[0].detail.length>0 && attrs.length">
  99. <template v-for="(attr,index) in attrs">
  100. <Form-Item>
  101. <Row>
  102. <template v-for="(item,index) in attr.detail">
  103. <i-Col span="3" style="margin-right: 3px">
  104. {{index}}:{{item}}
  105. </i-Col>
  106. </template>
  107. <i-Col span="5" style="margin-right: 3px">
  108. <span :class="attr.check ? 'check':''">金额:</span><i-Input placeholder="请输入金额" v-model="attr.price" style="width: 68%"
  109. :number="true"></i-Input>
  110. </i-Col>
  111. <i-Col span="5" style="margin-right: 3px">
  112. <span :class="attr.check ? 'check':''">库存:</span><i-Input placeholder="请输入库存" v-model="attr.sales" style="width: 68%"
  113. :number="true"></i-Input>
  114. </i-Col>
  115. <i-Col span="2" offset="1" style="margin-right: 3px">
  116. <div class="demo-upload">
  117. <img :src="attr.pic">
  118. <div class="demo-upload-cover">
  119. <Icon type="ios-eye-outline" @click.native="openPic(attr.pic)" ></Icon>
  120. <Upload
  121. :show-upload-list="false"
  122. :on-success="uploadSuccess(attr)"
  123. :on-error="uploadError"
  124. :format="['jpg','jpeg','png']"
  125. :max-size="2048"
  126. accept="image/*"
  127. :on-format-error="uploadFormatError"
  128. action="{:Url('upload')}"
  129. style="display: inline-block"
  130. :goods="attr"
  131. >
  132. <Icon type="ios-cloud-upload-outline"></Icon>
  133. </Upload>
  134. </div>
  135. </div>
  136. </i-Col>
  137. <i-Col span="2" style="margin-right: 3px">
  138. <i-Button type="ghost" @click="removeGoods(index)">删除</i-Button>
  139. </i-Col>
  140. </Row>
  141. </Form-Item>
  142. </template>
  143. <Form-Item>
  144. <Row>
  145. <!-- <i-Col span="10">-->
  146. <!-- <i-Button type="dashed" long @click="addGoods" icon="plus-round">添加新商品</i-Button>-->
  147. <!-- </i-Col>-->
  148. <i-Col span="2" offset="2">
  149. <i-Button type="primary" @click="submit">提交</i-Button>
  150. </i-Col>
  151. <i-Col span="2" offset="1">
  152. <i-Button type="error" @click="clear">清空所有属性</i-Button>
  153. </i-Col>
  154. </Row>
  155. </Form-Item>
  156. </template>
  157. </i-Form>
  158. <Spin fix v-show="submiting == true">保存中...</Spin>
  159. </div>
  160. <script>
  161. var _vm ;
  162. mpFrame.start(function(Vue){
  163. new Vue({
  164. data () {
  165. return {
  166. hidden:false,
  167. submiting :false,
  168. items: <?php echo $result && isset($result['attr']) && !empty($result['attr']) ? json_encode($result['attr']) : 'false'; ?> || [
  169. {
  170. value: '',
  171. detailValue:'',
  172. attrHidden:false,
  173. detail:[]
  174. }
  175. ],
  176. attrs:<?php echo $result && isset($result['value']) && !empty($result['value']) ? json_encode($result['value']) : '[]'; ?>
  177. }
  178. },
  179. watch:{
  180. items:{
  181. handler:function(){
  182. // this.attrs = [];
  183. },
  184. deep:true
  185. }
  186. },
  187. methods: {
  188. attrHiddenBool(item){
  189. if(item.value == ''){
  190. $eb.message('error','请填写规则名称');
  191. }else{
  192. item.attrHidden = true;
  193. }
  194. },
  195. hiddenBool(){
  196. this.hidden = true;
  197. },
  198. handleAdd () {
  199. if(!this.checkAttr())return ;
  200. this.items.push({
  201. value: '',
  202. detailValue:'',
  203. attrHidden:false,
  204. detail:[]
  205. });
  206. },
  207. checkAttr(){
  208. var bool = true;
  209. this.items.map(function(item){
  210. if(!bool) return;
  211. if(!item.value){
  212. $eb.message('error','请填写规则名称');
  213. bool = false;
  214. }else if(!item.detail.length){
  215. $eb.message('error','请设置规则属性');
  216. bool = false;
  217. }
  218. });
  219. return bool;
  220. },
  221. attrAdd (item) {
  222. if(!item.detailValue) return false;
  223. item.detail.push(item.detailValue);
  224. item.detailValue = '';
  225. },
  226. handleRemove (index) {
  227. if(this.items.length > 1)
  228. this.items.splice(index,1);
  229. else
  230. $eb.message('error','请设置至少一个规则');
  231. },
  232. attrRemove(item,k){
  233. if(1==item.detail.length){
  234. $eb.message('error','请设置至少一个属性');
  235. return false;
  236. }
  237. item.detail.splice(k,1);
  238. },
  239. removeGoods(index){
  240. this.attrs.splice(index,1);
  241. },
  242. checkGoods(){
  243. var bool = true;
  244. this.attrs.map(function(attr){
  245. if(!bool) return ;
  246. if(!Object.keys(attr.detail).length){
  247. $eb.message('error','请选择至少一个属性');
  248. bool = false;
  249. }else if(attr.price != parseFloat(attr.price) || attr.price < 0){
  250. $eb.message('error','请输入正确的商品价格');
  251. bool = false;
  252. }else if(attr.sales != parseInt(attr.sales) || attr.sales < 0){
  253. $eb.message('error','请输入正确的商品库存');
  254. bool = false;
  255. }
  256. });
  257. return bool;
  258. },
  259. addGoods(type){
  260. var that = this;
  261. if(this.attrs.length){
  262. if(!this.checkGoods())return ;
  263. }
  264. $eb.axios.post("{:Url('is_format_attr',array('id'=>$id))}",{items:this.items,attrs:this.attrs}).then(function(res){
  265. if(res.data.code == 200){
  266. that.attrs = res.data.data
  267. }else{
  268. $eb.message('error',res.data.msg);
  269. }
  270. }).catch(function(err){
  271. if(res.data.code == 200){
  272. that.attrs = res.data.data
  273. }else{
  274. $eb.message('error',res.data.msg);
  275. }
  276. })
  277. // if(type === true){
  278. // this.attrs = [{
  279. // detail:{},
  280. // price:'',
  281. // sales:'',
  282. // pic:'{$image}'
  283. // }];
  284. // }else{
  285. // this.attrs.push({
  286. // detail:{},
  287. // price:'',
  288. // sales:'',
  289. // pic:'{$image}'
  290. // });
  291. // }
  292. },
  293. openPic(src){
  294. $eb.openImage(src);
  295. },
  296. uploadSuccess(data){
  297. return function(response, file, fileList){
  298. if(response.code == 200){
  299. data.pic = response.data.url;
  300. }else{
  301. $eb.message('error',response.data.msg || '图片上传失败!');
  302. }
  303. }
  304. },
  305. uploadError(error, file, fileList){
  306. $eb.message('error',error);
  307. },
  308. uploadFormatError(file, fileList){
  309. $eb.message('error','图片格式错误');
  310. },
  311. submit(){
  312. var that = this;
  313. that.submiting = true;
  314. if(!this.checkAttr() || !this.checkGoods()) return ;
  315. for(let attr in that.attrs){
  316. that.attrs[attr].check = false;
  317. }
  318. $eb.axios.post("{:Url('set_attr',array('id'=>$id))}",{items:this.items,attrs:this.attrs}).then(function(res){
  319. that.submiting = false;
  320. if(res.status == 200 && res.data.code == 200){
  321. $eb.message('success',res.data.msg || '编辑成功!');
  322. $eb.closeModalFrame(window.name);
  323. }else{
  324. $eb.message('error',res.data.msg || '请求失败!');
  325. }
  326. }).catch(function(err){
  327. $eb.message('error',err);
  328. })
  329. },
  330. clear(){
  331. var that = this;
  332. requirejs(['sweetalert'], function (swel) {
  333. swel({
  334. title: "您确定要清空商品属性吗",
  335. text: "删除后将无法恢复,请谨慎操作!",
  336. type: "warning",
  337. showCancelButton: true,
  338. confirmButtonColor: "#DD6B55",
  339. confirmButtonText: "是的,我要清空!",
  340. cancelButtonText: "让我再考虑一下…",
  341. closeOnConfirm: false,
  342. closeOnCancel: false
  343. }).then(function () {
  344. $eb.axios.post("{:Url('clear_attr',array('id'=>$id))}", {
  345. items: that.items,
  346. attrs: that.attrs
  347. }).then(function (res) {
  348. if (res.status == 200 && res.data.code == 200) {
  349. $eb.message('success', res.data.msg || '清空成功!');
  350. window.location.reload();
  351. } else {
  352. $eb.message('error', res.data.msg || '清空失败!');
  353. }
  354. }).catch(function (err) {
  355. $eb.message('error', err);
  356. })
  357. }).catch(console.log);
  358. });
  359. }
  360. },
  361. mounted (){
  362. _vm = this;
  363. var resultAdmin = <?php echo $result && isset($result['attr']) && !empty($result['attr']) ? json_encode($result['attr']) : 'false'; ?>;
  364. if(resultAdmin) this.hidden = true;
  365. }
  366. }).$mount(document.getElementById('store-attr'));
  367. });
  368. </script>
  369. </body>