Kaynağa Gözat

上传文件至 ''

linwenxin 4 ay önce
ebeveyn
işleme
d1d911a1e5
2 değiştirilmiş dosya ile 249 ekleme ve 0 silme
  1. 222 0
      axios.js
  2. 27 0
      index.js

+ 222 - 0
axios.js

@@ -0,0 +1,222 @@
+import {
+	base_url,
+	appId
+} from '@/utils/config.js';
+import {
+	urlEncode
+} from '@/utils/utils.js';
+import store from '../store/index.js';
+//封装的通用请求方法
+export const axios = function(obj = {}) {
+	if (typeof obj !== 'object') {
+		throw new Error('arguments must be object');
+	}
+	if (!obj.url) {
+		throw new Error('url not defined');
+	}
+
+	if (obj.isLoading) {
+		uni.showLoading({
+			title: '加载中',
+			mask: true,
+		})
+	}
+	let _url = base_url + obj.url;
+	let isGet = obj.method && obj.method.toLowerCase() == 'get';
+	if (isGet) {
+		_url += '?' + urlEncode(obj.params)
+	}
+
+
+
+	if (
+		// 已经获取用户详情以及当前商家数据
+		(uni.getStorageSync('token') && uni.getStorageSync('websitIdSj')) ||
+		// 获取用户详情
+		!!~_url.indexOf('/user/auth') ||
+		// 获取当前商家数据
+		(uni.getStorageSync('token') && !!~_url.indexOf("/user/userWebsit"))
+	) {
+		return new Promise((resolve, reject) => {
+			uni.request({
+				url: _url,
+				data: isGet ? '' : (obj.params || ''),
+				header: {
+					'content-type': obj.type || 'application/x-www-form-urlencoded',
+					"x-token": uni.getStorageSync('token') || '',
+					'APPID': appId
+				},
+				method: obj.method ? obj.method : "POST",
+				success: function(res) {
+					if (obj.isLoading) {
+						uni.hideLoading();
+					}
+					if (res.statusCode === 200) {
+						if (res.data.code == 200 || res.data.code == 1100) {
+							resolve(res.data)
+						} else if (res.data.code == 1001) {
+							uni.showToast({
+								title: '未登录',
+								icon: 'none'
+							});
+							store.commit('changeIsLogin', false);
+							uni.removeStorageSync('token');
+							uni.navigateTo({
+								url: '/pages/login/index',
+							})
+							reject(res.data);
+						} else {
+							uni.showToast({
+								title: res.data.message,
+								icon: 'none'
+							});
+							reject(res.data);
+						}
+					} else {
+						uni.showToast({
+							title: '错误码:' + res.statusCode,
+							icon: 'none'
+						})
+						reject(res.data);
+					}
+				},
+				fail: function(err) {
+					uni.showModal({
+						title: '提示',
+						content: '网络连接失败' + JSON.stringify(err),
+					})
+					reject(err);
+				},
+			})
+		})
+	} else {
+		return new Promise((resolve, reject) => {
+			var digui = function() {
+				if (
+					(uni.getStorageSync('token') && uni.getStorageSync('websitIdSj')) ||
+					(uni.getStorageSync('token') && !!~_url.indexOf("/user/userWebsit")) || 
+					(uni.getStorageSync('token') && !!~_url.indexOf("/user/detail"))
+				) {
+					uni.request({
+						url: _url,
+						data: isGet ? '' : (obj.params || ''),
+						header: {
+							'content-type': obj.type || 'application/x-www-form-urlencoded',
+							"x-token": uni.getStorageSync('token') || '',
+							'APPID': appId
+						},
+						method: obj.method ? obj.method : "POST",
+						success: function(res) {
+							if (obj.isLoading) {
+								uni.hideLoading();
+							}
+							if (res.statusCode === 200) {
+								if (res.data.code == 200 || res.data.code == 1100) {
+									resolve(res.data)
+								} else if (res.data.code == 1001) {
+									uni.showToast({
+										title: '未登录',
+										icon: 'none'
+									});
+									store.commit('changeIsLogin', false);
+									uni.removeStorageSync('token');
+									uni.navigateTo({
+										url: '/pages/login/index',
+									})
+									reject(res.data);
+								} else {
+									uni.showToast({
+										title: res.data.message,
+										icon: 'none'
+									});
+									reject(res.data);
+								}
+							} else {
+								uni.showToast({
+									title: '错误码:' + res.statusCode,
+									icon: 'none'
+								})
+								reject(res.data);
+							}
+						},
+						fail: function(err) {
+							uni.showModal({
+								title: '提示',
+								content: '网络连接失败' + JSON.stringify(err),
+							})
+							reject(err);
+						},
+					})
+				} else {
+					setTimeout(digui, 100)
+				}
+			}
+			digui()
+		})
+	}
+
+
+
+}
+
+const getUUID = function() {
+	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
+		return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
+	})
+}
+
+const createName = function(name) {
+	const date = Date.now()
+	const uuid = getUUID()
+	const fileSuffix = name.substring(name.lastIndexOf('.') + 1)
+	return `${date}${uuid}.${fileSuffix}`
+}
+
+// 图片上传
+export const uploadImg = async function(file) {
+	console.log(file);
+	uni.showLoading({
+		mask: true,
+	})
+	// 获取oss配置
+	const par = await axios({
+		url: '/common/oss/config',
+		method: 'get'
+	}).then(res => {
+		return res.data;
+	}).catch(err => {
+		uni.hideLoading();
+	})
+	const fileKey = par.dir + createName(file.path);
+	return new Promise((resolve, reject) => {
+		uni.uploadFile({
+			url: par.host,
+			// header: {
+			// 	"Content-Type": 'multipart/form-data',
+			// },
+			name: 'file',
+			formData: {
+				...par,
+				name: file.name,
+				key: fileKey
+			},
+			filePath: file.path,
+			success(res) {
+				resolve({
+					url: fileKey
+				})
+			},
+			fail(err) {
+				reject(err)
+			},
+			complete(res) {
+				uni.hideLoading();
+			}
+		})
+	})
+}
+
+export default {
+	axios,
+	uploadImg,
+}

+ 27 - 0
index.js

@@ -0,0 +1,27 @@
+import { axios } from './axios';
+import store from '../store/index.js';
+
+// 获取用户信息
+export const getUserInfo = async function(userId) {
+	const result = new Promise((resolve, reject) => {
+		axios({
+			url: '/user/user/detail',
+			method: 'get',
+			params: {
+				userId: userId || store.state.userId
+			}
+		}).then(res => {
+			store.commit('changeUserInfo', res.data);
+			store.commit('changeUserId', res.data.userId);
+			uni.setStorageSync('userInfo', res.data);
+			uni.setStorageSync('userId', res.data.userId);
+			resolve(res.data);
+		})
+	})
+	return result;
+}
+
+export default{
+	axios,
+	getUserInfo
+}