yuanyiming 1 年之前
父节点
当前提交
3595f102f4
共有 6 个文件被更改,包括 65 次插入127 次删除
  1. 5 4
      App.vue
  2. 47 0
      api/request.js
  3. 4 0
      main.js
  4. 5 13
      pages.json
  5. 4 15
      pages/login/login.vue
  6. 0 95
      pages/login_cs/login_cs.vue

+ 5 - 4
App.vue

@@ -6,10 +6,11 @@
 			// 微信登录
 			wx.login({
 				success({code}) {
-					console.log('code:'+code);
+					// console.log('code:'+code);
+					// this.$request('/weChatCode','GET',{data:code})
 					uni.request({
-						url:'http://localhost:3001/login2',
-						method:'POST',
+						url:'http://10.3.13.84:7003/api/pbl/weChatCode',
+						method:'GET',
 						data:{code}
 					}).then(res=>{
 						console.log(res.data);
@@ -17,7 +18,7 @@
 					})
 				}
 			})
-
+ 
 			uni.setStorageSync('login', '0');
 			// uni.setStorageSync('msgLogin', '0');
 

+ 47 - 0
api/request.js

@@ -0,0 +1,47 @@
+// import config from './config.js';
+ 
+const request = (url = '', method = 'GET', data = {}, header = {   //这里这样封装是为了后续具体组件中使用时可以直接传参,需按此顺序传参;而不需要写url:xxx等键值对传参
+	//具体的header和后端商同后再编写,这里以常见的token为例
+	'Authorization': 'Bearer '+ (uni.getStorageSync('token') ? uni.getStorageSync('token') : ''),
+}) => {
+	return new Promise((resolve, reject) => {
+		// console.log(header);
+		uni.request({
+			url: 'http://10.3.13.84:7003' + url, //接口地址:前缀+方法中传入的地址
+			method: method, //请求方法
+			data: data, //传递参数
+			header: header, //自定义头部,和后端商同后编写
+			success: (res) => {
+				// console.log('request.js文件的通用接口请求封装返回的结果数据',res);
+				//注:因为这里对请求成功的没有统一设置抛错提示,所以后续具体组件中使用接口请求的res除200(实际以后端同事定好的为准)成功外的其他code需要额外写抛错提示
+				if (res.data.code == 405) { //自定请求失败的情况,这里以常见的token失效或过期为例
+					uni.removeStorageSync('token');
+					uni.showModal({
+						showCancel: false,
+						title: '温馨提示',
+						content: res.data.msg,
+						success: function(result) {
+							if (result.confirm) {
+								uni.reLaunch({
+									url: '/pages/login/login'    //这里需用绝对路径才可
+								});
+							}
+						}
+					});
+				}
+				resolve(res.data) //成功
+			},
+			// 这里的接口请求,如果出现问题就输出接口请求失败的msg;
+			//注:因为这里对于请求失败的设置统一抛错提示了,所以后续具体组件中使用接口请求的catch中不需要再写抛错提示
+			fail: (err) => {
+				uni.showToast({
+					title: "" + err.msg,
+					icon: 'none'
+				});
+				reject(err)
+			}
+		})
+	})
+}
+        
+export default request;

+ 4 - 0
main.js

@@ -4,6 +4,10 @@ import App from './App'
 import Vue from 'vue'
 import './uni.promisify.adaptor'
 Vue.config.productionTip = false
+
+import request from './api/request.js'
+Vue.prototype.$request=request
+
 App.mpType = 'app'
 const app = new Vue({
   ...App

+ 5 - 13
pages.json

@@ -7,30 +7,22 @@
 		}
 	},
 	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
