| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <template>
- <view class="default_page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
- <cus-header title='填写所在团队信息'></cus-header>
- <cus-team-info-fill ref="teamRef" :submitting="creationState.submitting || creationState.completed" @handleConfirm="handleConfirm" :confirmText="confirmText" :qtype="qtype"></cus-team-info-fill>
- </view>
- </template>
- <script>
- import CusTeamInfoFill from '@/components/CusTeamInfoFill/index.vue'
- import teamBackgroundRules from '@/utils/teamBackgroundFile.js'
- const {
- beginTeamSubmission,
- createTeamCreationState,
- createTeamPayloadSnapshot,
- createTeamSubmitPayload,
- finishTeamSubmission,
- rememberCreatedTeam,
- setTeamSubmissionStage
- } = teamBackgroundRules
- export default {
- components:{ CusTeamInfoFill },
- data(){
- return {
- title:'',
- type:'',
- next:'',
- questionnaireId:'',
- confirmText:'下一步',
- qtype:'',
- creationState:createTeamCreationState(),
- navigationTimer:null,
- navigationReject:null,
- pageUnloaded:false
- }
- },
- onLoad(options) {
- this.pageUnloaded = false;
- this.title = options.title||'';
- this.type = options.type;
- this.qtype = options.qtype||'';
- this.questionnaireId = options.questionnaireId||'';
- this.confirmText = options.type?'确定':'下一步';
- this.next = options.next;
- if(this.next) this.confirmText = '下一步';
- },
- onUnload() {
- this.pageUnloaded = true
- this.cancelPendingNavigation(new Error('页面已离开'))
- },
- methods:{
- async handleConfirm(team){
- if(!beginTeamSubmission(this.creationState)) return
- const payload = createTeamSubmitPayload(Object.assign({}, team, {
- questionnaireId:this.questionnaireId
- }))
- const payloadSnapshot = createTeamPayloadSnapshot(payload)
- try {
- let teamId = this.creationState.createdTeamId
- const resumeNavigation = this.creationState.stage === 'navigate'
- if(!teamId){
- setTeamSubmissionStage(this.creationState, 'save')
- const { data:res } = await this.$api.post('/core/user/team',payload)
- if(res.code!==0){
- this.$showToast(res.msg || '团队保存失败,请重试')
- return
- }
- teamId = res.data && res.data.teamId
- if(!teamId){
- this.$showToast('团队保存未返回团队ID,请重试')
- return
- }
- rememberCreatedTeam(this.creationState, teamId)
- this.creationState.teamPayloadSnapshot = payloadSnapshot
- } else if(this.creationState.teamPayloadSnapshot !== payloadSnapshot) {
- setTeamSubmissionStage(this.creationState, 'save')
- let res
- try {
- const response = await this.$api.put('/core/user/team', Object.assign({}, payload, {
- id:teamId
- }))
- res = response.data
- if(res.code!==0) throw new Error(res.msg || '团队更新请求失败')
- } catch(error) {
- if(resumeNavigation) setTeamSubmissionStage(this.creationState, 'navigate')
- throw error
- }
- this.creationState.teamPayloadSnapshot = payloadSnapshot
- }
- if(resumeNavigation) {
- setTeamSubmissionStage(this.creationState, 'navigate')
- await this.continueAfterTeamCreated(teamId)
- finishTeamSubmission(this.creationState, true)
- return
- }
- setTeamSubmissionStage(this.creationState, 'upload')
- const uploadResult = await this.$refs.teamRef.flushBackgroundFiles(teamId)
- if(uploadResult.failed > 0 || this.$refs.teamRef.hasUnresolvedBackgroundFiles()){
- await this.$showModal('仍有背景资料未上传成功,请重试后继续;团队已保存,无需再次创建')
- return
- }
- if(this.$refs.teamRef.hasDeferredPcUpload()){
- setTeamSubmissionStage(this.creationState, 'pcSession')
- await this.$refs.teamRef.waitForDeferredPcUpload(teamId)
- }
- if(this.next && !this.creationState.teamQuestionnaireId) {
- setTeamSubmissionStage(this.creationState, 'publish')
- const publishResult = await this.publishQuestionnaire(teamId)
- this.creationState.publishResult = publishResult
- this.creationState.teamQuestionnaireId = publishResult.data
- }
- setTeamSubmissionStage(this.creationState, 'navigate')
- await this.continueAfterTeamCreated(teamId)
- finishTeamSubmission(this.creationState, true)
- } catch(error) {
- this.showStageError(error)
- } finally {
- finishTeamSubmission(this.creationState)
- }
- },
- showStageError(error) {
- if(this.pageUnloaded) return
- const prefix = {
- save:'团队保存失败',
- upload:'背景资料处理失败',
- pcSession:'电脑上传流程失败',
- publish:'问卷发布失败',
- navigate:'页面跳转失败'
- }[this.creationState.stage] || '操作失败'
- const message = error && (error.message || error.msg)
- this.$showToast(message ? `${prefix}:${message}` : `${prefix},请重试`)
- },
- async publishQuestionnaire(teamId) {
- const response = await this.$api.post('/core/team/questionnaire/publish',{
- answerSetting:1,
- coachId:JSON.parse(uni.getStorageSync('userInfo')).id,
- startTime:new Date().Format('yyyy-MM-dd hh:mm:ss'),
- endTime:new Date(new Date().setDate(new Date().getDate()+30)).Format('yyyy-MM-dd hh:mm:ss'),
- questionnaireId:this.questionnaireId,
- teamId,
- type:1
- })
- const result = response.data
- if(result.code!==0) throw new Error(result.msg || '发布请求失败')
- return result
- },
- async continueAfterTeamCreated(teamId) {
- if(this.next){
- this.$showToast('保存成功,即将填写问卷')
- await this.runDelayedNavigation(({ success, fail }) => {
- uni.navigateTo({
- url:`/pagesPublish/questionnaireFill?teamQuestionnaireId=${this.creationState.teamQuestionnaireId}&teamId=${teamId}&type=${this.type}&title=${this.title}`,
- success,
- fail
- })
- })
- uni.removeStorageSync('newUser')
- return
- }
- this.$showToast('团队新增成功')
- await this.runDelayedNavigation(({ success, fail }) => {
- uni.navigateBack({ success, fail })
- })
- const eventChannel = this.getOpenerEventChannel()
- if(eventChannel) eventChannel.emit('saveTeamInfo')
- },
- runDelayedNavigation(invokeNavigation) {
- return new Promise((resolve, reject) => {
- if(this.pageUnloaded) {
- reject(new Error('页面已离开'))
- return
- }
- let settled = false
- const settle = (callback, value) => {
- if(settled) return
- settled = true
- this.navigationTimer = null
- this.navigationReject = null
- callback(value)
- }
- this.navigationReject = error => settle(reject, error)
- this.navigationTimer = setTimeout(() => {
- this.navigationTimer = null
- if(this.pageUnloaded) {
- settle(reject, new Error('页面已离开'))
- return
- }
- try {
- invokeNavigation({
- success: result => {
- if(this.pageUnloaded) settle(reject, new Error('页面已离开'))
- else settle(resolve, result)
- },
- fail: error => settle(reject, new Error(error && (error.errMsg || error.message) || '跳转失败'))
- })
- } catch(error) {
- settle(reject, error)
- }
- },1500)
- })
- },
- cancelPendingNavigation(error) {
- if(this.navigationTimer) clearTimeout(this.navigationTimer)
- this.navigationTimer = null
- const reject = this.navigationReject
- this.navigationReject = null
- if(reject) reject(error || new Error('跳转已取消'))
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .default_page{
- box-sizing: border-box;
- }
- </style>
|