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.
318 lines
8.8 KiB
318 lines
8.8 KiB
// const socketHttp = "wss://*****.com/wss"
|
|
const event = require('./event.js')
|
|
const util = require('./util.js')
|
|
const storage = require('./storage.js')
|
|
const app = getApp()
|
|
const urlList = ['/recycle-service', '/payment-settlement-center', '/message-center', '/information-center']
|
|
var modal = false
|
|
|
|
function fun(url, method, data, header) {
|
|
data = data || {}
|
|
var flag = false
|
|
for (let index = 0; index < urlList.length; index++) {
|
|
if(url.indexOf(urlList[index]) != -1){
|
|
flag = true
|
|
break
|
|
}
|
|
}
|
|
if(util.isEmpty(app.globalData.token)){
|
|
var authorization = wx.getStorageSync('Authorization')
|
|
if (authorization) {
|
|
app.globalData.token = authorization
|
|
}
|
|
}
|
|
if(flag){
|
|
header = {
|
|
'X-APP-ID': app.xAppId,
|
|
'Authorization': 'QNT ' + app.globalData.token,
|
|
appversion: app.version
|
|
}
|
|
} else {
|
|
header = {
|
|
'user-token': app.globalData.userToken,
|
|
appversion: app.version
|
|
}
|
|
}
|
|
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 === 401) {
|
|
app.globalData.token = null
|
|
app.globalData.userToken = null
|
|
wx.removeStorageSync('accessToken')
|
|
wx.removeStorageSync('Authorization')
|
|
event.emit('EventMessage', { what: 666, desc: 'Logout' })
|
|
let pages = getCurrentPages(); //当前页面栈
|
|
let prevPage = pages[pages.length - 1]; //当前页面
|
|
if (prevPage.route != 'pages/login/index' && !modal) {
|
|
wx.showModal({
|
|
title: '温馨提示',
|
|
content: '登录信息已经过期,请重新登录',
|
|
showCancel: false,
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
wx.reLaunch({ url: '/pages/login/index' })
|
|
}
|
|
},
|
|
complete: function(){
|
|
modal = false
|
|
}
|
|
})
|
|
modal = true
|
|
}
|
|
var message = '网络错误'
|
|
if (result.data.message) {
|
|
message = result.data.message
|
|
}
|
|
reject(message)
|
|
} else {
|
|
var message = '数据错误'
|
|
if (result.data.message) {
|
|
message = result.data.message
|
|
}
|
|
reject(message)
|
|
}
|
|
} else {
|
|
reject('网络异常')
|
|
}
|
|
},
|
|
fail: function (err) {
|
|
// util.showToast(JSON.stringify(err))
|
|
reject('网络异常')
|
|
}
|
|
})
|
|
})
|
|
return promise
|
|
}
|
|
|
|
function upload(url, name, filePath) {
|
|
let header = {
|
|
'token': app.globalData.userToken
|
|
}
|
|
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
|
|
}
|
|
// ****************************************************************************************************************************************
|
|
// /authorize/by-wechat-applet小程序登录
|
|
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
|
|
})
|
|
var tempToken = storage.get('tempToken')
|
|
if(!util.isEmpty(tempToken)){
|
|
form.tmpAuthToken = tempToken
|
|
unionWechat(form, resolve, reject)
|
|
} else {
|
|
wx.login({
|
|
success: res => {
|
|
// 获取openId
|
|
wx.request({
|
|
header: { 'X-APP-ID': app.xAppId },
|
|
url: app.httpUrl + '/recycle-service/authorize/wechat-applet/user',
|
|
data: { authCode: res.code },
|
|
method: 'POST',
|
|
success: function (result) {
|
|
if (result.data.code == 0) {
|
|
form.tmpAuthToken = result.data.data.tmpAuthToken
|
|
storage.put('tempToken', form.tmpAuthToken, 98)
|
|
// 微信登录,获取unionId
|
|
unionWechat(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
|
|
}
|
|
|
|
function unionWechat(form, resolve, reject) {
|
|
wx.request({
|
|
header: { 'X-APP-ID': app.xAppId },
|
|
url: app.httpUrl + '/recycle-service/authorize/by-wechat-applet',
|
|
data: form,
|
|
method: 'POST',
|
|
success: function (result) {
|
|
wx.hideLoading()
|
|
if (result.data.code == 0) {
|
|
storage.remove('tempToken')
|
|
resolve(result.data)
|
|
} else {
|
|
reject(result.data)
|
|
}
|
|
},
|
|
fail: function () {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '获取用户信息,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
},
|
|
})
|
|
}
|
|
// ****************************************************************************************************************************************
|
|
// 通过微信的方式登录,onGotPhoneNumber中统一处理
|
|
function gotPhoneNumber(form, resolve, reject) {
|
|
// 获取手机号码 /authorize/encrypt/mobile
|
|
wx.request({
|
|
header: { 'X-APP-ID': app.xAppId },
|
|
url: app.httpUrl + '/recycle-service/authorize/encrypt/mobile',
|
|
data: form,
|
|
method: 'POST',
|
|
success: function (result) {
|
|
// 绑定手机号码,进行登录
|
|
wx.hideLoading()
|
|
if (result.data.code != 0) {
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
return
|
|
}
|
|
result.data.tmpAuthToken = form.tmpAuthToken
|
|
resolve(result.data)
|
|
},
|
|
fail: function (phoneResp) {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '登录失败,请稍后再试',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
}
|
|
})
|
|
}
|
|
|
|
function phoneWechat(form) {
|
|
let promise = new Promise(function (resolve, reject) {
|
|
if (!form.encryptedData) {
|
|
wx.showToast({
|
|
title: '授权被拒绝,登录失败',
|
|
icon: "none"
|
|
})
|
|
reject
|
|
return
|
|
}
|
|
wx.showLoading({
|
|
title: '登录中',
|
|
mask: true
|
|
})
|
|
// 没有获取到openId,获取之后进行登录
|
|
wx.login({
|
|
success: res => {
|
|
// 获取openId
|
|
wx.request({
|
|
header: { 'X-APP-ID': app.xAppId },
|
|
url: app.httpUrl + '/recycle-service/authorize/wechat-applet/user',
|
|
data: { authCode: res.code },
|
|
method: 'POST',
|
|
success: function (result) {
|
|
if (result.data.code == 0) {
|
|
form.tmpAuthToken = result.data.data.tmpAuthToken
|
|
// 获取手机号码
|
|
gotPhoneNumber(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)
|
|
},
|
|
phoneWechat: phoneWechat,
|
|
loginWechat: loginWechat
|
|
}
|