|
|
@ -15,26 +15,28 @@ |
|
|
</template> |
|
|
</template> |
|
|
|
|
|
|
|
|
<script> |
|
|
<script> |
|
|
import {Api_url} from '@/common/config.js' |
|
|
import { |
|
|
import apiRoute from '@/api/apiRoute.js' |
|
|
Api_url |
|
|
|
|
|
} from '@/common/config.js' |
|
|
|
|
|
import apiRoute from '@/api/apiRoute.js' |
|
|
export default { |
|
|
export default { |
|
|
data() { |
|
|
data() { |
|
|
return { |
|
|
return { |
|
|
|
|
|
userInfo:{} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
methods: { |
|
|
methods: { |
|
|
//退出登陆 |
|
|
//退出登陆 |
|
|
loginOut(){ |
|
|
loginOut() { |
|
|
this.$util.loginOut() |
|
|
this.$util.loginOut() |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
privacy_agreement(type){ |
|
|
privacy_agreement(type) { |
|
|
uni.navigateTo({ |
|
|
uni.navigateTo({ |
|
|
url: '/pages-common/privacy_agreement?type='+type |
|
|
url: '/pages-common/privacy_agreement?type=' + type |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
update_pass(){ |
|
|
update_pass() { |
|
|
uni.navigateTo({ |
|
|
uni.navigateTo({ |
|
|
url: '/pages-market/my/update_pass' |
|
|
url: '/pages-market/my/update_pass' |
|
|
}) |
|
|
}) |
|
|
@ -86,284 +88,310 @@ import apiRoute from '@/api/apiRoute.js' |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
//绑定微信 |
|
|
//绑定微信 |
|
|
async buildwx(){ |
|
|
async buildwx() { |
|
|
try { |
|
|
await this.getUserInfo()//获取用户详情 |
|
|
console.log('开始微信绑定流程') |
|
|
try { |
|
|
|
|
|
console.log('开始微信绑定流程') |
|
|
// 检查当前环境 |
|
|
|
|
|
// #ifdef MP-WEIXIN |
|
|
// 检查当前环境 |
|
|
console.log('微信小程序环境') |
|
|
// #ifdef MP-WEIXIN |
|
|
await this.bindWeixinInMiniProgram() |
|
|
console.log('微信小程序环境') |
|
|
// #endif |
|
|
await this.bindWeixinInMiniProgram() |
|
|
|
|
|
// #endif |
|
|
// #ifdef H5 |
|
|
|
|
|
console.log('H5环境') |
|
|
// #ifdef H5 |
|
|
if (this.isWeixinBrowser()) { |
|
|
// await this.bindWeixinInH5() |
|
|
console.log('微信浏览器环境') |
|
|
if (this.isWeixinBrowser()) { |
|
|
await this.bindWeixinInH5() |
|
|
console.log('微信浏览器环境') |
|
|
} else { |
|
|
await this.bindWeixinInH5() |
|
|
console.log('非微信浏览器环境') |
|
|
} else { |
|
|
uni.showModal({ |
|
|
console.log('非微信浏览器环境') |
|
|
title: '提示', |
|
|
uni.showModal({ |
|
|
content: '请在微信中打开此页面进行微信绑定', |
|
|
title: '提示', |
|
|
showCancel: false, |
|
|
content: '请在微信中打开此页面进行微信绑定', |
|
|
confirmText: '我知道了' |
|
|
showCancel: false, |
|
|
}) |
|
|
confirmText: '我知道了' |
|
|
return |
|
|
}) |
|
|
} |
|
|
return |
|
|
// #endif |
|
|
} |
|
|
|
|
|
// #endif |
|
|
// #ifndef MP-WEIXIN || H5 |
|
|
|
|
|
// 其他环境 |
|
|
// #ifndef MP-WEIXIN || H5 |
|
|
uni.showToast({ |
|
|
// 其他环境 |
|
|
title: '当前环境不支持微信绑定', |
|
|
uni.showToast({ |
|
|
icon: 'none' |
|
|
title: '当前环境不支持微信绑定', |
|
|
}) |
|
|
icon: 'none' |
|
|
// #endif |
|
|
}) |
|
|
|
|
|
// #endif |
|
|
} catch (error) { |
|
|
|
|
|
console.error('绑定微信失败:', error) |
|
|
} catch (error) { |
|
|
uni.hideLoading() |
|
|
console.error('绑定微信失败:', error) |
|
|
uni.showToast({ |
|
|
uni.hideLoading() |
|
|
title: '绑定失败,请重试', |
|
|
uni.showToast({ |
|
|
icon: 'none' |
|
|
title: '绑定失败,请重试', |
|
|
}) |
|
|
icon: 'none' |
|
|
} |
|
|
}) |
|
|
}, |
|
|
} |
|
|
|
|
|
}, |
|
|
// 小程序环境绑定流程 - 先绑定小程序openid,然后跳转webview绑定公众号 |
|
|
|
|
|
async bindWeixinInMiniProgram() { |
|
|
// 小程序环境绑定流程 - 先绑定小程序openid,然后跳转webview绑定公众号 |
|
|
uni.showLoading({ |
|
|
async bindWeixinInMiniProgram() { |
|
|
title: '正在获取微信信息...' |
|
|
uni.showLoading({ |
|
|
}) |
|
|
title: '正在获取微信信息...' |
|
|
|
|
|
}) |
|
|
// 1. 先获取小程序 openid |
|
|
|
|
|
console.log('步骤1: 获取小程序openid') |
|
|
// 1. 先获取小程序 openid |
|
|
const miniProgramOpenid = await this.getMiniProgramOpenid() |
|
|
console.log('步骤1: 获取小程序openid') |
|
|
console.log('获取到的openid:', miniProgramOpenid) |
|
|
const miniProgramOpenid = await this.getMiniProgramOpenid() |
|
|
|
|
|
console.log('获取到的openid:', miniProgramOpenid) |
|
|
if (!miniProgramOpenid) { |
|
|
|
|
|
uni.hideLoading() |
|
|
if (!miniProgramOpenid) { |
|
|
uni.showToast({ |
|
|
uni.hideLoading() |
|
|
title: '获取小程序openid失败', |
|
|
uni.showToast({ |
|
|
icon: 'none' |
|
|
title: '获取小程序openid失败', |
|
|
}) |
|
|
icon: 'none' |
|
|
return |
|
|
}) |
|
|
} |
|
|
return |
|
|
|
|
|
} |
|
|
// 2. 绑定小程序openid |
|
|
|
|
|
console.log('步骤2: 绑定小程序openid') |
|
|
// 2. 绑定小程序openid |
|
|
const bindResult = await this.bindMiniProgramOpenid(miniProgramOpenid) |
|
|
console.log('步骤2: 绑定小程序openid') |
|
|
console.log('绑定结果:', bindResult) |
|
|
const bindResult = await this.bindMiniProgramOpenid(miniProgramOpenid) |
|
|
|
|
|
console.log('绑定结果:', bindResult) |
|
|
if (!bindResult) { |
|
|
|
|
|
uni.hideLoading() |
|
|
if (!bindResult) { |
|
|
return |
|
|
uni.hideLoading() |
|
|
} |
|
|
return |
|
|
|
|
|
} |
|
|
uni.hideLoading() |
|
|
|
|
|
|
|
|
uni.hideLoading() |
|
|
// 3. 跳转webview绑定公众号openid |
|
|
|
|
|
console.log('步骤3: 跳转webview绑定公众号') |
|
|
// 3. 跳转webview绑定公众号openid |
|
|
const webviewUrl = this.buildWebviewUrl(miniProgramOpenid) |
|
|
console.log('步骤3: 跳转webview绑定公众号') |
|
|
console.log('webview URL:', webviewUrl) |
|
|
const webviewUrl = this.buildWebviewUrl(miniProgramOpenid) |
|
|
|
|
|
console.log('webview URL:', webviewUrl) |
|
|
uni.navigateTo({ |
|
|
|
|
|
url: `/pages-common/webview/wechat_bind?url=${encodeURIComponent(webviewUrl)}` |
|
|
uni.navigateTo({ |
|
|
}) |
|
|
url: `/pages-common/webview/wechat_bind?url=${encodeURIComponent(webviewUrl)}` |
|
|
}, |
|
|
}) |
|
|
|
|
|
}, |
|
|
// H5微信浏览器环境绑定流程 |
|
|
|
|
|
async bindWeixinInH5() { |
|
|
// H5微信浏览器环境绑定流程 |
|
|
uni.showLoading({ |
|
|
async bindWeixinInH5() { |
|
|
title: '正在跳转微信授权...' |
|
|
|
|
|
}) |
|
|
uni.showLoading({ |
|
|
|
|
|
title: '正在跳转微信授权...' |
|
|
try { |
|
|
}) |
|
|
// H5环境直接跳转到公众号授权 |
|
|
|
|
|
const baseUrl = Api_url |
|
|
try { |
|
|
const redirectUri = encodeURIComponent(`${baseUrl}/personnel/wechatCallback`) |
|
|
// H5环境直接跳转到公众号授权 |
|
|
const state = encodeURIComponent(JSON.stringify({ |
|
|
const baseUrl = Api_url |
|
|
personnel_id: this.$store.state.userInfo.id, |
|
|
const redirectUri = encodeURIComponent(`${baseUrl}/personnel/wechatCallback`) |
|
|
from: 'h5', |
|
|
const state = encodeURIComponent(JSON.stringify({ |
|
|
timestamp: Date.now() |
|
|
personnel_id: this.userInfo?.id, |
|
|
})) |
|
|
from: 'h5', |
|
|
|
|
|
timestamp: Date.now() |
|
|
const authUrl = `${baseUrl}/personnel/wechatAuthorize?redirect_uri=${redirectUri}&state=${state}` |
|
|
})) |
|
|
console.log('H5授权URL:', authUrl) |
|
|
|
|
|
|
|
|
|
|
|
uni.hideLoading() |
|
|
|
|
|
|
|
|
const authUrl = `${baseUrl}/personnel/wechatAuthorize?redirect_uri=${redirectUri}&state=${state}` |
|
|
// 直接跳转到授权页面 |
|
|
console.log('H5授权URL:', authUrl) |
|
|
location.href = authUrl |
|
|
|
|
|
|
|
|
uni.hideLoading() |
|
|
} catch (error) { |
|
|
|
|
|
console.error('H5微信绑定失败:', error) |
|
|
// 直接跳转到授权页面 |
|
|
uni.hideLoading() |
|
|
location.href = authUrl |
|
|
uni.showToast({ |
|
|
|
|
|
title: '授权跳转失败', |
|
|
} catch (error) { |
|
|
icon: 'none' |
|
|
console.error('H5微信绑定失败:', error) |
|
|
}) |
|
|
uni.hideLoading() |
|
|
} |
|
|
uni.showToast({ |
|
|
}, |
|
|
title: '授权跳转失败', |
|
|
|
|
|
icon: 'none' |
|
|
// 获取小程序openid |
|
|
}) |
|
|
getMiniProgramOpenid() { |
|
|
} |
|
|
return new Promise((resolve, reject) => { |
|
|
}, |
|
|
console.log('开始获取小程序openid') |
|
|
async getUserInfo() { |
|
|
|
|
|
let data = {} |
|
|
// #ifdef MP-WEIXIN |
|
|
let res = await apiRoute.getPersonnelInfo(data); |
|
|
console.log('在微信小程序环境中') |
|
|
if (res.code != 1) { |
|
|
uni.login({ |
|
|
uni.showToast({ |
|
|
provider: 'weixin', |
|
|
title: res.msg, |
|
|
success: (loginResult) => { |
|
|
icon: 'none' |
|
|
console.log('微信登录成功,code:', loginResult.code) |
|
|
}) |
|
|
if (loginResult.code) { |
|
|
return |
|
|
// 调用后端接口换取openid |
|
|
} |
|
|
apiRoute.getWechatOpenid({ |
|
|
|
|
|
code: loginResult.code, |
|
|
res.data.cameus_dept_arr.forEach((v, k) => { |
|
|
type: 'miniprogram' |
|
|
let d_arr = [] |
|
|
}).then(res => { |
|
|
v.dept_arr.forEach((dv, dk) => { |
|
|
console.log('获取openid接口响应:', res) |
|
|
d_arr.push(dv.dept_name) |
|
|
if (res.code === 1) { |
|
|
}) |
|
|
resolve(res.data.openid) |
|
|
//数组转字符串 |
|
|
} else { |
|
|
v.dept_name_str = d_arr.join(',') |
|
|
uni.showToast({ |
|
|
}) |
|
|
title: res.msg || '获取openid失败', |
|
|
|
|
|
icon: 'none' |
|
|
this.userInfo = res.data |
|
|
}) |
|
|
}, |
|
|
resolve(null) |
|
|
// 获取小程序openid |
|
|
} |
|
|
getMiniProgramOpenid() { |
|
|
}).catch(error => { |
|
|
return new Promise((resolve, reject) => { |
|
|
console.error('获取openid接口调用失败:', error) |
|
|
console.log('开始获取小程序openid') |
|
|
resolve(null) |
|
|
|
|
|
}) |
|
|
// #ifdef MP-WEIXIN |
|
|
} else { |
|
|
console.log('在微信小程序环境中') |
|
|
uni.showToast({ |
|
|
uni.login({ |
|
|
title: '微信登录失败', |
|
|
provider: 'weixin', |
|
|
icon: 'none' |
|
|
success: (loginResult) => { |
|
|
}) |
|
|
console.log('微信登录成功,code:', loginResult.code) |
|
|
resolve(null) |
|
|
if (loginResult.code) { |
|
|
} |
|
|
// 调用后端接口换取openid |
|
|
}, |
|
|
apiRoute.getWechatOpenid({ |
|
|
fail: (error) => { |
|
|
code: loginResult.code, |
|
|
console.error('微信登录失败:', error) |
|
|
type: 'miniprogram' |
|
|
uni.showToast({ |
|
|
}).then(res => { |
|
|
title: '微信登录失败', |
|
|
console.log('获取openid接口响应:', res) |
|
|
icon: 'none' |
|
|
if (res.code === 1) { |
|
|
}) |
|
|
resolve(res.data.openid) |
|
|
resolve(null) |
|
|
} else { |
|
|
} |
|
|
uni.showToast({ |
|
|
}) |
|
|
title: res.msg || '获取openid失败', |
|
|
// #endif |
|
|
icon: 'none' |
|
|
|
|
|
}) |
|
|
// #ifndef MP-WEIXIN |
|
|
resolve(null) |
|
|
console.log('非微信小程序环境,启用测试模式') |
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
// 开发阶段测试:使用模拟的code调用API接口 |
|
|
console.error('获取openid接口调用失败:', error) |
|
|
uni.showModal({ |
|
|
resolve(null) |
|
|
title: '测试模式', |
|
|
}) |
|
|
content: '当前非小程序环境,是否使用测试code调试接口?', |
|
|
} else { |
|
|
success: (res) => { |
|
|
uni.showToast({ |
|
|
if (res.confirm) { |
|
|
title: '微信登录失败', |
|
|
console.log('用户选择测试模式') |
|
|
icon: 'none' |
|
|
// 使用测试code调用接口 |
|
|
}) |
|
|
apiRoute.getWechatOpenid({ |
|
|
resolve(null) |
|
|
code: 'test_code_' + Date.now(), |
|
|
} |
|
|
type: 'miniprogram' |
|
|
}, |
|
|
}).then(res => { |
|
|
fail: (error) => { |
|
|
console.log('测试模式接口响应:', res) |
|
|
console.error('微信登录失败:', error) |
|
|
if (res.code === 1) { |
|
|
uni.showToast({ |
|
|
resolve(res.data.openid) |
|
|
title: '微信登录失败', |
|
|
} else { |
|
|
icon: 'none' |
|
|
console.log('API返回错误:', res.msg) |
|
|
}) |
|
|
uni.showToast({ |
|
|
resolve(null) |
|
|
title: '测试模式:' + (res.msg || '获取openid失败'), |
|
|
} |
|
|
icon: 'none', |
|
|
}) |
|
|
duration: 3000 |
|
|
// #endif |
|
|
}) |
|
|
|
|
|
// 即使失败也返回一个测试openid用于调试流程 |
|
|
// #ifndef MP-WEIXIN |
|
|
resolve('test_openid_' + Date.now()) |
|
|
console.log('非微信小程序环境,启用测试模式') |
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
// 开发阶段测试:使用模拟的code调用API接口 |
|
|
console.error('测试模式接口调用失败:', error) |
|
|
uni.showModal({ |
|
|
// 返回测试openid用于调试流程 |
|
|
title: '测试模式', |
|
|
resolve('test_openid_' + Date.now()) |
|
|
content: '当前非小程序环境,是否使用测试code调试接口?', |
|
|
}) |
|
|
success: (res) => { |
|
|
} else { |
|
|
if (res.confirm) { |
|
|
console.log('用户取消测试') |
|
|
console.log('用户选择测试模式') |
|
|
resolve(null) |
|
|
// 使用测试code调用接口 |
|
|
} |
|
|
apiRoute.getWechatOpenid({ |
|
|
} |
|
|
code: 'test_code_' + Date.now(), |
|
|
}) |
|
|
type: 'miniprogram' |
|
|
// #endif |
|
|
}).then(res => { |
|
|
}) |
|
|
console.log('测试模式接口响应:', res) |
|
|
}, |
|
|
if (res.code === 1) { |
|
|
|
|
|
resolve(res.data.openid) |
|
|
// 绑定小程序openid |
|
|
} else { |
|
|
bindMiniProgramOpenid(openid) { |
|
|
console.log('API返回错误:', res.msg) |
|
|
return new Promise((resolve) => { |
|
|
uni.showToast({ |
|
|
apiRoute.bindWechatOpenid({ |
|
|
title: '测试模式:' + (res.msg || '获取openid失败'), |
|
|
miniprogram_openid: openid, |
|
|
icon: 'none', |
|
|
type: 'miniprogram' |
|
|
duration: 3000 |
|
|
}).then(res => { |
|
|
}) |
|
|
if (res.code === 1) { |
|
|
// 即使失败也返回一个测试openid用于调试流程 |
|
|
uni.showToast({ |
|
|
resolve('test_openid_' + Date.now()) |
|
|
title: '小程序绑定成功', |
|
|
} |
|
|
icon: 'success' |
|
|
}).catch(error => { |
|
|
}) |
|
|
console.error('测试模式接口调用失败:', error) |
|
|
resolve(true) |
|
|
// 返回测试openid用于调试流程 |
|
|
} else { |
|
|
resolve('test_openid_' + Date.now()) |
|
|
uni.showToast({ |
|
|
}) |
|
|
title: res.msg || '小程序绑定失败', |
|
|
} else { |
|
|
icon: 'none' |
|
|
console.log('用户取消测试') |
|
|
}) |
|
|
resolve(null) |
|
|
resolve(false) |
|
|
} |
|
|
} |
|
|
} |
|
|
}).catch(error => { |
|
|
}) |
|
|
console.error('绑定小程序openid失败:', error) |
|
|
// #endif |
|
|
uni.showToast({ |
|
|
}) |
|
|
title: '绑定失败,请重试', |
|
|
}, |
|
|
icon: 'none' |
|
|
|
|
|
}) |
|
|
// 绑定小程序openid |
|
|
resolve(false) |
|
|
bindMiniProgramOpenid(openid) { |
|
|
}) |
|
|
return new Promise((resolve) => { |
|
|
}) |
|
|
apiRoute.bindWechatOpenid({ |
|
|
}, |
|
|
miniprogram_openid: openid, |
|
|
|
|
|
type: 'miniprogram' |
|
|
// 构建webview URL |
|
|
}).then(res => { |
|
|
buildWebviewUrl(miniOpenid) { |
|
|
if (res.code === 1) { |
|
|
const baseUrl = Api_url |
|
|
uni.showToast({ |
|
|
const redirectUri = encodeURIComponent(`${baseUrl}/personnel/wechatCallback`) |
|
|
title: '小程序绑定成功', |
|
|
const state = encodeURIComponent(JSON.stringify({ |
|
|
icon: 'success' |
|
|
mini_openid: miniOpenid, |
|
|
}) |
|
|
personnel_id: this.$store.state.userInfo.id, |
|
|
resolve(true) |
|
|
from: 'miniprogram', |
|
|
} else { |
|
|
timestamp: Date.now() |
|
|
uni.showToast({ |
|
|
})) |
|
|
title: res.msg || '小程序绑定失败', |
|
|
|
|
|
icon: 'none' |
|
|
const authUrl = `${baseUrl}/personnel/wechatAuthorize?redirect_uri=${redirectUri}&state=${state}` |
|
|
}) |
|
|
console.log('构建的授权URL:', authUrl) |
|
|
resolve(false) |
|
|
return authUrl |
|
|
} |
|
|
}, |
|
|
}).catch(error => { |
|
|
|
|
|
console.error('绑定小程序openid失败:', error) |
|
|
// 检测是否为微信环境 |
|
|
uni.showToast({ |
|
|
isWeixinBrowser() { |
|
|
title: '绑定失败,请重试', |
|
|
const ua = navigator.userAgent.toLowerCase() |
|
|
icon: 'none' |
|
|
return ua.includes('micromessenger') |
|
|
}) |
|
|
} |
|
|
resolve(false) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 构建webview URL |
|
|
|
|
|
buildWebviewUrl(miniOpenid) { |
|
|
|
|
|
const baseUrl = Api_url |
|
|
|
|
|
const redirectUri = encodeURIComponent(`${baseUrl}/personnel/wechatCallback`) |
|
|
|
|
|
const state = encodeURIComponent(JSON.stringify({ |
|
|
|
|
|
mini_openid: miniOpenid, |
|
|
|
|
|
personnel_id: this.userInfo?.id, |
|
|
|
|
|
from: 'miniprogram', |
|
|
|
|
|
timestamp: Date.now() |
|
|
|
|
|
})) |
|
|
|
|
|
|
|
|
|
|
|
const authUrl = `${baseUrl}/personnel/wechatAuthorize?redirect_uri=${redirectUri}&state=${state}` |
|
|
|
|
|
console.log('构建的授权URL:', authUrl) |
|
|
|
|
|
return authUrl |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 检测是否为微信环境 |
|
|
|
|
|
isWeixinBrowser() { |
|
|
|
|
|
const ua = navigator.userAgent.toLowerCase() |
|
|
|
|
|
return ua.includes('micromessenger') |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
</script> |
|
|
</script> |
|
|
|
|
|
|
|
|
<style lang="less" scoped> |
|
|
<style lang="less" scoped> |
|
|
.assemble{ |
|
|
.assemble { |
|
|
width: 100%; |
|
|
width: 100%; |
|
|
height: 100vh; |
|
|
height: 100vh; |
|
|
background: #333333; |
|
|
background: #333333; |
|
|
} |
|
|
} |
|
|
.option{ |
|
|
|
|
|
|
|
|
.option { |
|
|
margin-bottom: 20rpx; |
|
|
margin-bottom: 20rpx; |
|
|
background: #404045; |
|
|
background: #404045; |
|
|
width: 100%; |
|
|
width: 100%; |
|
|
|