| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import config from './config'
- class Http {
- request(method, api, data, loading = false, toast = true) {
- let info = {
- url: config.domain + '/api/service/' + api,
- data,
- header: {
- 'content-type': 'application/json',
- 'token': wx.getStorageSync('token') || ''
- },
- method: method,
- timeout: 0
- }
- return new Promise((resolve, reject) => {
- if (loading) {
- wx.showLoading({
- title: '加载中',
- mask: true,
- })
- }
- wx.request({
- ...info,
- success: (result) => {
- if (result.statusCode === 401) {
- wx.navigateTo({
- url: '/service/login/login',
- })
- reject(result)
- }
- if (result.data.code !== 1) {
- if (toast) {
- wx.showToast({
- title: (result && result.data && result.data.msg) ? result.data.msg : '请稍后重试',
- icon: 'none',
- duration: 2000
- })
- } else {
- reject(result)
- }
- } else {
- resolve(result.data)
- }
- },
- fail: (res) => {
- reject(res)
- },
- complete: (res) => {
- wx.hideLoading({
- noConflict: true
- })
- },
- })
- })
- }
- post(url, data, loading, toast) {
- return this.request('POST', url, data, loading, toast)
- }
- get(url, data, loading, toast) {
- return this.request('GET', url, data, loading, toast)
- }
- upload(file, data, loading = false, toast = true) {
- return new Promise((resolve, reject) => {
- if (loading) {
- wx.showLoading({
- title: '加载中',
- mask: true,
- })
- }
- wx.uploadFile({
- url: config.domain + '/api/common/upload', //仅为示例,非真实的接口地址
- filePath: file,
- name: 'file',
- formData: {
- token: wx.getStorageSync('token'),
- ...data
- },
- success: (result) => {
- result.data = JSON.parse(result.data)
- if (result.statusCode === 401) {
- wx.navigateTo({
- url: '/service/login/login',
- })
- reject(result)
- }
- if (result.data.code !== 1) {
- if (toast) {
- wx.showToast({
- title: (result && result.data && result.data.msg) ? result.data.msg : '请稍后重试',
- icon: 'none',
- duration: 2000
- })
- }
- reject(result)
- }
- resolve(result.data)
- },
- fail: (res) => {
- reject(res)
- },
- complete: (res) => {
- wx.hideLoading({
- noConflict: true
- })
- },
- })
- })
- }
- chooseImg(source, loading, toast, sizeType) {
- return new Promise((resolve, reject) => {
- wx.chooseMedia({
- count: 1,
- mediaType: ['image'],
- sourceType: source,
- sizeType: sizeType ? sizeType : ['compressed'],
- camera: 'back',
- success: (res) => {
- this.upload(res.tempFiles[0].tempFilePath, {
- type: 'image'
- }, loading, toast).then(res => {
- resolve(res)
- }).catch(err => {
- reject(err)
- })
- },
- fail: (err) => {
- reject(err)
- }
- })
- })
- }
- chooseLocation(lat = '', lng = '') {
- return new Promise((resolve, reject) => {
- let params = {}
- if (lat) {
- params = {
- latitude: lat,
- longitude: lng,
- }
- }
- wx.chooseLocation({
- ...params,
- success: (res) => {
- let lat = res.latitude
- let lng = res.longitude
- let name = res.name
- this.post('user/getlocation', {
- lat: res.latitude,
- lng: res.longitude
- }).then(res => {
- if (res.data == false) {
- wx.showModal({
- title: '提示',
- content: '获取位置失败,请联系管理员',
- showCancel: false, //没有取消按钮的弹框
- buttonText: '确定',
- success: function (res) {
- if (res.confirm) {
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- return
- }
- let city = ''
- let province = res.data.regeocode.addressComponent.province
- if (res.data.regeocode.addressComponent.city === '[]' || res.data.regeocode.addressComponent.city.length === 0) {
- city = province
- } else {
- city = res.data.regeocode.addressComponent.city
- }
- let district = res.data.regeocode.addressComponent.district
- name = name || (res.data.regeocode.aois[0] && res.data.regeocode.aois[0].name) || res.data.regeocode.formatted_address
- let data = {
- city,
- province,
- district,
- name,
- lat,
- lng
- }
- resolve(data)
- }).catch(err => {
- reject(err)
- })
- },
- fail: (e) => {
- let data = {
- province: '河南省',
- city: '郑州市',
- district: '中原区',
- name: '郑州市',
- lat: '34.74725',
- lng: '113.62493'
- }
- reject(data)
- // wx.getSetting({
- // withSubscriptions: true,
- // success:(res)=>{
- // console.log(res)
- // let authSetting = res.authSetting
- // if(!authSetting['scope.userLocation']){
- // wx.showModal({
- // title: '您未开启地理位置授权',
- // content: '请在设置中打开位置授权,以便我们能够更好的提供服务',
- // success (res) {
- // if (res.confirm) {
- // wx.openSetting()
- // } else if (res.cancel) {
- // let data = {
- // province: '河南省',
- // city: '郑州市',
- // district: '中原区',
- // name: '郑州市',
- // lat: '34.74725',
- // lng: '113.62493'
- // }
- // reject(data)
- // }
- // }
- // })
- // }
- // }
- // })
- }
- })
- })
- }
- getLocation(lat = '', lng = '') {
- return new Promise((resolve, reject) => {
- wx.getLocation({
- type: 'wgs84',
- success: (res) => {
- let lat = res.latitude
- let lng = res.longitude
- this.post('user/getlocation', {
- lat: res.latitude,
- lng: res.longitude
- }).then(res => {
- if (res.data == false) {
- wx.showModal({
- title: '提示',
- content: '获取位置失败,请联系管理员',
- showCancel: false, //没有取消按钮的弹框
- buttonText: '确定',
- success: function (res) {
- if (res.confirm) {
- console.log('用户点击确定');
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- return
- }
- let city = ''
- let province = res.data.regeocode.addressComponent.province
- if (res.data.regeocode.addressComponent.city === '[]' || res.data.regeocode.addressComponent.city.length === 0) {
- city = province
- } else {
- city = res.data.regeocode.addressComponent.city
- }
- let district = res.data.regeocode.addressComponent.district
- let name = (res.data.regeocode.aois[0] && res.data.regeocode.aois[0].name) || res.data.regeocode.formatted_address
- let data = {
- city,
- province,
- district,
- name,
- lat,
- lng
- }
- resolve(data)
- }).catch(err => {
- reject(err)
- })
- },
- fail: (e) => {
- wx.getSetting({
- withSubscriptions: true,
- success: (res) => {
- let authSetting = res.authSetting
- if (!authSetting['scope.userLocation']) {
- wx.showModal({
- title: '您未开启地理位置授权',
- content: '请在设置中打开位置授权,以便我们能够更好的提供服务',
- success(res) {
- if (res.confirm) {
- wx.openSetting()
- } else if (res.cancel) {
- let data = {
- province: '河南省',
- city: '郑州市',
- district: '中原区',
- name: '郑州市',
- lat: '34.74725',
- lng: '113.62493'
- }
- reject(data)
- }
- }
- })
- }
- }
- })
- }
- })
- })
- }
- }
- const http = new Http()
- export default http
|