Browse Source

feat(api): 通讯录api
feat(config): 配置文件
feat(contacts): 通讯录获取后端数据
feat(utils): 封装request

wangpx 1 year ago
parent
commit
108858a052
7 changed files with 200 additions and 684 deletions
  1. 16 0
      api/contacts.js
  2. 3 0
      config.js
  3. 34 684
      pages/contacts/index.vue
  4. 13 0
      utils/auth.js
  5. 54 0
      utils/common.js
  6. 6 0
      utils/errorCode.js
  7. 74 0
      utils/request.js

+ 16 - 0
api/contacts.js

@@ -0,0 +1,16 @@
+import request from '@/utils/request.js'
+
+export function getContactAllUser(unitId) {
+	return request({
+		// header: [
+			// isToken: false,
+		// 	Accept: 'application/json'
+		// ],
+		'data': {
+			'serviceId': 'eu_2015V01loadAllUser',
+			'params': {
+				"groupId": unitId
+			}
+		}
+	})
+}

+ 3 - 0
config.js

@@ -0,0 +1,3 @@
+export default {
+	baseUrl: 'http://118.195.146.34:20076/ynet/clientServices.do?iscrypt=1'
+}

+ 34 - 684
pages/contacts/index.vue

@@ -26,9 +26,8 @@
 					<uv-index-anchor :text="indexList[index]" size="16"></uv-index-anchor>
 					<view class="chat_list">
 						<uni-list :border="true">
-							<uni-list-chat
-								v-for="(item, index) in items" @click="clickChat(item)" :title="item.name" :avatar="item.avatar"
-								:note="item.phone" :clickable="true" :avatar-circle="true" :key="index">
+							<uni-list-chat v-for="(item, index) in items" @click="clickChat(item)" :title="item.name"
+								:avatar="item.avatar" :note="item.desktop_phone" :clickable="true" :avatar-circle="true" :key="index">
 								<view class="chat-custom-right">
 									<text class="chat-custom-text" v-text="item.dept"></text>
 								</view>
@@ -42,695 +41,46 @@
 </template>
 
 <script setup lang="ts">
-	import { onMounted, reactive, ref } from 'vue'
+	import { onMounted, ref } from 'vue'
 	import headImg from "/static/images/mine/headImg.jpg";
 	import $tab from '@/plugins/tab.js'
+	import { getContactAllUser } from '@/api/contacts.js'
+	onMounted(() => {
+		const unitId = '47517153919893'
+		getContactAllUser(unitId).then(res => {
+			getContactList(res.returnParams)
+		})
+	})
 	// 索引列表
 	const indexList = ref([])
 	const itemArr = ref([])