+
 		{
 			"path": "pages/index/index",
 			"style": {
 				"navigationBarTitleText": "首页"
-		
+
 			}
-		},{
+		},
+		{
 			"path": "pages/login/login",
 			"style": {
 				"navigationBarTitleText": "登录",
 				"enablePullDownRefresh": false
 			}
 
-		}, {
-			"path": "pages/login_cs/login_cs",
-			"style": {
-				"navigationStyle": "default",
-				"navigationBarTitleText": "登录测试",
-				"enablePullDownRefresh": false
-			}
-
-		}, 
-
-
+		},
 		{
 			"path": "pages/login_two/login_two",
 			"style": {

+ 4 - 15
pages/login/login.vue

@@ -21,7 +21,7 @@
 		<button class="btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
 			<image class="avatar" :src="avatarUrl"></image>
 		</button>
-		<!-- <button class="btn" @click="gotoIndex">一键登录</button> -->
+		<button class="btn" @click="gotoIndex">一键登录</button>
 		<!-- <button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键登录</button> -->
 		<view class="footer" @click="goto">
 			<text>手机号登录/账号密码登录</text>
@@ -74,20 +74,9 @@
 			gotoIndex() {
 				const value = uni.setStorageSync('login', '1');
 
-				// uni.switchTab({
-				// 	url: '/pages/index/index'
-				// })
-				// if(this.isMsg==1){
-				// 	uni.setStorageSync('msgLogin', '1');
-				// 	uni.switchTab({
-				// 		url: '/pages/message/message'
-				// 	})
-				// }else{
-				// 	const value = uni.setStorageSync('login', '1');
-				// 	uni.switchTab({
-				// 		url: '/pages/index/index'
-				// 	})
-				// }
+				this.$request('/a','GET',{u:1}).then(res=>{
+					console.log(res);
+				})
 			},
 			goto() {
 				uni.navigateTo({

+ 0 - 95
pages/login_cs/login_cs.vue

@@ -1,95 +0,0 @@
-<template>
-	<view class="login_cs">
-		<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
-			<image class="avatar" :src="avatarUrl"></image>
-		</button>
-		<input type="nickname" style="margin:30rpx auto;width: 400rpx;background-color: aqua;" placeholder="请输入昵称" />
-
-		<button class="avatar-wrapper" @click="login">登录</button>
-	</view>
-</template>
-
-<script>
-	export default {
-		data() {
-			return {
-				avatarUrl: '',
-			};
-		},
-		methods: {
-			getPhoneNumber(e) {
-				console.log(e.detail.code) // 动态令牌
-				console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)
-				console.log(e.detail.errno) // 错误码(失败时返回)
-			},
-			onChooseAvatar(e) {
-				console.log(e);
-				const {
-					avatarUrl
-				} = e.detail
-				this.avatarUrl = avatarUrl
-
-				//对临时图片链接进行base64编码
-				var avatarUrl_base64 = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(this.avatarUrl,
-					'base64')
-				console.log(avatarUrl_base64);
-				
-				
-				uni.request({
-					url: 'http://localhost:3001/login2',
-					method:'POST',
-					data:avatarUrl_base64,
-					success(res) {
-						console.log(res);
-					}
-				})
-			},
-			login() {
-				wx.login({
-					success: (res) => {
-					 return	console.log(res)
-						var code = res.code //获取code
-
-						
-
-						wx.request({ //调用后端接口
-							url: 'http://localhost:3001/login2',
-							method: 'POST',
-							// header: {
-							// 	'content-type': 'application/json'
-							// },
-							data: {
-								code: code, //请求体中封装code
-							},
-							success(res) {
-								console.log(res)
-								//页面跳转
-								// wx.navigateTo({
-								// 	//携带用户头像信息和用户昵称信息
-								// 	url: '/index/index?userAvatarUrl=' + res.data.data
-								// 		.userAvatarUrl + '&userName=' + res.data.data.userName,
-								// })
-							}
-						})
-					},
-				})
-
-
-			}
-		}
-	}
-</script>
-
-<style lang="scss">
-	.avatar-wrapper {
-		width: 300rpx;
-		background-color: #fff;
-		margin: auto;
-	}
-
-	.avatar {
-		width: 60rpx;
-		height: 60rpx;
-
-	}
-</style>