UserSubscribe.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace crmeb\subscribes;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreOrderCartInfo;
  5. use app\models\store\StoreProduct;
  6. use app\models\user\User;
  7. use app\models\user\UserNotice;
  8. use app\models\user\WechatUser;
  9. use think\facade\Cookie;
  10. use app\admin\model\system\SystemAttachment;
  11. use app\models\user\UserLevel;
  12. /**
  13. * 用户事件
  14. * Class UserSubscribe
  15. * @package crmeb\subscribes
  16. */
  17. class UserSubscribe
  18. {
  19. public function handle()
  20. {
  21. }
  22. /**
  23. * 管理员后台给用户添加金额
  24. * @param $event
  25. */
  26. public function onAdminAddMoney($event)
  27. {
  28. list($user, $money) = $event;
  29. //$user 用户信息
  30. //$money 添加的金额
  31. }
  32. /**
  33. * 微信授权成功后
  34. * @param $event
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. */
  39. public function onWechatOauthAfter($event)
  40. {
  41. list($openid, $wechatInfo, $spreadId, $login_type) = $event;
  42. if (!User::be(['uid' => $spreadId])) $spreadId = 0;
  43. $wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
  44. Cookie::set('is_login', 1);
  45. if (isset($wechatInfo['unionid']) && $wechatInfo['unionid'] != '' && ($uid = WechatUser::where('unionid', $wechatInfo['unionid'])->where('user_type', '<>', 'h5')->value('uid'))) {
  46. WechatUser::edit($wechatInfo, $uid, 'uid');
  47. if (!User::be(['uid' => $uid])) {
  48. $wechatInfo = WechatUser::where('uid', $uid)->find();
  49. User::setWechatUser($wechatInfo, $spreadId);
  50. } else {
  51. if ($login_type) $wechatInfo['login_type'] = $login_type;
  52. User::updateWechatUser($wechatInfo, $uid);
  53. }
  54. } else if ($uid = WechatUser::where(['openid' => $wechatInfo['openid']])->where('user_type', '<>', 'h5')->value('uid')) {
  55. WechatUser::edit($wechatInfo, $uid, 'uid');
  56. if ($login_type) $wechatInfo['login_type'] = $login_type;
  57. User::updateWechatUser($wechatInfo, $uid);
  58. } else {
  59. if (isset($wechatInfo['subscribe_scene'])) unset($wechatInfo['subscribe_scene']);
  60. if (isset($wechatInfo['qr_scene'])) unset($wechatInfo['qr_scene']);
  61. if (isset($wechatInfo['qr_scene_str'])) unset($wechatInfo['qr_scene_str']);
  62. // $isLogin = request()->isLogin();
  63. // $bind = false;
  64. // if($isLogin){
  65. // $loginUid = request()->user();
  66. // $isUser = $loginUid ? request()->tokenData()->type === 'user' : false;
  67. // $bind = $loginUid && $isUser && !$loginUid->openid && !User::be(['openid' => $wechatInfo['openid']]);
  68. // //微信用户绑定 h5用户
  69. // if ($bind) {
  70. // $wechatInfo['uid'] = $loginUid->uid;
  71. // };
  72. // }
  73. $wechatInfo = WechatUser::create($wechatInfo);
  74. // if ($isLogin && $bind)
  75. // User::where('uid', $wechatInfo['uid'])
  76. // ->limit(1)->update(['openid' => $wechatInfo['openid']]);
  77. // else
  78. User::setWechatUser($wechatInfo, $spreadId);
  79. }
  80. $uid = WechatUser::openidToUid($openid, 'openid');
  81. // 设置推广关系
  82. User::setSpread($spreadId, $uid);
  83. User::where('uid', $uid)
  84. ->limit(1)->update(['last_time' => time(), 'last_ip' => app('request')->ip()]);
  85. }
  86. /**
  87. * 用户访问记录
  88. * @param $event
  89. */
  90. public function onInitLogin($event)
  91. {
  92. list($userInfo) = $event;
  93. $request = app('request');
  94. User::edit(['last_time' => time(), 'last_ip' => $request->ip()], $userInfo->uid, 'uid');
  95. }
  96. /**
  97. * 检查是否能成为会员
  98. * @param $event
  99. */
  100. public function onUserLevelAfter($event)
  101. {
  102. list($userUid) = $event;
  103. UserLevel::setLevelComplete($userUid);
  104. }
  105. /**
  106. * 发货通知
  107. * @param $event
  108. * @return void
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. public function onDeliverGoodsNews($event)
  114. {
  115. $order = StoreOrder::where('id', $event)->find();
  116. $orderInfo = StoreOrderCartInfo::where('oid', $event)->find();
  117. $product = StoreProduct::where('id', $orderInfo['product_id'])->value('store_name');
  118. UserNotice::create(['uid' => $order['uid'], 'type' => 4, 'title' => '发货', 'content' => '你够购买的商品'.$product.'在'.date('Y-m-d H:i:s', time()).'已成功发货', 'add_time' => time()]); // 通知消息
  119. }
  120. /**
  121. * 投注通知
  122. * @param $event
  123. * @return void
  124. */
  125. public function onMany($event)
  126. {
  127. UserNotice::create(['uid' => $event['uid'], 'type' => 5, 'title' => '打怪', 'content' => '你于'.date('Y-m-d H:i:s', time()).'成功参与'.$event['many']['name'].'场次,第'.$event['many']['stage'].'期,打怪'.$event['data']['price'].'成功,快去邀请好友一起来玩吧', 'add_time' => time()]); // 通知消息
  128. }
  129. }