form.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <!-- -->
  2. <template>
  3. <div class="m-container">
  4. <h2>
  5. <el-page-header @back="onCancel"
  6. :content="(payType == 'create' ? '新增服务' : '修改服务')"></el-page-header>
  7. </h2>
  8. <div class="m-core" style="overflow: hidden; background-color: #eef4f9; padding: 0;">
  9. <el-form :model="form"
  10. ref="accountForm"
  11. :inline="true"
  12. label-position="right"
  13. label-width="120px">
  14. <div style="background: #fff;padding: 16px 34px 10px;margin-bottom: 20px;">
  15. <el-alert
  16. title="服务信息"
  17. type="info"
  18. :closable="false"
  19. class="smallTitle"
  20. ></el-alert>
  21. <el-form-item label="服务名称" prop="name" :rules="[{ required: true, message: '请输入服务名称', trigger: 'blur' }]">
  22. <el-input v-model="form.name" placeholder="请输入服务名称"></el-input>
  23. </el-form-item>
  24. <el-form-item label="产品服务" prop="productId" :rules="[{ required: true, message: '请选择产品服务', trigger: 'change' }]">
  25. <el-select v-model="form.productId" multiple collapse-tags clearable placeholder="请选择产品服务">
  26. <el-option v-for="(item, index) in productList"
  27. :key="index"
  28. :label="item.name"
  29. :value="item.id"></el-option>
  30. </el-select>
  31. </el-form-item>
  32. </div>
  33. <div style="background: #fff;padding: 16px 34px 10px;margin-bottom: 20px;">
  34. <el-alert
  35. title="服务属性"
  36. type="info"
  37. :closable="false"
  38. class="smallTitle"
  39. ></el-alert>
  40. <div class="serviceAttr" v-for="(attrList, index) in form.attrList" :key="index">
  41. <div class="serviceBlock">
  42. <el-form-item label="付费模式"
  43. :prop="'attrList.' + index + '.mode'"
  44. :rules="[{ required: true, message: '请选择付费模式', trigger: 'change' }]">
  45. <el-select v-model="attrList.mode" placeholder="请选择付费模式">
  46. <el-option label="月" value="MONTH"></el-option>
  47. <el-option label="年" value="YEAR"></el-option>
  48. </el-select>
  49. </el-form-item>
  50. <el-form-item class="add">
  51. <el-button icon="el-icon-plus" plain style="width: 140px;border-color: #00A79D;color: #00A79D;" @click="onAddItem('child', attrList.attr)">添加模式</el-button>
  52. </el-form-item>
  53. </div>
  54. <div class="serviceBlock">
  55. <div v-for="(attrItem, i) in attrList.attr" :key="i">
  56. <el-form-item label="学员上限"
  57. :prop="'attrList.' + index + '.attr.' + i + '.studentUpLimit'"
  58. :rules="[{ required: true, message: '请输入学员上限', trigger: 'blur' },
  59. {type: 'number', message: '学员上限必须为数字值' }]">
  60. <el-input v-model.number="attrItem.studentUpLimit" placeholder="请输入学员上限">
  61. <span slot="append">人</span>
  62. </el-input>
  63. </el-form-item>
  64. <el-form-item label="服务原价"
  65. :prop="'attrList.' + index + '.attr.' + i + '.originalPrice'"
  66. :rules="[{ required: true, message: '请输入服务原价', trigger: 'blur' },
  67. {type: 'number', message: '服务原价必须为数字值' }]">
  68. <el-input v-model.number="attrItem.originalPrice" placeholder="请输入服务原价">
  69. <span slot="append">元</span>
  70. </el-input>
  71. </el-form-item>
  72. <el-button circle type="text" @click="onRemoveItem(attrList.attr, i)" :disabled="attrList.attr.length <= 1">删除</el-button>
  73. </div>
  74. </div>
  75. <div class="list-remove">
  76. <el-button type="text" icon="el-icon-delete" @click="onRemoveItem(form.attrList, index)" :disabled="form.attrList.length <= 1">删除</el-button>
  77. </div>
  78. </div>
  79. <el-form-item class="add" style="margin-top: 18px; padding-left: 50px;">
  80. <el-button icon="el-icon-plus" type="primary" style="width: 160px" :disabled="form.attrList.length >= 2" @click="onAddItem('parent')">添加模式</el-button>
  81. </el-form-item>
  82. <div class="btn-group">
  83. <el-button @click="onCancel" >取 消</el-button>
  84. <el-button type="primary" @click="onSubmit('accountForm')">确 定</el-button>
  85. </div>
  86. </div>
  87. </el-form>
  88. </div>
  89. </div>
  90. </template>
  91. <script>
  92. import { platformProductQueryPage } from '../productManger/api'
  93. import { platformServeAdd, platformServeUpdate, platformServeQueryInfo } from './api'
  94. export default {
  95. name: 'serviceManger',
  96. data () {
  97. const query = this.$route.query
  98. return {
  99. payType: query.type,
  100. productList: [],
  101. form: {
  102. attrList: [{
  103. mode: null,
  104. attr: [{
  105. originalPrice: null,
  106. studentUpLimit: null,
  107. }]
  108. }],
  109. name: null,
  110. productId: [],
  111. id: query.id || null
  112. }
  113. };
  114. },
  115. mounted () {
  116. this.__init()
  117. },
  118. methods: {
  119. async __init () {
  120. try {
  121. const res = await platformProductQueryPage({ page: 1, rows: 999 })
  122. this.productList = res.data?.rows || []
  123. if (this.payType == 'update') {
  124. const queryInfo = await platformServeQueryInfo({ id: this.form.id })
  125. console.log(queryInfo)
  126. const result = queryInfo.data || null
  127. if(result) {
  128. this.form.productId = result.productId.split(',').map(i => Number(i)),
  129. this.form.name = result.name,
  130. this.form.attrList = this.formatAttr(result.detailList, 'asc')
  131. }
  132. }
  133. } catch(e) {}
  134. },
  135. onAddItem(type, row) {
  136. // 添加子项目
  137. if(type == 'child') {
  138. row.push({
  139. mode: null,
  140. originalPrice: null,
  141. studentUpLimit: null,
  142. })
  143. } else if(type == 'parent') {
  144. this.form.attrList.push({
  145. mode: null,
  146. attr: [{
  147. originalPrice: null,
  148. studentUpLimit: null,
  149. }]
  150. })
  151. }
  152. },
  153. onRemoveItem(row, index) {
  154. this.$confirm("是否删除?", "提示", {
  155. confirmButtonText: "确定",
  156. cancelButtonText: "取消",
  157. type: "warning",
  158. }).then( async() => {
  159. row.splice(index, 1)
  160. });
  161. },
  162. async onSubmit (formName) {
  163. this.$refs[formName].validate(valid => {
  164. if (valid) {
  165. const { attrList, productId, ...res } = this.form
  166. let isYear = 0, isMonth = 0
  167. attrList.forEach(attr => {
  168. if(attr.mode == 'YEAR') {
  169. isYear++
  170. } else if(attr.mode == 'MONTH') {
  171. isMonth ++
  172. }
  173. })
  174. if(isYear > 1 || isMonth > 1) {
  175. this.$message.error('付费模式重复')
  176. return
  177. }
  178. const params = {
  179. ...res,
  180. productId: productId.join(','),
  181. detailList: this.formatAttr(attrList)
  182. }
  183. if (this.payType == 'create') {
  184. platformServeAdd(params).then(res => {
  185. if (res.code == 200) {
  186. this.$message.success('保存成功')
  187. this.onCancel()
  188. } else {
  189. this.$message.error(res.msg)
  190. }
  191. })
  192. } else if (this.payType == 'update') {
  193. platformServeUpdate(params).then(res => {
  194. if (res.code == 200) {
  195. this.$message.success('保存成功')
  196. this.onCancel()
  197. } else {
  198. this.$message.error(res.msg)
  199. }
  200. })
  201. }
  202. } else {
  203. this.$nextTick(() => {
  204. let isError = document.getElementsByClassName('is-error')
  205. isError[0].scrollIntoView({
  206. block: 'center',
  207. behavior: 'smooth',
  208. })
  209. })
  210. return false;
  211. }
  212. });
  213. },
  214. formatAttr(row, type) { // 格式化数据
  215. let resultArray = []
  216. if(!row) return
  217. if(type == 'asc') { // 修改时获取数据重新拼装数据
  218. let isType = []
  219. row.forEach(ele => {
  220. if(isType.includes(ele.mode)) {
  221. resultArray.forEach(arr => {
  222. if(arr.mode == ele.mode) {
  223. arr.attr.push({
  224. originalPrice: ele.originalPrice,
  225. studentUpLimit: ele.studentUpLimit
  226. })
  227. }
  228. })
  229. } else {
  230. isType.push(ele.mode)
  231. resultArray.push({
  232. mode: ele.mode,
  233. attr: [{
  234. originalPrice: ele.originalPrice,
  235. studentUpLimit: ele.studentUpLimit
  236. }]
  237. })
  238. }
  239. });
  240. } else {
  241. for(let item of row) {
  242. if(item.attr && item.attr.length > 0) {
  243. for(let child of item.attr) {
  244. resultArray.push({
  245. mode: item.mode,
  246. originalPrice: child.originalPrice,
  247. studentUpLimit: child.studentUpLimit
  248. })
  249. }
  250. }
  251. }
  252. }
  253. return resultArray
  254. },
  255. onCancel () {
  256. this.$store.dispatch('delVisitedViews', this.$route)
  257. this.$router.push({
  258. path: "/serviceManager/serviceList"
  259. });
  260. }
  261. },
  262. };
  263. </script>
  264. <style lang='scss' scoped>
  265. .serviceAttr {
  266. position: relative;
  267. margin-top: 18px;
  268. padding-top: 18px;
  269. overflow: hidden;
  270. display: flex;
  271. flex-wrap: wrap;
  272. flex-direction: column;
  273. }
  274. .list-remove {
  275. position: absolute;
  276. right: 20px;
  277. top: 18px;
  278. .el-button--text {
  279. font-size: 18px;
  280. color: #F56C6C;
  281. &.is-disabled {
  282. color: #C0C4CC;
  283. }
  284. }
  285. }
  286. .el-select, .el-input {
  287. width: 200px !important;
  288. }
  289. .btn-group {
  290. text-align: center;
  291. padding-bottom: 20px;
  292. }
  293. .smallTitle {
  294. background: #fff;
  295. border-bottom: 1px solid #E8E8E8;
  296. margin-bottom: 20px;
  297. padding-bottom: 15px;
  298. color: #393F3E;
  299. padding-left: 0;
  300. &::before {
  301. content: ' ';
  302. display: inline-block;
  303. height: 16px;
  304. width: 3px;
  305. border-radius: 2px;
  306. background-color: var(--color-primary);
  307. }
  308. /deep/.el-alert__title {
  309. font-size: 16px !important;
  310. }
  311. }
  312. </style>