123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- var siteinfo = require('../siteinfo.js'); //require这个js模块
- const http = {}
- const headers = {
- 'content-type': 'application/x-www-form-urlencoded'
- }
- http.request = (url, data, method='POST', hideErr = true, hideLoading = true) => {
- if(!hideLoading){
- uni.showLoading({
- title: '加载中...'
- });
- }
- let controller = data.controller =='admin' ? 'admin/':'api/';
- return new Promise((resolve, reject) => {
- uni.request({
- url: siteinfo.siteroot + '/index.php?s=/'+controller + url,
- method:'POST',
- data: data,
- dataType: 'json',
- header: headers,
- success: res => {
- // console.log(res,url,"ddddddddddd")
- if (res.statusCode == 200) {
- if (res.data.code == 200) {
- resolve(res.data); //返回成功提示信息
- } else if(res.data.code =="10000"||res.data.code ==10000){
- // 会员卡
- resolve(res.data);
- }else {
- reject(res.data); //返回错误提示信息
- if (!hideErr) {
- setTimeout(() => {
- uni.showToast({
- title: res.data.message,
- icon: 'none'
- })
- }, 10)
- }
- }
- } else {
- // setTimeout(() => {
- // uni.showToast({
- // title: '网络出小差了~',
- // icon: 'none'
- // })
- // }, 10)
- }
- },
- fail: res => {
- console.log('uni.request:fail', res)
- // setTimeout(() => {
- // uni.showToast({
- // title: '网络连接错误',
- // icon: 'none'
- // })
- // }, 10)
- // let err = {
- // code: '-1',
- // msg: "网络连接错误"
- // }
- // reject(err);
- //返回错误提示信息
- },
- complete: function(res) {
- if (!hideLoading) {
- uni.hideLoading()
- }
- }
- })
- });
- }
- http.uploadImg = (filePath) => {
- // console.log(filePath)
- uni.showLoading({
- title: '上传中...'
- });
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: 'http://business.coffunity.cn/admin.php/api/common/uploadingImg', //仅为示例,非真实的接口地址
- filePath: filePath,
- name: 'file',
- success: res => {
- if (res.statusCode == 200) {
- let res_data = JSON.parse(res.data)
- if (res_data.code == 200) {
- resolve(res_data); //返回成功提示信息
- } else {
- reject(res_data); //返回错误提示信息
- setTimeout(() => {
- uni.showToast({
- title: res_data.msg,
- icon: 'none'
- })
- }, 10)
- }
- } else {
- setTimeout(() => {
- uni.showToast({
- title: '服务器异常',
- icon: 'none'
- })
- }, 10)
- }
- },
- fail: res => {
- setTimeout(() => {
- uni.showToast({
- title: '网络连接错误',
- icon: 'none'
- })
- }, 10)
- let err = {
- code: '-1',
- msg: "网络连接错误"
- }
- reject(err); //返回错误提示信息
- },
- complete: function(res) {
- uni.hideLoading()
- }
- });
- })
- }
- export default http
|