マイクロ信ウィジェットパッケージネットワーク要求(promise方式)

5834 ワード

1.0構造は以下の通り:在这里插入图片描述 2.0コードは以下のconfig.js
	// es6    
	export const baseURL = 'http://www.xxxx/api'

network.js
	import {baseURL} from './config.js'
	export default function(options) {
		return new Promise((resolve, reject) => {
			wx.request({
				url: `${baseURL}/options.url`,
				header: {
				'content-type': 'application/json' //     ,     "content-type": "application/x-www-form-urlencoded"
				} ,
				type: options.type || 'get',
				data: options.data || {},
				success: resolve,
				fail: reject
			})
		})
	}

3.0使用
	//      
	import request from '../services/network.js'
	//       
	request({
		url: 'getManage',
		type: 'POST',
		data: {
			u_id: 123,
			uname: '  '
		}
	}).then(res => {  //             
		
	}).catch(err => { //           
		wx.showModal({
        title: '    ',
        content: '    ,     ',
        showCancel: false
      })
	})

promiseパッケージを使用したネットワークリクエスト推奨記事1