com-attr.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <style>
  2. .com-attr .box {
  3. line-height: 64px;
  4. border-top: 1px solid #E8EAEE;
  5. border-left: 1px solid #E8EAEE;
  6. border-right: 1px solid #E8EAEE;
  7. padding: 0 16px;
  8. }
  9. .com-attr .box .batch {
  10. margin-left: -10px;
  11. margin-right: 20px;
  12. }
  13. .com-attr .el-select .el-input {
  14. width: 130px;
  15. }
  16. .com-attr .input-with-select .el-input-group__prepend {
  17. background-color: #fff;
  18. }
  19. .com-attr .header-require:before {
  20. content: '*';
  21. color: #F56C6C;
  22. margin-right: 2px;
  23. }
  24. .text .el-input-group__append{
  25. padding: 0 10px;
  26. }
  27. </style>
  28. <template id="com-attr">
  29. <div class="com-attr">
  30. <div class="box">
  31. <el-checkbox v-model="attrBatch" @change="selectClick" :disabled="!cData || cData.length == 0"
  32. style="position:absolute">全选
  33. </el-checkbox>
  34. <el-form-item label="批量设置" style="margin-bottom:0;padding:18px 0">
  35. <el-input @keyup.enter.native="batchAttr(selectData)"
  36. :type="selectData == 'goods_no' ? 'text' : 'number'" v-model="batch">
  37. <el-select v-model="selectData" slot="prepend">
  38. <el-option v-for="(item, index) in cList" :value="index" :key="index" v-if="index!='pic_url'" :label="item+''">{{item}}
  39. </el-option>
  40. </el-select>
  41. <template slot="append">
  42. <el-button @click="batchAttr(selectData)">
  43. 确定
  44. </el-button>
  45. </template>
  46. </el-input>
  47. </el-form-item>
  48. </div>
  49. <el-table ref="multipleTable" :data="cData" border stripe style="width: 100%"
  50. @selection-change="handleSelectionChange">
  51. <el-table-column type="selection" width="55"></el-table-column>
  52. <el-table-column
  53. v-for="(item, index) in attrGroups"
  54. :key="item.id"
  55. :prop="'attr_list['+index+'].attr_name'"
  56. :label="item.attr_group_name">
  57. </el-table-column>
  58. <el-table-column v-if="cList"
  59. v-for="(item, key, index) in cList"
  60. :key="index"
  61. :property="key"
  62. :label="item + (append ? '(' + append + ')' : '')">
  63. <template slot="header" v-if="key===`price` || key=== `stock`|| key=== `original_price`||key=== `cost_price`">
  64. <div class="header-require">{{item}}</div>
  65. </template>
  66. <template slot-scope="scope">
  67. <template v-if="!isLevel">
  68. <div flex="box:first" v-if="scope.column.property == 'pic_url'" style="padding: 10px;">
  69. <div flex="cross:center" style="margin-right: 10px;position: relative;">
  70. <com-attachment :multiple="false" :params="scope.row" :max="1"
  71. v-model="scope.row[scope.column.property]">
  72. <com-gallery :url="scope.row[scope.column.property]"
  73. width="50px" height="50px"></com-gallery>
  74. </com-attachment>
  75. <el-button v-if="scope.row[scope.column.property]" style="position: absolute; right: -8px; top: -8px; padding: 4px 4px;"
  76. size="mini" type="danger" icon="el-icon-close" circle
  77. @click="scope.row[scope.column.property] = ''"></el-button>
  78. </div>
  79. </div>
  80. <span v-else-if="scope.column.property == 'is_show'">
  81. <el-checkbox v-model="scope.row[scope.column.property]" @change="checked=>handleHide(checked, scope.row)">显示</el-checkbox>
  82. </span>
  83. <el-input v-else-if="scope.column.property == 'goods_no'" v-model="scope.row[scope.column.property]"></el-input>
  84. <!-- <div v-else-if="scope.column.property.indexOf('price') > -1 || scope.column.property.indexOf('level')">{{scope.column.property}}</div> -->
  85. <el-input v-else-if="scope.column.property.indexOf('price') > -1 || scope.column.property.indexOf('level')" type="number" v-model="scope.row[scope.column.property]"></el-input>
  86. <el-input v-else oninput="this.value = this.value.replace(/[^0-9]/g, '');" v-model="scope.row[scope.column.property]">
  87. <template v-if="append" slot="append">{{append}}</template>
  88. </el-input>
  89. </template>
  90. <template v-else>
  91. <el-input :data-id="scope.row[scope.column.label]" type="number" class="text"
  92. v-model="scope.row[memberSettingKey][scope.column.property]">
  93. <template v-if="scope.row.levelPrice && scope.row.levelPrice[scope.column.property] >= 0" slot="append" style="padding: 10px;">参考价:{{scope.row.levelPrice[scope.column.property]}}元</template>
  94. </el-input>
  95. </template>
  96. </template>
  97. </el-table-column>
  98. </el-table>
  99. </div>
  100. </template>
  101. <script>
  102. Vue.component('com-attr', {
  103. template: '#com-attr',
  104. props: {
  105. value: Array, // 商品规格信息
  106. attrGroups: Array, // 商品规格组
  107. extra: Object, // 额外的数据信息
  108. list: Object | Array, // 从排列数据信息
  109. isLevel: Boolean, // 是否是会员
  110. members: Array, // 会员等级列表
  111. share: Array, // 分销等级列表
  112. append: String, // 输入框后缀
  113. paramKey: {
  114. type: String,
  115. default() {
  116. return 'member_price';
  117. }
  118. },
  119. isShowAttr: {
  120. type: Boolean,
  121. default: function () {
  122. return false
  123. }
  124. }
  125. },
  126. data() {
  127. return {
  128. attrBatch: false,
  129. data: {
  130. original_price: '划线价',
  131. cost_price: '成本价',
  132. price: '售价',
  133. stock: '库存',
  134. weight: '重量(克)',
  135. goods_no: '货号',
  136. //pic_url: '规格图片',
  137. },
  138. selectData: '',
  139. batch: 0,
  140. selectList: [],
  141. memberSettingKey: 'member_price_setting',
  142. };
  143. },
  144. created() {
  145. let that = this;
  146. // console.info(this.value,"members");
  147. if (this.isLevel) {
  148. for (let i in this.value){
  149. if (this.value[i][this.memberSettingKey]) {
  150. continue;
  151. }
  152. let price_key = this.value[i].sign_id;
  153. let members = JSON.parse(JSON.stringify(this.members));
  154. let obj = {};
  155. members.forEach(function (memberLevelItem, memberLevelIndex) {
  156. let key = 'level' + memberLevelItem.level;
  157. // console.info(memberLevelItem.value,"memberLevelItem.value",price_key)
  158. if (memberLevelItem.value && JSON.stringify(memberLevelItem.value) != "{}") {
  159. memberLevelItem.value[price_key] = memberLevelItem.value[price_key]?memberLevelItem.value[price_key]:0;
  160. that.$set(obj,key,memberLevelItem.value[price_key]);
  161. } else {
  162. that.$set(obj,key,0);
  163. }
  164. });
  165. that.$set(this.value[i],this.memberSettingKey,obj);
  166. }
  167. console.info(this.value);
  168. }
  169. },
  170. watch: {
  171. 'selectList'(oldData, newData) {
  172. const self = this;
  173. let sign = 0;
  174. this.value.forEach(function (item, index) {
  175. self.selectList.map((item1) => {
  176. if (JSON.stringify(item1.attr_list) === JSON.stringify(item.attr_list)) {
  177. sign++;
  178. }
  179. });
  180. });
  181. self.attrBatch = self.value.length === sign;
  182. },
  183. },
  184. computed: {
  185. cList() {
  186. // TODO 分销数据暂时
  187. if (this.share) {
  188. let share = JSON.parse(JSON.stringify(this.share));
  189. let obj = {};
  190. for (let i = 0; i < share.length; i++) {
  191. obj[share[i].value] = share[i].label;
  192. }
  193. return obj;
  194. }
  195. // TODO 会员数据暂时
  196. if (this.isLevel) {
  197. let members = JSON.parse(JSON.stringify(this.members));
  198. let obj = {};
  199. for (let i = 0; i < members.length; i++) {
  200. if(members[i].level == 0){
  201. continue;
  202. }
  203. obj['level' + members[i].level] = members[i].name
  204. }
  205. return obj;
  206. } else {
  207. if(this.isShowAttr && !this.data.hasOwnProperty('is_show')){
  208. this.data['is_show'] = '显示sku'
  209. }
  210. if (this.extra) {
  211. return Object.assign(this.data, JSON.parse(JSON.stringify(this.extra)));
  212. } else if (this.list) {
  213. return JSON.parse(JSON.stringify(this.list))
  214. } else {
  215. return this.data;
  216. }
  217. }
  218. },
  219. cData() {
  220. if (this.attrGroups && this.attrGroups.length > 0 && this.attrGroups[0].attr_list.length === 0) {
  221. return [];
  222. } else {
  223. return this.value;
  224. }
  225. },
  226. },
  227. methods: {
  228. handleHide(checked, row){
  229. // 确保每个规格组下的规格值至少有一个是显示的
  230. if(checked == true) return
  231. let cData = this.cData
  232. let attrGroups = this.attrGroups
  233. for(let j = 0; j < attrGroups.length; j++){
  234. let attrList = attrGroups[j]['attr_list']
  235. if(attrGroups.length > 1){// 多个规格组
  236. for(let m = 0; m < attrList.length; m++){
  237. // 显示的sku数量
  238. let count = 0
  239. for(let i = 0; i < cData.length; i++){
  240. let sign_id_arr = cData[i].sign_id.split(':')
  241. if(sign_id_arr.indexOf(attrList[m].attr_id.toString()) > -1){
  242. // 存在显示的规格值
  243. if(cData[i].is_show == true){
  244. count = 1
  245. break
  246. }
  247. }
  248. }
  249. if(count <= 0){
  250. this.$message.warning('每个规格组下至少有一个规格值是显示的')
  251. row.is_show = true
  252. return
  253. }
  254. }
  255. }else{//一个规格组
  256. let count = 0
  257. for(let i = 0; i < cData.length; i++){
  258. if(cData[i].is_show == true){
  259. count = 1
  260. break
  261. }
  262. }
  263. if(count <= 0){
  264. this.$message.warning('每个规格组下至少有一个规格值是显示的')
  265. row.is_show = true
  266. return
  267. }
  268. }
  269. }
  270. },
  271. selectClick() {
  272. this.$refs.multipleTable.toggleAllSelection();
  273. },
  274. handleSelectionChange(data) {
  275. this.selectList = data;
  276. },
  277. batchAttr(param) {
  278. if (!param) {
  279. this.$message.warning('请选择批量设置');
  280. return;
  281. }
  282. if (!this.selectList || this.selectList.length === 0) {
  283. this.$message.warning('请勾选商品规格');
  284. return;
  285. }
  286. let self = this;
  287. let batch = self.batch;
  288. this.value.forEach((item, index) => {
  289. console.info(item,self.selectList);
  290. let sign = false;
  291. self.selectList.map((item1) => {
  292. if (JSON.stringify(item1.attr_list) === JSON.stringify(item.attr_list)) {
  293. sign = true;
  294. }
  295. });
  296. if (sign) {
  297. // 批量设置会员价
  298. // 判断字符串是否出现过,并返回位置
  299. console.info(param);
  300. if (param.indexOf('level') !== -1) {
  301. // item[this.paramKey][param] = batch;
  302. // item.levelPrice[param] = batch;
  303. item[self.memberSettingKey][param] = batch;
  304. } else {
  305. self.$set(item,param,batch)
  306. // item[memberSettingKey][param] = batch;
  307. }
  308. }
  309. });
  310. }
  311. }
  312. });
  313. </script>