NewOpenidNotice3Job.php 8.9 KB

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