Wechat.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace addons\shopro\job;
  3. use think\queue\Job;
  4. /**
  5. * 微信任务
  6. */
  7. class Wechat extends BaseJob
  8. {
  9. /**
  10. * 异步分批创建新的队列任务
  11. */
  12. public function createQueueByOpenIdsArray(Job $job, $openIdsArray)
  13. {
  14. try {
  15. $count = count($openIdsArray);
  16. if ($count > 0) {
  17. $page = ceil($count / 100);
  18. for ($i = 0; $i < $page; $i++) {
  19. \think\Queue::push('\addons\shopro\job\Wechat@saveSubscribeUserInfo', array_slice($openIdsArray, $i * 100, 100), 'shopro');
  20. }
  21. }
  22. // 删除 job
  23. $job->delete();
  24. } catch (\Exception $e) {
  25. // 队列执行失败
  26. \think\Log::error('queue-' . get_class() . '-createQueueByOpenIdsArray' . ':执行失败,错误信息:' . $e->getMessage());
  27. }
  28. }
  29. /**
  30. * 保存更新关注用户
  31. */
  32. public function saveSubscribeUserInfo(Job $job, $openIdsArray)
  33. {
  34. try {
  35. $wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
  36. $result = $wechat->getSubscribeUserInfoByOpenId($openIdsArray);
  37. if (isset($result['user_info_list'])) {
  38. $userInfoList = $result['user_info_list'];
  39. $insertData = [];
  40. foreach ($userInfoList as $u) {
  41. $wechatFans = \app\admin\model\shopro\wechat\Fans::get(['openid' => $u['openid']]);
  42. if ($wechatFans) {
  43. $wechatFans->save([
  44. 'nickname' => $u['nickname'],
  45. 'headimgurl' => $u['headimgurl'],
  46. 'sex' => $u['sex'],
  47. 'country' => $u['country'],
  48. 'province' => $u['province'],
  49. 'city' => $u['city'],
  50. 'subscribe' => 1,
  51. 'subscribe_time' => $u['subscribe_time']
  52. ]);
  53. }else{
  54. $insertData[] = $u;
  55. }
  56. }
  57. if (count($insertData) > 0) {
  58. $wechatFans = new \app\admin\model\shopro\wechat\Fans;
  59. $wechatFans->allowField(true)->saveAll($insertData);
  60. }
  61. }
  62. $job->delete();
  63. } catch (\Exception $e) {
  64. // 队列执行失败
  65. \think\Log::error('queue-' . get_class() . '-saveSubscribeUserInfo' . ':执行失败,错误信息2:' . $e->getMessage() . '|' . json_encode($insertData, JSON_UNESCAPED_UNICODE));
  66. }
  67. }
  68. }