QappUserService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Modules\User\Services;
  3. use App\Jobs\QappTikTokUser;
  4. use App\Modules\User\Models\QappPackage;
  5. use App\Modules\User\Models\QappUser;
  6. use App\Modules\User\Models\User;
  7. use Exception;
  8. use Illuminate\Support\Facades\DB;
  9. use Tymon\JWTAuth\Facades\JWTAuth;
  10. /**
  11. *
  12. */
  13. class QappUserService
  14. {
  15. /**
  16. * 登录
  17. */
  18. public function login(array $data)
  19. {
  20. $device_no = $data['device_no'];
  21. $channel_id = $this->findChannelId($data['package']);
  22. $qapp_user = $this->getQAppUserByDeviceNo($device_no, $channel_id);
  23. if (!$qapp_user) {
  24. $qapp_user = $this->createQuickAppUser($data);
  25. }
  26. $user = $qapp_user->user;
  27. $uid = $user->id;
  28. $time = strtotime("+1 month");
  29. $token = JWTAuth::fromUser($user);
  30. if ($data['send_order_id']) {
  31. UserService::setUserSendOrder($uid, $data['send_order_id']);
  32. }
  33. return compact('token', 'time', 'uid');
  34. }
  35. public function getGolableUser()
  36. {
  37. $qapp_user = app()->make('qapp_user');
  38. return $qapp_user;
  39. }
  40. /**
  41. * 绑定手机号
  42. * 多个账号可以绑定一个手机号
  43. */
  44. public function bindPhone(int $uid, string $phone)
  45. {
  46. $qapp_user = QappUser::where('uid', $uid)->first();
  47. if ($qapp_user->phone && $qapp_user->phone != $phone) {
  48. return false;
  49. } else {
  50. try {
  51. DB::beginTransaction();
  52. if (!$qapp_user->phone) {
  53. $reward = 100;
  54. User::where('id', $uid)->update(
  55. [
  56. 'balance' => DB::raw('balance+' . $reward),
  57. 'reward_balance' => DB::raw('reward_balance+' . $reward)
  58. ]
  59. );
  60. }
  61. $qapp_user->phone = $phone;
  62. $qapp_user->save();
  63. DB::commit();
  64. } catch (Exception $e) {
  65. myLog('bindPhone')->error($e->getMessage());
  66. }
  67. return true;
  68. }
  69. }
  70. public function setGolableUser(int $uid)
  71. {
  72. $user_info = $this->getQAppUserByUid($uid);
  73. $qapp_user = app()->make('qapp_user');
  74. $qapp_user->id = $user_info->id;
  75. $qapp_user->uid = $user_info->uid;
  76. $qapp_user->send_order_id = $user_info->send_order_id;
  77. $qapp_user->device_no = $user_info->device_no;
  78. $qapp_user->device_info = $user_info->device_info;
  79. $qapp_user->phone = $user_info->phone;
  80. $qapp_user->user = $user_info->user;
  81. $qapp_user->app_pay_merchat_id = $user_info->app_pay_merchat_id;
  82. $qapp_user->h5_pay_merchat_id = $user_info->h5_pay_merchat_id;
  83. $qapp_user->ali_pay_merchat_id = $user_info->ali_pay_merchat_id;
  84. }
  85. /**
  86. * 根据设备号获取快应用用户信息
  87. */
  88. public function getQAppUserByDeviceNo(string $device_no, int $channel_id)
  89. {
  90. $qapp_user = QappUser::where('device_no', $device_no)->where('channel_id', $channel_id)->first();
  91. if ($qapp_user) {
  92. $user = User::find($qapp_user->uid);
  93. $qapp_user->user = $user;
  94. }
  95. return $qapp_user;
  96. }
  97. /**
  98. * 根据uid获取快应用用户信息
  99. */
  100. public function getQAppUserByUid(int $uid)
  101. {
  102. $qapp_user = QappUser::where('uid', $uid)->first();
  103. if ($qapp_user) {
  104. $user = User::find($uid);
  105. $qapp_user->user = $user;
  106. $qapp_user->send_order_id = UserService::getUserSendOrder($uid);
  107. $package_info = QappPackage::where('channel_id', $user->distribution_channel_id)->first();
  108. $qapp_user->app_pay_merchat_id = $package_info->app_pay_merchat_id;
  109. $qapp_user->h5_pay_merchat_id = $package_info->h5_pay_merchat_id;
  110. $qapp_user->ali_pay_merchat_id = $package_info->ali_pay_merchat_id;
  111. }
  112. return $qapp_user;
  113. }
  114. /**
  115. * 创建快应用用户信息
  116. */
  117. public function createQuickAppUser(array $data)
  118. {
  119. try {
  120. DB::beginTransaction();
  121. $user = $this->createUser($data);
  122. $channel_id = $user->distribution_channel_id;
  123. $qapp_user = QappUser::firstOrCreate([
  124. 'device_no' => $data['device_no'],
  125. 'channel_id' => $channel_id,
  126. ], [
  127. 'imei' => $data['imei'],
  128. 'androidid' => $data['androidid'],
  129. 'mac' => $data['mac'],
  130. 'uid' => $user->id,
  131. 'device_info' => $data['device_info'],
  132. ]);
  133. $qapp_user->user = $user;
  134. DB::commit();
  135. $job = new QappTikTokUser($user->register_ip, $data['device_no'], $data['mac'], $channel_id, $user->id, $user->created_at, true);
  136. dispatch($job->onConnection('rabbitmq')->onQueue('qapp_tiktok_user_register_queue'));
  137. return $qapp_user;
  138. } catch (Exception $e) {
  139. myLog('create_user')->error($e->getMessage());
  140. }
  141. }
  142. private function findChannelId(string $package)
  143. {
  144. $channel_id = env('QUICKAPP_SITE');
  145. if ($package) {
  146. $package_info = QappPackage::where('package', $package)->first();
  147. $channel_id = $package_info ? $package_info->channel_id : $channel_id;
  148. }
  149. return $channel_id;
  150. }
  151. /**
  152. * 创建用户
  153. */
  154. private function createUser(array $data)
  155. {
  156. $openid = $data['device_no'];
  157. $unionid = $data['device_no'];
  158. $register_ip = _getIp();
  159. $distribution_channel_id = $this->findChannelId($data['package']);
  160. $send_order_id = $data['send_order_id'];
  161. $unique_key = compact('unionid', 'distribution_channel_id');
  162. $data = compact('openid', 'register_ip', 'send_order_id');
  163. return User::firstOrCreate($unique_key, $data);
  164. }
  165. }