zqwang 1 year ago
parent
commit
a68e430239
12 changed files with 209 additions and 105 deletions
  1. 1 1
      .gitignore
  2. 4 8
      README.md
  3. 1 1
      common/animate.css
  4. 6 0
      common/apis/common.js
  5. 6 0
      common/apis/my.js
  6. 3 3
      common/config.js
  7. 115 0
      common/http.js
  8. 13 26
      common/request.js
  9. 46 57
      common/utils.js
  10. 6 4
      main.js
  11. 6 3
      pages/client/index.vue
  12. 2 2
      static/js/request.js

+ 1 - 1
.gitignore

@@ -29,7 +29,7 @@ package-lock.json
 *.log
 functions/*
 .temp/**
-
+.env
 # umi
 .umi
 .umi-production

+ 4 - 8
README.md

@@ -13,11 +13,7 @@
 # 4、我的
 
 # 5、分销
-
-
-# 总结
-
-功能基本上完善,欢迎大家测试...
-
-h5地址:https://xjc.demo.hongcd.com/h5 复制手机浏览器打开
-
+ 
+http 请求封装路径: common/http.js
+api 接口对接请求文件放在 common/apis 目录下
+域名配置文件:common/config.js

+ 1 - 1
common/animate.css

@@ -1,5 +1,5 @@
 @charset "UTF-8";
-
+ 
 /*!
  * animate.css -http://daneden.me/animate
  * Version - 3.7.0

+ 6 - 0
common/apis/common.js

@@ -0,0 +1,6 @@
+import http from "../http.js";
+
+// 获取客服
+export const getKefu = () => {
+	return http.get('/api/wxapp/getkefu');
+} 

+ 6 - 0
common/apis/my.js

@@ -0,0 +1,6 @@
+import http from "../http.js";
+
+// 获取客服
+export const getUserInfo = () => {
+	return http.get('/api/wxapp/getUserInfo');
+} 

+ 3 - 3
common/config.js

@@ -1,12 +1,12 @@
 // 配置信息
 export default {
 	// api请求前缀
-	webUrl:"http://api.zyduanju.com",
-	// webUrl:'http://api.duanju.dududus.com',//uni.getStorageSync("config").weburl?uni.getStorageSync("config").weburl:'http://192.168.10.175',
+	apiUrl:"http://api.duanju.dududus.com",
+	webUrl:'http://api.duanju.dududus.com',//uni.getStorageSync("config").weburl?uni.getStorageSync("config").weburl:'http://192.168.10.175',
 	imgUrl:'https://xjc.demo.hongcd.com',//uni.getStorageSync("config").imgurl?uni.getStorageSync("config").imgurl:'http://192.168.10.175',
 	staticUrl:'https://xjc.demo.hongcd.com/img/',//静态图片地址https://xjc.demo.hongcd.com
 	// websocket地址
 	//websocketUrl:"https://wx.***.com",
 	// 消息提示tabbar索引  websocketUrl:"https://wx.***.com",
 	TabbarIndex:2
-}
+} 

+ 115 - 0
common/http.js

@@ -0,0 +1,115 @@
+// 引入配置文件
+import config from "./config.js";
+// #ifdef MP-WEIXIN
+import wxApp from "./wecaht.min.app.js"
+import {
+	getToken,
+	clearToken
+} from './token.js'
+// #endif
+const filterNoToken = ["/api/auth/refreshToken", "/api/auth/login"];
+
+export default {
+	config: {
+		baseUrl: config.apiUrl,
+		header: {
+			'Content-Type': 'application/json;charset=UTF-8',
+		},
+		data: {},
+		method: "GET",
+		dataType: "json",
+	},
+	async request(options = {}) {
+		options.header = options.header || this.config.header;
+		options.method = options.method || this.config.method;
+		options.dataType = options.dataType || this.config.dataType;
+		
+		let version = 'web';
+		// #ifdef MP-WEIXIN  
+		//	微信小程序处理
+		options.header.appid = wxApp.getWXAppId();
+		options.header.source = 'wxapp';
+		// 过滤不需要token的api
+		if (filterNoToken.indexOf(options.url) === -1) {
+			options.header.Authorization = await (this.getTokenString());
+		}
+		// #endif
+		options.url = this.config.baseUrl + options.url;
+		console.log('options.url ',options.url )
+		return uni.request(options).then(res => {
+			res = res[1].data;
+			if (res.code == 500201) {
+				// 未登录
+				clearToken();
+				getToken();
+				if (filterNoToken.indexOf(options.url) === -1) {
+					return this.request(options);
+				}
+			}
+			return res.data;
+		});
+	},
+	get(url, data, options = {}) {
+		options.url = url;
+		options.data = data;
+		options.method = 'GET';
+		return this.request(options);
+	},
+	post(url, data, options = {}) {
+		options.url = url;
+		options.data = data;
+		options.method = 'POST';
+		return this.request(options);
+	},
+
+	// 上传图片
+	upload(url, options = {}) {
+		options.url = this.config.baseUrl + url;
+		options.header = options.header || this.config.header;
+		options.fileType = options.fileType || "image";
+		options.formData = options.formData || {};
+		options.filePath = options.filePath;
+		options.name = options.name;
+		// TODO:token增加等操作
+		if (options.token) {
+			// 验证是否登录
+			if (!this.checkToken(options.checkToken)) return;
+			// 验证权限
+			if (!this.checkAuth(options.checkAuth)) return;
+			options.header.token = User.token;
+		}
+
+		return uni.uploadFile(options);
+	},
+	// 错误处理
+	errorCheck(err, res, errfun = false, resfun = false) {
+		if (err) {
+			typeof errfun === 'function' && errfun();
+			uni.showToast({
+				title: '加载失败',
+				icon: "none"
+			});
+			return false;
+		}
+		if (res.data.errorCode) {
+			typeof errfun === 'function' && resfun();
+			uni.showToast({
+				title: res.data.msg,
+				icon: "none"
+			});
+			return false;
+		}
+		return true;
+	},
+	async getTokenString() {
+		// console.log('getTokenString-----start');
+		await getToken();
+		let token = uni.getStorageSync('token');
+		// token格式化
+		if (token && typeof token === "string") {
+			token = JSON.parse(token);
+		}
+		// console.log('getTokenString--end', token);
+		return token.token;
+	}
+}

+ 13 - 26
common/request.js

@@ -1,40 +1,27 @@
 // 引入配置文件
 import config from "./config.js";
-// #ifdef MP-WEIXIN
-	import  wxApp from "./wecaht.min.app.js"
-	import {getToken}  from './utils.js'
-	const token = getToken();
-// #endif
-const filterNoToken = ["/api/auth/refreshToken", "/api/auth/login"];
-
 export default{
 	config:{
 		baseUrl:config.webUrl,
 		header:{
 			'Content-Type':'application/json;charset=UTF-8',
+			'Content-Type':'application/x-www-form-urlencoded'
 		},
 		data: {},
 		method: "GET",
-		dataType: "json", 
+		dataType: "json",
 	},
-	async request(options = {}){  
+	request(options = {}){
 		options.header = options.header || this.config.header;
 		options.method = options.method || this.config.method;
 		options.dataType = options.dataType || this.config.dataType;
-		// TODO:token增加等操作
-		  
 		options.url = this.config.baseUrl+options.url;
-		
-		var version='web';  
-		// #ifdef MP-WEIXIN  
-			options.header.appid = wxApp.getWXAppId(); 
-			options.header.source = 'wxapp';
-			if(filterNoToken.indexOf(options.url) === -1){
-				options.header.Authorization = token;
-			}  
-			version = "wxapp"; 
-		// #endif
-		console.log('ctttttttttttttttttt',token)
+		// TODO:token增加等操作
+		if (options.token) {
+			options.data.token = uni.getStorageSync("userinfo").token;
+			options.data.uid = uni.getStorageSync("userinfo").id;
+		}
+		var version='web';
 		// #ifdef APP-PLUS
 		var version=plus.runtime.version
 		// #endif
@@ -43,26 +30,26 @@ export default{
 			if(res[1].data.msg=='version10001'){
 				uni.showModal({
 					title: '更新提示',
-					content:"当前版本需要更新,请及时更新", 
+					content:"当前版本需要更新,请及时更新",
 					showCancel: false,
 					confirmText: "确定",
 					success: function (ress) {
 						plus.runtime.openURL(res[1].data.data);
 						uni.reLaunch({
 							url:'/pages/public/login'
-						}) 
+						})
 					}
 				});
 				return false;
 			}
-			return res[1].data.data;
+			return res;
 		});
 	},
 	get(url,data,options={}){
 		options.url = url;
 		options.data = data;
 		options.method = 'GET';
-		return this.request(options); 
+		return this.request(options);
 	},
 	post(url,data,options={}){
 		options.url = url;

+ 46 - 57
common/utils.js

@@ -1,51 +1,51 @@
-
-
-import wxApp from "./wecaht.min.app.js";
- import request from "./request.js"
+import wxApp from "./wecaht.min.app.js";
+import request from "./http.js"
 let token = null;
-let requestLoginTime = 0;
+let requestLoginTime = 0;
 
 // 获取token
-export const getToken = async () => {
-
-	if (getToken.promise) return getToken.promise;
-	if(!token) token = uni.getStorageSync('token');
-	
+export const getToken = async () => {
+	if (getToken.promise) return getToken.promise;
+	if (!token) token = uni.getStorageSync('token');
 	// token格式化
-	if (token && typeof token === "string") token = JSON.parse(token);
-	
-	console.log("checkToken", checkToken(token));
-	
-	if (!token) getToken.promise = login();
-	else if (checkToken(token)) return Promise.resolve(token.token);
-	else {
+	if (token && typeof token === "string") {
+		token = JSON.parse(token);
+	}
+	console.log("checkToken", checkToken(token));
+	if (!token) {
 		getToken.promise = login();
+	} else if (checkToken(token)) {
+		return Promise.resolve(token.token);
+	} else {
+		getToken = refreshToken();
 	}
-	
+
 	getToken.promise = getToken.promise
-		.finally(() => {
+		.finally(() => {
 			getToken.promise = null;
 		})
 		.then(r => {
-			console.log("refresh");
+			console.log("refresh",r); 
 			return setToken(r);
 		});
-
 	return getToken.promise;
+
 };
 
 
 // 登录
 export const login = async () => {
-	requestLoginTime++;
-	if (requestLoginTime != 1) {
-		let loginResponed = await loginPromise();
-	}
-	let code = await (wxApp.getWxMinAppUuerCode());
-	let res = request.post("/api/auth/login",{code:code,appid:wxApp.getWXAppId()});
+	 requestLoginTime++;
+	  if (requestLoginTime != 1) {
+	    let loginResponed = await loginPromise();
+	  }
+	let code = await (wxApp.getWxMinAppUuerCode());
+	let res = await request.post("/api/auth/login", {
+		code: code,
+		appid: wxApp.getWXAppId() 
+	});
 	requestLoginTime = 0;
 	return res;
-	// return 
 };
 
 
@@ -68,26 +68,13 @@ export const checkResult = r => {
 };
 
 // 刷新token
-const refreshToken = () => {
-	
-	// let res = request.post("/api/auth/refreshToken",{refreshToken:code,appid:wxApp.getWXAppId()});
-	console.log('11111111111sssssssssssssssss',token); 
-	// return fly
-	// 	.get("/refreshToken", {
-	// 		token: token.token
-	// 	}, {
-	// 		headers: {
-	// 			Authorization: token.token 
-	// 		}
-	// 	}) 
-	// 	.then(r => {
-	// 		return r;
-	// 	})
-	// 	.catch(e => {
-	// 		console.log(e);
-	// 		console.error("刷新token失败");
-	// 		return Promise.reject(e);
-	// 	});
+const refreshToken = () => {
+	let res = request.post("/api/auth/refreshToken", {
+		refreshToken: token.refreshToken,
+		appid: wxApp.getWXAppId()
+	});
+	console.log('refreshTokenrrrrr', res);
+	return res;
 };
 
 // 检查token的有效期
@@ -97,21 +84,23 @@ const checkToken = t => {
 
 // 设置token
 const setToken = async t => {
-	token = formatToken(t);
-	uni.setStorageSync("token",token);
+	console.log('bbbbbbbbbbbbbbbbbbbbbbb', t);
+	token = formatToken(t);
+	console.log('tttttttttt', token);
+	uni.setStorageSync("token", token);
 	return t.token;
 };
 
-// 清除token 
+// 清除token  
 export const clearToken = async () => {
-	token = null;
+	token = null;
 	uni.removeStorageSync('token');
-	console.log("clear token done"); 
-};
+	console.log("clear token done");
+};
 
 // 处理token的格式
-const formatToken = t => { 
+const formatToken = t => {
 	t.token = "Bearer " + t.accessToken;
-	t.time = ((t.expiration -600)* 1000);
-	return t; 
+	t.time = ((t.expiration - 600) * 1000);
+	return t;
 };

+ 6 - 4
main.js

@@ -3,20 +3,22 @@ import App from './App'
 import store from './static/js/store/store.js';
 import config from './static/js/config.js';
 
+
+
 Vue.prototype.$config = config;
-import * as http from './static/js/request.js';
+import * as http from './static/js/request.js'; 
 Vue.prototype.$http  = http;
 
 import md5 from './static/js/md5.js';
 Vue.prototype.$Md5  =  md5;
-
 //权限相关的判断
-//  
+import {common} from './static/js/mixin/common.js';
+Vue.mixin(common);
 // 挂载全局方法
 import configs from "./common/config.js"
 Vue.prototype.configs=configs
 // import lib from "./common/lib.js"
-// Vue.prototype.lib=lib
+// Vue.prototype.lib=lib 
 
 import request from "./common/request.js";
 Vue.prototype.$httpas = request;

+ 6 - 3
pages/client/index.vue

@@ -123,6 +123,7 @@
 	</view>
 </template>
 <script>
+	import {getKefu} from "@/common/apis/common.js"
 	export default{
 		data(){
 			return {
@@ -193,12 +194,14 @@
 			// })
 			//this.getList();
 		},
-		onShow() {
-			this.getList();
+		async onShow() {
+			let res = await getKefu();
+			console.log('kefu',res);
+			// this.getList();
 			// this.getList(1);
 			// this.getList(2);
 			// this.getList(3);
-			let this_=this
+			// let this_=this
 		},
 		methods:{
 			changeIndex(index){

+ 2 - 2
static/js/request.js

@@ -30,9 +30,9 @@ const  post   =  function (url,params){
 			method:'POST',
 		    success: (res) => {
 				uni.hideLoading(); 
-				if(res.statusCode == 200){
+				if(res.statusCode == 200){ 
 					if(res.data.code == 200){
-						if(res.data.code == 100){ //未登录的情况
+						if(res.data.code == 100){ //未登录的情况 
 							store.commit('setMember',null);
 							uni.removeStorageSync('member-token');
 							reject({