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.
226 lines
5.6 KiB
226 lines
5.6 KiB
// const socketHttp = "wss://*****.com/wss"
|
|
const event = require('./event.js')
|
|
const util = require('./util.js')
|
|
const app = getApp()
|
|
const header = {
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
}
|
|
|
|
function fun(url, method, data, header) {
|
|
data = data || {}
|
|
header = header || {
|
|
'user-token': app.globalData.token
|
|
}
|
|
let promise = new Promise(function (resolve, reject) {
|
|
wx.request({
|
|
url: app.httpUrl + url,
|
|
header: header,
|
|
data: data,
|
|
method: method,
|
|
success: function (result) {
|
|
if (typeof result.data === "object") {
|
|
if (result.data.code === 0) {
|
|
resolve(result.data)
|
|
} else if (result.data.code === 666) {
|
|
app.globalData.token = null
|
|
event.emit('EventMessage', {
|
|
what: 666,
|
|
desc: 'Logout'
|
|
})
|
|
reject(result.data)
|
|
} else {
|
|
var msg = '数据错误'
|
|
if (result.data.message) {
|
|
msg = result.data.message
|
|
}
|
|
reject(msg)
|
|
}
|
|
} else {
|
|
reject('网络异常')
|
|
}
|
|
},
|
|
fail: function (err) {
|
|
reject('网络异常')
|
|
}
|
|
})
|
|
})
|
|
return promise
|
|
}
|
|
|
|
function upload(url, name, filePath) {
|
|
let header = {
|
|
'token': app.globalData.token
|
|
}
|
|
let promise = new Promise(function (resolve, reject) {
|
|
wx.uploadFile({
|
|
url: app.httpUrl + url,
|
|
filePath: filePath,
|
|
name: name,
|
|
header: header,
|
|
success: function (res) {
|
|
resolve(res)
|
|
},
|
|
fail: reject
|
|
})
|
|
})
|
|
return promise
|
|
}
|
|
|
|
function loginByOpenId(form, resolve, reject) {
|
|
wx.request({
|
|
url: app.httpUrl + '/user-centre/commonLogin/login',
|
|
data: form,
|
|
method: 'POST',
|
|
success: function (result) {
|
|
if (result.data.code == 0 && result.data.data.token) {
|
|
app.globalData.token = result.data.data.token
|
|
wx.showToast({
|
|
title: '登录成功',
|
|
icon: "none"
|
|
})
|
|
event.emit('EventMessage', {
|
|
what: 1,
|
|
desc: 'Login'
|
|
})
|
|
resolve(result.data)
|
|
} else {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
}
|
|
},
|
|
fail: function () {
|
|
reject
|
|
},
|
|
})
|
|
}
|
|
|
|
function bindPhone(form, resolve, reject) {
|
|
// 获取手机号码
|
|
wx.request({
|
|
url: app.httpUrl + '/user-centre/wx/phone',
|
|
data: form,
|
|
method: 'POST',
|
|
success: function (result) {
|
|
// 绑定手机号码,进行登录
|
|
if (result.data.code != 0) {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
return
|
|
}
|
|
var data = {
|
|
openid: form.openid,
|
|
phone: result.data.data.phoneNumber
|
|
}
|
|
loginByOpenId(data, resolve, reject)
|
|
},
|
|
fail: function (phoneResp) {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
}
|
|
})
|
|
}
|
|
|
|
// 通过微信的方式登录,onGotPhoneNumber中统一处理
|
|
function loginWechat(form) {
|
|
let promise = new Promise(function (resolve, reject) {
|
|
if (!form.encryptedData) {
|
|
wx.showToast({
|
|
title: '授权被拒绝,登录失败',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
return
|
|
}
|
|
wx.showLoading({
|
|
title: '登录中',
|
|
mask: true
|
|
})
|
|
if (app.globalData.openId) {
|
|
// 如果已经获取到了openId
|
|
form.openid = app.globalData.openId
|
|
if (!util.isEmpty(form.phone)) {
|
|
loginByOpenId(form, resolve, reject)
|
|
} else {
|
|
bindPhone(form, resolve, reject)
|
|
}
|
|
} else {
|
|
// 没有获取到openId,获取之后进行登录
|
|
wx.login({
|
|
success: res => {
|
|
// 获取openId
|
|
wx.request({
|
|
url: app.httpUrl + '/user-centre/wx/bxe/session',
|
|
data: {
|
|
code: res.code
|
|
},
|
|
method: 'POST',
|
|
success: function (result) {
|
|
console.log(result.data)
|
|
if (result.data.code == 0) {
|
|
app.globalData.openId = result.data.data.openid
|
|
form.openid = app.globalData.openId
|
|
// 获取手机号码
|
|
bindPhone(form, resolve, reject)
|
|
} else {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
}
|
|
},
|
|
fail: function (sessionResp) {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
}
|
|
})
|
|
},
|
|
fail: function (loginResp) {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
}
|
|
})
|
|
}
|
|
})
|
|
return promise
|
|
}
|
|
|
|
module.exports = {
|
|
"get": function (url, data, header) {
|
|
return fun(url, "GET", data, header)
|
|
},
|
|
"post": function (url, data, header) {
|
|
return fun(url, "POST", data, header)
|
|
},
|
|
"put": function (url, data, header) {
|
|
return fun(url, "PUT", data, header)
|
|
},
|
|
"delete": function (url, data, header) {
|
|
return fun(url, "DELETE", data, header)
|
|
},
|
|
upload: function (url, name, filePath) {
|
|
return upload(url, name, filePath)
|
|
},
|
|
loginWechat: loginWechat
|
|
}
|