Browse Source

fix:axio封装测试登录

sunChengjie 11 months ago
parent
commit
5255c1917c
7 changed files with 130 additions and 100 deletions
  1. 2 1
      config/dev.env.js
  2. 1 1
      package.json
  3. 8 0
      src/api/index.js
  4. 1 0
      src/main.js
  5. 84 75
      src/pages/login.vue
  6. 7 4
      src/utils/request.js
  7. 27 19
      yarn.lock

+ 2 - 1
config/dev.env.js

@@ -3,5 +3,6 @@ const merge = require('webpack-merge')
 const prodEnv = require('./prod.env')
 
 module.exports = merge(prodEnv, {
-  NODE_ENV: '"development"'
+  NODE_ENV: '"development"',
+  VUE_APP_BASE_API:"'http://47.101.145.23/tianzong'"
 })

+ 1 - 1
package.json

@@ -14,7 +14,7 @@
     "build": "node build/build.js"
   },
   "dependencies": {
-    "axios": "^0.8.0",
+    "axios": "^0.28.0",
     "echarts": "^5.1.2",
     "jquery": "^3.6.0",
     "sass-loader": "^10.1.1",

+ 8 - 0
src/api/index.js

@@ -1,2 +1,10 @@
 import request from '../utils/request';
 
+export  function solicLogin(data,config) {
+    return request({
+        url: '/appSysUser/solic_login',
+        method: 'post',
+        data: data,
+        ...config
+    })
+}

+ 1 - 0
src/main.js

@@ -17,6 +17,7 @@ import Vant from 'vant'
 import 'vant/lib/index.css'
 import request from "@/utils/request" 
 Vue.prototype.$axios = request
+// Vue.use(request)
 Vue.component('tabbar', tabbar)
 Vue.component('banner', banner)
 Vue.component('back', back)

+ 84 - 75
src/pages/login.vue

@@ -4,13 +4,13 @@
 		<div class="login">
 			<div class="item">
 				<van-cell-group>
-			       <van-field v-model="name"  placeholder="请输入登录名" />
-			    </van-cell-group>
+					<van-field v-model="name" placeholder="请输入登录名" />
+				</van-cell-group>
 			</div>
 			<div class="item">
 				<van-cell-group>
-			       <van-field v-model="password" type="password"  placeholder="请输入密码" />
-			    </van-cell-group>
+					<van-field v-model="password" type="password" placeholder="请输入密码" />
+				</van-cell-group>
 			</div>
 			<!-- <div class="item">
 				<van-cell-group>
@@ -33,87 +33,96 @@
 
 
 <script>
-	import {Toast,Dialog} from 'vant'
-	export default{
-		data(){
-			return{
-				name:"",
-				password:"",
-				yzm:""
+import { Toast, Dialog } from 'vant'
+import { solicLogin } from '../api/index'
+export default {
+	data() {
+		return {
+			name: "",
+			password: "",
+			yzm: ""
+		}
+	},
+	created: function () {
+
+	},
+	methods: {
+		//登录
+		async login() {
+			if (this.name == '' || this.name == undefined) {
+				Toast('请输入登录名');
+				return;
+			}
+			if (this.password == '' || this.password == undefined) {
+				Toast('请输入登录名');
+				return;
 			}
-		},
-		created:function(){
-			
-		},
-		methods:{
-			//登录
-			login(){
-				if(this.name == '' || this.name == undefined){
-					Toast('请输入登录名');
-					return;
-				}
-				if(this.password == '' || this.password == undefined){
-					Toast('请输入登录名');
-					return;
-				}
-				this.$http.post(this.$store.state.host+"/appSysUser/solic_login",{USERNAME:this.name,PASSWORD:this.password},{emulateJSON:true})
-					.then(res=>{
-						//发送成功
-						if(res.body.result=='success'){
-							//localStorage
-							localStorage.setItem("userId",res.body.userId);
-							localStorage.setItem("roomId",res.body.roomId);
-							this.$router.push({path:'/'});
-						}else{
-							Toast('登录失败!');
-						}
-				},res=>{
-						//发送失败
-					Toast("登录失败!")
-				})
+			// this.$http.post(this.$store.state.host+"/appSysUser/solic_login",{USERNAME:this.name,PASSWORD:this.password},{emulateJSON:true})
+			const res = await solicLogin({ USERNAME: this.name, PASSWORD: this.password },{emulateJSON:true})
+			console.log(res,'res')
+
+			if (res.result == 'success') {
+				//localStorage
+				localStorage.setItem("userId", res.userId);
+				localStorage.setItem("roomId", res.roomId);
+				this.$router.push({ path: '/' });
+			} else {
+				Toast('登录失败!');
 			}
+
+			//发送失败
+			// Toast("登录失败!")
+
 		}
 	}
+}
 </script>
 <style scoped lang="less">
-	.content{
-		.loginimg{
-			width:3.48rem;
-			margin: 0 auto;
-			display: block;
-			padding: 2.15rem .8rem;
+.content {
+	.loginimg {
+		width: 3.48rem;
+		margin: 0 auto;
+		display: block;
+		padding: 2.15rem .8rem;
+	}
+
+	.login {
+		width: 5.8rem;
+		margin: 0 auto;
+
+		.item {
+			border: .01rem solid #bfbfbf;
+			border-radius: .1rem;
+			margin-bottom: .5rem;
+			background: white;
+			overflow: hidden;
+			color: #999999;
 		}
-		.login{
-			width: 5.8rem;
-			margin: 0 auto;
-			.item{
-				border: .01rem solid #bfbfbf;
-				border-radius: .1rem;
-				margin-bottom: .5rem;
-				background: white;
-				overflow: hidden;
-				color: #999999;
-			}
-			.item:nth-of-type(3){
-				margin-bottom: .2rem;
+
+		.item:nth-of-type(3) {
+			margin-bottom: .2rem;
+		}
+
+		.btn {
+			button {
+				width: 100%;
 			}
-			.btn{
-				button{
-					width: 100%;
-				}
+		}
+
+		.register {
+			font-size: .24rem;
+			display: flex;
+			justify-content: space-between;
+			margin-top: .2rem;
+
+			a {
+				color: #999999;
 			}
-			.register{
-				font-size: .24rem;
-				display: flex;
-				justify-content: space-between;
-				margin-top: .2rem;
-				a{
-					color: #999999;
-				}
-				a:nth-of-type(1){
-					color: #02bfb3;
-				}
+
+			a:nth-of-type(1) {
+				color: #02bfb3;
 			}
 		}
 	}
+}
 </style>

+ 7 - 4
src/utils/request.js

@@ -1,7 +1,9 @@
 // api/request.js  
 import axios from 'axios';  
+import qs from "qs"
 // 创建请求实例  
 const instance = axios.create({  
+  // baseURL: 'http://47.101.145.23/tianzong', // 设置接口基础路径  
   baseURL: process.env.VUE_APP_BASE_API, // 设置接口基础路径  
   timeout: 5000 // 设置请求超时时间  
 });  
@@ -9,22 +11,23 @@ const instance = axios.create({
 // 设置请求拦截器  
 instance.interceptors.request.use(  
   config => {  
+    if(config.emulateJSON){
+      config.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"
+    }
     // 在请求发送前进行一些操作,如设置请求头、处理请求参数等  
     return config;  
   },  
   error => {  
     // 对请求错误进行处理  
     return Promise.reject(error);  
-  }
+  }  
 );  
 
 // 设置响应拦截器  
 instance.interceptors.response.use(  
   response => {  
     // 对接口返回的数据进行处理,如统一处理错误码、格式化数据等  
-    return {
-      body:response.data
-    };  
+    return response.data;  
   },  
   error => {  
     // 对响应错误进行处理  

+ 27 - 19
yarn.lock

@@ -709,12 +709,14 @@ aws4@^1.8.0:
   resolved "https://registry.npmmirror.com/aws4/-/aws4-1.12.0.tgz#ce1c9d143389679e253b314241ea9aa5cec980d3"
   integrity sha512-NmWvPnx0F1SfrQbYwOi7OeaNGokp9XhzNioJ/CSBs8Qa4vxug81mhJEAVZwxXuBmYB5KDRfMq/F3RR0BIU7sWg==
 
-axios@^0.8.0:
-  version "0.8.1"
-  resolved "https://registry.npmmirror.com/axios/-/axios-0.8.1.tgz#e0eafec0f346139527dc3b79fdcbff8034a24045"
-  integrity sha512-YnMreaDO4t7xufT7xeGyetgG/UTifuwzJtcCEa3QnV9RHiIkN5Be/euRHdP8g4TFL38aPxfIfpndc68VPwKeYQ==
+axios@^0.28.0:
+  version "0.28.0"
+  resolved "https://registry.npmmirror.com/axios/-/axios-0.28.0.tgz#801a4d991d0404961bccef46800e1170f8278c89"
+  integrity sha512-Tu7NYoGY4Yoc7I+Npf9HhUMtEEpV7ZiLH9yndTCoNhcpBH0kwcvFbzYN9/u5QKI5A6uefjsNNWaz5olJVYS62Q==
   dependencies:
-    follow-redirects "0.0.7"
+    follow-redirects "^1.15.0"
+    form-data "^4.0.0"
+    proxy-from-env "^1.1.0"
 
 babel-code-frame@^6.22.0, babel-code-frame@^6.26.0:
   version "6.26.0"
@@ -2306,7 +2308,7 @@ colors@~1.1.2:
   resolved "https://registry.npmmirror.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"
   integrity sha512-ENwblkFQpqqia6b++zLD/KUWafYlVY/UNnAp7oz7LY7E924wmpye416wBOmvv/HMWzl8gL1kJlfvId/1Dg176w==
 
-combined-stream@^1.0.6, combined-stream@~1.0.6:
+combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
   version "1.0.8"
   resolved "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
   integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
@@ -4185,19 +4187,16 @@ flush-write-stream@^1.0.0:
     inherits "^2.0.3"
     readable-stream "^2.3.6"
 
-follow-redirects@0.0.7:
-  version "0.0.7"
-  resolved "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-0.0.7.tgz#34b90bab2a911aa347571da90f22bd36ecd8a919"
-  integrity sha512-RxpX808lAA4IZ2cNqzRedcsPfVuo2AJEL8mmGvGeN0KGLJWZf5fidmUkcB0DWUCrmLD+GAQ0J2WOBORw8BS/Uw==
-  dependencies:
-    debug "^2.2.0"
-    stream-consume "^0.1.0"
-
 follow-redirects@^1.0.0:
   version "1.15.3"
   resolved "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
   integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==
 
+follow-redirects@^1.15.0:
+  version "1.15.6"
+  resolved "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
+  integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==
+
 for-each@^0.3.3:
   version "0.3.3"
   resolved "https://registry.npmmirror.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
@@ -4230,6 +4229,15 @@ forever-agent@~0.6.1:
   resolved "https://registry.npmmirror.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
   integrity sha512-j0KLYPhm6zeac4lz3oJ3o65qvgQCcPubiyotZrXqEaG4hNagNYO8qdlUrX5vwqv9ohqeT/Z3j6+yW067yWWdUw==
 
+form-data@^4.0.0:
+  version "4.0.0"
+  resolved "https://registry.npmmirror.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
+  integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
+  dependencies:
+    asynckit "^0.4.0"
+    combined-stream "^1.0.8"
+    mime-types "^2.1.12"
+
 form-data@~2.3.2:
   version "2.3.3"
   resolved "https://registry.npmmirror.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
@@ -8380,6 +8388,11 @@ proxy-agent@2.0.0:
     pac-proxy-agent "1"
     socks-proxy-agent "2"
 
+proxy-from-env@^1.1.0:
+  version "1.1.0"
+  resolved "https://registry.npmmirror.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
+  integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
+
 prr@~1.0.1:
   version "1.0.1"
   resolved "https://registry.npmmirror.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
@@ -9573,11 +9586,6 @@ stream-browserify@^2.0.1:
     inherits "~2.0.1"
     readable-stream "^2.0.2"
 
-stream-consume@^0.1.0:
-  version "0.1.1"
-  resolved "https://registry.npmmirror.com/stream-consume/-/stream-consume-0.1.1.tgz#d3bdb598c2bd0ae82b8cac7ac50b1107a7996c48"
-  integrity sha512-tNa3hzgkjEP7XbCkbRXe1jpg+ievoa0O4SCFlMOYEscGSS4JJsckGL8swUyAa/ApGU3Ae4t6Honor4HhL+tRyg==
-
 stream-each@^1.1.0:
   version "1.2.3"
   resolved "https://registry.npmmirror.com/stream-each/-/stream-each-1.2.3.tgz#ebe27a0c389b04fbcc233642952e10731afa9bae"