| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /**
- * 自定义封装http请求
- * 在此处可以自定义其他的请求方法
- * 直接在main.js中引入挂载即可在页面中使用
- * 也可以引入到/modules中,使用$u进行api的挂载
- * 建议基于uniapp的request进行封装,请勿使用其他三方非uniapp请求
- */
- export const VUE_APP_API_URL = ''
- export const httpRequest = (opts, data) => {
- let httpDefaultOpts = {
- url: baseUrl + opts.url,
- data: data,
- method: opts.method,
- header: opts.method == 'GET' ? {
- 'X-Requested-With': 'XMLHttpRequest',
- "Accept": "application/json",
- "Content-Type": "application/json; charset=UTF-8"
- } : {
- 'X-Requested-With': 'XMLHttpRequest',
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- dataType: 'json',
- }
- let promise = new Promise(function(resolve, reject) {
- uni.request(httpDefaultOpts).then(
- (res) => {
- resolve(res[1])
- }
- ).catch(
- (response) => {
- reject(response)
- }
- )
- })
- return promise
- };
- export const httpRequestUploadFile = (opts, filePath, data) => {
- let token = "";
- uni.getStorage({
- key: 'token',
- success: function(ress) {
- token = ress.data
- }
- });
- // token="dd0847da213958adf77e88a2cfb661e5"
- let httpDefaultOpts = {
- url: baseUrl + opts.url,
- header: {
- token: token
- },
- name: "file",
- filePath: filePath,
- name: 'file',
- formData: data,
- }
- let promise = new Promise(function(resolve, reject) {
- uni.uploadFile(httpDefaultOpts).then(
- (res) => {
- resolve(res[1])
- }
- ).catch(
- (response) => {
- reject(response)
- }
- )
- })
- return promise
- };
- //带Token请求
- export const httpTokenRequest = (opts, data) => {
- let token = "";
- uni.getStorage({
- key: 'token',
- success: function(ress) {
- token = ress.data
- }
- });
- // 测试用
- token = "dd0847da213958adf77e88a2cfb661e5"
- //此token是登录成功后后台返回保存在storage中的
- let httpDefaultOpts = {
- url: baseUrl + opts.url,
- data: data,
- method: opts.method,
- header: opts.method == 'GET' ? {
- 'Token': token,
- 'X-Requested-With': 'XMLHttpRequest',
- "Accept": "application/json",
- "Content-Type": "application/json; charset=UTF-8"
- } : {
- 'Token': token,
- 'X-Requested-With': 'XMLHttpRequest',
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- dataType: 'json',
- }
- let promise = new Promise(function(resolve, reject) {
- uni.request(httpDefaultOpts).then(
- (res) => {
- resolve(res[1])
- }
- ).catch(
- (response) => {
- reject(response)
- }
- )
- })
- return promise
- };
- //自定义请求
- export const httpCustomeRequest = (opts, data) => {
- let httpDefaultOpts = {
- url: opts.url,
- data: data,
- method: opts.method,
- header: opts.method == 'GET' ? {
- 'X-Requested-With': 'XMLHttpRequest',
- "Accept": "application/json",
- "Content-Type": "application/json; charset=UTF-8"
- } : {
- 'X-Requested-With': 'XMLHttpRequest',
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- dataType: 'json',
- }
- let promise = new Promise(function(resolve, reject) {
- uni.request(httpDefaultOpts).then(
- (res) => {
- resolve(res[1])
- }
- ).catch(
- (response) => {
- reject(response)
- }
- )
- })
- return promise
- };
|