Subscribe.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 通用模板消息
  7. */
  8. namespace app\common\widget;
  9. use app\common\facade\WechatProgram;
  10. use app\common\model\MemberWechatTpl;
  11. use app\common\model\MemberMiniapp;
  12. use app\common\model\MemberSubscribeQueue;
  13. use app\common\model\User;
  14. use Exception;
  15. class Subscribe{
  16. /**
  17. * 订阅通知
  18. * @param int $uid 用户ID
  19. * @param int $miniapp_id 小程序ID
  20. * @param array $param
  21. * $param['title'] = 标题;
  22. * $param['content'] = 内容;
  23. * $param['state'] = 状态;
  24. * @return void
  25. */
  26. public function sms(int $uid,int $miniapp_id,array $param){
  27. if(empty($uid) || empty($param['content'])){
  28. return;
  29. }
  30. return MemberSubscribeQueue::create(['uid' => $uid,'member_miniapp_id' => $miniapp_id,'param' => json_encode($param),'is_send' => 0,'create_time' => time()]);
  31. }
  32. /**
  33. * 群发队列服务
  34. */
  35. public function subscribeQueue(){
  36. $info = MemberSubscribeQueue::where(['is_send' => 0])->find();
  37. if(empty($info)){
  38. return;
  39. }
  40. $miniapp_id = $info->member_miniapp_id;
  41. $uid = $info->uid;
  42. $param = json_decode($info->param,true);
  43. $content = $param['content']; //内容
  44. $state = empty($param['state']) ? '待审' : $param['state']; //状态
  45. $url = empty($param['url']) ? 'pages/index/index' : $param['url']; //访问地址
  46. $miniapp = MemberMiniapp::where(['id' => $miniapp_id])->field('id,mp_appid,miniapp_appid')->find();
  47. if(empty($miniapp)){
  48. return;
  49. }
  50. $user = User::where(['id' => $uid])->field('miniapp_uid')->find();
  51. if (empty($user->miniapp_uid)) {
  52. MemberSubscribeQueue::where(['id' => $info->id])->update(['is_send' => 1]);
  53. return;
  54. }
  55. //用户类别
  56. $wechat = WechatProgram::isTypes($miniapp->id);
  57. if (empty($wechat)) {
  58. return;
  59. }
  60. $setting = MemberWechatTpl::getConfig($miniapp->id);
  61. if (empty($setting)) {
  62. return;
  63. }
  64. //订阅消息
  65. if(!empty($miniapp->mp_appid) || !empty($setting->tplmsg_common_wechat)){
  66. try {
  67. $rel = $wechat->subscribe_message->send([
  68. 'touser' => $user->miniapp_uid,
  69. 'template_id' => $setting->tplmsg_common_app,
  70. 'page' => $url,
  71. 'data' => [
  72. 'thing01' => [
  73. 'value' => $content
  74. ],
  75. 'phrase01' => [
  76. 'value' => $state
  77. ],
  78. 'time01' => [
  79. 'value' => date('Y-m-d H:i')
  80. ],
  81. ],
  82. ]);
  83. if($rel['errcode'] == 0){
  84. MemberSubscribeQueue::where(['id' => $info->id])->update(['is_send' => 1]);
  85. return true;
  86. }
  87. }catch (Exception $e) {
  88. return;
  89. }
  90. }
  91. }
  92. }