//ajax请求公共方法
export const ajaxRequest = (params) => {
return new Promise((resolve, reject) => {
uni.request({
url: getApp().globalData.BaseUrl + params.url,
// #ifdef H5
withCredentials: true,
header: params.header || null,
// #endif
// #ifndef H5
header: params.header || {
'Content-Type': 'application/x-www-form-urlencoded'
},
// #endif
method: params.method || 'GET',
dataType: params.dataType || 'json',
data: params.data || {},
success: (res) => {
resolve(res);
},
fail: (err) => {
uni.showToast({
title: '请求失败',
icon: 'none',
duration: 3000
})
reject(err);
}
})
})
}
//APP和H5登录
export const loginForAppAndH5 = function() {
uni.navigateTo({
url: '/pages/login/login'
});
};
//微信小程序登录
export const loginForWeiXinApplet = async function() {
let code = '';
await new Promise(resolve => {
uni.login({
success: result => {
code = result.code;
resolve();
}
});
});
const res = await this.$ajaxRequest({
url: '/thirdPartLogin/applet/login',
method: 'POST',
data: {
code: code
}
});
uni.setStorageSync('ticket', res.header['Set-Cookie']);
if (res.data.code === 200) {
//登录成功处理
uni.showToast({
title: res.data.message,
duration: 3000,
icon: 'none'
});
uni.reLaunch({
url: '/pages/tabbar/my'
});
return true;
} else {
uni.showToast({
title: res.data.err,
duration: 3000,
icon: 'none'
});
return false;
}
};