QappUserService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (!$qapp_user->phone) {
  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. }
  62. $qapp_user->phone = $phone;
  63. $qapp_user->save();
  64. DB::commit();
  65. } catch (Exception $e) {
  66. myLog('bindPhone')->error($e->getMessage());
  67. }
  68. return true;
  69. }
  70. }
  71. public function setGolableUser(int $uid)
  72. {
  73. $user_info = $this->getQAppUserByUid($uid);
  74. $qapp_user = app()->make('qapp_user');
  75. $qapp_user->id = $user_info->id;
  76. $qapp_user->uid = $user_info->uid;
  77. $qapp_user->send_order_id = $user_info->send_order_id;
  78. $qapp_user->device_no = $user_info->device_no;
  79. $qapp_user->device_info = $user_info->device_info;
  80. $qapp_user->phone = $user_info->phone;
  81. $qapp_user->user = $user_info->user;
  82. $qapp_user->app_pay_merchat_id = $user_info->app_pay_merchat_id;
  83. $qapp_user->h5_pay_merchat_id = $user_info->h5_pay_merchat_id;
  84. $qapp_user->ali_pay_merchat_id = $user_info->ali_pay_merchat_id;
  85. }
  86. /**
  87. * 根据设备号获取快应用用户信息
  88. */
  89. public function getQAppUserByDeviceNo(string $device_no, int $channel_id)
  90. {
  91. $qapp_user = QappUser::where('device_no', $device_no)->where('channel_id', $channel_id)->first();
  92. if ($qapp_user) {
  93. $user = User::find($qapp_user->uid);
  94. $qapp_user->user = $user;
  95. }
  96. return $qapp_user;
  97. }
  98. /**
  99. * 根据uid获取快应用用户信息
  100. */
  101. public function getQAppUserByUid(int $uid)
  102. {
  103. $qapp_user = QappUser::where('uid', $uid)->first();
  104. if ($qapp_user) {
  105. $user = User::find($uid);
  106. $qapp_user->user = $user;
  107. $qapp_user->send_order_id = UserService::getUserSendOrder($uid);
  108. $package_info = QappPackage::where('channel_id', $user->distribution_channel_id)->first();
  109. $qapp_user->app_pay_merchat_id = $package_info->app_pay_merchat_id;
  110. $qapp_user->h5_pay_merchat_id = $package_info->h5_pay_merchat_id;
  111. $qapp_user->ali_pay_merchat_id = $package_info->ali_pay_merchat_id;
  112. }
  113. return $qapp_user;
  114. }
  115. /**
  116. * 创建快应用用户信息
  117. */
  118. public function createQuickAppUser(array $data)
  119. {
  120. try {
  121. DB::beginTransaction();
  122. $user = $this->createUser($data);
  123. $channel_id = $user->distribution_channel_id;
  124. $qapp_user = QappUser::firstOrCreate([
  125. 'device_no' => $data['device_no'],
  126. 'channel_id' => $channel_id,
  127. ], [
  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($data['device_no'], $data['mac'], $channel_id, $user->id, $user->created_at);
  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. }