封装公共方法,并存在异步请求接口情况 封装及调用
**封装公共方法,并存在异步请求接口情况 **
utilsTwo/login.js文件夹下
封装代码如下:
// 扫一扫加入班组
export async function sweep(parameter) {
// console.log("登录信息", userInfo)
return await new Promise(function(resolve, reject) {
// console.log("接收到了传输的参数",parameter)
let obj = parameter // 接收到的参数
uni.showModal({
title: obj.tit || '',
content: '您还没有班组,请扫班组二维码加入班组',
showCancel: false,
confirmText: '扫一扫',
success: function(ressm) {
if (ressm.confirm) {
// console.log('用户点击确定');
// 调起扫一扫
uni.scanCode({
success: function(ressaoma) {
// 识别二维码上班组的信息
// console.log('条码类型:' + ressaoma.scanType);
// console.log('条码内容:' + ressaoma.result);
uni.showLoading({
title: '加载中...'
});
let teams_id = ressaoma.result || ''
addTeamsWorker({
"teams_id": teams_id,
"worker_id": parameter.worker_id
})
.then(resdata => {
obj.teams_id = teams_id
uni.hideLoading();
// resolve(obj);
// console.log("用户信息--------",resdata)
uni.showLoading({
title: '重新登录中...'
});
phoneLogin({
phone: obj.phone
})
.then(resdataqqqq => {
const resdataaaaa = resdataqqqq.data
uni.hideLoading();
console.log("用户重新登录信息--------",resdataqqqq)
if (resdataqqqq.code == 200) {
uni.showToast({
title: "登录成功",
mask: true,
icon: 'none'
})
uni.setStorageSync( 'userInfo',
resdataaaaa.userinfo
);
if (resdataaaaa.userinfo
.type == 4) {
// 农民工直接跳转到打卡页
uni.reLaunch({
url: '/pagesA/work/timecard/index'
});
return false
}
uni.reLaunch({
url: '/pages/index/index'
})
}else{
uni.showToast({
title: resdataqqqq.msg,
duration: 2000,
icon: 'none'
})
}
resolve(obj); //给调用处返回的信息
})
.catch(error => {
uni.hideLoading();
obj.result = '失败'
resolve(obj);
});
})
.catch(error => {
uni.hideLoading();
obj.result = '失败'
resolve(obj);
});
}
});
}
}
});
});
}
引用代码如下:
import { sweep, } from '@/utilsTwo/login.js'
sweep({
phone: obj.phone,//给封装的方法传参
worker_id: than.worker_id,
tit: '注册成功'
}).then((ref) => {
console.log("返回页面的内容", ref)
if (ref.result == '失败') {
uni.showToast({
title: "绑定班组失败!!!",
duration: 2000,
icon: 'none'
});
}
}).catch((error) => {});