You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
3.6 KiB
170 lines
3.6 KiB
import {
|
|
isEmpty
|
|
} from './TextUtils.js'
|
|
|
|
const wxAuthLogin = () => {
|
|
// 判断是否已经登陆
|
|
if (getApp().globalData.isLogin) {
|
|
return new Promise((resolve, reject) => {
|
|
resolve(getApp().globalData.sysUserSid)
|
|
})
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
// 授权登录
|
|
wx.login({
|
|
success: function(res) {
|
|
if (res.code) {
|
|
|
|
if (getApp().globalData.isDebug) {
|
|
console.log('Http网络请求info', {
|
|
"wxCode": res.code
|
|
})
|
|
}
|
|
uni.showLoading({
|
|
title: '加载中...',
|
|
mask: true
|
|
});
|
|
|
|
uni.request({
|
|
// 组装请求地址
|
|
url: getApp().globalData.wxAuthLoginURL,
|
|
// 请求方式 GET POST
|
|
method: "GET",
|
|
header: {
|
|
// 传参方式
|
|
'content-type': "application/x-www-form-urlencoded"
|
|
},
|
|
// 具体参数
|
|
data: {
|
|
"wxCode": res.code
|
|
},
|
|
|
|
success: res => {
|
|
|
|
// 关闭显示框
|
|
uni.hideLoading();
|
|
|
|
console.log(res)
|
|
|
|
if (getApp().globalData.isDebug) {
|
|
console.log('Http网络路径', getApp().globalData.wxAuthLoginURL)
|
|
console.log('Http网络请求结果', JSON.parse(JSON.stringify(
|
|
res.data)))
|
|
}
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
// 下面是接口返回的数据
|
|
if (!res.data.success) {
|
|
if ("A01001" == res.data.code || "A01002" == res.data.code ) {
|
|
// 绑定手机号
|
|
uni.navigateTo({
|
|
url: '../index/BindPhone?sysUserWxAuthSid=' + res.data.data
|
|
})
|
|
// reject("绑定手机号")
|
|
}
|
|
else {
|
|
// 错误提示
|
|
if (isEmpty(res.data)) {
|
|
// 未成功获取到服务器返回的json
|
|
uni.showToast({
|
|
// 不能超过7个字
|
|
title: "服务器响应为空",
|
|
icon: "error"
|
|
})
|
|
reject("服务器响应为空")
|
|
} else {
|
|
|
|
let errorMsg = res.data.msg
|
|
|
|
if (isEmpty(errorMsg)) {
|
|
errorMsg = url+"服务器未返回错误信息"
|
|
}
|
|
|
|
uni.showToast({
|
|
title: errorMsg,
|
|
// 保证文字长度
|
|
icon: "none",
|
|
duration: 3000
|
|
})
|
|
}
|
|
|
|
reject(errorMsg)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 保存
|
|
uni.setStorageSync("sysUserSid", res.data.data.sysUserSid);
|
|
uni.setStorageSync("isLogin", res.data.data.isLogin);
|
|
getApp().globalData.isLogin = res.data.data.isLogin
|
|
getApp().globalData.sysUserSid = res.data.data.sysUserSid
|
|
getApp().globalData.token = res.data.data.token
|
|
|
|
// 直接返回Response
|
|
resolve(getApp().globalData.sysUserSid)
|
|
|
|
|
|
} else {
|
|
|
|
uni.showToast({
|
|
title: res.statusCode + ":" + res
|
|
.errMsg,
|
|
// 保证文字长度
|
|
icon: "none",
|
|
duration: 3000
|
|
})
|
|
|
|
reject(res.errMsg)
|
|
}
|
|
|
|
},
|
|
fail: (err) => {
|
|
// 关闭显示框
|
|
uni.hideLoading();
|
|
|
|
if (getApp().globalData.isDebug) {
|
|
console.log("Http网络请求fail", err)
|
|
}
|
|
|
|
|
|
uni.showToast({
|
|
title: '请检查网络',
|
|
icon: 'error'
|
|
})
|
|
|
|
reject(err)
|
|
|
|
},
|
|
complete: () => {
|
|
|
|
}
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
uni.showToast({
|
|
title: "授权登录获取code失败:" + res.errMsg,
|
|
icon: 'none'
|
|
})
|
|
|
|
reject(res.errMsg)
|
|
}
|
|
},
|
|
fail: function(res) {
|
|
uni.showToast({
|
|
title: "授权登录失败:" + res.errMsg,
|
|
icon: 'none'
|
|
})
|
|
|
|
reject(res.errMsg)
|
|
}
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export default wxAuthLogin
|
|
|