uniapp 登录页面的接口调用 封装

封装接口  文件夹   需要重新创建一个   

然后  在文件夹中定义一些   基本请求数据的封装

//判断当前调试和打包(基本差不多了)
const BASE_URL = process.env.NODE_ENV === 'development' ? '' :'https://192.158.2.160:7890'   //路径是瞎写的  换成自己的

//定义请求头
const header = {
    // 'content-type': 'application/json'
}
// 判断token的类型  是固定的  还是后端的
const token = uni.getStorageSync('token') ?? 15704426824366   //请求头 和后端核对一下
if (token) {
    header = {
        'content-type': 'application/json',
        'Authorization':  token
    }
}

// 暴露  request 方法 使用异步调用  
export const request = (option) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + option.url,     //路径
            method: option.method || 'GET', //请求方式
            header: header,                 //请求头
            data: option.data,              //数据
            success(res) {                  // 成功的回调
                resolve(res.data)
            },
            fail(err) {                     //失败的回调
                reject(err)
            }
        })
    })
}

引入页面   在页面进行数据的引入  

<template>
	<view class="login-content">
		<view class="login-title">
			登录
		</view>
		<view class="iphone">
			<input placeholder="输入手机号" :value="username" v-model="username" />
		</view>

		<view class="password">
			<input placeholder="请输入密码" v-model="password" :password="password" />
		</view>

		<u-radio-group class="rememberMe">
			<u-radio :customStyle="{marginBottom: '8px'}" v-model="rememberMe"></u-radio>
			<view class="">记住我</view>
		</u-radio-group>

		<view class="login-btn" @click="dengLu()">点击登录</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/sever'
	import uradiogroup from '../../uni_modules/uview-plus/index.js'
	import {
		ref
	} from 'vue'
	export default {
		components: {
			uradiogroup,
		},
		setup() {
			const username = ref('test000')
			const password = ref('test000')
			const rememberMe = ref(true)
			
			async function dengLu() {
				if (this.username == '' || this.password == '') {
					uni.showToast({
						title: '账号或密码不能为空',
						duration: 2000,
						icon: 'error'
					})
					return;
				}
				console.log("要开始登陆了" + this.username + "连接符" + this.password);
				const option = {
					url: '/app/api/v1/auth/login',
					method: 'post',
					data: {
						username: this.username,
						password: this.password,
						captchaAnswer: "",
						captchaUUID: "",
						rememberMe: this.rememberMe,
						tenantId: 0,
					},
				}
				const resData = await request(option)
				console.log(resData, "resData");
				if (resData.code == 200) {

					console.log("请求成功");
					uni.showToast({
						title: "登录成功",
						icon: "success",
						duration: 2000
					});
					//登录成功 跳转到首页
					uni.switchTab({
						url: '../map/map'
					});
				} else { //登入失败
					uni.showToast({
						title: "账号或密码错误",
						icon: "error",
						duration: 2000
					});
				}
			}
			return {
				username,
				password,
				rememberMe,
				dengLu
			}
		}
	}
</script>

<style scoped>
	.login-content {
		padding: 70px 10px 35px;
		text-align: center;
		color: #333333;
	}

	.login-title {
		font-size: 26px;
		font-weight: bold;
		margin-bottom: 31px;
	}

	.login-content input {
		height: 50px;
		background: #F8F8F8;
		border-radius: 25px;
		text-align: left;
		padding: 15px;
		box-sizing: border-box;
		font-size: 15px;
	}

	.iphone,
	.password,
	.test {
		position: relative;
		margin-bottom: 30px;
	}

	.iphone .uni-icons,
	.password .uni-icons {
		position: absolute;
		top: 14px;
		right: 30px;
	}

	.test-btn,
	.password-btn {
		color: #ff8b33;
		font-size: 15px;
		text-align: right;
	}

	.get-test {
		color: #ff8b33;
		font-size: 15px;
		width: 122px;
		height: 50px;
		border: 1px solid #FF8B33;
		border-radius: 25px;
		line-height: 50px;
	}

	.test {
		display: flex;
		justify-content: space-between;
	}

	.login-btn {
		width: 355px;
		height: 45px;
		background: #FF8B33;
		border-radius: 36px;
		color: #fff;
		font-size: 20px;
		text-align: center;
		line-height: 45px;
		position: fixed;
		bottom: 60px;
	}
</style>

效果展示

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在给uniapp封装登录接口的例子中,首先在文件夹中定义了一些基本请求数据的封装,其中包括了请求的基本URL、请求头以及判断token的类型和赋值等操作。 然后,通过引入页面,可以在页面中使用封装好的登录接口进行数据的引入和调用。 需要注意的是,这个例子是基于Python的Web自动化操作集成工具,它使用了POM模式封装了页面和元素的常用方法,并提供了简洁直观的元素定位语法,同时实现了浏览器和requests之间的无缝切换。这样做可以兼顾浏览器自动化的便利性。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [uniapp 登录页面接口调用 封装](https://blog.csdn.net/weixin_68266812/article/details/129682814)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [基于 python 的 Web 自动化操作集成工具.zip](https://download.csdn.net/download/sohoqq/88275282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值