-	onMounted(() => {
-		// indexList.value = Object.keys(con)
-		// itemArr.value = Object.values(con)
-		indexList.value = ["A", "B", "C", "D", "E", "F", "G"]
-		itemArr.value = [
-			[
-				{
-					id: 1,
-					name: '张三',
-					phone: '13579543684',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 2,
-					name: '李四',
-					phone: '13987654321',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 3,
-					name: '王五',
-					phone: '13812345678',
-					avatar: headImg,
-					dept: '研发部'
-				},
-			],
-			[
-				{
-					id: 4,
-					name: '赵六',
-					phone: '13765432109',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 5,
-					name: '孙七',
-					phone: '13698765432',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 6,
-					name: '周八',
-					phone: '13524689753',
-					avatar: headImg,
-					dept: '业务部'
-				}
-			],
-			[
-				{
-					id: 7,
-					name: '吴九',
-					phone: '13456789012',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 8,
-					name: '郑十',
-					phone: '13367890123',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 9,
-					name: '钱十一',
-					phone: '13278901234',
-					avatar: headImg,
-					dept: '业务部'
-				}
-			],
-			[
-				{
-					id: 10,
-					name: '孙十二',
-					phone: '13123456789',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 11,
-					name: '李十三',
-					phone: '13034567890',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 12,
-					name: '王十四',
-					phone: '13909876543',
-					avatar: headImg,
-					dept: '业务部'
-				}
-			],
-			[
-				{
-					id: 13,
-					name: '李十五',
-					phone: '13909876543',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 14,
-					name: '孙十六',
-					phone: '13098765432',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 15,
-					name: '钱十七',
-					phone: '13123456789',
-					avatar: headImg,
-					dept: '研发部'
-				}
-			],
-			[
-				{
-					id: 16,
-					name: '郑十八',
-					phone: '13876543210',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 17,
-					name: '吴十九',
-					phone: '13712345678',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 18,
-					name: '周二十',
-					phone: '13534567890',
-					avatar: headImg,
-					dept: '研发部'
-				}
-			],
-			[
-				{
-					id: 19,
-					name: '二十一',
-					phone: '13445678901',
-					avatar: headImg,
-					dept: '研发部'
-				},
-				{
-					id: 20,
-					name: '二十二',
-					phone: '13356789012',
-					avatar: headImg,
-					dept: '业务部'
-				},
-				{
-					id: 21,
-					name: '二十三',
-					phone: '13267890123',
-					avatar: headImg,
-					dept: '研发部'
-				}
-			]
-		]
-	})
-	// function clickTop() {
-	// 	console.log('clickTop');
-	// }
+	function getContactList(data) {
+		console.log('data', data);
+		
+		for(let key in data) {
+			// hasOwnProperty 排除 __proto__等数据
+			if (data.hasOwnProperty(key)) {
+				// 过滤 userState 为 0 的 User
+				data[key] = data[key].filter(user => user.userState !== '0')
+			} else {
+				continue
+			}
+			// 删除 过滤后 数据为0的 索引 
+			if (data[key].length === 0) delete data[key]
+			
+		}
+		// 获取索引
+		indexList.value = Object.keys(data)
+		// 获取索引数据
+		itemArr.value = Object.values(data)
+	}
+	function clickTop() {
+		console.log('clickTop');
+	}
 	// 点击通讯录列表
 	function clickChat(e) {
-		// console.log('clickChat', e);
-		// console.log('con', Object.keys(con));
-		// console.log('con', Object.values(con));
-		$tab.navigateTo('/pages/mine/personal_message/personal_message?id=' + e.id + '&name=' + e.name)
+		$tab.navigateTo('/pages/mine/personal_message/personal_message?id=' + e.useId + '&name=' + e.name)
 	}
