MockThumbJob.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace App\Jobs;
  3. use App\Http\Controllers\Wechat\Kfaccount;
  4. use App\Models\Common\FormidModel;
  5. use App\Models\Common\TokenModel;
  6. use App\Models\PraiseModel;
  7. use App\Models\User\AuthKey;
  8. use App\Models\User\MockThumbModel;
  9. use App\Models\User\Openid;
  10. use App\Models\User\OpenidUserModel;
  11. use App\Models\User\UserModel;
  12. use App\Services\Log\NoticeLogService;
  13. use App\Services\NoticeService\Channels\WeChatTemplateChannel;
  14. use Carbon\Carbon;
  15. use Illuminate\Bus\Queueable;
  16. use Illuminate\Database\Eloquent\ModelNotFoundException;
  17. use Illuminate\Queue\SerializesModels;
  18. use Illuminate\Queue\InteractsWithQueue;
  19. use Illuminate\Contracts\Queue\ShouldQueue;
  20. use Illuminate\Foundation\Bus\Dispatchable;
  21. use Ixudra\Curl\Facades\Curl;
  22. /**
  23. * Class MockThumbJob
  24. * @package App\Jobs
  25. */
  26. class MockThumbJob implements ShouldQueue
  27. {
  28. use Dispatchable;
  29. use InteractsWithQueue;
  30. use Queueable;
  31. use SerializesModels;
  32. /**
  33. * @var mixed 用户id|openid
  34. */
  35. private $uidopenid;
  36. private $days = 1;
  37. private $isTiming = false;
  38. public const BOY = [1001, 1003, 1005, 1007, 1009, 1011];
  39. public const GIRL = [1002, 1004, 1006, 1008, 1010, 1012];
  40. /**
  41. * Create a new job instance.
  42. * 登录用户模拟点赞
  43. *
  44. * @param mixed $uidopenid
  45. * @param int $days
  46. * @param int $isTiming mock途径 0:进入mock;1:定时mock(关注公众号);2:定时mock(未关注)
  47. */
  48. public function __construct($uidopenid, int $days = 1, int $isTiming = 0)
  49. {
  50. $this->uidopenid = $uidopenid;
  51. $this->days = $days;
  52. $this->isTiming = $isTiming;
  53. }
  54. /**
  55. * Execute the job.
  56. *
  57. * @return void
  58. */
  59. public function handle()
  60. {
  61. try {
  62. if (is_int($this->uidopenid)) {
  63. /** @var UserModel $user */
  64. $user = UserModel::findOrFail($this->uidopenid);
  65. if (0 == $user->partner_id) {
  66. if (1 == $this->days && MockThumbModel::where('thumb_user', $user->uid)->exists()) {
  67. return;
  68. } else {
  69. $whereuids = self::GIRL;
  70. 2 == $user->sex && $whereuids = self::BOY;
  71. $mockedUids = MockThumbModel::where('thumb_user', $user->uid)->whereIn(
  72. 'uid',
  73. $whereuids
  74. )->get(['uid'])->pluck('uid')->toArray();
  75. $uid = self::GIRL[rand(0, count(array_diff(self::GIRL, $mockedUids)) - 1)];
  76. 2 == $user->sex && $uid = self::BOY[rand(0, count(array_diff(self::BOY, $mockedUids)) - 1)];
  77. MockThumbModel::create([
  78. 'uid' => $uid,
  79. 'thumb_user' => $user->uid,
  80. 'is_timing' => intval($this->isTiming)
  81. ]);
  82. $this->thumbMe($user->uid);
  83. }
  84. } else {
  85. if (
  86. PraiseModel::where([['partner_id', $user->partner_id], ['type', 1]])->whereNotIn(
  87. 'uid',
  88. [1, 2]
  89. )->exists()
  90. ) {
  91. return;
  92. } else {
  93. $whereuids = self::GIRL;
  94. 2 == $user->sex && $whereuids = self::BOY;
  95. $mockedUids = PraiseModel::where('partner_id', $user->partner_id)->whereIn(
  96. 'uid',
  97. $whereuids
  98. )->get(['uid'])->pluck('uid')->toArray();
  99. $uid = self::GIRL[rand(0, count(array_diff(self::GIRL, $mockedUids)) - 1)];
  100. 2 == $user->sex && $uid = self::BOY[rand(0, count(array_diff(self::BOY, $mockedUids)) - 1)];
  101. PraiseModel::updateOrCreate(['uid' => $uid, 'partner_id' => $user->partner_id], [
  102. 'uid' => $uid,
  103. 'partner_id' => $user->partner_id,
  104. 'create_at' => time(),
  105. 'created_at' => 1,
  106. 'updated_at' => time(),
  107. 'type' => 1,
  108. ]);
  109. $this->thumbMe($user->uid);
  110. }
  111. }
  112. } else {
  113. if (1 == $this->days && MockThumbModel::where('thumb_user', $this->uidopenid)->exists()) {
  114. return;
  115. } else {
  116. $mockedUids = MockThumbModel::where(
  117. 'thumb_user',
  118. $this->uidopenid
  119. )->get(['uid'])->pluck('uid')->toArray();
  120. $uid = self::GIRL[rand(0, count(array_diff(self::GIRL, $mockedUids)) - 1)];
  121. try {
  122. $user = OpenidUserModel::findOrFail($this->uidopenid);
  123. 2 == $user->sex && $uid = self::BOY[rand(0, count(array_diff(self::BOY, $mockedUids)) - 1)];
  124. } catch (ModelNotFoundException $exception) {
  125. }
  126. MockThumbModel::create([
  127. 'uid' => $uid,
  128. 'thumb_user' => $this->uidopenid,
  129. 'is_timing' => intval($this->isTiming)
  130. ]);
  131. $this->thumbMe($this->uidopenid);
  132. }
  133. }
  134. } catch (\Exception $exception) {
  135. }
  136. }
  137. /**
  138. * @param int $to_user
  139. */
  140. private function thumbMe($to_user)
  141. {
  142. if (in_array($this->isTiming, [0, 1])) {
  143. $this->notice($to_user);
  144. } else {
  145. $this->noticeSms($to_user);
  146. }
  147. switch ($this->days) {
  148. case 1:
  149. $addDays = 2;
  150. break;
  151. case 3:
  152. $addDays = 1;
  153. break;
  154. default:
  155. return;
  156. }
  157. MockThumbJob::dispatch(
  158. $to_user,
  159. $this->days + $addDays,
  160. $this->isTiming
  161. )->delay(Carbon::now()->addDays($addDays));
  162. }
  163. /**
  164. * 发出通知
  165. * @param int|string $to_user
  166. * @return bool
  167. */
  168. public function notice($to_user)
  169. {
  170. if (is_int($to_user)) {
  171. $uid = $to_user;
  172. $openid = Openid::ofPublic($to_user, config("wechat.fpdx.public_id"))->value('openid');
  173. $miniopenid = AuthKey::where([
  174. ['auth_type', config("miniprogram.public_id")],
  175. ['uid', $uid]
  176. ])->value('auth_key');
  177. } else {
  178. $uid = 0;
  179. $openid = $to_user;
  180. $miniopenid = $openid;
  181. }
  182. $title = "模拟喜欢我的卡片通知";
  183. $uuid = uuid();
  184. $noticeLog = new NoticeLogService();
  185. $content = "❤️收到1个新的心动!";
  186. $remark = "";
  187. try {
  188. // 客服消息
  189. $page = "pages/starter/starter?log_type=notice&log_id={$uuid}&launch_type=free&url=%2Fpages%2Ffriend%2Ffriend";
  190. $kfcontent = "{$content}\n\n{$remark}\n\n<a data-miniprogram-appid='wx4c1722a4055bd229' data-miniprogram-path='{$page}'>去小程序查看</a>";
  191. $response = Kfaccount::text([
  192. 'touser' => $openid,
  193. 'content' => $kfcontent,
  194. 'kf_account' => 'notice@admin'
  195. ]);
  196. if ($response['code'] == 0) {
  197. $noticeLog->record($openid, $title, "公众号客服消息", $uuid, 1, $kfcontent);
  198. return true;
  199. }
  200. // fpdx模板消息
  201. $public_id = config('wechat.fpdx.public_id');
  202. $template_id = "ugxHaIxl5sFf2T00QZdCc0l4qELRUfEtw1HnCE7PDww";
  203. $page = "/pages/friend/friend";
  204. $parameters = [
  205. 'first' => [
  206. 'value' => $content . "\n"
  207. ],
  208. 'keyword1' => [
  209. 'value' => "收到一个新的心动"
  210. ],
  211. 'keyword2' => [
  212. 'value' => "心动"
  213. ],
  214. 'keyword3' => [
  215. 'value' => "心动通知"
  216. ],
  217. 'keyword4' => [
  218. 'value' => date('Y-m-d')
  219. ],
  220. 'remark' => [
  221. 'value' => $remark,
  222. "color" => "#FF7E98"
  223. ],
  224. ];
  225. $app = (new WeChatTemplateChannel())->setAppId($public_id);
  226. $app->setTitle($title)->toUser($uid)->setPage($page)
  227. ->setTemplateId($template_id)->setParameters($parameters)
  228. ->send();
  229. if (0 == $app->getCode()) {
  230. return true;
  231. }
  232. // 小程序模板消息
  233. $templates = array(
  234. [
  235. 'template' => 'lNEemth7Kml5ZmOvBcbo842h3whkYMM2c8d2YJOQlEU',
  236. 'data' => [
  237. 'keyword1' => array(
  238. 'value' => "你的心动通知"
  239. ),
  240. 'keyword2' => array(
  241. 'value' => "✉收到一个新的心动"
  242. ),
  243. 'keyword3' => array(
  244. 'value' => $remark
  245. )
  246. ]
  247. ],
  248. [
  249. 'template' => 'Xcp8AnnJr-cJmDMcr9WyIQmKgpN4qu6q6lStS0ZAiqs',
  250. 'data' => [
  251. 'keyword1' => array(
  252. 'value' => "你的心动通知"
  253. ),
  254. 'keyword2' => array(
  255. 'value' => "✉收到一个新的心动"
  256. ),
  257. 'keyword3' => array(
  258. 'value' => $remark
  259. )
  260. ]
  261. ],
  262. [
  263. 'template' => 'fsptIIrBoCB6ftxT_6-ApQeqGoE_rJsC5VBDPUbdScg',
  264. 'data' => [
  265. 'keyword1' => array(
  266. 'value' => "你的心动通知"
  267. ),
  268. 'keyword2' => array(
  269. 'value' => "✉收到一个新的心动"
  270. ),
  271. 'keyword3' => array(
  272. 'value' => $remark
  273. )
  274. ]
  275. ],
  276. );
  277. $template = $templates[rand(0, count($templates) - 1)];
  278. $page = "pages/starter/starter?launch_type=free&url=%2Fpages%2Ffriend%2Ffriend&log_type=notice&log_id={$uuid}";
  279. $public_id = config("miniprogram.public_id");
  280. $form = FormidModel::where([
  281. ['openid', $miniopenid],
  282. ['public_id', $public_id],
  283. ['state', 0],
  284. ['created_at', '>', time() - 86400 * 6.5]
  285. ])->orderBy('created_at', 'asc')->first();
  286. if (collect($form)->isEmpty()) {
  287. return false;
  288. }
  289. $access_token = TokenModel::getToken($public_id);
  290. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$access_token}";
  291. $data = [
  292. 'touser' => $form->openid,
  293. 'template_id' => $template['template'],
  294. 'page' => $page,
  295. 'form_id' => $form->form_id,
  296. 'data' => $template['data']
  297. ];
  298. $result = Curl::to($url)->withData($data)->asJson()->post();
  299. $form->state = 1;
  300. $form->save();
  301. if (isset($result->errcode) && $result->errcode) {
  302. } else {
  303. $noticeLog->record($openid, $title, "小程序模板消息", $uuid, 1, $template['data']);
  304. return true;
  305. }
  306. } catch (\Exception $exception) {
  307. dump($exception);
  308. }
  309. $noticeLog->record($openid, $title, "发送失败", $uuid);
  310. return false;
  311. }
  312. public function noticeSms($to_user)
  313. {
  314. $user = UserModel::find($to_user);
  315. if ($user->isEmpty() || empty($user->phone)) {
  316. return false;
  317. }
  318. // 发送短信
  319. }
  320. }