|
|
@ -21,7 +21,7 @@ export default { |
|
|
ip: '', |
|
|
ip: '', |
|
|
sourceUrl: '', |
|
|
sourceUrl: '', |
|
|
uniqueToken: '', |
|
|
uniqueToken: '', |
|
|
kefuid: '' |
|
|
customerid: '' |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
@ -43,15 +43,15 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS |
|
|
// #ifdef APP-PLUS |
|
|
this.dataForm.sourceUrl = 'app'; |
|
|
this.dataForm.sourceUrl = 'app'; |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN |
|
|
// #ifdef MP-WEIXIN |
|
|
this.dataForm.sourceUrl = 'wechat-miniprogram'; |
|
|
this.dataForm.sourceUrl = 'wechat-miniprogram'; |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// 如果没有获取到,设置默认值 |
|
|
// 如果没有获取到,设置默认值 |
|
|
if (!this.dataForm.sourceUrl) { |
|
|
if (!this.dataForm.sourceUrl) { |
|
|
this.dataForm.sourceUrl = 'unknown'; |
|
|
this.dataForm.sourceUrl = 'unknown'; |
|
|
@ -59,35 +59,35 @@ export default { |
|
|
this.dataForm.sourceUrl = this.dataForm.sourceUrl.replace(/\/+$/, ''); |
|
|
this.dataForm.sourceUrl = this.dataForm.sourceUrl.replace(/\/+$/, ''); |
|
|
console.log("来源网址:", this.dataForm.sourceUrl); |
|
|
console.log("来源网址:", this.dataForm.sourceUrl); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 获取URL参数中的token和kefuid |
|
|
// 获取URL参数中的token和customerid |
|
|
getTokenFromUrl() { |
|
|
getTokenFromUrl() { |
|
|
// #ifdef H5 |
|
|
// #ifdef H5 |
|
|
const pages = getCurrentPages(); |
|
|
const pages = getCurrentPages(); |
|
|
const currentPage = pages[pages.length - 1]; |
|
|
const currentPage = pages[pages.length - 1]; |
|
|
const options = currentPage.options || {}; |
|
|
const options = currentPage.options || {}; |
|
|
|
|
|
|
|
|
if (options.token) { |
|
|
if (options.token) { |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (options.kefuid) { |
|
|
if (options.customerid) { |
|
|
this.dataForm.kefuid = options.kefuid; |
|
|
this.dataForm.customerid = options.customerid; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const urlParams = new URLSearchParams(window.location.search); |
|
|
const urlParams = new URLSearchParams(window.location.search); |
|
|
const token = urlParams.get('token'); |
|
|
const token = urlParams.get('token'); |
|
|
const kefuid = urlParams.get('kefuid'); |
|
|
const customerid = urlParams.get('customerid'); |
|
|
|
|
|
|
|
|
if (token) { |
|
|
if (token) { |
|
|
this.dataForm.uniqueToken = token; |
|
|
this.dataForm.uniqueToken = token; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (kefuid) { |
|
|
if (customerid) { |
|
|
this.dataForm.kefuid = kefuid; |
|
|
this.dataForm.customerid = customerid; |
|
|
} |
|
|
} |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS |
|
|
// #ifdef APP-PLUS |
|
|
// App端获取参数 |
|
|
// App端获取参数 |
|
|
const pages = getCurrentPages(); |
|
|
const pages = getCurrentPages(); |
|
|
@ -96,11 +96,11 @@ export default { |
|
|
if (options.token) { |
|
|
if (options.token) { |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
} |
|
|
} |
|
|
if (options.kefuid) { |
|
|
if (options.customerid) { |
|
|
this.dataForm.kefuid = options.kefuid; |
|
|
this.dataForm.customerid = options.customerid; |
|
|
} |
|
|
} |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN |
|
|
// #ifdef MP-WEIXIN |
|
|
// 小程序端获取参数 |
|
|
// 小程序端获取参数 |
|
|
const pages = getCurrentPages(); |
|
|
const pages = getCurrentPages(); |
|
|
@ -110,41 +110,40 @@ export default { |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
} |
|
|
} |
|
|
console.log(options); |
|
|
console.log(options); |
|
|
if (options.kefuid) { |
|
|
if (options.customerid) { |
|
|
this.dataForm.kefuid = options.kefuid; |
|
|
this.dataForm.customerid = options.customerid; |
|
|
} |
|
|
} |
|
|
// #endif |
|
|
// #endif |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
async autoLogin() { |
|
|
async autoLogin() { |
|
|
if (GLOBAL_AUTO_LOGIN_LOCK) return; |
|
|
if (GLOBAL_AUTO_LOGIN_LOCK) return; |
|
|
GLOBAL_AUTO_LOGIN_LOCK = true; |
|
|
GLOBAL_AUTO_LOGIN_LOCK = true; |
|
|
|
|
|
|
|
|
// 获取token(优先使用URL中的token) |
|
|
// 获取token(优先使用URL中的token) |
|
|
this.getTokenFromUrl(); |
|
|
this.getTokenFromUrl(); |
|
|
|
|
|
|
|
|
// 判断是否有 token,没有则跳转 404 |
|
|
// 判断是否有 token,没有则跳转 404 |
|
|
if (!this.dataForm.uniqueToken) { |
|
|
if (!this.dataForm.uniqueToken) { |
|
|
console.log("未携带token,跳转到404页面"); |
|
|
console.log("未携带token,跳转到404页面"); |
|
|
// #ifdef H5 |
|
|
// #ifdef H5 |
|
|
// 考虑基础路径 /h5/ |
|
|
|
|
|
window.location.href = '/h5/#/pages/login/404'; |
|
|
window.location.href = '/h5/#/pages/login/404'; |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// #ifndef H5 |
|
|
// #ifndef H5 |
|
|
uni.redirectTo({ |
|
|
uni.redirectTo({ |
|
|
url: '/pages/login/404' |
|
|
url: '/pages/login/404' |
|
|
}); |
|
|
}); |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
GLOBAL_AUTO_LOGIN_LOCK = false; |
|
|
GLOBAL_AUTO_LOGIN_LOCK = false; |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 获取来源网址 |
|
|
// 获取来源网址 |
|
|
this.getSourceUrl(); |
|
|
this.getSourceUrl(); |
|
|
await this.getIp(); |
|
|
await this.getIp(); |
|
|
|
|
|
|
|
|
// 准备登录数据 |
|
|
// 准备登录数据 |
|
|
const loginData = { |
|
|
const loginData = { |
|
|
terminal: this.dataForm.terminal, |
|
|
terminal: this.dataForm.terminal, |
|
|
@ -153,9 +152,9 @@ export default { |
|
|
ip: this.dataForm.ip, |
|
|
ip: this.dataForm.ip, |
|
|
sourceUrl: this.dataForm.sourceUrl, |
|
|
sourceUrl: this.dataForm.sourceUrl, |
|
|
uniqueToken: this.dataForm.uniqueToken, |
|
|
uniqueToken: this.dataForm.uniqueToken, |
|
|
keFuId: this.dataForm.kefuid, // 添加kefuid参数 |
|
|
customerId: this.dataForm.customerid, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.$http({ |
|
|
this.$http({ |
|
|
url: '/login', |
|
|
url: '/login', |
|
|
data: loginData, |
|
|
data: loginData, |
|
|
@ -163,56 +162,61 @@ export default { |
|
|
}).then(loginInfo => { |
|
|
}).then(loginInfo => { |
|
|
uni.setStorageSync("isAgree", this.isAgree); |
|
|
uni.setStorageSync("isAgree", this.isAgree); |
|
|
uni.setStorageSync("loginInfo", loginInfo); |
|
|
uni.setStorageSync("loginInfo", loginInfo); |
|
|
|
|
|
|
|
|
getApp().$vm.init() |
|
|
getApp().$vm.init() |
|
|
getApp().$vm.unloadStore(); |
|
|
getApp().$vm.unloadStore(); |
|
|
this.$http({ |
|
|
this.$http({ |
|
|
url: "/friend/add?friendId=" + loginInfo.customerServiceId, |
|
|
url: "/friend/add?friendId=" + loginInfo.customerServiceId, |
|
|
method: "POST" |
|
|
method: "POST" |
|
|
}).then((data) => { |
|
|
}).then((data) => { |
|
|
let friend = { |
|
|
let friend = { |
|
|
id: loginInfo.user.id, |
|
|
id: loginInfo.user.id, |
|
|
nickName: loginInfo.user.nickName, |
|
|
nickName: loginInfo.user.nickName, |
|
|
headImage: loginInfo.user.headImageThumb, |
|
|
headImage: loginInfo.user.headImageThumb, |
|
|
online: loginInfo.user.online, |
|
|
online: loginInfo.user.online, |
|
|
delete: false |
|
|
delete: false |
|
|
} |
|
|
} |
|
|
this.friendStore.addFriend(friend); |
|
|
this.friendStore.addFriend(friend); |
|
|
|
|
|
|
|
|
// 清除 URL 中的 token 参数,跳转到聊天页面 |
|
|
this.clearUrlParams(); |
|
|
// #ifdef H5 |
|
|
|
|
|
const cleanUrl = window.location.origin + '/h5/' + '#/pages/chat/chat-box?targetId=' + loginInfo.customerServiceId + '&type=PRIVATE'; |
|
|
|
|
|
window.location.href = cleanUrl; |
|
|
|
|
|
// #endif |
|
|
|
|
|
|
|
|
|
|
|
// #ifndef H5 |
|
|
|
|
|
uni.reLaunch({ |
|
|
|
|
|
url: `/pages/chat/chat-box?targetId=${loginInfo.customerServiceId}&type=PRIVATE` |
|
|
|
|
|
}); |
|
|
|
|
|
// #endif |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.log("自动登录失败", err); |
|
|
|
|
|
GLOBAL_AUTO_LOGIN_LOCK = false; |
|
|
|
|
|
|
|
|
|
|
|
// 如果是认证失败(如token无效),也可以跳转404 |
|
|
|
|
|
if (err && (err.code === 401 || err.statusCode === 401)) { |
|
|
|
|
|
// #ifdef H5 |
|
|
// #ifdef H5 |
|
|
window.location.href = '/h5/pages/login/404'; |
|
|
const cleanUrl = window.location.origin + '/h5/#/pages/chat/chat-box?targetId=' + loginInfo.customerServiceId + '&type=PRIVATE'; |
|
|
|
|
|
window.location.href = cleanUrl; |
|
|
// #endif |
|
|
// #endif |
|
|
|
|
|
|
|
|
// #ifndef H5 |
|
|
// #ifndef H5 |
|
|
uni.redirectTo({ |
|
|
uni.reLaunch({ |
|
|
url: '/pages/login/404' |
|
|
url: `/pages/chat/chat-box?targetId=${loginInfo.customerServiceId}&type=PRIVATE` |
|
|
}); |
|
|
}); |
|
|
// #endif |
|
|
// #endif |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.log("自动登录失败", err); |
|
|
|
|
|
GLOBAL_AUTO_LOGIN_LOCK = false; |
|
|
|
|
|
this.clearUrlParams(); |
|
|
|
|
|
|
|
|
|
|
|
// #ifdef H5 |
|
|
|
|
|
window.location.href = '/h5/#/pages/login/404'; |
|
|
|
|
|
// #endif |
|
|
|
|
|
|
|
|
|
|
|
// #ifndef H5 |
|
|
|
|
|
uni.redirectTo({ url: '/pages/login/404' }); |
|
|
|
|
|
// #endif |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
clearUrlParams() { |
|
|
|
|
|
// #ifdef H5 |
|
|
|
|
|
const url = new URL(window.location.href); |
|
|
|
|
|
url.searchParams.delete('token'); |
|
|
|
|
|
url.searchParams.delete('customerid'); |
|
|
|
|
|
window.history.replaceState(null, '', url.pathname + url.search + url.hash); |
|
|
|
|
|
// #endif |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
getIp() { |
|
|
getIp() { |
|
|
// 获取本机ip |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
uni.request({ |
|
|
uni.request({ |
|
|
url: 'https://api.ipify.org?format=json', |
|
|
url: 'https://api.ipify.org?format=json', |
|
|
@ -235,16 +239,14 @@ export default { |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onLoad(options) { |
|
|
onLoad(options) { |
|
|
// 在onLoad中直接获取options中的token |
|
|
|
|
|
if (options.token) { |
|
|
if (options.token) { |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
this.dataForm.uniqueToken = options.token; |
|
|
console.log("onLoad获取到token:", this.dataForm.uniqueToken); |
|
|
console.log("onLoad获取到token:", this.dataForm.uniqueToken); |
|
|
} |
|
|
} |
|
|
if (options.kefuid) { |
|
|
if (options.customerid) { |
|
|
this.dataForm.kefuid = options.kefuid; |
|
|
this.dataForm.customerid = options.customerid; |
|
|
console.log("onLoad获取到kefuid:", this.dataForm.kefuid); |
|
|
console.log("onLoad获取到customerid:", this.dataForm.customerid); |
|
|
} |
|
|
} |
|
|
// 延迟执行,彻底避开双加载 |
|
|
|
|
|
setTimeout(() => { |
|
|
setTimeout(() => { |
|
|
this.autoLogin(); |
|
|
this.autoLogin(); |
|
|
}, 50); |
|
|
}, 50); |
|
|
@ -254,4 +256,4 @@ export default { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
</script> |
|
|
</script> |
|
|
|