Create.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ $t('system.text_130', [$t('system.text_317')]) }}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc"
  7. v-bind="formItemLayout">
  8. <a-form-item :label="$t('dictionary.domain')" v-if="isAdminMode">
  9. <base-select
  10. style="width: 300px;"
  11. v-decorator="decorators.domain"
  12. resource="domains"
  13. version="v1"
  14. remote
  15. :isDefaultSelect="true"
  16. :remote-fn="q => ({ filter: `name.contains(${q})` })"
  17. :select-props="{ placeholder: $t('rules.domain'), allowClear: true }"
  18. @change="handleDomainChange" />
  19. </a-form-item>
  20. <a-form-item :label="$t('dictionary.user')">
  21. <base-select
  22. style="width: 300px;"
  23. v-decorator="decorators.user"
  24. resource="users"
  25. version="v1"
  26. remote
  27. :isDefaultSelect="true"
  28. :needParams="true"
  29. :params="userParams"
  30. :mapper="mapperUserData"
  31. :remote-fn="q => ({ filter: `name.contains(${q})` })"
  32. :select-props="{ placeholder: $t('rules.user'), allowClear: true }" />
  33. </a-form-item>
  34. <a-form-item :label="$t('system.text_131')">
  35. <mobile-input v-decorator="decorators.international_mobile" />
  36. </a-form-item>
  37. <a-form-item :label="$t('system.text_132')">
  38. <a-input v-decorator="decorators.email" />
  39. </a-form-item>
  40. <a-form-item>
  41. <span slot="label">
  42. {{ $t('common_599') }}
  43. <a-tooltip effect="dark" placement="top">
  44. <a-icon type="info-circle" />
  45. <div slot="title">{{$t('system.contact')}}</div>
  46. </a-tooltip>
  47. </span>
  48. <a-checkbox-group
  49. v-decorator="decorators.enabled_contact_types">
  50. <a-checkbox
  51. v-for="(v, index) in contactArrOpts"
  52. :key="index"
  53. :value="v.value"
  54. :disabled="v.disabled">
  55. {{ v.label }}
  56. </a-checkbox>
  57. </a-checkbox-group>
  58. </a-form-item>
  59. </a-form>
  60. </div>
  61. <div slot="footer">
  62. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  63. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  64. </div>
  65. </base-dialog>
  66. </template>
  67. <script>
  68. import { mapGetters } from 'vuex'
  69. import { contactMap } from '@/constants'
  70. import DialogMixin from '@/mixins/dialog'
  71. import WindowsMixin from '@/mixins/windows'
  72. import MobileInput from '@/sections/MobileInput'
  73. import { findAndUnshift } from '@/utils/utils'
  74. export default {
  75. name: 'ContactCreateDialog',
  76. components: { MobileInput },
  77. mixins: [DialogMixin, WindowsMixin],
  78. data () {
  79. return {
  80. loading: false,
  81. userParams: {
  82. limit: 20,
  83. is_system_account: false,
  84. have_contacts: false,
  85. scope: this.$store.getters.scope,
  86. },
  87. form: {
  88. fc: this.$form.createForm(this),
  89. },
  90. contactArrOpts: [],
  91. userOptions: [],
  92. decorators: {
  93. domain: [
  94. 'domain',
  95. ],
  96. user: [
  97. 'user',
  98. {
  99. rules: [
  100. {
  101. required: true,
  102. message: this.$t('rules.user'),
  103. whitespace: true,
  104. },
  105. ],
  106. },
  107. ],
  108. international_mobile: [
  109. 'international_mobile',
  110. {
  111. initialValue: { mobile: '', area_code: '86' },
  112. rules: [
  113. {
  114. type: 'object',
  115. required: true,
  116. },
  117. { validator: this.checkInternationalMobile },
  118. ],
  119. },
  120. ],
  121. email: [
  122. 'email',
  123. {
  124. rules: [
  125. {
  126. required: true,
  127. message: this.$t('system.text_139'),
  128. whitespace: true,
  129. },
  130. {
  131. validator: this.$validate('email'),
  132. message: this.$t('system.text_140'),
  133. whitespace: true,
  134. },
  135. ],
  136. },
  137. ],
  138. enabled_contact_types: [
  139. 'enabled_contact_types',
  140. {
  141. initialValue: ['webconsole'],
  142. },
  143. ],
  144. },
  145. formItemLayout: {
  146. wrapperCol: {
  147. span: 18,
  148. },
  149. labelCol: {
  150. span: 6,
  151. },
  152. },
  153. receiverUserIds: [],
  154. }
  155. },
  156. computed: {
  157. ...mapGetters(['isAdminMode', 'isDomainMode', 'scope', 'userInfo']),
  158. },
  159. destroyed () {
  160. this.manager = null
  161. },
  162. created () {
  163. this.manager = new this.$Manager('receivers', 'v1')
  164. this.generateContactArrOpts()
  165. this.fetchReceivers()
  166. // this.isDomainMode && this.fetchUsers()
  167. },
  168. methods: {
  169. async handleConfirm () {
  170. this.loading = true
  171. try {
  172. const values = await this.form.fc.validateFields()
  173. const { user: uid, domain, email, international_mobile, enabled_contact_types } = values
  174. const data = {
  175. uid,
  176. email,
  177. international_mobile,
  178. domain,
  179. enabled_contact_types,
  180. force_verified: true,
  181. }
  182. await this.manager.create({
  183. data,
  184. })
  185. this.loading = false
  186. this.params.success()
  187. this.cancelDialog()
  188. } catch (error) {
  189. this.loading = false
  190. throw error
  191. }
  192. },
  193. handleDomainChange (val) {
  194. this.form.fc.setFieldsValue({
  195. user: '',
  196. })
  197. this.userParams = {
  198. limit: 20,
  199. is_system_account: false,
  200. have_contacts: false,
  201. scope: this.$store.getters.scope,
  202. domain_id: val,
  203. }
  204. // this.fetchUsers()
  205. this.generateContactArrOpts({ domain_ids: [val] })
  206. },
  207. async generateContactArrOpts (params) {
  208. let config_contact_types = []
  209. try {
  210. config_contact_types = await this.fetchConfig(params)
  211. } catch (error) {
  212. throw error
  213. }
  214. const contactArrOpts = config_contact_types.filter((item) => {
  215. return !item.includes('robot')
  216. }).map((item) => {
  217. return {
  218. label: contactMap[item].label || item,
  219. value: item,
  220. disabled: item === 'webconsole',
  221. }
  222. })
  223. this.contactArrOpts = contactArrOpts
  224. },
  225. fetchConfig (params) {
  226. return new Promise((resolve, reject) => {
  227. new this.$Manager('receivers', 'v1').performClassAction({ action: 'get-types', data: params }).then((res) => {
  228. const { types } = res.data
  229. const sortData = findAndUnshift(types, item => item === 'webconsole')
  230. resolve(sortData)
  231. }).catch((err) => {
  232. reject(err)
  233. })
  234. })
  235. },
  236. async fetchUsers () {
  237. const userManager = new this.$Manager('users', 'v1')
  238. const receiverManager = new this.$Manager('receivers', 'v1')
  239. try {
  240. const userRes = await userManager.list({ params: this.userParams })
  241. const users = userRes.data.data
  242. const receiverRes = await receiverManager.list({ params: { scope: this.scope } })
  243. const receivers = receiverRes.data.data
  244. const receiverUsers = receivers.map((item) => { return item.name })
  245. const noExsitUser = users.filter((item) => { return !receiverUsers.includes(item.name) })
  246. this.userOptions = noExsitUser.map((item) => {
  247. return {
  248. label: item.name,
  249. value: item.id,
  250. }
  251. })
  252. } catch (error) {
  253. throw error
  254. }
  255. },
  256. checkInternationalMobile (rule, value, callback) {
  257. if (!value.mobile) {
  258. callback(this.$t('system.text_138'))
  259. }
  260. if (!/^[0-9-]{6,14}$/.test(value.mobile)) {
  261. callback(this.$t('validator.phone'))
  262. }
  263. if (!value.area_code) {
  264. callback(this.$t('system.country_code'))
  265. }
  266. callback()
  267. },
  268. async fetchReceivers () {
  269. const receiverManager = new this.$Manager('receivers', 'v1')
  270. try {
  271. const params = { scope: this.scope, force_no_paging: true }
  272. const receiverRes = await receiverManager.list({ params })
  273. const receivers = receiverRes.data.data
  274. const receiverUserIds = receivers.map((item) => { return item.id })
  275. this.receiverUserIds = receiverUserIds
  276. } catch (error) {
  277. throw error
  278. }
  279. },
  280. mapperUserData (list) {
  281. return list.map(item => {
  282. if (this.receiverUserIds.includes(item.id)) {
  283. item.__disabled = true
  284. }
  285. return item
  286. }).sort((a, b) => a.__disabled ? 1 : -1)
  287. },
  288. },
  289. }
  290. </script>