QappUserService.php 5.6 KB

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