NewOpenidNotice2Job.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Fpdx\ActivityModel;
  4. use App\Models\Fpdx\PairModel;
  5. use App\Models\User\AuthKey;
  6. use App\Services\QQMiniApp\Template;
  7. use Carbon\Carbon;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use App\Models\Common\FormidModel;
  14. use App\Models\Common\TokenModel;
  15. use App\Models\User\UserModel;
  16. use App\Services\Log\NoticeLogService;
  17. use Ixudra\Curl\Facades\Curl;
  18. /**
  19. * 新用户通知第2天
  20. * Class NewOpenidNotice2Job
  21. * @package App\Jobs
  22. */
  23. class NewOpenidNotice2Job implements ShouldQueue
  24. {
  25. use Dispatchable;
  26. use InteractsWithQueue;
  27. use Queueable;
  28. use SerializesModels;
  29. private $openid;
  30. /**
  31. * @var int 当前执行次数
  32. */
  33. private $execCnt;
  34. /**
  35. * Create a new job instance.
  36. *
  37. * @param string $openid
  38. * @param int $execCnt
  39. */
  40. public function __construct(string $openid, int $execCnt = 0)
  41. {
  42. $this->openid = $openid;
  43. $this->execCnt = $execCnt;
  44. }
  45. /**
  46. * Execute the job.
  47. *
  48. * @return void
  49. */
  50. public function handle()
  51. {
  52. if ($this->sendWx($this->openid) || $this->sendQQ($this->openid)) {
  53. $this->dispatchNotice3Job($this->openid);
  54. } else {
  55. if ($this->execCnt < 3) {
  56. NewOpenidNotice2Job::dispatch($this->openid, $this->execCnt + 1)->delay(Carbon::now()->addDay(1));
  57. }
  58. }
  59. }
  60. /**
  61. * 分发通知到第3天
  62. * @param string $openid
  63. * @return bool
  64. */
  65. public function dispatchNotice3Job(string $openid)
  66. {
  67. try {
  68. $auth = AuthKey::where("auth_key", $openid)->firstOrFail();
  69. $user = UserModel::findOrFail($auth->uid);
  70. if (PairModel::where('uid', $user->uid)->exists() || $user->partner_id > 0) {
  71. return false;
  72. }
  73. } catch (\Exception $exception) {
  74. }
  75. $now = Carbon::now();
  76. if (time() < mktime(9, 0, 0)) {
  77. $now->addHours(9);
  78. }
  79. if (time() > mktime(23, 0, 0)) {
  80. $now->addHours(-1);
  81. }
  82. NewOpenidNotice3Job::dispatch($openid)->delay($now->addDay(1));
  83. }
  84. /**
  85. * 发送微信通知
  86. * @param string $openid
  87. * @return bool
  88. */
  89. public function sendWx(string $openid)
  90. {
  91. $noticeLog = new NoticeLogService();
  92. $title = '新用户引导隔1日通知';
  93. $uuid = uuid();
  94. try {
  95. $public_id = config('miniprogram.public_id');
  96. $tm = time() - 86400 * 6 + 43200;
  97. $form = FormidModel::where(array(
  98. ['openid', $openid],
  99. ['public_id', $public_id],
  100. ['state', 0],
  101. ['created_at', '>', $tm]
  102. ))->orderBy('id', 'asc')->firstOrFail();
  103. $stage_id = ActivityModel::getActivitys()['next'];
  104. if ($form->uid > 0) {
  105. $user = UserModel::findOrFail($form->uid);
  106. if ($user->login_at > mktime(0, 0, 0)) {
  107. return true;
  108. }
  109. if (PairModel::where('uid', $user->uid)->exists() || $user->partner_id > 0) {
  110. return true;
  111. }
  112. }
  113. $cnt = PairModel::where('stage_id', $stage_id)->count();
  114. $templates = array(
  115. [
  116. 'template' => 'vdZmmmUmHMyFLjV3CTvRANavzruks5fm5ixYegRfZ1U',
  117. 'data' => [
  118. 'keyword1' => array(
  119. 'value' => "报名未生效通知"
  120. ),
  121. 'keyword2' => array(
  122. 'value' => "当前报名人数已达{$cnt},完善报名信息,生效后才可以参与匹配心动的人哦"
  123. ),
  124. 'keyword3' => array(
  125. 'value' => date("Y-m-d")
  126. )
  127. ]
  128. ],
  129. [
  130. 'template' => 'hc6Av4hGLXLzcVgc4JkDNmrYJ18rztBPU_NCfYUNy7U',
  131. 'data' => [
  132. 'keyword1' => array(
  133. 'value' => "报名未生效通知"
  134. ),
  135. 'keyword2' => array(
  136. 'value' => ""
  137. ),
  138. 'keyword3' => array(
  139. 'value' => "当前报名人数已达{$cnt},完善报名信息,生效后才可以参与匹配心动的人哦"
  140. ),
  141. 'keyword4' => array(
  142. 'value' => date("Y-m-d")
  143. )
  144. ]
  145. ],
  146. );
  147. $template = $templates[rand(0, count($templates) - 1)];
  148. $page = "pages/starter/starter?launch_type=fpdx" . "&log_type=notice&log_id={$uuid}";
  149. $access_token = TokenModel::getToken($public_id);
  150. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$access_token}";
  151. $data = [
  152. 'touser' => $form->openid,
  153. 'template_id' => $template['template'],
  154. 'page' => $page,
  155. 'form_id' => $form->form_id,
  156. 'data' => $template['data']
  157. ];
  158. $result = Curl::to($url)->withData($data)->asJson()->post();
  159. $form->state = 1;
  160. $form->save();
  161. if (isset($result->errcode) && $result->errcode) {
  162. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 0, $template['data']);
  163. return false;
  164. } else {
  165. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 1, $template['data']);
  166. return true;
  167. }
  168. } catch (\Exception $exception) {
  169. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 0);
  170. return false;
  171. }
  172. }
  173. /**
  174. * 发送QQ通知
  175. * @param string $openid
  176. * @return bool
  177. */
  178. public function sendQQ(string $openid)
  179. {
  180. try {
  181. $stage_id = ActivityModel::getActivitys()['next'];
  182. $auth = AuthKey::where('auth_key', $openid)->first();
  183. if (collect($auth)->isEmpty()) {
  184. } else {
  185. try {
  186. $user = UserModel::findOrFail($auth->uid);
  187. if ($user->login_at > mktime(0, 0, 0)) {
  188. return true;
  189. }
  190. if (PairModel::where('uid', $user->uid)->exists() || $user->partner_id > 0) {
  191. return true;
  192. }
  193. } catch (\Exception $exception) {
  194. }
  195. }
  196. $cnt = PairModel::where('stage_id', $stage_id)->count();
  197. $templates = array(
  198. [
  199. 'template_id' => '3cbfba30266dc0722475249d37a5c884',
  200. 'data' => [
  201. 'keyword1' => array(
  202. 'value' => "报名未生效通知"
  203. ),
  204. 'keyword2' => array(
  205. 'value' => "72小时恋爱体验"
  206. ),
  207. 'keyword3' => array(
  208. 'value' => "当前报名人数已达{$cnt},完善报名信息,生效后才可以参与匹配心动的人哦"
  209. ),
  210. 'keyword4' => array(
  211. 'value' => date("Y-m-d")
  212. )
  213. ]
  214. ]
  215. );
  216. $template = $templates[rand(0, count($templates) - 1)];
  217. $title = '新用户引导隔1日通知';
  218. $page = "fpdx/pages/starter/starter";
  219. $app = new Template(config("qqprogram.app_id"));
  220. $app->setTitle($title)
  221. ->setUuid(uuid())
  222. ->setParameters($template['data'])
  223. ->setTemplateId($template['template_id'])
  224. ->toUser($openid)
  225. ->setPage($page)
  226. ->send();
  227. if (0 == $app->getCode()) {
  228. return true;
  229. }
  230. return false;
  231. } catch (\Exception $exception) {
  232. return false;
  233. }
  234. }
  235. }