WechatMessageServices.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\wechat;
  12. use app\dao\wechat\WechatMessageDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\ApiException;
  15. use crmeb\services\CacheService;
  16. class WechatMessageServices extends BaseServices
  17. {
  18. /**
  19. * 构造方法
  20. * WechatMessageServices constructor.
  21. * @param WechatMessageDao $dao
  22. */
  23. public function __construct(WechatMessageDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * @param $result
  29. * @param $openid
  30. * @param $type
  31. * @return \think\Model
  32. */
  33. public function setMessage($result, $openid, $type)
  34. {
  35. if (is_object($result) || is_array($result)) $result = json_encode($result);
  36. $add_time = time();
  37. $data = compact('result', 'openid', 'type', 'add_time');
  38. return $this->dao->save($data);
  39. }
  40. public function setOnceMessage($result, $openid, $type, $unique, $cacheTime = 172800)
  41. {
  42. $cacheName = 'wechat_message_' . $type . '_' . $unique;
  43. if (CacheService::has($cacheName)) return true;
  44. $res = $this->setMessage($result, $openid, $type);
  45. if ($res) CacheService::set($cacheName, 1, $cacheTime);
  46. return $res;
  47. }
  48. /**
  49. * 微信消息前置操作
  50. * @param $message
  51. * @return bool
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function wechatMessageBefore($message)
  57. {
  58. //后台开启,用户直接关注公众号才会生成用户
  59. if (intval(sys_config('create_wechat_user', 0))) {
  60. /** @var WechatUserServices $wechatUser */
  61. $wechatUser = app()->make(WechatUserServices::class);
  62. $wechatUser->saveUser($message->FromUserName);
  63. }
  64. $event = isset($message->Event) ?
  65. $message->MsgType . (
  66. $message->Event == 'subscribe' && isset($message->EventKey) ? '_scan' : ''
  67. ) . '_' . $message->Event : $message->MsgType;
  68. $result = json_encode($message);
  69. $openid = $message->FromUserName;
  70. $type = strtolower($event);
  71. $add_time = time();
  72. if (!$this->dao->save(compact('result', 'openid', 'type', 'add_time'))) {
  73. throw new ApiException(410080);
  74. }
  75. return true;
  76. }
  77. }