🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[toc] # 7.1 登录注册页面开发 ## 7.1.1 实现登录注册页面 ``` <template> <view> <view class="flex flex-column align-center justify-center" style="height:350rpx;"> <text style="font-size:50rpx; ">YOU-LOGIN</text> </view> <view class="px-3"> <input type="text" v-model="form.username" class="bg-light px-3 mb-3 font" placeholder="请输入用户名" style="height:100rpx;" /> <input type="password" v-model="form.password" class="bg-light px-3 mb-3 font" placeholder="请输入密码" style="height:100rpx;" /> <input v-if="type !== 'login'" type="password" v-model="form.repassword" class="bg-light px-3 mb-3 font" placeholder="请输入确认密码" style="height:100rpx;" /> </view> <view class="p-3 flex justify-center align-center"> <view hover-class="bg-main-hover" class=" bg-main rounded p-3 flex align-center justify-center flex-1"> <text class="text-white font-md">{{type === "login" ? "登 录" : "注 册"}}</text> </view> </view> <view class="flex justify-center align-center"> <text class="text-light-muted font p-2" @click="changeType"> {{type === "login" ? "注册账号" : "去登录"}} </text> </view> </view> </template> <script> export default { data() { return { type : "login", form : { username : "", password : "", repassword : "" } }; }, methods : { changeType(){ this.type = this.type == "login" ? "reg" : "login" } } } </script> <style lang="scss"> </style> ``` ## 7.1.2 实现request.js文件的封装 - config.js ``` export default { /* 注意:以下接口改成你自己部署的后端 */ baseUrl:"http://liveapi2.dishait.cn", socketUrl:"http://liveapi2.dishait.cn", imageUrl:"http://liveapi2.dishait.cn", // 拉流前缀 livePlayBaseUrl:"http://liveapi2.dishait.cn:23481", // 推流前缀 livePushBaseUrl:"rtmp://liveapi2.dishait.cn:23480", } ``` - request.js ``` // import $store from '@/store/index.js'; import $C from './config.js'; export default { // 全局配置 common:{ baseUrl:$C.baseUrl +"/api", header:{ 'Content-Type':'application/json;charset=UTF-8', }, data:{}, method:'GET', dataType:'json', token:false }, // 请求 返回promise request(options = {}){ // 组织参数 options.url = this.common.baseUrl + options.url options.header = options.header || this.common.header options.data = options.data || this.common.data options.method = options.method || this.common.method options.dataType = options.dataType || this.common.dataType options.token = options.token === true ? true : this.common.token // 请求 return new Promise((res,rej)=>{ // 请求之前验证... // token验证 if (options.token) { let token = uni.getStorageSync('token') // 二次验证 if (!token && options.noJump !== true) { uni.showToast({ title: '请先登录', icon: 'none' }); // token不存在时跳转 uni.navigateTo({ url: '/pages/login/login', }); return rej("请先登录") } // 往header头中添加token options.header.token = token } // 请求中... uni.request({ ...options, success: (result) => { // 返回原始数据 if(options.native){ return res(result) } // 服务端失败 if(result.statusCode !== 200){ if (options.toast !== false) { uni.showToast({ title: result.data.data || '服务端失败', icon: 'none' }); } // token不合法,直接退出登录 if(result.data.data === 'Token 令牌不合法!'){ // 退出登录操作 // $store.dispatch('logout') } return rej(result.data) } // 其他验证... // 成功 let data = result.data.data res(data) }, fail: (error) => { uni.showToast({ title: error.errMsg || '请求失败', icon: 'none' }); return rej(error) } }); }) }, // get请求 get(url,options = {}){ options.url = url options.data = {} options.method = 'GET' return this.request(options) }, // post请求 post(url,data = {},options = {}){ options.url = url options.data = data options.method = 'POST' return this.request(options) }, // delete请求 del(url,data = {},options = {}){ options.url = url options.data = data options.method = 'DELETE' return this.request(options) }, // 上传文件 upload(url,data,onProgress = false){ return new Promise((result,reject)=>{ // 上传 let token = uni.getStorageSync('token') if (!token) { uni.showToast({ title: '请先登录', icon: 'none' }); // token不存在时跳转 return uni.reLaunch({ url: '/pages/login/login', }); } const uploadTask = uni.uploadFile({ url:this.common.baseUrl + url, filePath:data.filePath, name:data.name || "files", header:{ token }, formData:data.formData || {}, success: (res) => { if(res.statusCode !== 200){ result(false) return uni.showToast({ title: '上传失败', icon: 'none' }); } let message = JSON.parse(res.data) result(message.data); }, fail: (err) => { console.log(err); reject(err) } }) uploadTask.onProgressUpdate((res) => { if(typeof onProgress === 'function'){ onProgress(res.progress) } }); }) } } ``` ## 7.1.3 在项目集成vuex 1. 创建store文件夹 2. 在store文件夹内创建index.js文件 3. 实例化vuex以及核心方法