-	
-	const con = reactive({
-	"B": [{
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"name": "鲍尾俤",
-		"password": "AC68123A832CF39FEDA3CAFFCC23E78C",
-		"pos": 4,
-		"position": "261937211312800",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381397982300,
-		"userName": "baowd",
-		"userState": "0"
-	}],
-	"C": [{
-		"address": "福建省福州市闽侯县南屿镇南井溪路32号1号楼3004室",
-		"birthday": "2001-09-10",
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"desktop_phone": "15225483609",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "岑梦源",
-		"password": "1C9C20A589FE9340341D7FEC2664E364",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 697039334351644,
-		"userName": "cenmy",
-		"userState": "0"
-	}],
-	"D": [{
-		"address": "",
-		"birthday": "1998-08-27",
-		"companyId": 3,
-		"contact_tel1": "13860036014",
-		"deleteSymbol": "0",
-		"desktop_phone": "",
-		"email": "info@syuan365.com",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "347517153919893",
-		"homeno": "",
-		"intro": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "单位管理员",
-		"password": "AC1EC720D7D6CF1FE1C089EB45C45725",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "12346535445",
-		"rePassword": "",
-		"sex": "F",
-		"unitId": 347517153919893,
-		"useId": 347517210967865,
-		"userName": "yyadmin",
-		"userState": "1"
-	}],
-	"H": [
-        {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336004594300",
-		"name": "何茂鸿",
-		"password": "CDF33BDD235334BDC364C7C196D4E804",
-		"pos": 2,
-		"position": "261878924049400",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381382211000,
-		"userName": "hemh",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336004594300",
-		"name": "何茂雄",
-		"password": "887005748DB2859BB4AFE1CC3AF79D98",
-		"pos": 1,
-		"position": "261870861332700",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381380647600,
-		"userName": "hemx",
-		"userState": "1"
-	}, {
-		"address": "",
-		"birthday": "2002-08-10",
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"desktop_phone": "15037749081",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "韩明阳",
-		"password": "D08DB484CC74BF674C35D88D13CA5DAD",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 511341581866278,
-		"userName": "hanmy",
-		"userState": "1"
-	}, {
-		"address": "",
-		"birthday": "2001-08-15",
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"desktop_phone": "15270946547",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "284138810475600",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "黄树彬",
-		"password": "C51DF6D89F9A4AFC958146E3E2387478",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 511413363379704,
-		"userName": "huangsb",
-		"userState": "1"
-	}],
-	"L": [
-        {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336004594300",
-		"intro": "",
-		"msnnum": null,
-		"name": "林方挺",
-		"password": "52AFDEC797E0068886C4FEA00C29B153",
-		"pos": 6,
-		"position": "261901397478400",
-		"qqnum": "124127047",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381389957000,
-		"userName": "linft",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336004594300",
-		"name": "刘亚光",
-		"password": "ED783A2A5B447E7E87DD0B1FA96CB1AD",
-		"pos": 5,
-		"position": "261901397478400",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381388107500,
-		"userName": "liuyg",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336004594300",
-		"name": "林明东",
-		"password": "EF60BDC22B6CFF067656498A352DAF23",
-		"pos": 3,
-		"position": "261878924049400",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381384120700,
-		"userName": "linmd",
-		"userState": "1"
-	}],
-	"O": [
-        {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"name": "欧阳榕太",
-		"password": "BACC8389B59778F9FA30080B25A0E05E",
-		"pos": 2,
-		"position": "261937211312800",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381394077800,
-		"userName": "ouyrt",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"name": "欧阳杰",
-		"password": "93C67FEC648F070293EB2C4C4F5A2715",
-		"pos": 1,
-		"position": "261937211312800",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381392106000,
-		"userName": "ouyj",
-		"userState": "1"
-	}],
-	"W": [{
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"name": "吴海彪",
-		"password": "63A8758D07F9FD9ECD9D568F7FEF3529",
-		"pos": 5,
-		"position": "261937211312800",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381408316500,
-		"userName": "wuhb",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336004594300",
-		"name": "王新影",
-		"password": "EE1822E3A1940B6A7B0A437496A4DF7C",
-		"pos": 4,
-		"position": "61692348726459",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381386140500,
-		"userName": "wangxy",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"name": "王明",
-		"password": "38C812CF30F1F4811D69ECF42A8CDC1F",
-		"pos": 3,
-		"position": "261937211312800",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381395978900,
-		"userName": "wangm",
-		"userState": "1"
-	}, {
-		"address": "福州市闽侯县南屿镇高岐村工由72号",
-		"birthday": "1988-03-20",
-		"companyId": 3,
-		"contact_tel1": "13859868182",
-		"deleteSymbol": "0",
-		"desktop_phone": "13859868182",
-		"email": "545008867@qq.com",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "284138810475600",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "温广地",
-		"password": "42D3ED47DE1CD144920E1C2A75BC1218",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 8004679116357,
-		"userName": "wengd",
-		"userState": "1"
-	}, {
-		"address": "广东省普宁市洪阳镇水吼村黄厝寮4号",
-		"birthday": "2001-01-26",
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"desktop_phone": "17620456846",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "284138810475600",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "王沛翔",
-		"password": "FCB8E00D6C503270DC86526A61E3583A",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 776635403571619,
-		"userName": "wangpx",
-		"userState": "1"
-	}, {
-		"address": "",
-		"birthday": "1996-08-03",
-		"companyId": 3,
-		"contact_tel1": "17712653403",
-		"deleteSymbol": "0",
-		"desktop_phone": "17712653403",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "347517153919893",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "王璐婷",
-		"password": "BC03FD91366607E9586CB62FE47D01BD",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "F",
-		"unitId": 347517153919893,
-		"useId": 7642691700874,
-		"userName": "wanglt",
-		"userState": "1"
-	}],
-	"Y": [
-        {
-		"address": "福州市马尾区名城国际25#1701",
-		"birthday": "2000-05-08",
-		"companyId": 3,
-		"contact_tel1": "13799319516",
-		"deleteSymbol": "0",
-		"desktop_phone": "13799319516",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "叶轩辰",
-		"password": "6C73633D87B0F268B268DB781BFF1C12",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 784030331214506,
-		"userName": "yexc",
-		"userState": "1"
-	}, {
-		"address": "福建省福州市闽侯县南屿镇南井溪路32号1号楼303室",
-		"birthday": "2002-04-23",
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"desktop_phone": "19859610745",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "杨晴晴",
-		"password": "DEB22331EDF45C7A94C23CF5607AF8CC",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "F",
-		"unitId": 347517153919893,
-		"useId": 696832519230498,
-		"userName": "yangqq",
-		"userState": "1"
-	}],
-	"Z": [
-        {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "284138810475600",
-		"name": "张文腾",
-		"password": "DD724D4B065C59E03D34891D33EBDE2C",
-		"pos": 3,
-		"position": "262035339116900",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381405920700,
-		"userName": "zhangwt",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264392006086800",
-		"name": "张燕琴",
-		"password": "C82C743DAB58AB6F417F50CE79F757BF",
-		"pos": 2,
-		"position": "261972596720800",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381402357600,
-		"userName": "zhangyq",
-		"userState": "1"
-	}, {
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"email": "yt@163.com",
-		"groupXUser": {},
-		"groupid": "264336013866500",
-		"name": "郑晓明",
-		"password": "6E870E864136DB81F08D04150F18B260",
-		"pos": 2,
-		"position": "261995803042300",
-		"rePassword": "",
-		"unitId": 0,
-		"useId": 284381400023500,
-		"userName": "zhengxm",
-		"userState": "1"
-	}, {
-		"address": "",
-		"birthday": "2024-06-20",
-		"companyId": 3,
-		"contact_tel1": "",
-		"deleteSymbol": "0",
-		"desktop_phone": "",
-		"email": "",
-		"fax": "",
-		"fixed_phone": "",
-		"groupXUser": {},
-		"groupid": "264336008819099",
-		"homeno": "",
-		"loginType": "1",
-		"msnnum": "",
-		"name": "张功汶",
-		"password": "2B273FFD0C1B6DDE573100D64DF9173C",
-		"pos": 0,
-		"postcode": "",
-		"qqnum": "",
-		"rePassword": "",
-		"sex": "M",
-		"unitId": 347517153919893,
-		"useId": 695345154337400,
-		"userName": "zhanggw",
-		"userState": "1"
-	}]
-})
 </script>
 
 <style lang="scss">

