小程序客服消息处理

三种消息类型:

  1. // 文本消息
  2. {
  3. "signature": "ee6e400de7972484ec6f3014c2f77504925a4707",
  4. "timestamp": "1552298008",
  5. "nonce": "540004383",
  6. "openid": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
  7. "encrypt_type": "aes",
  8. "msg_signature": "74c419155e49cefab8ab1dbd440b9389acc47e2a",
  9. "URL": "http:\/\/qiang.lt.ngapp.net\/wechat\/noti/fy",
  10. "ToUserName": "zhaishuaigan",
  11. "FromUserName": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
  12. "CreateTime": 1552293166,
  13. "MsgType": "text",
  14. "Content": "hello",
  15. "MsgId": 1,
  16. "Encrypt": "PloOW0ucj9SkMpMC...."
  17. }
  18. // 事件消息
  19. {
  20. "signature": "3e5860da822266b9f03f8d5380615f9be0ae2db7",
  21. "timestamp": "1552301914",
  22. "nonce": "693467019",
  23. "openid": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
  24. "encrypt_type": "aes",
  25. "msg_signature": "eda0cdd567b35df4501ee2041e0d191db81798f2",
  26. "ToUserName": "gh_cc23a0a84984",
  27. "FromUserName": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
  28. "CreateTime": 1552301914,
  29. "MsgType": "event",
  30. "Event": "user_enter_tempsession",
  31. "SessionFrom": "open-type='contact'",
  32. "Encrypt": "oMP\/xo2RdcbK07vvoxxxx....."
  33. }
  34. // 图片消息
  35. {
  36. "signature": "ebd79f0c44f01dc26ef83ffeeadbdb3af5b960da",
  37. "timestamp": "1552302190",
  38. "nonce": "1322948029",
  39. "openid": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
  40. "encrypt_type": "aes",
  41. "msg_signature": "06613ca44e189d95140166898f80a27ab10baa80",
  42. "ToUserName": "gh_cc23a0a84984",
  43. "FromUserName": "oPzrj5EAP25lzOVW6qa0m8MUlLXA",
  44. "CreateTime": 1552302190,
  45. "MsgType": "image",
  46. "PicUrl": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WwPGxFUiaTRZ6zVvTTGVbhfa9Vic801ux7OHobAAiaD3xRMYeJzbic4ISvwn736dpK0OMTlaYvX7GoTRgE8LqObkIQ\/0",
  47. "MsgId": 22223624286040849,
  48. "MediaId": "3WiHLigPtkJtqoLvPD2HgPsAtb3vzDvTWyo0sP5s-dshtS7oZOlCW7c3RuD1nwBt",
  49. "Encrypt": "gictSCCy+Zxxxx...."
  50. }

ThinkPHP 5.1保存消息代码

  1. $request = request()->param();
  2. $data = [
  3. 'signature' => $request['signature'],
  4. 'timestamp' => $request['timestamp'],
  5. 'nonce' => $request['nonce'],
  6. 'openid' => $request['openid'],
  7. 'encrypt_type' => $request['encrypt_type'],
  8. 'msg_signature' => $request['msg_signature'],
  9. 'to_username' => $request['ToUserName'],
  10. 'from_username' => $request['FromUserName'],
  11. 'msg_type' => $request['MsgType'],
  12. 'msg_id' => isset($request['MsgId']) ? $request['MsgId'] : '',
  13. 'encrypt' => isset($request['Encrypt']) ? $request['Encrypt'] : '',
  14. ];
  15. $msgInfo = [];
  16. switch ($request['MsgType']) {
  17. case 'text':
  18. $msgInfo['text'] = $request['Content'];
  19. break;
  20. case 'image':
  21. $msgInfo['pic'] = $request['PicUrl'];
  22. $msgInfo['media_id'] = $request['MediaId'];
  23. break;
  24. case 'event':
  25. $msgInfo['event'] = $request['Event'];
  26. $msgInfo['session_from'] = $request['SessionFrom'];
  27. break;
  28. }
  29. $data['msg_info'] = json_encode($msgInfo);
  30. CustomerMessage::create($data);
  31. return '';

数据库表结构设计

  1. <?php
  2. use think\migration\Migrator;
  3. use think\migration\db\Column;
  4. class CreateCustomerMessageTable extends Migrator
  5. {
  6. public function change()
  7. {
  8. $this->table('customer_message')
  9. ->addColumn(Column::string('signature')
  10. ->setDefault('')
  11. ->setComment('签名'))
  12. ->addColumn(Column::integer('timestamp')
  13. ->setDefault(0)
  14. ->setComment('时间戳'))
  15. ->addColumn(Column::string('nonce')
  16. ->setDefault('')
  17. ->setComment('随机数'))
  18. ->addColumn(Column::string('openid')
  19. ->setDefault('')
  20. ->setComment('客户id'))
  21. ->addColumn(Column::string('encrypt_type')
  22. ->setDefault('')
  23. ->setComment('加密方式'))
  24. ->addColumn(Column::string('msg_signature')
  25. ->setDefault('')
  26. ->setComment('消息签名'))
  27. ->addColumn(Column::string('to_username')
  28. ->setDefault('')
  29. ->setComment('消息接收者'))
  30. ->addColumn(Column::string('from_username')
  31. ->setDefault('')
  32. ->setComment('消息来源'))
  33. ->addColumn(Column::string('msg_type')
  34. ->setDefault('')
  35. ->setComment('消息类型'))
  36. ->addColumn(Column::text('msg_info')
  37. ->setNull(true)
  38. ->setComment('消息内容'))
  39. ->addColumn(Column::string('msg_id')
  40. ->setDefault('')
  41. ->setComment('消息id'))
  42. ->addColumn(Column::text('encrypt')
  43. ->setNull(true)
  44. ->setComment('加密数据'))
  45. ->addColumn(Column::dateTime('create_time')
  46. ->setDefault('CURRENT_TIMESTAMP')
  47. ->setComment('创建时间'))
  48. ->addColumn(Column::dateTime('update_time')
  49. ->setDefault('CURRENT_TIMESTAMP')
  50. ->setComment('更新时间'))
  51. ->addColumn(Column::dateTime('delete_time')
  52. ->setNull(true)
  53. ->setComment('删除时间'))
  54. ->create();
  55. }
  56. }
文章不错, 赏你二两银子

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续努力!