三种消息类型:
// 文本消息
{
"signature": "ee6e400de7972484ec6f3014c2f77504925a4707",
"timestamp": "1552298008",
"nonce": "540004383",
"openid": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
"encrypt_type": "aes",
"msg_signature": "74c419155e49cefab8ab1dbd440b9389acc47e2a",
"URL": "http:\/\/qiang.lt.ngapp.net\/wechat\/noti/fy",
"ToUserName": "zhaishuaigan",
"FromUserName": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
"CreateTime": 1552293166,
"MsgType": "text",
"Content": "hello",
"MsgId": 1,
"Encrypt": "PloOW0ucj9SkMpMC...."
}
// 事件消息
{
"signature": "3e5860da822266b9f03f8d5380615f9be0ae2db7",
"timestamp": "1552301914",
"nonce": "693467019",
"openid": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
"encrypt_type": "aes",
"msg_signature": "eda0cdd567b35df4501ee2041e0d191db81798f2",
"ToUserName": "gh_cc23a0a84984",
"FromUserName": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
"CreateTime": 1552301914,
"MsgType": "event",
"Event": "user_enter_tempsession",
"SessionFrom": "open-type='contact'",
"Encrypt": "oMP\/xo2RdcbK07vvoxxxx....."
}
// 图片消息
{
"signature": "ebd79f0c44f01dc26ef83ffeeadbdb3af5b960da",
"timestamp": "1552302190",
"nonce": "1322948029",
"openid": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
"encrypt_type": "aes",
"msg_signature": "06613ca44e189d95140166898f80a27ab10baa80",
"ToUserName": "gh_cc23a0a84984",
"FromUserName": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
"CreateTime": 1552302190,
"MsgType": "image",
"PicUrl": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WwPGxFUiaTRZ6zVvTTGVbhfa9Vic801ux7OHobAAiaD3xRMYeJzbic4ISvwn736dpK0OMTlaYvX7GoTRgE8LqObkIQ\/0",
"MsgId": 22223624286040849,
"MediaId": "3WiHLigPtkJtqoLvPD2HgPsAtb3vzDvTWyo0sP5s-dshtS7oZOlCW7c3RuD1nwBt",
"Encrypt": "gictSCCy+Zxxxx...."
}
ThinkPHP 5.1保存消息代码
$request = request()->param();
$data = [
'signature' => $request['signature'],
'timestamp' => $request['timestamp'],
'nonce' => $request['nonce'],
'openid' => $request['openid'],
'encrypt_type' => $request['encrypt_type'],
'msg_signature' => $request['msg_signature'],
'to_username' => $request['ToUserName'],
'from_username' => $request['FromUserName'],
'msg_type' => $request['MsgType'],
'msg_id' => isset($request['MsgId']) ? $request['MsgId'] : '',
'encrypt' => isset($request['Encrypt']) ? $request['Encrypt'] : '',
];
$msgInfo = [];
switch ($request['MsgType']) {
case 'text':
$msgInfo['text'] = $request['Content'];
break;
case 'image':
$msgInfo['pic'] = $request['PicUrl'];
$msgInfo['media_id'] = $request['MediaId'];
break;
case 'event':
$msgInfo['event'] = $request['Event'];
$msgInfo['session_from'] = $request['SessionFrom'];
break;
}
$data['msg_info'] = json_encode($msgInfo);
CustomerMessage::create($data);
return '';
数据库表结构设计
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class CreateCustomerMessageTable extends Migrator
{
public function change()
{
$this->table('customer_message')
->addColumn(Column::string('signature')
->setDefault('')
->setComment('签名'))
->addColumn(Column::integer('timestamp')
->setDefault(0)
->setComment('时间戳'))
->addColumn(Column::string('nonce')
->setDefault('')
->setComment('随机数'))
->addColumn(Column::string('openid')
->setDefault('')
->setComment('客户id'))
->addColumn(Column::string('encrypt_type')
->setDefault('')
->setComment('加密方式'))
->addColumn(Column::string('msg_signature')
->setDefault('')
->setComment('消息签名'))
->addColumn(Column::string('to_username')
->setDefault('')
->setComment('消息接收者'))
->addColumn(Column::string('from_username')
->setDefault('')
->setComment('消息来源'))
->addColumn(Column::string('msg_type')
->setDefault('')
->setComment('消息类型'))
->addColumn(Column::text('msg_info')
->setNull(true)
->setComment('消息内容'))
->addColumn(Column::string('msg_id')
->setDefault('')
->setComment('消息id'))
->addColumn(Column::text('encrypt')
->setNull(true)
->setComment('加密数据'))
->addColumn(Column::dateTime('create_time')
->setDefault('CURRENT_TIMESTAMP')
->setComment('创建时间'))
->addColumn(Column::dateTime('update_time')
->setDefault('CURRENT_TIMESTAMP')
->setComment('更新时间'))
->addColumn(Column::dateTime('delete_time')
->setNull(true)
->setComment('删除时间'))
->create();
}
}