提交 d5dc58a1 作者: 毛细亚

保存log

上级 d97282f3
# 游戏日志收集功能使用说明
## 概述
`gameLogMixin` 是一个统一的游戏日志收集混入,用于自动监听 Vuex 中的 `send_game_log` 状态变化,并在发送消息成功后自动调用 `send_log_add` 接口记录日志。
## 功能特性
1. **自动监听**: 自动监听 Vuex 中 `send_game_log` 的变化
2. **自动记录**: 在 `sendChatMessage` 成功后自动记录游戏日志
3. **统一管理**: 提供统一的日志记录逻辑,避免重复代码
4. **兼容性**: 兼容 `chatUser``chatUserInfo` 两种用户信息格式
5. **错误处理**: 提供完善的错误处理机制
## 使用方法
### 1. 引入 mixin
```javascript
import gameLogMixin from '@/mixins/gameLogMixin'
export default {
mixins: [gameLogMixin],
// 其他组件配置...
}
```
### 2. 记录游戏日志
有两种方式记录游戏日志:
#### 方式一:手动调用 sendGameLog 方法
```javascript
// 在组件方法中调用
this.sendGameLog({
game_id: 123,
game_name: '游戏名称',
game_type: 1,
main_game_id: 456,
weixin_blongs_id: 789,
type: 2
})
// 然后发送消息
this.sendChatMessage('消息内容', 'text')
```
#### 方式二:直接使用 set_send_game_log
```javascript
this.set_send_game_log({
game_id: 123,
game_name: '游戏名称',
game_type: 1,
main_game_id: 456,
weixin_blongs_id: 789,
type: 2
})
// 然后发送消息
this.sendChatMessage('消息内容', 'text')
```
### 3. 发送消息
mixin 重写了 `sendChatMessage` 方法,支持自动日志记录:
```javascript
// 发送文本消息
this.sendChatMessage('游戏链接: https://example.com', 'text')
// 发送图片消息
this.sendChatMessage('https://example.com/image.jpg', 'image')
// 发送小程序消息
this.sendChatMessage({
appid: 'wx123',
title: '小程序标题',
// 其他小程序参数...
}, 'miniprogram')
```
## 参数说明
### sendGameLog 参数
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| game_id | String/Number | 是 | 游戏ID |
| game_name | String | 是 | 游戏名称 |
| game_type | String/Number | 否 | 游戏类型 |
| main_game_id | String/Number | 否 | 主游戏ID |
| weixin_blongs_id | String/Number | 否 | 微信归属ID |
| type | Number | 否 | 日志类型,默认为2 |
### sendChatMessage 参数
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| content | Any | 是 | 消息内容 |
| type | String | 是 | 消息类型:'text', 'image', 'miniprogram' 等 |
## 生命周期
1. 组件创建时,mixin 开始监听 `send_game_log` 变化
2. 调用 `sendGameLog``set_send_game_log` 时,将游戏信息存储到 `pendingGameLog`
3. 调用 `sendChatMessage` 发送消息
4. 消息发送成功后,自动调用 `send_log_add` 接口记录日志
5. 日志记录完成后,清空 `pendingGameLog` 和 Vuex 中的 `send_game_log`
## 注意事项
1. **组件必须有用户信息**: 确保组件中存在 `chatUser``chatUserInfo` 属性
2. **避免重复引入方法**: 使用 mixin 后不需要再手动引入 `sendChatMessage` 和相关 mutations
3. **错误处理**: 即使日志记录失败,也不会影响消息发送功能
4. **性能考虑**: 日志记录是异步进行的,不会阻塞用户操作
## 控制方法
### 禁用日志记录
```javascript
this.disableGameLogging()
```
### 启用日志记录
```javascript
this.enableGameLogging()
```
## 已应用的组件
- `sendGame.vue` - 主游戏发送组件
- `selectChannel.vue` - 渠道选择组件
- `sendSelectChannel.vue` - 发送选择渠道组件
- `SendTransWxGame.vue` - 微信小游戏转端组件
- `SendTransAppGame.vue` - App游戏转端组件
## 调试信息
mixin 会在控制台输出以下调试信息:
- `准备记录游戏日志:` - 当有新的游戏日志信息时
- `发送游戏日志:` - 发送日志到接口时
- `游戏日志记录成功` - 日志记录成功时
- `游戏日志记录失败:` - 日志记录失败时
## 故障排除
### 1. 日志没有记录
- 检查是否正确调用了 `sendGameLog` 方法
- 检查 `game_id``game_name` 是否有效
- 检查组件是否有用户信息(`chatUser``chatUserInfo`
### 2. 消息发送失败
- 检查网络连接
- 检查消息内容格式是否正确
- 查看控制台错误信息
### 3. 重复日志记录
- 确保没有重复调用 `sendGameLog` 方法
- 检查是否有多个地方调用了 `set_send_game_log`
\ No newline at end of file
import { mapState, mapMutations } from 'vuex'
import { send_log_add } from '@/api/works'
import { sendChatMessage as originalSendChatMessage } from '@/utils/index'
export default {
data() {
return {
pendingGameLog: null, // 待发送的游戏日志信息
isLoggingEnabled: true // 日志记录开关
}
},
computed: {
...mapState('game', ['send_game_log']),
// 统一处理chatUser,兼容chatUser和chatUserInfo
chatUserForLog() {
return this.chatUser || this.chatUserInfo || {}
}
},
watch: {
// 监听send_game_log变化,当有新的日志信息时准备记录
send_game_log: {
handler(newVal) {
if (newVal && this.isLoggingEnabled) {
this.pendingGameLog = { ...newVal }
console.log('准备记录游戏日志:', this.pendingGameLog)
}
},
immediate: false
}
},
methods: {
...mapMutations('game', ['set_send_game_log']),
/**
* 重写sendChatMessage方法,在发送成功后自动记录日志
* @param {*} content 消息内容
* @param {*} type 消息类型
*/
async sendChatMessage(content, type) {
try {
// 调用原始的sendChatMessage方法
const result = await originalSendChatMessage(content, type)
// 如果有待记录的游戏日志,则记录
if (this.pendingGameLog && this.isLoggingEnabled) {
await this.recordGameLog(result, content, type)
}
return result
} catch (error) {
console.error('发送消息失败:', error)
throw error
}
},
/**
* 记录游戏日志
* @param {Object} messageInfo 发送消息的返回信息
* @param {*} content 消息内容
* @param {String} type 消息类型
*/
async recordGameLog(messageInfo, content, type) {
if (!this.pendingGameLog) {
return
}
try {
const chatUser = this.chatUserForLog
// 构建日志数据
const logData = {
game_id: this.pendingGameLog.game_id,
game_name: this.pendingGameLog.game_name,
game_type: this.pendingGameLog.game_type || '',
main_game_id: this.pendingGameLog.main_game_id || '',
weixin_blongs_id: this.pendingGameLog.weixin_blongs_id || '',
content: this.formatLogContent(content, type),
type: this.pendingGameLog.type || 2,
frontend_message_id: messageInfo?.frontend_message_id || '',
session_id: messageInfo?.session_id || '',
userid: chatUser.userid || '',
external_userid: chatUser.external_userid || ''
}
console.log('发送游戏日志:', logData)
// 调用日志记录接口
const response = await send_log_add(logData)
if (response.status_code === 1) {
console.log('游戏日志记录成功')
} else {
console.warn('游戏日志记录失败:', response.msg)
}
// 清空待记录的日志信息
this.pendingGameLog = null
this.set_send_game_log(null)
} catch (error) {
console.error('记录游戏日志失败:', error)
// 即使日志记录失败,也要清空待记录信息,避免影响后续操作
this.pendingGameLog = null
this.set_send_game_log(null)
}
},
/**
* 格式化日志内容
* @param {*} content 消息内容
* @param {String} type 消息类型
* @returns {String} 格式化后的内容
*/
formatLogContent(content, type) {
if (type === 'text') {
return content
} else if (type === 'image') {
return `[图片] ${content}`
} else if (type === 'miniprogram') {
return `[小程序] ${content.title || ''}`
} else {
return `[${type}] ${JSON.stringify(content)}`
}
},
/**
* 手动记录游戏日志(兼容现有的sendGameLog方法)
* @param {Object} gameInfo 游戏信息
*/
sendGameLog(gameInfo) {
if (gameInfo && gameInfo.game_id && gameInfo.game_name) {
const logInfo = {
game_id: gameInfo.game_id,
game_name: gameInfo.game_name,
game_type: gameInfo.game_type || '',
main_game_id: gameInfo.main_game_id || '',
weixin_blongs_id: gameInfo.weixin_blongs_id || '',
type: gameInfo.type || 2
}
this.set_send_game_log(logInfo)
}
},
/**
* 禁用日志记录(在某些特殊情况下使用)
*/
disableGameLogging() {
this.isLoggingEnabled = false
},
/**
* 启用日志记录
*/
enableGameLogging() {
this.isLoggingEnabled = true
}
},
beforeDestroy() {
// 组件销毁前清理
this.pendingGameLog = null
}
}
\ No newline at end of file
......@@ -327,7 +327,10 @@
import SendTransAppGame from './sendGame/SendTransAppGame.vue'
import selectChannel from './sendGame/selectChannel.vue'
import sendSelectChannel from './sendGame/sendSelectChannel.vue'
import gameLogMixin from '@/mixins/gameLogMixin'
export default {
mixins: [gameLogMixin],
components: {
createChannel,
sendPage,
......@@ -972,30 +975,7 @@
* 7.你可以封装成一个 minix 或者你有更好的处理逻辑 来完成 发送游戏日志的收集工作
* 8.总结:每次调用 set_send_game_log 方法的时候 是在 vuex 中记录 需要发送的日志信息 你要监听 send_game_log 变量的变化 然后调用 send_log_add 方法 完成日志的收集工作 你需要帮我选一个最佳的方案来完成这个功能 尽量保证不出问题
*/
send_log_add(info) {
const data = {
game_id: this.send_game_log.game_id,
game_name: this.send_game_log.game_name,
game_type: this.send_game_log.game_type || '',
main_game_id: this.send_game_log.main_game_id || '',
weixin_blongs_id: this.send_game_log.weixin_blongs_id || '',
content: info.message,
type: this.send_game_log.type || 2,
frontend_message_id: info.frontend_message_id,
session_id: info.session_id,
userid: this.chatUser.userid,
external_userid: this.chatUser.external_userid
}
this.set_send_game_log(null)
send_log_add(data).then((res) => {
if (res.status_code === 1) {
console.log('日志记录成功')
}
})
},
sendGameLog(item) {
item && item.game_id && item.game_name ? this.set_send_game_log({ game_id: item.game_id, game_name: item.game_name, game_type: item.game_type || '', main_game_id: item.main_game_id, weixin_blongs_id: item.weixin_blongs_id, type: item.type || '' }) : ''
},
handleSendType(data) {
console.log(data, '获取一键发送的信息')
// 转端发送优先逻辑 不需要了 现在发送当前会话框选中的账号对应的注册游戏
......@@ -1076,7 +1056,9 @@
}
}
]
this.set_sendSkillMessage(list)
// 这里需要特殊处理,因为有taskInfo参数
// this.set_sendSkillMessage(list)
this.sendChatMessage(`${str}${item.game_url} \n账号:${username} \n密码:${res.data.encryptPassword}`, 'text')
item.type = 3
this.sendGameLog(item)
})
......@@ -1219,10 +1201,7 @@
// this.set_sendSkillMessage(list)
this.sendChatMessage(`游戏地址:${data.landing_page_url} \n账号:${data.username} \n密码:${data.password}`,'text')
},
async sendChatMessage(content, type) {
// 调用公共的sendChatMessage方法
await sendChatMessage(content, type)
},
async getMediaId(picurl){
// 发送图片作为链接消息
if (picurl) {
......
......@@ -112,8 +112,11 @@
<script>
import { channelList, getLandingPageMemberLink } from '@/api/game'
import { mapState, mapMutations } from 'vuex'
import gameLogMixin from '@/mixins/gameLogMixin'
export default {
name: 'SendTransAppGame',
mixins: [gameLogMixin],
components: {
},
props: {
......@@ -216,7 +219,6 @@
this.iosChannelList = []
},
methods: {
...mapMutations('game', ['set_send_game_log']),
// 获取渠道列表的方法
async channelList(items, type) {
try {
......@@ -257,9 +259,7 @@
}
}
},
sendGameLog(item) {
item && item.game_id && item.game_name ? this.set_send_game_log({ game_id: item.game_id, game_name: item.game_name, game_type: item.game_type || '', main_game_id: item.main_game_id, weixin_blongs_id: item.weixin_blongs_id, type: item.type || '' }) : ''
},
handleConfirm() {
this.handleWxGameChannel()
},
......
......@@ -83,8 +83,11 @@
import { mapState, mapMutations } from 'vuex'
import { throttleStart } from '@/utils/index'
import html2canvas from 'html2canvas'
import gameLogMixin from '@/mixins/gameLogMixin'
export default {
name: 'SendTransWxGame',
mixins: [gameLogMixin],
components: {
},
props: {
......@@ -154,7 +157,6 @@
if (this.$refs.qrImg) this.$refs.qrImg.src = ''
},
methods: {
...mapMutations('game', ['set_send_game_log']),
closeMessage() {
this.messageInfo ? this.messageInfo.close() : ''
this.loading = false
......@@ -336,9 +338,7 @@
handleConfirm() {
this.handleWxGameChannel()
},
sendGameLog(item) {
item && item.game_id && item.game_name ? this.set_send_game_log({ game_id: item.game_id, game_name: item.game_name, game_type: item.game_type || '', main_game_id: item.main_game_id, weixin_blongs_id: item.weixin_blongs_id, type: item.type || '' }) : ''
},
// 保存微信小游戏的信息
async handleWxGameChannel() {
if (!this.wxGameForm.wx_game_channel) {
......
......@@ -46,10 +46,13 @@
</template>
<script>
import { recallChannelSeq, passwardEncryption, autoResetPassword } from '@/api/game'
import { mapState, mapMutations } from 'vuex'
import { throttle,sendChatMessage } from '@/utils/index'
import { zyouGetMemberLink } from '@/api/works'
export default {
import { mapState, mapMutations } from 'vuex'
import { throttle } from '@/utils/index'
import { zyouGetMemberLink } from '@/api/works'
import gameLogMixin from '@/mixins/gameLogMixin'
export default {
mixins: [gameLogMixin],
props: ['show', 'channelInfoList', 'chatUser'],
data() {
return {
......@@ -64,7 +67,6 @@
...mapState('user', ['userInfo'])
},
methods: {
...mapMutations('game', ['set_send_game_log']),
close() {
this.$emit('update:show', false)
},
......@@ -94,7 +96,14 @@
})
} else {
this.sendMessageCall()
this.set_send_game_log({ game_id: this.webForm.channel_id.game_id, game_name: this.webForm.channel_id.game_name, main_game_id: this.webForm.channel_id.main_game_id })
this.sendGameLog({
game_id: this.webForm.channel_id.game_id,
game_name: this.webForm.channel_id.game_name,
main_game_id: this.webForm.channel_id.main_game_id,
game_type: this.webForm.channel_id.game_type,
weixin_blongs_id: this.webForm.channel_id.weixin_blongs_id,
type: this.channelInfoList.use_type
})
}
},
// 召回染色
......@@ -288,15 +297,19 @@
}
}
if (this.channelInfoList.use_type == 2) {
this.set_send_game_log({ game_id: this.webForm.channel_id.game_id, game_name: this.webForm.channel_id.game_name })
this.sendGameLog({
game_id: this.webForm.channel_id.game_id,
game_name: this.webForm.channel_id.game_name,
game_type: this.webForm.channel_id.game_type,
main_game_id: this.webForm.channel_id.main_game_id,
weixin_blongs_id: this.webForm.channel_id.weixin_blongs_id,
type: this.channelInfoList.use_type
})
}
// this.set_sendSkillMessage(list)
// this.sendChatMessage(str,'text')
},
async sendChatMessage(content, type) {
// 调用公共的sendChatMessage方法
await sendChatMessage(content, type)
},
}
}
</script>
......
......@@ -5,8 +5,11 @@
import { recallChannelSeq, autoResetPassword, passwardEncryption } from '@/api/game'
import { mapState, mapMutations } from 'vuex'
import { zyouGetMemberLink } from '@/api/works'
import { throttle,sendChatMessage } from '@/utils/index'
import { throttle } from '@/utils/index'
import gameLogMixin from '@/mixins/gameLogMixin'
export default {
mixins: [gameLogMixin],
props: ['show', 'channelInfoList', 'chatUser'],
data() {
return {
......@@ -30,14 +33,10 @@ export default {
...mapState('user', ['userInfo'])
},
methods: {
...mapMutations('game', ['set_send_game_log']),
close() {
this.$emit('update:show', false)
},
async sendChatMessage(content, type) {
// 调用公共的sendChatMessage方法
await sendChatMessage(content, type)
},
// const list = [{ msgtype: 'text', text: { content: `${str}${item.url}` }}]
// this.set_sendSkillMessage(list)
onConfirm() {
......@@ -260,7 +259,14 @@ export default {
}
// 转游的时候记录发送转游日志
if (this.channelInfoList.use_type == 2) {
this.set_send_game_log({ game_id: this.webForm.channel_id.game_id, game_name: this.webForm.channel_id.main_game_name })
this.sendGameLog({
game_id: this.webForm.channel_id.game_id,
game_name: this.webForm.channel_id.main_game_name,
game_type: this.webForm.channel_id.game_type,
main_game_id: this.webForm.channel_id.main_game_id,
weixin_blongs_id: this.webForm.channel_id.weixin_blongs_id,
type: this.channelInfoList.use_type
})
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论