wx.requestの微信ウィジェットデータ要求パッケージ

10787 ワード

アプレットデータ要求パッケージ
微信小プログラムのデータ要求パッケージは、モジュール化の開発とメンテナンスに有利である.
//      
 requestUrl:function ( 
		url,
		params,
		method = "post"
	) {
		wx.showLoading({
			title: '   ',
		});
		let server = 'https://xxx.com/xxxx/'; //    
		let sessionId = wx.getStorageSync("token"),
			that = this;
		if (sessionId != "" && sessionId != null) {
			var header = {
				'content-type': 'application/x-www-form-urlencoded',
				'token': sessionId
			}
		} else {
			var header = {
				'content-type': 'application/x-www-form-urlencoded'
			}
		}
		return new Promise(function(resolve, reject) {
				wx.request({
					url: server + url,
					method: method,
					data: params,
					header: header,
					success: (res) => {
						wx.hideLoading();
						if (sessionId == "" || sessionId == null) {
							wx.setStorageSync('token',res.data.token) //                      sessionId     
						}
            // console.log(res.data.code)
            if (res.data.code == '-4001') {
                wx.reLaunch({
                  url: '/pages/login/login',
                })
            }
						if (res.data.code == '0' || res['statusCode'] !== 200) {
							wx.showToast({
								title: res.data.msg || '    ,    ',
								icon: 'none',
								duration: 2000,
								mask: true
							});

						}
						resolve(res.data)
					},
					fail: function(res) {
						wx.hideLoading();
						wx.showToast({
							title: res.data.msg || '',
							icon: 'none',
							duration: 2000,
							mask: true
						})
						reject(res.data)
					},
					complete: function() {
						wx.hideLoading()
					}
				})
			})
			.catch((res) => {})
	}