WelcomeJob.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Common\FormidModel;
  4. use App\Models\Common\TokenModel;
  5. use App\Models\Fpdx\PairModel;
  6. use App\Models\Log\FpdxMiniprogramIntoQueueModel;
  7. use App\Models\User\AuthKey;
  8. use App\Models\User\OpenidUserModel;
  9. use App\Models\User\UserModel;
  10. use App\Services\Log\NoticeLogService;
  11. use App\Services\QQMiniApp\Template;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. use Illuminate\Contracts\Queue\ShouldQueue;
  16. use Illuminate\Foundation\Bus\Dispatchable;
  17. use Illuminate\Support\Carbon;
  18. use Ixudra\Curl\Facades\Curl;
  19. class WelcomeJob implements ShouldQueue
  20. {
  21. use Dispatchable;
  22. use InteractsWithQueue;
  23. use Queueable;
  24. use SerializesModels;
  25. /**
  26. * @var string 用户身份
  27. */
  28. private $openid;
  29. /**
  30. * @var int 当前执行次数
  31. */
  32. private $execCnt;
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @param string $openid
  37. * @param int $execCnt
  38. */
  39. public function __construct(string $openid, int $execCnt = 0)
  40. {
  41. $this->openid = $openid;
  42. $this->execCnt = $execCnt;
  43. }
  44. /**
  45. * Execute the job.
  46. *
  47. * @return \Illuminate\Foundation\Bus\PendingDispatch
  48. */
  49. public function handle()
  50. {
  51. $openid = $this->openid;
  52. if ($this->sendQQ($openid) || $this->sendWx($openid)) {
  53. return $this->dispatchNotice2Job($openid);
  54. } else {
  55. if ($this->execCnt < 3) {
  56. return WelcomeJob::dispatch($openid, $this->execCnt + 1)->delay(Carbon::now()->addDay(1));
  57. }
  58. }
  59. }
  60. public function dispatchNotice2Job(string $openid)
  61. {
  62. try {
  63. $auth = AuthKey::where("auth_key", $openid)->firstOrFail();
  64. $user = UserModel::findOrFail($auth->uid);
  65. if (PairModel::where('uid', $user->uid)->exists() || $user->partner_id > 0) {
  66. return false;
  67. }
  68. } catch (\Exception $exception) {
  69. }
  70. $now = Carbon::now();
  71. if (time() < mktime(9, 0, 0)) {
  72. $now->addHours(9);
  73. }
  74. if (time() > mktime(23, 0, 0)) {
  75. $now->addHours(-1);
  76. }
  77. NewOpenidNotice2Job::dispatch($openid)->delay($now->addDay(1));
  78. }
  79. public function sendWx($openid): bool
  80. {
  81. $noticeLog = new NoticeLogService();
  82. $title = '新用户引导相互心动通知';
  83. $uuid = uuid();
  84. try {
  85. $public_id = config('miniprogram.public_id');
  86. $tm = time() - 86400 * 6 + 43200;
  87. $form = FormidModel::where(array(
  88. ['openid', $openid],
  89. ['public_id', $public_id],
  90. ['state', 0],
  91. ['created_at', '>', $tm]
  92. ))->orderBy('id', 'asc')->firstOrFail();
  93. $nickname = "萌萌哒小象";
  94. if ($form->uid > 0) {
  95. try {
  96. $user = UserModel::findOrFail($form->uid);
  97. if (2 == $user->sex) {
  98. $nickname = "爱睡觉的小象";
  99. }
  100. } catch (\Exception $exception) {
  101. try {
  102. $openuser = OpenidUserModel::where('openid', $openid)->firstOrFail();
  103. if (2 == $openuser->sex) {
  104. $nickname = "爱睡觉的小象";
  105. }
  106. } catch (\Exception $exception) {
  107. }
  108. }
  109. } else {
  110. try {
  111. $openuser = OpenidUserModel::where('openid', $openid)->firstOrFail();
  112. if (2 == $openuser->sex) {
  113. $nickname = "爱睡觉的小象";
  114. }
  115. } catch (\Exception $exception) {
  116. }
  117. }
  118. $contents = [
  119. "💕真开心!你心动的对象「{$nickname}」也心动了你",
  120. "💕你和「{$nickname}」相互心动了呢!快去看看吧"
  121. ];
  122. $content = $contents[mt_rand(0, 1)];
  123. $templates = array(
  124. [
  125. 'template' => 'lNEemth7Kml5ZmOvBcbo842h3whkYMM2c8d2YJOQlEU',
  126. 'data' => [
  127. 'keyword1' => array(
  128. 'value' => "你的心动通知"
  129. ),
  130. 'keyword2' => array(
  131. 'value' => "{$nickname}&你相互心动啦"
  132. ),
  133. 'keyword3' => array(
  134. 'value' => $content
  135. )
  136. ]
  137. ],
  138. [
  139. 'template' => 'Xcp8AnnJr-cJmDMcr9WyIQmKgpN4qu6q6lStS0ZAiqs',
  140. 'data' => [
  141. 'keyword1' => array(
  142. 'value' => "你的心动通知"
  143. ),
  144. 'keyword2' => array(
  145. 'value' => "{$nickname}&你相互心动啦"
  146. ),
  147. 'keyword3' => array(
  148. 'value' => $content
  149. )
  150. ]
  151. ],
  152. [
  153. 'template' => 'fsptIIrBoCB6ftxT_6-ApQeqGoE_rJsC5VBDPUbdScg',
  154. 'data' => [
  155. 'keyword1' => array(
  156. 'value' => "你的心动通知"
  157. ),
  158. 'keyword2' => array(
  159. 'value' => "{$nickname}&你相互心动啦"
  160. ),
  161. 'keyword3' => array(
  162. 'value' => $content
  163. )
  164. ]
  165. ],
  166. );
  167. $template = $templates[rand(0, count($templates) - 1)];
  168. $page = "pages/starter/starter?launch_type=free&url=%2Fpages%2Ffriend%2Ffriend&log_type=notice&log_id={$uuid}";
  169. $access_token = TokenModel::getToken($public_id);
  170. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$access_token}";
  171. $data = [
  172. 'touser' => $form->openid,
  173. 'template_id' => $template['template'],
  174. 'page' => $page,
  175. 'form_id' => $form->form_id,
  176. 'data' => $template['data']
  177. ];
  178. $result = Curl::to($url)->withData($data)->asJson()->post();
  179. $form->state = 1;
  180. $form->save();
  181. if (isset($result->errcode) && $result->errcode) {
  182. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 0, $template['data']);
  183. return false;
  184. } else {
  185. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 1, $template['data']);
  186. return true;
  187. }
  188. } catch (\Exception $exception) {
  189. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 0);
  190. }
  191. return false;
  192. }
  193. public function sendQQ($openid): bool
  194. {
  195. try {
  196. $nickname = "萌萌哒小象";
  197. $auth = AuthKey::where('auth_key', $openid)->first();
  198. if (collect($auth)->isEmpty()) {
  199. try {
  200. $openuser = OpenidUserModel::where('openid', $openid)->firstOrFail();
  201. if (2 == $openuser->sex) {
  202. $nickname = "爱睡觉的小象";
  203. }
  204. } catch (\Exception $exception) {
  205. }
  206. } else {
  207. $user = UserModel::findOrFail($auth->uid);
  208. if (2 == $user->sex) {
  209. $nickname = "爱睡觉的小象";
  210. }
  211. }
  212. $contents = [
  213. "💕真开心!你心动的对象「{$nickname}」也心动了你",
  214. "💕你和「{$nickname}」相互心动了呢!快去看看吧"
  215. ];
  216. $content = $contents[mt_rand(0, 1)];
  217. $templates = array(
  218. [
  219. 'template_id' => 'f8c20ac9f919e9c480e957f4576048c7',
  220. 'data' => [
  221. 'keyword1' => array(
  222. 'value' => "你的心动通知"
  223. ),
  224. 'keyword2' => array(
  225. 'value' => "{$nickname}&你相互心动啦"
  226. ),
  227. 'keyword3' => array(
  228. 'value' => $content
  229. )
  230. ]
  231. ]
  232. );
  233. $template = $templates[rand(0, count($templates) - 1)];
  234. $title = '新用户引导相互心动通知';
  235. $page = "pages/friend/friend";
  236. $app = new Template(config("qqprogram.app_id"));
  237. $app->setTitle($title)
  238. ->setUuid(uuid())
  239. ->setParameters($template['data'])
  240. ->setTemplateId($template['template_id'])
  241. ->toUser($openid)
  242. ->setPage($page)
  243. ->send();
  244. if (0 == $app->getCode()) {
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. } catch (\Exception $exception) {
  250. return false;
  251. }
  252. }
  253. }