123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace App\Modules\User\Services;
- use App\Consts\QuickConst;
- use App\Jobs\QappTikTokUser;
- use App\Modules\BaseService;
- use App\Modules\User\Models\QappPackage;
- use App\Modules\User\Models\QappUser;
- use App\Modules\User\Models\User;
- use Exception;
- use Illuminate\Support\Facades\DB;
- use Tymon\JWTAuth\Facades\JWTAuth;
- /**
- *
- */
- class QappUserService
- {
- /**
- * 登录
- */
- public function login(array $data)
- {
- $isNewRegister = false;
- $device_no = $data['device_no'];
- $channel_id = $this->findChannelId($data['package']);
- $qapp_user = $this->getQAppUserByDeviceNo($device_no, $channel_id);
- if (!$qapp_user) {
- $isNewRegister = true;
- $qapp_user = $this->createQuickAppUser($data);
- }
- $user = $qapp_user->user;
- $uid = $user->id;
- $time = strtotime("+1 month");
- $token = JWTAuth::fromUser($user);
- if ($data['send_order_id']) {
- UserService::setUserSendOrder($uid, $data['send_order_id']);
- }
- // 新注册统计
- if ($isNewRegister && $qapp_user) {
- QappAddDeskTopService::incrAddDeskTop($uid, QuickConst::FIELD_REGISTER);
- }
- return compact('token', 'time', 'uid');
- }
- public function getGolableUser()
- {
- $qapp_user = app()->make('qapp_user');
- return $qapp_user;
- }
- /**
- * 绑定手机号
- * 多个账号可以绑定一个手机号
- */
- public function bindPhone(int $uid, string $phone)
- {
- $qapp_user = QappUser::where('uid', $uid)->first();
- if ($qapp_user->phone && $qapp_user->phone != $phone) {
- return false;
- } else {
- try {
- DB::beginTransaction();
- if (!$qapp_user->phone) {
- $reward = 100;
- User::where('id', $uid)->update(
- [
- 'balance' => DB::raw('balance+' . $reward),
- 'reward_balance' => DB::raw('reward_balance+' . $reward)
- ]
- );
- }
- $qapp_user->phone = $phone;
- $qapp_user->save();
- DB::commit();
- } catch (Exception $e) {
- myLog('bindPhone')->error($e->getMessage());
- }
- return true;
- }
- }
- public function setGolableUser(int $uid)
- {
- $user_info = $this->getQAppUserByUid($uid);
- $qapp_user = app()->make('qapp_user');
- $qapp_user->id = $user_info->id;
- $qapp_user->uid = $user_info->uid;
- $qapp_user->send_order_id = $user_info->send_order_id;
- $qapp_user->device_no = $user_info->device_no;
- $qapp_user->device_info = $user_info->device_info;
- $qapp_user->phone = $user_info->phone;
- $qapp_user->user = $user_info->user;
- $qapp_user->app_pay_merchat_id = $user_info->app_pay_merchat_id;
- $qapp_user->h5_pay_merchat_id = $user_info->h5_pay_merchat_id;
- $qapp_user->ali_pay_merchat_id = $user_info->ali_pay_merchat_id;
- }
- /**
- * 根据设备号获取快应用用户信息
- */
- public function getQAppUserByDeviceNo(string $device_no, int $channel_id)
- {
- $qapp_user = QappUser::where('device_no', $device_no)->where('channel_id', $channel_id)->first();
- if ($qapp_user) {
- $user = User::find($qapp_user->uid);
- $qapp_user->user = $user;
- }
- return $qapp_user;
- }
- /**
- * 根据uid获取快应用用户信息
- */
- public function getQAppUserByUid(int $uid)
- {
- $qapp_user = QappUser::where('uid', $uid)->first();
- if ($qapp_user) {
- $user = User::find($uid);
- $qapp_user->user = $user;
- $qapp_user->send_order_id = UserService::getUserSendOrder($uid);
- $package_info = QappPackage::where('channel_id', $user->distribution_channel_id)->first();
- $qapp_user->app_pay_merchat_id = $package_info->app_pay_merchat_id;
- $qapp_user->h5_pay_merchat_id = $package_info->h5_pay_merchat_id;
- $qapp_user->ali_pay_merchat_id = $package_info->ali_pay_merchat_id;
- }
- return $qapp_user;
- }
- /**
- * 创建快应用用户信息
- */
- public function createQuickAppUser(array $data)
- {
- try {
- DB::beginTransaction();
- $user = $this->createUser($data);
- $channel_id = $user->distribution_channel_id;
- $qapp_user = QappUser::firstOrCreate([
- 'device_no' => $data['device_no'],
- 'channel_id' => $channel_id,
- ], [
- 'imei' => $data['imei'],
- 'androidid' => $data['androidid'],
- 'mac' => $data['mac'],
- 'uid' => $user->id,
- 'device_info' => $data['device_info'],
- ]);
- $qapp_user->user = $user;
- DB::commit();
- $job = new QappTikTokUser($user->register_ip, $data['device_no'], $data['mac'], $channel_id, $user->id, $user->created_at, true);
- dispatch($job->onConnection('rabbitmq')->onQueue('qapp_tiktok_user_register_queue'));
- return $qapp_user;
- } catch (Exception $e) {
- myLog('create_user')->error($e->getMessage());
- }
- }
- private function findChannelId(string $package)
- {
- $channel_id = env('QUICKAPP_SITE');
- if ($package) {
- $package_info = QappPackage::where('package', $package)->first();
- $channel_id = $package_info ? $package_info->channel_id : $channel_id;
- }
- return $channel_id;
- }
- /**
- * 创建用户
- */
- private function createUser(array $data)
- {
- $openid = $data['device_no'];
- $unionid = $data['device_no'];
- $register_ip = _getIp();
- $distribution_channel_id = $this->findChannelId($data['package']);
- $send_order_id = $data['send_order_id'];
- $unique_key = compact('unionid', 'distribution_channel_id');
- $data = compact('openid', 'register_ip', 'send_order_id');
- return User::firstOrCreate($unique_key, $data);
- }
- }
|