123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <template>
- <view class="container">
- <view class="loading">
- <text>正在获取用户信息...</text>
- <view>{{userInfo}}000</view>
- <view>{{token}}{{res}}</view>
- <view @click="copy">复制res</view>
- </view>
- </view>
- </template>
- <script>
- import uni from "@/utils/uni.webview.1.5.4.js";
- import { loginByMp} from '@/api/user'
- export default {
- data() {
- return {
- code: '',
- userInfo: null,
- token:null,
- res:{}
- }
- },
- onLoad(options) {
- // 获取URL中的code参数(微信授权返回的)
- this.code = this.getUrlParam('code')
-
- if (!this.code) {
- uni.showToast({
- title: '授权失败,未获取到code',
- icon: 'none'
- })
- return
- }
-
- // 通过code获取用户信息
- this.getUserInfoByCode(this.code)
- },
- methods: {
- copy(){
- uni.setClipboardData({
- data: this.res, //data是需要复制的数据
- showToast: true, //配置是否弹出提示,默认弹出提示
- success: function () {
- console.log('success'); //复制成功的回调函数
- },
- fail: function () {
- console.log('fail'); //复制失败的回调函数
- },
- complete: function () {
- console.log('complete'); //接口调用结束的回调函数(调用成功、失败都会执行)
- },
- });
- },
- // 获取URL参数
- getUrlParam(name) {
- const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
- const r = window.location.search.substr(1).match(reg)
- if (r != null) return decodeURIComponent(r[2])
- return null
- },
-
- // 通过code获取用户信息
- getUserInfoByCode(code) {
- // 调用后端API,通过code换取用户信息
- uni.showLoading({
- title: "处理中..."
- });
- var data = {
- code: this.code,
- videoId: this.videoId,
- companyId: this.companyId,
- companyUserId: this.companyUserId
- }
- loginByMp(data).then(res => {
- this.res=res
- uni.hideLoading();
- if (res.code == 200) {
- uni.setStorageSync('TOKEN_WEXIN', res.token);
- this.userInfo = res.user
- this.token= res.token
- // 将用户信息传递给小程序
- setTimeout(() => {
- this.postMessageToMiniProgram()
- }, 200)
- } else {
- console.error('获取用户信息失败:', res.msg)
- uni.showToast({
- title: res.msg || '获取用户信息失败',
- icon: 'none'
- })
- }
- },
- err => {}
- ).catch(err=>{
- uni.hideLoading();
- });
- },
- // 向小程序发送消息
- postMessageToMiniProgram() {
- // 判断是否在小程序环境中
- uni.showToast({
- title:'获取用户信息失败22',
- icon: 'none'
- })
- // if (wx && wx.miniProgram) {
- // wx.miniProgram.postMessage({ data: ['这是从H5页面发送的消息', 'wdwdsdsf'] });
- // console.log(uni.postMessage);
- // console.log(111111);
- // } else {
- // console.error('当前环境不支持微信小程序API');
- // }
- // uni.showToast({
- // title:'获取用户信息失败123',
- // icon: 'none'
- // })
- // setTimeout(() => {
- // uni.navigateBack()
- // }, 500)
- if (window.__wxjs_environment === 'miniprogram') {
- // 向小程序发送用户信息
- uni.postMessage({
- data: {
- type: 'user_info',
- data: this.userInfo,
- token: this.token,
- }
- })
- uni.showToast({
- title:'获取用户信息失败123',
- icon: 'none'
- })
- // 延迟关闭当前页面,确保消息发送成功
- setTimeout(() => {
- uni.navigateBack()
- }, 500)
- } else {
- uni.showToast({
- title:'获取用户信息失败456',
- icon: 'none'
- })
- // 在浏览器环境中,可以做H5版本的处理
- console.log('非小程序环境,H5版本处理用户信息:', this.userInfo)
- uni.setStorageSync('userInfo', this.userInfo)
- uni.setStorageSync('TOKEN_WEXIN', this.token)
- console.error('TOKEN_WEXIN:', this.token)
- uni.navigateTo({
- url: '/pages/index/index'
- })
- }
- }
- }
- }
- </script>
- <style>
- .container {
- width: 100%;
- height: 100vh;
- display: flex;
- justify-content: center;
- align-items: center;
- background-color: #fff;
- }
- .loading {
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- </style>
|