+ 13 - 0
utils/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)
+}

+ 54 - 0
utils/common.js

@@ -0,0 +1,54 @@
+/**
+* 显示消息提示框
+* @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
+}

+ 6 - 0
utils/errorCode.js

@@ -0,0 +1,6 @@
+export default {
+  '401': '认证失败,无法访问系统资源',
+  '403': '当前操作没有权限',
+  '404': '访问资源不存在',
+  'default': '系统未知错误,请反馈给管理员'
+}

+ 74 - 0
utils/request.js

@@ -0,0 +1,74 @@
+// import store from '@/store'
+import config from '@/config'
+import { getToken } from '@/utils/auth'
+import errorCode from '@/utils/errorCode'
+import { toast, showConfirm, tansParams } from '@/utils/common'
+
+let timeout = 10000
+const baseUrl = config.baseUrl
+
+const request = config => {
+  // 是否需要设置 token
+  const isToken = (config.headers || {}).isToken === false
+  config.header = config.header || {}
+  if (getToken() && !isToken) {
+    config.header['Authorization'] = 'Bearer ' + getToken()
+  }
+  // get请求映射params参数
+  if (config.params) {
+    let url = config.url + '?' + tansParams(config.params)
+    url = url.slice(0, -1)
+    config.url = url
+  }
+  return new Promise((resolve, reject) => {
+    uni.request({
+        method: config.method || 'get',
+        timeout: config.timeout ||  timeout,
+        url: config.baseUrl || baseUrl + config.url,
+        data: config.data,
+        header: config.header,
+        dataType: 'json'
+      }).then(response => {
+        // let [error, res] = response
+        let { statusCode, data } = response
+        // if (data.returnCode == 0) {
+        //   toast('后端接口连接异常')
+        //   reject('后端接口连接异常')
+        //   return
+        // }
+        const code = statusCode || 200
+        const msg = errorCode[code] || data.returnMsg || errorCode['default']
+        if (code === 401) {
+          showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
+            // if (res.confirm) {
+              // store.dispatch('LogOut').then(res => {
+              //   uni.reLaunch({ url: '/pages/login' })
+              // })
+            // }
+          })
+          reject('无效的会话,或者会话已过期,请重新登录。')
+        } else if (code === 500) {
+          toast(msg)
+          reject('500')
+        } else if (code !== 200) {
+          toast(msg)
+          reject(code)
+        }
+        resolve(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)
+      })
+  })
+}
+
+export default request