其它编码.md 3.9 KB

在方法中,分支不要嵌套

login(){
	if(!this.hasRead){
		uni.showModal({
			title: '提示',
			content: '请勾选用户协议',
			showCancel: false,
		});
	}else{
		if(this.phone == '' || !this.phone){
			uni.showModal({
				title: '提示',
				content: '请填写手机号码',
				showCancel: false,
			});
		}else{
			if(this.loginType == 0 && (this.code == '' || !this.code)){
				uni.showModal({
					title: '提示',
					content: '请填写验证码',
					showCancel: false,
				});
			}else{
				if(this.loginType == 1 && (this.password == '' || !this.password)){
					uni.showModal({
						title: '提示',
						content: '请输入密码',
						showCancel: false,
					});
				}else{
					var that = this;
					console.log('openid',that.openid);
					uni.request({
						url: config.apiHost + config.apiUrl.sendLoginData,
						data: {phone: that.phone, type: 'code', code: that.code, openId: that.openid, loginType: that.loginType, password: that.password}	,
						success: function (res) {
							let response = res.data;
							if(response.code == 0){
								uni.setStorageSync('adminId', response.data.id);
								uni.setStorageSync('schoolId', response.data.schoolId);
								let redirect = uni.getStorageSync('redirectUrl');
								if(!response.data.mp_open_id || response.data.mp_open_id == ''){
									//跳转新的webview页面获取用户公众号的openid
									uni.navigateTo({
										url: '/pages/index/open?userId='+ response.data.id
									})
									return;
								}
								// uni.setStorageSync('adminTypes', response.data.types);
								uni.showToast({
									title: '登录成功,即将跳转首页',
									duration: 2000
								});
								if(redirect){
									uni.reLaunch({
										url: redirect
									});
								}else{
									uni.reLaunch({
										url: '/pages/index/index'
									});
								}
							}else{
								uni.showModal({
									title: '提示',
									content: response.msg,
									showCancel: false,
								});
							}
						}
					});
				}
			}
		}
	}
},

修改后

login0() {
	if (!this.hasRead) {
		uni.showModal({
			title: "提示",
			content: "请勾选用户协议",
			showCancel: false
		});
		return;
	}
	if (this.phone == "" || !this.phone) {
		uni.showModal({
			title: "提示",
			content: "请填写手机号码",
			showCancel: false
		});
		return;
	}
	if (this.loginType == 0 && (this.code == "" || !this.code)) {
		uni.showModal({
			title: "提示",
			content: "请填写验证码",
			showCancel: false
		});
		return;
	}
	if (this.loginType == 1 && (this.password == "" || !this.password)) {
		uni.showModal({
			title: "提示",
			content: "请输入密码",
			showCancel: false
		});
		return;
	} else {
		var that = this;
		console.log("openid", that.openid);
		uni.request({
			url: config.apiHost + config.apiUrl.sendLoginData,
			data: { phone: that.phone, type: "code", code: that.code, openId: that.openid, loginType: that.loginType, password: that.password },
			success: function (res) {
				let response = res.data;
				if (response.code == 0) {
					uni.setStorageSync("adminId", response.data.id);
					uni.setStorageSync("schoolId", response.data.schoolId);
					let redirect = uni.getStorageSync("redirectUrl");
					if (!response.data.mp_open_id || response.data.mp_open_id == "") {
						//跳转新的webview页面获取用户公众号的openid
						uni.navigateTo({
							url: "/pages/index/open?userId=" + response.data.id
						});
						return;
					}
					// uni.setStorageSync('adminTypes', response.data.types);
					uni.showToast({
						title: "登录成功,即将跳转首页",
						duration: 2000
					});
					if (redirect) {
						uni.reLaunch({
							url: redirect
						});
					} else {
						uni.reLaunch({
							url: "/pages/index/index"
						});
					}
				} else {
					uni.showModal({
						title: "提示",
						content: response.msg,
						showCancel: false
					});
				}
			}
		});
	}
},