addEdit.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="12vh"
  8. width="900px"
  9. :close-on-click-modal="false"
  10. :visible.sync="showModelThis"
  11. append-to-body
  12. element-loading-text="拼命加载中"
  13. element-loading-spinner="el-icon-loading"
  14. element-loading-background="rgba(0, 0, 0, 0.8)"
  15. @close="showModelThis = false"
  16. >
  17. <el-card style="margin-top: -20px">
  18. <el-row :gutter="10">
  19. <el-col :span="24">
  20. <el-form
  21. ref="ruleForm"
  22. label-position="right"
  23. label-width="110px"
  24. :model="ruleForm"
  25. :rules="rulesThis"
  26. size="mini"
  27. >
  28. <el-row>
  29. <el-col :span="8">
  30. <el-form-item label="组织/企业:" prop="region">
  31. <el-select
  32. v-model="ruleForm.region"
  33. style="width: 100%"
  34. :disabled="isDetail !== '003'"
  35. placeholder="组织/企业"
  36. >
  37. <el-option label="组织" value="0" />
  38. <el-option label="企业" value="1" />
  39. </el-select>
  40. </el-form-item>
  41. </el-col>
  42. <!-- <el-col :span="12">
  43. <el-form-item label="上级组织" prop="pid">
  44. <el-input
  45. v-model="organName"
  46. disabled
  47. :placeholder="organName ? '上级组织' : '暂无上级组织'"
  48. maxlength="100"
  49. />
  50. </el-form-item>
  51. </el-col> -->
  52. <el-col v-if="ruleForm.region === '0'" :span="16">
  53. <el-form-item label="组织名称:" prop="name">
  54. <el-input
  55. v-model="ruleForm.name"
  56. :disabled="isDetail == '007'"
  57. placeholder="组织名称"
  58. maxlength="50"
  59. ><template v-if="organName" slot="prepend">{{
  60. organName
  61. }}</template></el-input>
  62. </el-form-item>
  63. </el-col>
  64. <el-col v-if="ruleForm.region === '1'" :span="16">
  65. <el-form-item label="企业所在区域:" label-width="130px" prop="code">
  66. <div style="display:flex;">
  67. <el-radio v-model="area" label="0" @change="handleFreedomAreaChange">不限</el-radio>
  68. <search-area
  69. :value="ruleForm.code"
  70. :disabled="isDetail == '007'"
  71. :size="'mini'"
  72. :is-detail="true"
  73. :level="2"
  74. :names="ruleForm.area"
  75. :placeholder="'企业所在区域'"
  76. @searchChange="areasearchChange"
  77. />
  78. </div>
  79. </el-form-item>
  80. </el-col>
  81. <el-col v-if="ruleForm.region === '1'" :span="24">
  82. <el-form-item label="企业名称:" prop="companyName">
  83. <el-input
  84. v-model="ruleForm.companyName"
  85. :disabled="isDetail == '007'"
  86. placeholder="企业名称"
  87. maxlength="50"
  88. ><template v-if="organName" slot="prepend">{{
  89. organName
  90. }}</template></el-input>
  91. </el-form-item>
  92. </el-col>
  93. <el-col
  94. v-if="ruleForm.region === '1'"
  95. :span="24"
  96. style="padding: 0 0 16px 0"
  97. >
  98. <el-table :data="tableData" :size="'mini'" border style="width: 100%">
  99. <el-table-column
  100. prop="contactor"
  101. label="联系人"
  102. show-overflow-tooltip
  103. />
  104. <el-table-column
  105. prop="commobile"
  106. label="联系电话"
  107. show-overflow-tooltip
  108. />
  109. <el-table-column prop="position" label="职位" show-overflow-tooltip />
  110. <el-table-column
  111. prop="comdepart"
  112. label="所在部门"
  113. show-overflow-tooltip
  114. />
  115. <el-table-column
  116. prop="wxaccount"
  117. label="微信号"
  118. show-overflow-tooltip
  119. />
  120. <el-table-column prop="qqaccount" label="qq号" show-overflow-tooltip />
  121. <el-table-column prop="email" label="邮箱" show-overflow-tooltip />
  122. <el-table-column v-if="isDetail !== '007'" fixed="right">
  123. <template slot="header" slot-scope="scope">
  124. <span>操作</span>
  125. <el-tooltip
  126. class="item"
  127. effect="dark"
  128. content="添加"
  129. placement="top"
  130. >
  131. <i
  132. class="el-icon-circle-plus-outline fr"
  133. style="font-size: 18px; margin-top: 2px"
  134. @click="openEdit('-1', {})"
  135. />
  136. </el-tooltip>
  137. </template>
  138. <template slot-scope="scope">
  139. <el-tooltip effect="dark" content="修改" placement="top">
  140. <i
  141. class="el-icon-edit tb-icon"
  142. @click="openEdit(scope.$index, scope.row)"
  143. />
  144. </el-tooltip>
  145. <el-tooltip effect="dark" content="禁/启用" placement="top">
  146. <i
  147. class="el-icon-delete tb-icon"
  148. @click="openDelete(scope.$index)"
  149. />
  150. </el-tooltip>
  151. </template>
  152. </el-table-column>
  153. </el-table>
  154. <contact-modal
  155. :show-model="editModel"
  156. :index="findex"
  157. :sitem="sitem"
  158. @cancel="editModel = false"
  159. @refresh="contactRefresh"
  160. />
  161. </el-col>
  162. <el-col :span="24" style="text-align: right">
  163. <el-button
  164. v-if="isDetail !== '007'"
  165. size="mini"
  166. type="primary"
  167. @click="submitForm"
  168. >保 存
  169. </el-button>
  170. <el-button size="mini" @click="showModelThis = false">{{
  171. isDetail == "007" ? "关 闭" : "取 消"
  172. }}</el-button>
  173. </el-col>
  174. </el-row>
  175. </el-form>
  176. </el-col>
  177. </el-row>
  178. </el-card>
  179. </el-dialog>
  180. </template>
  181. <script>
  182. import resToken from '@/mixins/resToken'
  183. import asyncRequest from '@/apis/service/serviceParam/business'
  184. import contactModal from './contactModal'
  185. export default {
  186. name: 'GoodsBack',
  187. components: {
  188. contactModal
  189. },
  190. mixins: [resToken],
  191. props: ['showModel', 'isDetail', 'id', 'organ', 'kh'],
  192. data() {
  193. const validator = function(rule, value, callback) {
  194. const isValueValid = Array.isArray(value) && value.length > 0
  195. if (!isValueValid && this.area !== '0') {
  196. callback(new Error('企业所在区域不能为空'))
  197. } else {
  198. callback()
  199. }
  200. }
  201. return {
  202. area: '',
  203. findex: -1,
  204. sitem: null,
  205. editModel: false,
  206. loading: false,
  207. title: '添加企业/组织',
  208. showModelThis: this.showModel,
  209. ruleForm: {},
  210. rulesThis: this.rules,
  211. organName: '',
  212. tableData: [],
  213. old_tableData: [],
  214. rules: {
  215. region: [
  216. {
  217. required: true,
  218. message: '请选择组织/企业',
  219. trigger: 'change'
  220. }
  221. ],
  222. code: [
  223. {
  224. required: true,
  225. message: '企业所在区域不能为空',
  226. trigger: 'blur',
  227. validator: validator.bind(this)
  228. }
  229. ],
  230. name: [
  231. {
  232. required: true,
  233. message: '组织名称不能为空',
  234. trigger: 'blur'
  235. }
  236. ],
  237. companyName: [
  238. {
  239. required: false,
  240. message: '企业名称不能为空',
  241. trigger: 'blur'
  242. }
  243. ]
  244. }
  245. }
  246. },
  247. watch: {
  248. showModel: function(val) {
  249. this.showModelThis = val
  250. if (val) {
  251. this.initForm()
  252. }
  253. },
  254. showModelThis(val) {
  255. if (!val) {
  256. this.$emit('cancel')
  257. }
  258. },
  259. area(val) {
  260. if (val) this.ruleForm.code = ''
  261. },
  262. 'ruleForm.code': {
  263. handler(val) {
  264. if (val) this.area = ''
  265. },
  266. deep: true
  267. }
  268. },
  269. methods: {
  270. handleFreedomAreaChange() {
  271. this.$nextTick(() => this.$refs.ruleForm.validateField('code'))
  272. },
  273. async initForm() {
  274. this.loading = true
  275. this.tableData = []
  276. this.old_tableData = []
  277. this.organName = ''
  278. await this.resetForm()
  279. if (this.isDetail === '003') {
  280. this.title = '添加企业/组织'
  281. this.rulesThis = this.rules
  282. } else if (this.isDetail === '005') {
  283. this.title = '修改企业/组织'
  284. this.rulesThis = this.rules
  285. await this.initData()
  286. } else {
  287. this.title = '查看企业/组织'
  288. this.rulesThis = {}
  289. await this.initData()
  290. }
  291. this.setOrganName()
  292. this.loading = false
  293. },
  294. setOrganName() {
  295. this.organName = ''
  296. if (this.organ && this.organ.length > 1) {
  297. this.organ.forEach((e) => {
  298. if (e.id !== '0') {
  299. this.organName += e.name
  300. this.ruleForm.pid = e.id
  301. }
  302. })
  303. } else {
  304. this.organName = ''
  305. }
  306. const { companyName } = this.ruleForm
  307. const isok = companyName.indexOf(this.organName) == 0
  308. if (isok) {
  309. const str = companyName
  310. this.ruleForm.companyName = str.substring(
  311. this.organName.length,
  312. this.ruleForm.companyName.length
  313. )
  314. }
  315. },
  316. openEdit(index, sitem) {
  317. this.findex = index
  318. this.sitem = sitem
  319. this.editModel = true
  320. },
  321. openDelete(index) {
  322. this.tableData.splice(index, 1)
  323. },
  324. async resetForm() {
  325. await this.$nextTick(() => {
  326. if (this.$refs.ruleForm) {
  327. this.$refs.ruleForm.resetFields()
  328. this.$refs.ruleForm.clearValidate()
  329. this.ruleForm = {
  330. id: '',
  331. pid: '0',
  332. region: '0',
  333. name: '',
  334. companyName: '',
  335. code: [],
  336. area: ''
  337. }
  338. }
  339. })
  340. },
  341. async initData() {
  342. let res = {}
  343. if (this.kh + '' === '0') {
  344. res = await asyncRequest.zdetail({ id: this.id })
  345. } else {
  346. res = await asyncRequest.qdetail({ companyNo: this.id })
  347. }
  348. const { code, data, message } = res
  349. if (code === 0) {
  350. const { pid, name, pname, companyName, parent, member, id, middle, branch, area } = data
  351. this.organName = pname
  352. const isFreedom = String(middle) === '0' && String(branch) === '0' && String(area) === '不限区域'
  353. // console.log(middle, branch, area, isFreedom)
  354. if (isFreedom) this.area = '0'
  355. this.ruleForm = {
  356. id: id,
  357. region: this.kh + '' || '0',
  358. pid: parent || pid || '0',
  359. name: name || '',
  360. companyName: companyName || '',
  361. code: isFreedom || !middle ? [] : [middle],
  362. area: area ? [area] : []
  363. }
  364. if (member && member.length > 0) {
  365. this.old_tableData = JSON.parse(JSON.stringify(member))
  366. this.tableData = JSON.parse(JSON.stringify(member))
  367. } else {
  368. this.old_tableData = []
  369. this.tableData = []
  370. }
  371. this.$refs.ruleForm.resetFields()
  372. this.$refs.ruleForm.clearValidate()
  373. } else if (code >= 100 && code <= 104) {
  374. await this.logout()
  375. } else {
  376. this.$message.warning(message)
  377. }
  378. },
  379. contactRefresh(e) {
  380. this.editModel = false
  381. const {
  382. index,
  383. contactor,
  384. commobile,
  385. position,
  386. comdepart,
  387. wxaccount,
  388. qqaccount,
  389. email,
  390. id,
  391. is_del
  392. } = e
  393. if (index + '' === '-1') {
  394. this.tableData.push(e)
  395. } else {
  396. const findex = parseInt(index)
  397. this.tableData[findex].contactor = contactor || ''
  398. this.tableData[findex].commobile = commobile || ''
  399. this.tableData[findex].position = position || ''
  400. this.tableData[findex].comdepart = comdepart || ''
  401. this.tableData[findex].wxaccount = wxaccount || ''
  402. this.tableData[findex].qqaccount = qqaccount || ''
  403. this.tableData[findex].email = email || ''
  404. this.tableData[findex].id = id || ''
  405. this.tableData[findex].is_del = is_del || '0'
  406. }
  407. },
  408. areasearchChange(e) {
  409. const { id, city_code, province_code, label } = e
  410. this.ruleForm.code = e && id ? [id] : []
  411. this.ruleForm.area = e && id ? label : ''
  412. this.ruleForm.branch = e && id ? province_code : ''
  413. this.ruleForm.middle = e && id ? city_code : ''
  414. console.log(label, '--------')
  415. this.$refs.ruleForm.validateField('code')
  416. },
  417. async submitForm() {
  418. await this.$refs.ruleForm.validate(async(valid) => {
  419. if (valid) {
  420. if (this.loading) {
  421. return
  422. }
  423. this.loading = true
  424. const { id, pid, name, companyName, code: area_code, area } = this.ruleForm
  425. let res = {}
  426. if (this.ruleForm.region === '0') {
  427. const zitem = {
  428. id: id,
  429. pid: pid,
  430. name: name
  431. }
  432. if (this.isDetail === '003') {
  433. delete zitem['id']
  434. res = await asyncRequest.zadd(zitem)
  435. } else {
  436. res = await asyncRequest.zupdate(zitem)
  437. }
  438. } else {
  439. if (this.tableData.length === 0) {
  440. this.$message.warning('至少填写一个联系人!')
  441. this.loading = false
  442. return
  443. }
  444. const qitem = {
  445. id: id,
  446. companyName: this.organName + companyName,
  447. parent: pid,
  448. branch: this.area === '0' ? '0' : area_code[0],
  449. middle: this.area === '0' ? '0' : area_code[0],
  450. area: this.area === '0' ? '不限区域' : area,
  451. customer_member: this.getCustomer()
  452. }
  453. // console.log(qitem)
  454. // this.loading = false
  455. // return
  456. if (this.isDetail === '003') {
  457. delete qitem['id']
  458. res = await asyncRequest.qadd(qitem)
  459. } else {
  460. res = await asyncRequest.qupdate(qitem)
  461. }
  462. }
  463. this.loading = false
  464. const { code, data, message } = res
  465. if (code === 0) {
  466. const title = this.isDetail === '003' ? '添加成功!' : '修改成功!'
  467. this.$notify.success({
  468. title,
  469. message: ''
  470. })
  471. this.showModelThis = false
  472. // 刷新
  473. this.$emit('refresh')
  474. } else if (code >= 100 && code <= 104) {
  475. await this.logout()
  476. } else {
  477. this.$message.warning(message)
  478. }
  479. } else {
  480. console.log('error submit!!')
  481. return false
  482. }
  483. })
  484. },
  485. getCustomer() {
  486. const list = JSON.parse(JSON.stringify(this.tableData))
  487. const oldlist = JSON.parse(JSON.stringify(this.old_tableData))
  488. const hasIDlist = []
  489. list.forEach((a) => {
  490. if (a.id !== '') {
  491. hasIDlist.push(a)
  492. }
  493. })
  494. const newList = []
  495. const resList = []
  496. oldlist.forEach((a, ai) => {
  497. let item = null
  498. const index = hasIDlist.findIndex((b) => a.id + '' === b.id + '')
  499. if (index === -1) {
  500. item = JSON.parse(JSON.stringify(a))
  501. item.is_del = '1'
  502. } else {
  503. item = JSON.parse(JSON.stringify(hasIDlist[index]))
  504. }
  505. newList.push(item)
  506. })
  507. list.forEach((b) => {
  508. if (b.id === '') {
  509. b.is_del = '0'
  510. newList.push(b)
  511. }
  512. })
  513. newList.forEach((a) => {
  514. const m = {
  515. id: a.id,
  516. contactor: a.contactor,
  517. position: a.position,
  518. commobile: a.commobile,
  519. comdepart: a.comdepart,
  520. wxaccount: a.wxaccount,
  521. qqaccount: a.qqaccount,
  522. email: a.email,
  523. is_del: a.is_del || '0',
  524. status: a.status || '1'
  525. }
  526. resList.push(m)
  527. })
  528. return resList
  529. }
  530. }
  531. }
  532. </script>
  533. <style lang="scss" scoped>
  534. .storeAnomaly {
  535. // width: calc( 100% - 110px);
  536. }
  537. </style>