nanMing 1 year ago
parent
commit
38475466fa
7 changed files with 231 additions and 159 deletions
  1. 38 73
      api/login.js
  2. 13 0
      common/auth.js
  3. 97 0
      common/common.js
  4. 49 80
      common/request.js
  5. 4 6
      config.js
  6. 8 0
      pages.json
  7. 22 0
      pages/login/login.vue

+ 38 - 73
api/login.js

@@ -1,89 +1,54 @@
-import request from '@/common/request.js'
-import config from '@/config.js'
-import {getAppLocal} from './app.js'
-import store from "@/store"
+import request from '@/utils/request'
 
-//发送验证码
-export const sendYzm = async  (data) => {
-	let r = await request({
-		url: config.server + '/school/api/sendSms',
-		method: 'POST',
-		encryption:true,
+// 注册方法
+export function register(data) {
+	return request({
+		url: '/business/registration',
+		method: 'post',
 		data: data
 	})
-	uni.showToast({
-		title: '验证码已经发送',
-		icon: "none",
-		duration: 2000
-	})
 }
-//验证验证码是否正确
-export const checkYzm = async  (data) => {
-	let r = await request({
-		url: config.server + '/school/api/checkCode',
-		method: 'POST',
-		encryption:true,
-		data: data
-	})
-	//1验证成功 0 未成功
-	return r
-}
-//注册用户
-export const register = async  (data) => {
-	let r = await request({
-		url: config.server + '/school/api/register',
-		method: 'POST',
-		encryption:true,
+
+// 登录方法
+export function login(username, password) {
+	const data = {
+		username,
+		password
+	}
+	return request({
+		url: '/auth/wxlogin',
+		headers: {
+			isToken: false
+		},
+		method: 'post',
 		data: data
 	})
-	//1验证成功 0 未成功
-	return r
 }
 
-
-//用户登录
-export const login = async  (data) => {
-	let user = await request({
-		url: config.server + '/school/api/login',
-		method: 'POST',
-		encryption:true,
-		data: data
+// 获取用户详细信息
+export function getInfo() {
+	return request({
+		url: '/getInfo',
+		method: 'get'
 	})
-	//登录成功初始化数据
-	
-	user.login = data
-	
-	//保存用户登录信息
-	uni.setStorage({
-	    key: 'user',
-	    data: user
-	});
-	
-
-	
-	
-	//保存用户登录信息
-	console.log('[用户信息]',uni.getStorageSync('user'))
-	
-	return true
 }
 
-//绑定用户身份
-export const bindIdentity = async  (data) => {
-	let r = await request({
-		url: config.server + '/school/api/identity/bind',
-		method: 'POST',
-		encryption:false,
-		data: data
+// 退出方法
+export function logout() {
+	return request({
+		url: '/logout',
+		method: 'post'
 	})
-	//1验证成功 0 未成功
-	return r
 }
 
-//登出
-export const logout = async  (data) => {
-	uni.clearStorageSync()
-	uni.reLaunch({
-		url: '/pages/login/pages/login.nvue'
+// 获取验证码
+export function getCodeImg() {
+	return request({
+		url: '/captchaImage',
+		headers: {
+			isToken: false
+		},
+		method: 'get',
+		timeout: 20000
 	})
 }

+ 13 - 0
common/auth.js

@@ -0,0 +1,13 @@
+const TokenKey = 'App-Token'
+
+export function getToken() {
+  return uni.getStorageSync(TokenKey)
+}
+
+export function setToken(token) {
+  return uni.setStorageSync(TokenKey, token)
+}
+
+export function removeToken() {
+  return uni.removeStorageSync(TokenKey)
+}

+ 97 - 0
common/common.js

@@ -0,0 +1,97 @@
+/**
+ * 显示消息提示框
+ * @param content 提示的标题
+ */
+export function toast(content) {
+	uni.showToast({
+		icon: 'none',
+		title: content
+	})
+}
+
+/**
+ * 显示模态弹窗
+ * @param content 提示的标题
+ */
+export function showConfirm(content) {
+	return new Promise((resolve, reject) => {
+		uni.showModal({
+			title: '提示',
+			content: content,
+			cancelText: '取消',
+			confirmText: '确定',
+			success: function(res) {
+				resolve(res)
+			}
+		})
+	})
+}
+
+/**
+ * 参数处理
+ * @param params 参数
+ */
+export function tansParams(params) {
+	let result = ''
+	for (const propName of Object.keys(params)) {
+		const value = params[propName]
+		var part = encodeURIComponent(propName) + "="
+		if (value !== null && value !== "" && typeof(value) !== "undefined") {
+			if (typeof value === 'object') {
+				for (const key of Object.keys(value)) {
+					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
+						let params = propName + '[' + key + ']'
+						var subPart = encodeURIComponent(params) + "="
+						result += subPart + encodeURIComponent(value[key]) + "&"
+					}
+				}
+			} else {
+				result += part + encodeURIComponent(value) + "&"
+			}
+		}
+	}
+	return result
+}
+
+// 返回上一页
+export function back() {
+	uni.navigateBack({
+		delta: 1
+	})
+}
+
+// 页面跳转
+export function navigateTo(url) {
+	uni.navigateTo({
+		url: url
+	})
+}
+
+// 回显数据字典
+export function selectDictLabel(datas, value) {
+	if (value === undefined) {
+		return "";
+	}
+	var actions = [];
+	Object.keys(datas).some((key) => {
+		if (datas[key].dictValue == ('' + value)) {
+			actions.push(datas[key].dictLabel);
+			return true;
+		}
+	})
+	if (actions.length === 0) {
+		actions.push(value);
+	}
+	return actions.join('');
+}
+
+// 递归遍历多维数组(未完成)
+export function traverseArray(arr, callback) {
+	if (Array.isArray(arr)) {
+		arr.forEach((element) => {
+			traverseArray(element);
+		});
+	} else {
+		callback(arr)
+	}
+}

+ 49 - 80
common/request.js

@@ -1,88 +1,57 @@
-import config from '../config.js'
-import {login} from '@/api/login.js'
-//请求
-function request (req) {
-	
-	let data = req.data
-	const method = req.method
-	const url = req.url
-	let loading = false
-	if (req.data.loading){
-		loading = data.loading
-		delete data.loading
-	}
-	
-	let header = {
-		'Content-Type': 'application/json',
-		'timestamp': Date.parse(new Date()),
-		// 'token': uni.getStorageSync('user').access_token,
-		'Authorization':'Bearer ' + uni.getStorageSync('user').access_token
-	}
-	if (loading){
-		uni.hideLoading()
-		uni.showLoading({
-		    title: '请求中',
-			mask:true
-		})
+import { getToken } from './auth.js'
+import { toast } from './common.js'
+
+const BASE_URL = 'http://121.36.73.159:807/prod-api'
+
+const request = config => {
+	config.header = config.header || {}
+	if (getToken()) {
+		config.header['Authorization'] = 'Bearer ' + getToken()
 	}
+
 	return new Promise((resolve, reject) => {
-		console.log('REQ ==>', url)
-		console.log( data )
 		uni.request({
-			url: url, 
-			data: data,
-			header: header,
-			method: method.toUpperCase(),
-			timeout:1000 * 60 * 3, //超时三分钟
-			success: async (res) => {
-				uni.hideLoading()
-				console.log("RES <==", url)
-				console.log( res )
-				
-				
-				if (res.statusCode == 200 && res.data.code == 200){
-					resolve(res.data.data)
-				}else if (res.statusCode == 401){
-					//处理token验证出错
-					console.log('Token过期')
-					if (!uni.getStorageSync('user')){
-						//其他设备登录了
-						uni.reLaunch({
-							url: '/pages/login/pages/login'
-						});
-					}else{
-						//token续租
-						let r = await login(uni.getStorageSync('user').login)
-						let result = await request (req)
-					}
-				}else{
-					console.error(res.data.msg)
-					if (res.data.msg == '登录状态已过期'){
-						uni.reLaunch({
-							url: '/pages/login/pages/login'
-						})
+			header: config.header,
+			method: config.method.toUpperCase() || 'GET',
+			dataType: 'json',
+			timeout: config.timeout || 10000,
+			url: config.baseUrl || BASE_URL + config.url,
+			data: config.data,
+		}).then((response) => {
+			let [error, res] = response
+			if (error) {
+				toast('后端接口连接异常')
+				reject('后端接口连接异常')
+				return
+			}
+			const code = res.data.code || 200
+			const msg = res.data.msg || '未知错误,请反馈给管理员'
+
+			if (code === 401) {
+				showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录').then(res => {
+					if (res.confirm) {
+						uni.reLaunch({ url: '/pages/login/login' })
 					}
-					uni.showToast({
-					    title: res.data.msg,
-						icon: "none",
-						position:"bottom",
-					    duration: 3000
-					})
-					reject(false)
-				}
-			},
-			fail: (err) => {
-				uni.hideLoading()	
-				uni.showToast({
-				    title: '服务器休息中,请稍后再试',
-					icon: "none",
-					position:"center",
-				    duration: 3000
-				});
-				console.error(url, err)
-				reject(err)
+				})
+				reject('无效的会话,或者会话已过期,请重新登录')
+			} else if (code !== 200) {
+				toast(msg)
+				reject(code)
 			}
-		});
+			
+			resolve(res.data)
+		}).catch(error => {
+			let { message } = error
+			if (message === 'Network Error') {
+				message = '后端接口连接异常'
+			} else if (message.includes('timeout')) {
+				message = '系统接口请求超时'
+			} else if (message.includes('Request failed with status code')) {
+				message = '系统接口' + message.substr(message.length - 3) + '异常'
+			}
+			toast(message)
+			reject(error)
+		})
 	})
 }
 

+ 4 - 6
config.js

@@ -1,6 +1,4 @@
-const build = {
-	"service" : "https://www.baidu.com"
-}
-
-
-export default build
+// 全局公共配置
+module.exports = {
+	baseUrl: 'http://121.36.73.159:807/prod-api'
+}

+ 8 - 0
pages.json

@@ -45,6 +45,14 @@
 			"style": {
 				"navigationBarTitleText": "添加客户信息"
 			}
+		},
+		{
+			"path" : "pages/login/login",
+			"style" : 
+			{
+				"navigationBarTitleText" : "",
+				"enablePullDownRefresh" : false
+			}
 		}
 	],
 	"globalStyle": {

+ 22 - 0
pages/login/login.vue

@@ -0,0 +1,22 @@
+<template>
+	<view>
+		
+	</view>
+</template>
+
+<script>
+	export default {
+		data() {
+			return {
+				
+			}
+		},
+		methods: {
+			
+		}
+	}
+</script>
+
+<style>
+
+</style>