QappUserService.php 5.8 KB

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