uniapp 初始化项目
const baseUrl = 'http://10.92.1.17:6601/videoapi/';
//const baseUrl = '/videoapi/';
//对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18。
//对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。
//对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。
//postJson请求数据存放在requestBody中
const HttpPostJson = (url, data) => {
//登录后将用户的token存放在缓存中
let token = "";
uni.getStorage({
key: 'token',
success: function(ress) {
token = ress.data,
data.token = ress.data
}
});
let opts = {
url: baseUrl + url,
data: data,
method: 'POST',
header: {
"Content-Type": "application/json; charset=UTF-8",
"token": token
},
dataType: 'json'
}
let promise = new Promise(function(resolve, reject) {
uni.request(opts).then(
(res) => {
resolve(res[1].data)
}
).catch(
(response) => {
reject(response)
}
)
})
return promise
};
//get请求
const HttpGet = (url, data) => {
////登录后将用户的token存放在缓存中
let token = "";
uni.getStorage({
key: 'token',
success: function(ress) {
token = ress.data,
data.token = ress.data
}
});
let opts = {
url: baseUrl + url,
data: data,
method: 'GET',
header: {
"token": token
},
dataType: 'json'
}
let promise = new Promise(function(resolve, reject) {
uni.request(opts).then(
(res) => {
resolve(res[1])
}
).catch(
(response) => {
reject(response)
}
)
})
return promise
};
//表单提交
const HttpPostForm = (url, data) => {
//登录后将用户的token存放在缓存中
let token = "";
uni.getStorage({
key: 'token',
success: function(ress) {
token = ress.data,
data.token = ress.data
}
});
let opts = {
url: baseUrl + url,
data: data,
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"token": token
},
dataType: 'json'
}
let promise = new Promise(function(resolve, reject) {
uni.request(opts).then(
(res) => {
resolve(res[1])
}
).catch(
(response) => {
reject(response)
}
)
})
return promise
};
//文件上传(单文件上传)
/**
* fileType:文件类型,image/video/audio仅支付宝小程序,且必填。
* fileName:文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
* filePath:要上传文件资源的路径
* formData:HTTP 请求中其他额外的 form data
* files : 需要上传的文件列表。使用 files 时,filePath 和 name 不生效。
*/
const HttpPostFile = (url, formData, fileName, fileType, filePath) => {
//登录后将用户的token存放在缓存中
let token = "";
uni.getStorage({
key: 'token',
success: function(ress) {
token = ress.data,
data.token = ress.data
}
});
let opts = {
url: baseUrl + url,
formData: formData,
filePath: filePath,
fileType: fileType,
name: fileName,
method: 'POST',
header: {
"Content-Type": "multipart/form-data",
"token": token
},
dataType: 'json'
}
let promise = new Promise(function(resolve, reject) {
uni.uploadFile(opts).then(
(res) => {
resolve(res[1])
}
).catch(
(response) => {
reject(response)
}
)
})
return promise
};
/**
* 文件下载GET请求
*/
const HttpDownloadFile = (url) => {
//登录后将用户的token存放在缓存中
let token = "";
uni.getStorage({
key: 'token',
success: function(ress) {
token = ress.data,
data.token = ress.data
}
});
let opts = {
url: baseUrl + url,
method: 'GET',
header: {
"token": token
},
dataType: 'json'
}
let promise = new Promise(function(resolve, reject) {
uni.uploadFile(opts).then(
(res) => {
resolve(res[1])
}
).catch(
(response) => {
reject(response)
}
)
})
return promise
};
export default {
baseUrl,
HttpPostForm,
HttpGet,
HttpPostJson,
HttpPostFile,
HttpDownloadFile
}
<template>
<view class="content">
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
let object = {};
this.http.HttpPostJson('login/findImageCode', object).then(res => {
console.log(res);
//打印请求返回的数据
}, error => {
console.log(error);
})
},
methods: {
}
}
</script>
<style>
@import url("index.css");
</style>
import Vue from 'vue'
import App from './App'
import http from './common/http.js'
Vue.config.productionTip = false
//当前时间
Vue.prototype.now = Date.now || function() {
return new Date().getTime();
};
//请求接口
Vue.prototype.http = http;
//获取缓存中用户的token
Vue.prototype.token = uni.getStorageSync("token");
//获取用户信息
Vue.prototype.userInfo = uni.getStorageSync("userInfo");
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
解决跨域问题
"h5" : {
"devServer" : {
"inline":false,
"port" : 8080,
"host" : "0.0.0.0",
"disableHostCheck" : true,
"proxy" : {
"/videoapi" : {
"target" : "http://10.92.1.17:6601/",
"changeOrigin" : true,
"secure" : false,
"pathRewrite":{"^/dpc":""}
}
}
},
"router" : {
"mode" : "history"
},
"optimization" : {
"treeShaking" : {
"enable" : true
}
}
}
uniapp 初始化项目的更多相关文章
- 使用node初始化项目
初始化项目 在建项目的时候经常会建很多文件夹和文件,今天使用node初始化项目自动生成这些内容. 执行步骤 执行命令 node init 初始化项目生成package.json 设置配置文件 var ...
- vue初始化项目,构建vuex的后台管理项目架子
构架vuex的后台管理项目源码:https://github.com/saucxs/structure-admin-web 一.node安装 可以参考这篇文章http://www.mwcxs.top/ ...
- Git帮助之初始化项目设置向导
初始化项目设置向导 Git设置: git config --global user.name "Your Name Here" # 设置Git提交时的默认用户名,推荐使用本站用户名 ...
- 一步步建立 Vue + Cesium 初始化项目
一步步建立 Vue + Cesium 初始化项目 初始化 Vue 项目 升级 npm npm install -g npm 安装 @vue/cli 和 @vue/cli-service-global ...
- react native初始化项目
打开命令行窗口,进入我们想要创建项目的父目录,输入命令: npm install -g yarn react-native-cli react-native init 项目名 进入新建的项目目录,执行 ...
- git初始化项目 以及 git常用操作
初始化项目 $ git config --global user.name "Your Name" 配置用户名 $ git config --global user.email ...
- Git 初始化项目、创建合并分支、回滚等常用方法总结
就在刚才查看资料时候, 看见一句话, 写的特别好: 当我的才华撑不起我的梦想的时候, 应该安静下来学习 配上我最喜欢动漫的一个角色: 红莲 1. Git 初始化项目 1). 创建新的知识库 echo ...
- 初探angluar_01 目录结构分析及初始化项目
简单说明:angular是模块化的,因此所有功能功能都属于组件 一.目录结构 e2e 端到端的测试目录 用来做自动测试的 node_modules 安装地依赖存放目录,package.json里安装 ...
- React Native 0.56.1初始化项目运行出现错误(Module `AccessibilityInfo` does not exist in the Haste module map)
当使用react-native init myApp初始化项目时,出现以下错误 出现以上错误的原因是因为0.56.1版本初始化项目就有问题,请见 https://github.com/facebook ...
随机推荐
- P1004 成绩排名
转跳点:
- Oracle SQL 异常处理
今天学了异常处理 有预定义异常 与 用户自定义异常 还有 raise_application_error()函数raise_application_error() 只能把异常抛出而不能处理异常. 预定 ...
- 【转】ASP.NET Core WebAPI JWT Bearer 认证失败返回自定义数据 Json
应用场景:当前我们给微信小程序提供服务接口,接口中使用了权限认证这一块,当我使用 JWT Bearer 进行接口权限认证的时候,返回的结果不是我们客户端想要的,其它我们想要给客户端返回统一的数据结构, ...
- ACM-Alice and Bob
题目描述:Alice and Bob One day, Alice asks Bob to play a game called “K-in-a-row”. There is a game board ...
- Ubantu学习笔记3
修改PATH环境变量 ~/.profile --->(在文件最后一行添加) PATH="要增加的路径:$PATH" 或者 export 使用变量="调用参数&quo ...
- DAO三层架构及工厂模式
目录结构 1.在domain包中创建User实体类 package com.rick.domain; import java.util.Date; public class User { privat ...
- 面试题(7)之 leetcode-003
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
- UVALive 3634 数据结构模拟
这题真是坑啊,题意不明,其实就是往桟里面压入空的set集合,所以之前的询问大小都是只有0,只有add的时候,才会产生新的占空间的集合 用stack和set直接进行模拟 #include <ios ...
- vue-cli3.x 搭建项目目
安装文档 https://cli.vuejs.org/zh/guide/ 安装条件 npm 更至最新 node >=8.9 可以用npm -v 查看npm的版本号 1.关于旧版本 Vue CLI ...
- POJ 1663:Number Steps
Number Steps Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13664 Accepted: 7378 Des ...