| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- import * as api from '@/api'
- import Custom from '../js/custom'
- import Layout1 from '../Layout1'
- import Layout2 from '../Layout2'
- import Layoutileads1 from '../Layoutileads1'
- import Storage from '@/utils/storage'
- const lStorage = new Storage('localStorage')
- require('@/utils/jweixin-1.3.2.js')
- function getTrim (val) {
- return val.replace(/(^\s+)|(\s+$)/g, '')
- }
- export default {
- props: {
- edata: {
- type: Object
- },
- edataItem: {
- type: Object
- },
- isEdit: {
- type: Boolean
- }
- },
- components: {
- Layout1,
- Layout2,
- Layoutileads1,
- },
- data() {
- return {
- TITLE: '问卷调查',
- data: {
- result: [],
- topic: {}
- },
- layoutType: {
- '1': 'Layout1',
- '2': 'Layout2',
- '3': 'Layoutileads1'
- },
- configType: {
- 'radio': '单选',
- 'checkbox': '多选'
- },
- loading: {
- data: false,
- empty: false,
- submit: false
- },
- show: {
- submit: false
- },
- codeMess: {
- num: 3,
- message: '自动跳转',
- disabled: false,
- t: null
- }
- };
- },
- computed: {
- putLayoutCurrent() {
- let layout_type = this.data ? this.data.topic.layout_type : ''
- return this.layoutType[layout_type] || 'van-empty'
- }
- },
- watch: {
- edata() {
- this.inData()
- }
- },
- destroyed() {
- clearInterval(this.codeMess.t)
- },
- created() {
- let objData = lStorage.getItem('AHR') || {}
- let data = Object.keys(objData).map(k => objData[k])
- let d1 = new Date().toString('yyyyMMdd')
- data = data.filter(item => {
- if (item.timer) {
- return new Date(item.timer).toString('yyyyMMdd') === d1
- }
- })
- if (data.length && !this.$route.query.jump && !this.isEdit) {
- this.$router.replace({ name: 'FormResult', query: this.$route.query })
- } else {
- this.vueWatchRoute()
- }
- },
- methods: {
- vueWatchRoute (a, b) {
- this.inQuery()
- if (!b || a.name !== b.name) {
- clearInterval(this.codeMess.t)
- this.inData()
- }
- },
- // 秒倒计时
- async loopCode(num, callback) {
- let msg = '自动跳转'
- clearInterval(this.codeMess.t)
- function loopNum() {
- if (num < 1) {
- this.codeMess['message'] = msg
- this.codeMess['disabled'] = false
- clearInterval(this.codeMess.t)
- callback && callback()
- return
- }
- this.codeMess['disabled'] = true
- this.codeMess['message'] = `${num}s ${msg}`
- num = num - 1
- console.log(this.codeMess['message'])
- }
- loopNum.call(this)
- this.codeMess.t = setInterval(() => loopNum.call(this), 1000)
- },
- inQuery() {
- let { title } = this.$route.query
- if (title) {
- this.TITLE = title
- }
- },
- isForm(result, callback) {
- return new Promise((resolve, reject) => {
- let state = result.some(item => {
- if (!item.required) {
- return false
- }
- let b = item.val instanceof Array ? !!item.val.length : !!item.val
- !b && callback && callback(item)
- return !b
- })
- state ? reject() : resolve()
- })
- },
- async inData() {
- this.loading.data = true
- this.loading.empty = false
- try {
- let data = null
- if (this.isEdit) {
- data = this.edata
- } else {
- let { result_id } = this.$route.query
- let pm = result_id
- ? api.apiGetVolumeResult({ result_id })
- : api.apiGetVolumeInfo(this.$route.query)
- let res = await pm
- data = res.data
- }
-
- this.inTopicComponent(data, Custom)
- this.$set(this, 'data', data)
- this.inTITLE()
- } catch (error) {
- this.loading.empty = true
- console.log(error)
- }
- this.loading.data = false
- },
- inTITLE () {
- if (this.data) {
- this.TITLE = this.data.topic.name
- }
- },
- // 问卷组件初始化
- inTopicComponent(data, callback) {
- data = data || this.data
- data.result.forEach(item => {
- typeof callback === 'function' && callback(item)
- let { patient_mobile } = this.$route.query
- if (new RegExp('手机号码').test(item.title) && patient_mobile) {
- item.val = patient_mobile
- // item.disabled = true
- }
- if (item.type === 'certificate') {
- item.certificate = '身份证'
- }
- if (item.type === 'area') {
- item.area = ''
- item.val1 = ''
- item.val2 = ''
- }
- })
- },
- btnOpenSuccess(callback) {
- this.show.submit = true
- this.loopCode(this.codeMess.num, async () => {
- this.show.submit = false
- await this.$nextTick()
- callback && callback()
- })
- },
- async btnSubmit(datas, callbackSuccess, callbackError) {
- let pm
- let ld = null
- let edatas = datas || [this.data]
- this.loading.submit = true
- try {
- // await Promise.all(edatas.map(data => {
- // return this.isForm(data.result, item => {
- // this.$dialog.alert({
- // title: '必填项',
- // message: `${item.title}`,
- // })
- // })
- // }))
-
- let ruleData = {}
- let rule = {}
- function validatorArea (item, reg) {
- let obj = {}
- let cerFn = () => item.required && !item.area
- if (cerFn()) {
- obj = {
- validator: () => !!item.area,
- message: '请选择居住地址'
- }
- } else {
- obj = {
- validator: () => {
- return !!item.val1 && !!item.val2
- },
- message: '请填写详细地址'
- }
- }
- return obj
- }
- function validatorCertificate(item, reg) {
- let obj = {}
- let cerFn = () => item.required && !item.certificate
- if (cerFn()) {
- obj = {
- validator: () => !!item.certificate,
- message: '请选择证件类型'
- }
- } else if (item.certificate === '身份证') {
- obj = {
- validator: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/,
- message: '身份证格式错误'
- }
- }
- return obj
- }
- function validatorDefault (item, reg) {
- return {
- validator: item.val instanceof Array
- ? (value, rule) => !!value.length
- : reg,
- message: item.regexp_message
- }
- }
- edatas.forEach(({ result }, rIndex) => result.forEach((item, index) => {
- let k = `${rIndex}_${index}_val`
- ruleData[k] = typeof item.val !== 'string'
- ? item.val
- : item.val.replace(/(^\s+)|(\s+$)/g, '')
- let reg = item.reg ? new RegExp(item.reg) : null
- rule[k] = [{
- required: !!item.required,
- message: `必填项\n${item.title}`
- }]
- if (item.type === 'area') {
- rule[k].push(validatorArea(item, reg))
- }
- else if (item.type === 'certificate') {
- rule[k].push(validatorCertificate(item, reg))
- }
- else if (item.reg) {
- rule[k].push(validatorDefault(item, reg))
- }
- }))
- await this.$blRule(ruleData, rule, message => {
- this.$toast(message)
- })
- await this.$dialog.confirm({
- title: '提示',
- message: '请确认无误提交?',
- })
- ld = this.$toast.loading({ message: '提交中...', duration: 0 })
- if (this.isEdit) {
- await new Promise(r => setTimeout(r, 2500))
- this.btnOpenSuccess()
- throw new Error('模拟提交')
- }
- let arrData = edatas.map(d => {
- let data = JSON.parse(JSON.stringify(d))
- data.result.forEach(item => {
- let sp = '<#>'
- if (item.type === 'area') {
- item.val = [item.area, item.val1, item.val2, item.val].map(v => getTrim(v)).join(sp)
- }
- if (item.type === 'certificate') {
- item.val = [item.certificate, item.val].map(v => getTrim(v)).join(sp)
- }
- })
- return data
- })
- let pms = arrData.map(data => this.btnSubmitItem(data.result, data.query))
- let arrRes = await Promise.all(pms)
- if (pms.length === 1 && this.$route.query.topic_id == 1) {
- let res = arrRes[0]
- let { result_id } = res.data
- let dt = arrData[0]
- callbackSuccess && callbackSuccess(dt)
- this.$router.replace({ name: 'FormResult', query: { ...this.$route.query, result_id, jump: undefined, history: undefined } })
- } else {
- this.btnOpenSuccess(() => this.btnLinkBack())
- }
- pm = Promise.resolve(arrRes.length > 1 ? arrRes : arrRes[0])
- } catch (error) {
- pm = Promise.reject()
- callbackError && callbackError(error)
- }
- ld && ld.clear()
- this.loading.submit = false
- return pm
- },
- async btnSubmitItem(dataResult, option) {
- let pm
- dataResult = dataResult || this.data.result
- try {
- let result = dataResult.map(item => {
- return {
- detail_id: item.detail_id,
- val: item.val
- }
- })
- // 表单类型为 iLeads活动 - 2,时,额外爬取参数 -----------
- let patient = {}
- if (this.data.topic.flag === 2) {
- let config = {
- patient_name: new RegExp('姓名'),
- patient_mobile: new RegExp('手机号码'),
- age: new RegExp('年龄'),
- sex: new RegExp('性别'),
- birthday: new RegExp('出生日期'),
- sight_memo: new RegExp('视力')
- }
- let keys = Object.keys(config)
- let objs = dataResult.map(item => {
- let rst = { key: '', val: '' }
- keys.some(k => {
- let reg = config[k]
- if (reg.test(item.title)) {
- rst.key = k
- rst.val = item.val
- return true
- }
- })
- return rst
- }).filter(it => it)
- objs.forEach(({ key, val }) => {
- if (!patient[key]) {
- patient[key] = val
- }
- })
- }
- // ---------------------------------
- let query = {
- ...this.$route.query,
- ...option,
- ...patient,
- flag: this.data.topic.flag,
- data: JSON.stringify(result)
- }
- delete query.replace
- delete query.to
- delete query.go
- delete query.ids
- delete query.combos
-
- let res = await api.apiPostVolumeInfo(query)
- pm = Promise.resolve(res)
- } catch (error) {
- console.log(error)
- pm = Promise.reject(error)
- }
- return pm
- },
- btnLinkBack() {
- clearInterval(this.codeMess.t)
- var ua = navigator.userAgent.toLowerCase();
- let { go, replace, to } = this.$route.query
- if (replace) {
- location.replace(decodeURIComponent(replace))
- } else if (to) {
- location.href = decodeURIComponent(to)
- } else if (go) {
- this.$router.go(go)
- }
- else if (ua.match(/MicroMessenger/i) == "micromessenger") {
- this.btnLinkWeChat()
- } else {
- this.$router.go(-1)
- }
- },
- btnLinkWeChat () {
- try {
- if (ua.match(/MicroMessenger/i) == "micromessenger") {
- //ios的ua中无miniProgram,但都有MicroMessenger(表示是微信浏览器)
- wx.miniProgram.getEnv((res) => {
- if (res.miniprogram) {
- // 在小程序里
- console.log('miniprogram')
- wx.miniProgram.switchTab({ url: '/pages/index/index' })
- }
- })
- }
- } catch (err) {
- }
- }
- }
- }
|