QappUserService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. */
  45. public function bindPhone(int $uid, string $phone)
  46. {
  47. $qapp_user = QappUser::where('uid', $uid)->first();
  48. if ($qapp_user->phone && $qapp_user->phone != $phone) {
  49. return false;
  50. } else {
  51. try {
  52. DB::beginTransaction();
  53. $qapp_user->phone = $phone;
  54. $qapp_user->save();
  55. $reward = 100;
  56. User::where('id', $uid)->update(
  57. [
  58. 'balance' => DB::raw('balance+' . $reward),
  59. 'reward_balance' => DB::raw('reward_balance+' . $reward)
  60. ]
  61. );
  62. DB::commit();
  63. } catch (Exception $e) {
  64. myLog('bindPhone')->error($e->getMessage());
  65. }
  66. return true;
  67. }
  68. }
  69. public function setGolableUser(int $uid)
  70. {
  71. $user_info = $this->getQAppUserByUid($uid);
  72. $qapp_user = app()->make('qapp_user');
  73. $qapp_user->id = $user_info->id;
  74. $qapp_user->uid = $user_info->uid;
  75. $qapp_user->send_order_id = $user_info->send_order_id;
  76. $qapp_user->device_no = $user_info->device_no;
  77. $qapp_user->device_info = $user_info->device_info;
  78. $qapp_user->phone = $user_info->phone;
  79. $qapp_user->user = $user_info->user;
  80. $qapp_user->app_pay_merchat_id = $user_info->app_pay_merchat_id;
  81. $qapp_user->h5_pay_merchat_id = $user_info->h5_pay_merchat_id;
  82. $qapp_user->ali_pay_merchat_id = $user_info->ali_pay_merchat_id;
  83. }
  84. /**
  85. * 根据设备号获取快应用用户信息
  86. */
  87. public function getQAppUserByDeviceNo(string $device_no, int $channel_id)
  88. {
  89. $qapp_user = QappUser::where('device_no', $device_no)->where('channel_id', $channel_id)->first();
  90. if ($qapp_user) {
  91. $user = User::find($qapp_user->uid);
  92. $qapp_user->user = $user;
  93. }
  94. return $qapp_user;
  95. }
  96. /**
  97. * 根据uid获取快应用用户信息
  98. */
  99. public function getQAppUserByUid(int $uid)
  100. {
  101. $qapp_user = QappUser::where('uid', $uid)->first();
  102. if ($qapp_user) {
  103. $user = User::find($uid);
  104. $qapp_user->user = $user;
  105. $qapp_user->send_order_id = UserService::getUserSendOrder($uid);
  106. $package_info = QappPackage::where('channel_id', $user->distribution_channel_id)->first();
  107. $qapp_user->app_pay_merchat_id = $package_info->app_pay_merchat_id;
  108. $qapp_user->h5_pay_merchat_id = $package_info->h5_pay_merchat_id;
  109. $qapp_user->ali_pay_merchat_id = $package_info->ali_pay_merchat_id;
  110. }
  111. return $qapp_user;
  112. }
  113. /**
  114. * 创建快应用用户信息
  115. */
  116. public function createQuickAppUser(array $data)
  117. {
  118. try {
  119. DB::beginTransaction();
  120. $user = $this->createUser($data);
  121. $channel_id = $user->distribution_channel_id;
  122. $qapp_user = QappUser::firstOrCreate([
  123. 'device_no' => $data['device_no'],
  124. 'channel_id' => $channel_id,
  125. ], [
  126. 'androidid' => $data['androidid'],
  127. 'mac' => $data['mac'],
  128. 'uid' => $user->id,
  129. 'device_info' => $data['device_info'],
  130. ]);
  131. $qapp_user->user = $user;
  132. DB::commit();
  133. $job = new QappTikTokUser($data['device_no'], $data['mac'], $channel_id, $user->id, $user->created_at);
  134. dispatch($job->onConnection('rabbitmq')->onQueue('qapp_tiktok_user_register_queue'));
  135. return $qapp_user;
  136. } catch (Exception $e) {
  137. myLog('create_user')->error($e->getMessage());
  138. }
  139. }
  140. private function findChannelId(string $package)
  141. {
  142. $channel_id = env('QUICKAPP_SITE');
  143. if ($package) {
  144. $package_info = QappPackage::where('package', $package)->first();
  145. $channel_id = $package_info ? $package_info->channel_id : $channel_id;
  146. }
  147. return $channel_id;
  148. }
  149. /**
  150. * 创建用户
  151. */
  152. private function createUser(array $data)
  153. {
  154. $openid = $data['device_no'];
  155. $unionid = $data['device_no'];
  156. $register_ip = _getIp();
  157. $distribution_channel_id = $this->findChannelId($data['package']);
  158. $send_order_id = $data['send_order_id'];
  159. $unique_key = compact('unionid', 'distribution_channel_id');
  160. $data = compact('openid', 'register_ip', 'send_order_id');
  161. return User::firstOrCreate($unique_key, $data);
  162. }
  163. }