MessageController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * MessageController.php UTF-8
  4. * 客服消息
  5. *
  6. * @date : 2018/9/14 16:37
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace mini\sdk\controller;
  13. use huomp\model\weixin\MpConfModel;
  14. use huoMpMsg\controller\MiniMessage;
  15. use huoMpMsg\controller\OaMessage;
  16. use mini\common\controller\V2ApiBaseController;
  17. use think\Log;
  18. class MessageController extends V2ApiBaseController {
  19. function _initialize() {
  20. parent::_initialize();
  21. Log::write($this->request->getContent(), 'wechat', true);
  22. $this->response_type = 'json';
  23. }
  24. /**
  25. * 接收小程序客服消息
  26. * 【域名】/mp/msg/:conf_id
  27. * https://developers.weixin.qq.com/miniprogram/dev/api/custommsg/receive.html#文本消息
  28. *
  29. * @param string $mp_id 微信ID
  30. */
  31. public function receive($mp_id = '') {
  32. if (!empty($_GET["echostr"])) {
  33. $this->checkToken($mp_id);
  34. }
  35. $_param_json = $this->request->getContent();
  36. $_param = @json_decode($_param_json, true);
  37. if (empty($_param) || empty($mp_id)) {
  38. $this->error('参数错误');
  39. }
  40. $_rs = (new MiniMessage())->receive($mp_id, $_param);
  41. if (true == $_rs) {
  42. die('success');
  43. }
  44. die('');
  45. }
  46. /**
  47. * 第一次小程序校验token
  48. *
  49. * @param string $mp_id 公众号ID
  50. */
  51. public function checkToken($mp_id = '') {
  52. $_token = '';
  53. $_data = (new MpConfModel())->getDataByMpId($mp_id);
  54. if (!empty($_data['ext_info']['Token'])) {
  55. $_token = $_data['ext_info']['Token'];
  56. }
  57. $_signature = $_GET["signature"];
  58. $_timestamp = $_GET["timestamp"];
  59. $_nonce = $_GET["nonce"];
  60. $_echo_str = $_GET["echostr"];
  61. $_tmp_arr = array($_token, $_timestamp, $_nonce);
  62. sort($_tmp_arr, SORT_STRING);
  63. $_tmp_str = implode($_tmp_arr);
  64. $_tmp_str = sha1($_tmp_str);
  65. if ($_tmp_str == $_signature) {
  66. die($_echo_str);
  67. } else {
  68. die('fail');
  69. }
  70. }
  71. /**
  72. * 第一次校验微信公众号token
  73. * 接收小程序客服消息
  74. * 【域名】/oa/msg/:conf_id
  75. * https://developers.weixin.qq.com/miniprogram/dev/api/custommsg/receive.html#文本消息
  76. *
  77. * @param string $mp_id 公众号ID
  78. */
  79. public function checkOaToken($mp_id = '') {
  80. $_token = '';
  81. $_data = (new MpConfModel())->getDataByMpId($mp_id);
  82. if (!empty($_data['ext_info']['Token'])) {
  83. $_token = $_data['ext_info']['Token'];
  84. }
  85. $_signature = $_GET["signature"];
  86. $_timestamp = $_GET["timestamp"];
  87. $_nonce = $_GET["nonce"];
  88. $_echo_str = $_GET["echostr"];
  89. $_tmp_arr = array($_token, $_timestamp, $_nonce);
  90. sort($_tmp_arr, SORT_STRING);
  91. $_tmp_str = implode($_tmp_arr);
  92. $_tmp_str = sha1($_tmp_str);
  93. if ($_tmp_str == $_signature) {
  94. die($_echo_str);
  95. } else {
  96. die('fail');
  97. }
  98. }
  99. /**
  100. * 接收公众号客服消息
  101. * 【域名】/oa/msg/:conf_id
  102. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453
  103. *
  104. * @param string $mp_id 微信ID
  105. *
  106. * @return bool|string
  107. */
  108. public function receiveOaMsg($mp_id = '') {
  109. if (!empty($_GET["echostr"])) {
  110. $this->checkOaToken($mp_id);
  111. }
  112. $_param_xml = $this->request->getContent();
  113. $_param = \huolib\tool\Xml::xmlToArr($_param_xml);
  114. if (empty($_param) || empty($mp_id)) {
  115. $this->error('参数错误');
  116. }
  117. Log::write($_param, 'wechat', true);
  118. $_msg_type = $_param['MsgType'];
  119. switch ($_msg_type) {
  120. case 'event':
  121. (new OaMessage())->receiveEventMsg($mp_id, $_param);
  122. break;
  123. default:
  124. (new OaMessage())->receive($mp_id, $_param);
  125. }
  126. }
  127. }