diff --git a/packages/my/index.vue b/packages/my/index.vue index 8a85fc5..3339855 100644 --- a/packages/my/index.vue +++ b/packages/my/index.vue @@ -9,8 +9,9 @@ - - {{form.username}} + + + {{form.username}} @@ -132,6 +133,7 @@ data() { return { userAvatar: '', + canFillUsername: false, showMobile: false, showSex: false, myMobile: '', @@ -179,6 +181,13 @@ value: '女' }], rules: { + username: [{ + validator: (rule, value, callback) => { + return !this.canFillUsername || (typeof value === 'string' && value.trim().length > 0); + }, + message: '请输入姓名', + trigger: ['blur', 'change'], + }], sex: [{ required: true, message: '请选择性别', @@ -366,6 +375,8 @@ console.log("res", res) // this.form = this.base.requestToForm(res.user, this.form) this.form = this.base.deepCopy(res.user) + // Use the loaded name so typing does not hide the input. + this.canFillUsername = !String(res.user.username == null ? '' : res.user.username).trim() // 头像 this.userAvatar = res.user && res.user.headimgurl ? res.user.headimgurl : '' if (res.user.plate) { diff --git a/packages/register/index.vue b/packages/register/index.vue index c25a033..13ad655 100644 --- a/packages/register/index.vue +++ b/packages/register/index.vue @@ -30,7 +30,7 @@ - {{ saving ? '处理中…' : registrationComplete && fromBooking ? '返回场地预约' : registrationComplete && fromSupply ? '返回供需对接' : registrationComplete && survey_id ? '返回问卷' : '注册' }} + {{ saving ? '处理中…' : registrationComplete && fromBooking ? '返回场地预约' : registrationComplete && fromSupply ? '返回供需对接' : '注册' }} @@ -53,7 +53,7 @@ - 确定 + {{ bindingMobile || saving ? '处理中…' : '确定' }} @@ -95,7 +95,9 @@ return { fromSupply: false, fromBooking: false, survey_id: '', saving: false, + bindingMobile: false, registrationComplete: false, + lastRegisteredPayload: '', showMobile: false, showCompanySelector: false, showCompanyInput: false, @@ -134,6 +136,12 @@ }, form: { username: '', + name: '', + mobile: '', + email: '', + birthday: '', + company_name: '', + company_position: '', sex: '' }, sexList: [{ @@ -218,6 +226,18 @@ this.fromSupply = options.from_supply === '1' this.fromBooking = options.from_booking === '1' this.survey_id = /^\d+$/.test(String(options.survey_id || '')) ? String(options.survey_id) : '' + if (this.survey_id && this.$u && this.$u.api && this.$u.api.user) { + this.$u.api.user().then(res => { + if (!res || !res.user) return + const user = res.user + const form = { ...this.form } + for (const key of ['username', 'sex', 'birthday', 'email', 'company_name', 'company_position', 'mobile']) { + const value = key === 'username' ? user.name : user[key] + if ((form[key] == null || !String(form[key]).trim()) && value != null) form[key] = value + } + this.form = form + }).catch(() => this.base.toast('读取个人资料失败,请重试')) + } this.course_id = options.id ? options.id : null this.isDonate = options.isDonate?options.isDonate:false }, @@ -301,6 +321,8 @@ }) }, changeMobile() { + if (this.bindingMobile || this.saving) return + if (this.isBasicInfoIncomplete()) { this.base.toast('请先完善基础信息后再绑定'); return } if (this.base.isNull(this.myMobile)) { this.base.toast('请输入手机号') return @@ -310,16 +332,21 @@ return } const mobile = this.myMobile + this.bindingMobile = true return this.$u.api.bindMobile({ mobile: this.myMobile, code: this.myCode, is_bind: 1 - }).then(res => { - this.base.toast('绑定成功') - this.form.mobile = this.myMobile + }).then(async res => { + if (!res || !res.id || res.errcode) return false + this.$set(this.form, 'mobile', mobile) // that.myMobile = '' this.myCode = '' this.showMobile = false + // Wait for the verified phone to reach u-form's model before validating. + await this.$nextTick() + this.bindingMobile = false + return await this.saveUser({ stayOnPage: Boolean(this.survey_id) }) }).catch(error => { const data = error && error.data if (!data || data.errmsg !== '手机号已存在') return @@ -348,7 +375,7 @@ }) } }) - }) + }).finally(() => { this.bindingMobile = false }) }, selectSex(e) { this.form.sex = this.sexList[e[0]]['value'] @@ -400,61 +427,64 @@ this.manualCompanyName = ''; }, - saveUser() { - if (this.saving) return + async saveUser(options = {}) { + if (this.saving || this.bindingMobile) return false this.saving = true - if (this.registrationComplete && (this.fromSupply || this.fromBooking)) return returnToSupply(this).catch(() => this.base.toast('返回页面失败,请重试')).finally(() => { this.saving = false }) - if (this.registrationComplete && this.survey_id) { - return returnToSurvey(this.survey_id).catch(() => this.base.toast('返回问卷失败,请重试')).finally(() => { this.saving = false }) + const stayOnPage = options && options.stayOnPage === true + const payload = { ...this.form, name: this.form.username } + if (this.survey_id) payload.survey_id = this.survey_id + const signature = JSON.stringify(payload) + let currentSaved = this.registrationComplete && signature === this.lastRegisteredPayload + try { + if (!stayOnPage && currentSaved && (this.fromSupply || this.fromBooking)) { + await returnToSupply(this) + return true + } + if (!stayOnPage && currentSaved && this.survey_id) { + await returnToSurvey(this.survey_id) + return true + } + const valid = await new Promise(resolve => this.$refs.uForm.validate(resolve)) + if (!valid) { + this.base.toast('请检查并完善注册资料后重新提交') + return false + } + const res = await this.$u.api.saveUser(payload) + if (res == null || res === false || (typeof res === 'object' && res.errcode)) throw new Error('注册未成功') + currentSaved = true + this.registrationComplete = true + this.lastRegisteredPayload = signature + if (stayOnPage) { + this.base.toast('绑定并注册成功,请确认资料') + return true + } + if (this.fromSupply || this.fromBooking) { + this.registrationComplete = true + await returnToSupply(this) + return true + } + if (this.survey_id) { + this.registrationComplete = true + await returnToSurvey(this.survey_id) + return true + } + const profile = await this.$u.api.user() + this.$u.vuex('vuex_user', profile.user) + this.base.toast('注册成功', 2000, () => { + if (this.course_id) uni.redirectTo({ url: '/packages/course/detail?id=' + this.course_id }) + else if (this.isDonate) uni.redirectTo({ url: '/packages/donate/index' }) + else uni.switchTab({ url: '/pages/me/index' }) + }) + return true + } catch (error) { + this.base.toast(currentSaved + ? (this.fromBooking ? '注册已成功,返回场地预约失败,请重试' : this.fromSupply ? '注册已成功,返回供需对接失败,请重试' : '注册已成功,返回问卷失败,请点击返回问卷重试') + : '注册未完成,请检查提示后重试') + return false + } finally { + this.saving = false } - let that = this - this.$refs.uForm.validate(valid => { - if (valid) { - this.form.name = this.form.username - this.$u.api.saveUser(this.form).then(async res => { - if (res == null || res === false) throw new Error('注册未成功') - if (this.fromSupply || this.fromBooking) { this.registrationComplete = true; await returnToSupply(this); return } - if (this.survey_id) { - this.registrationComplete = true - await returnToSurvey(this.survey_id) - return - } - return this.$u.api.user().then(res => { - this.$u.vuex('vuex_user', res.user) - this.base.toast("注册成功", 2000, function() { - console.log("that.course_id", that.course_id) - if (that.course_id) { - uni.redirectTo({ - url: '/packages/course/detail?id=' + - that.course_id - }) - // uni.redirectTo({ - // url:'/pages/course/index' - // }) - }else if(that.isDonate){ - uni.redirectTo({ - url: '/packages/donate/index' - }) - } else { - uni.switchTab({ - url: '/pages/me/index' - }) - } - }) - - - }) - }).catch(() => { - this.base.toast(this.registrationComplete ? (this.fromBooking ? '注册已成功,返回场地预约失败,请重试' : this.fromSupply ? '注册已成功,返回供需对接失败,请重试' : '注册已成功,返回问卷失败,请点击返回问卷重试') : '注册未完成,请检查提示后重试') - }).finally(() => { this.saving = false }) - } else { - this.saving = false - console.log('验证失败'); - // this.base.toast("注册失败") - } - }); - - }, + }, } } diff --git a/packages/register/login.vue b/packages/register/login.vue index d13491b..c797271 100644 --- a/packages/register/login.vue +++ b/packages/register/login.vue @@ -1,282 +1,282 @@ - - - - - \ No newline at end of file diff --git a/packages/surveyFill/index.vue b/packages/surveyFill/index.vue index 5a59206..aa8b1ac 100644 --- a/packages/surveyFill/index.vue +++ b/packages/surveyFill/index.vue @@ -1,7 +1,7 @@