first
This commit is contained in:
52
modules/User/Traits/BelongsToUser.php
Normal file
52
modules/User/Traits/BelongsToUser.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Modules\User\Models\User;
|
||||
|
||||
trait BelongsToUser
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes : 所属用户
|
||||
*
|
||||
* @Date : 2021/3/15 1:09 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 用户 作用域查询
|
||||
*
|
||||
* @Date : 2021/6/21 8:57 上午
|
||||
* @Author : <Jason.C>
|
||||
* @param Builder $query
|
||||
* @param User $user
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeByUser(Builder $query, User $user): Builder
|
||||
{
|
||||
return $query->where('user_id', $user->getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 用户ID 作用域查询
|
||||
*
|
||||
* @Date : 2021/6/21 9:04 上午
|
||||
* @Author : <Jason.C>
|
||||
* @param Builder $query
|
||||
* @param int $userId
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeByUserId(Builder $query, int $userId): Builder
|
||||
{
|
||||
return $query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
}
|
||||
67
modules/User/Traits/RankDataTrait.php
Normal file
67
modules/User/Traits/RankDataTrait.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\User\Models\User;
|
||||
|
||||
trait RankDataTrait
|
||||
{
|
||||
/**
|
||||
* 根据时间范围获取推荐VIP排行
|
||||
*
|
||||
* @param array $timeBetween
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRankDataTrait(array $timeBetween)
|
||||
{
|
||||
$vipJion = DB::table('user_identity')
|
||||
->whereBetween('started_at', $timeBetween)
|
||||
->where('ended_at', '>=', now())
|
||||
->select('user_id as middle_user_Id',
|
||||
DB::raw('any_value(started_at) as started_at'),
|
||||
DB::raw('any_value(identity_id) as identity_id'))
|
||||
->groupBy('middle_user_Id');
|
||||
|
||||
$vipRelationJion = DB::table('user_relations')
|
||||
->leftJoinSub($vipJion, 'vipJion', function ($join) {
|
||||
$join->on('user_relations.user_id', '=', 'vipJion.middle_user_Id');
|
||||
})
|
||||
->where('identity_id', '>', 1)
|
||||
->select('parent_id',
|
||||
DB::raw('ifnull(count(user_id),0) as countChild'),
|
||||
DB::raw('max(started_at) as lastOpenTime'))
|
||||
->groupBy('parent_id');
|
||||
|
||||
return User::query()
|
||||
->leftJoinSub($vipRelationJion, 'vipRelationJion', function ($join) {
|
||||
$join->on('users.id', '=', 'vipRelationJion.parent_id');
|
||||
})
|
||||
->where('countChild', '>', 0)
|
||||
->orderBy('countChild', 'desc')
|
||||
->orderBy('lastOpenTime', 'asc')
|
||||
->limit(10)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getTotalUser(array $timeBetween)
|
||||
{
|
||||
$relationJion = DB::table('user_relations')
|
||||
->whereBetween('created_at', $timeBetween)
|
||||
->select('parent_id',
|
||||
DB::raw('ifnull(count(user_id),0) as countChild'),
|
||||
DB::raw('max(created_at) as lastRegTime')
|
||||
)
|
||||
->groupBy('parent_id');
|
||||
|
||||
return User::query()
|
||||
->leftJoinSub($relationJion, 'relationJion', function ($join) {
|
||||
$join->on('users.id', '=', 'relationJion.parent_id');
|
||||
})
|
||||
->where('countChild', '>', 0)
|
||||
->orderBy('countChild', 'desc')
|
||||
->orderBy('lastRegTime', 'asc')
|
||||
->limit(10)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
180
modules/User/Traits/WechatTrait.php
Normal file
180
modules/User/Traits/WechatTrait.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Traits;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Modules\User\Models\UserWechatSubscribe;
|
||||
use Modules\User\Models\UserWechat;
|
||||
use Modules\User\Models\UserWechatOfficial;
|
||||
|
||||
trait WechatTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes: 通过code获取用户信息
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/9/28 10:36
|
||||
* @return mixed|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getUserFromCode()
|
||||
{
|
||||
try {
|
||||
$code = request()->code;
|
||||
if (! $code) {
|
||||
throw new \Exception('缺少code');
|
||||
}
|
||||
$weChat = app('wechat.official_account');
|
||||
|
||||
$weUser = $weChat->oauth->userFromCode(request()->code);
|
||||
|
||||
return $weUser;
|
||||
} catch (\Exception $exception) {
|
||||
throw new \Exception($exception->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取用户信息
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/8/11 11:56
|
||||
* @param $openid
|
||||
* @return mixed
|
||||
*/
|
||||
public function getWechatUser($openid)
|
||||
{
|
||||
$weChat = app('wechat.official_account');
|
||||
|
||||
return $weChat->user->get($openid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 设置已关注列表
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/9/28 14:18
|
||||
* @param $unionid
|
||||
* @param $openid
|
||||
* @param int $subscribe
|
||||
*/
|
||||
public function setUserSubscribe($unionid, $openid, int $subscribe = 1)
|
||||
{
|
||||
UserWechatSubscribe::query()->updateOrCreate([
|
||||
'unionid' => $unionid,
|
||||
'openid' => $openid,
|
||||
], [
|
||||
'subscribe' => $subscribe,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 入库所有关注列表
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/9/28 14:41
|
||||
*/
|
||||
public function getAllSubscribeUsers()
|
||||
{
|
||||
$app = app('wechat.official_account');
|
||||
$users = $app->user->list();
|
||||
collect($users->data['openid'])
|
||||
->split(3)->map(function ($datas) use ($app) {
|
||||
$infos = $app->user->select($datas->toArray());
|
||||
foreach ($infos['user_info_list'] as $info) {
|
||||
$this->setUserSubscribe($info['unionid'], $info['openid'], 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 用户关注公众号
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/11/1 14:19
|
||||
*/
|
||||
public function userSubOfficial()
|
||||
{
|
||||
$app = app('wechat.official_account');
|
||||
$user = $app->user->get($this->payload->FromUserName);
|
||||
|
||||
if ($user->unionid) {
|
||||
$wechatUsers = UserWechat::query()
|
||||
->where('unionid', $user->unionid)
|
||||
->get();
|
||||
if ($wechatUsers->isNotEmpty()) {
|
||||
foreach ($wechatUsers as $wechatUser) {
|
||||
if ($wechatUser->official) {
|
||||
if ($wechatUser->official->subscribe != 1) {
|
||||
$wechatUser->official->update([
|
||||
'subscribe' => 1,
|
||||
'subscribed_at' => Carbon::parse($user->subscribe_time)->toDateTimeString(),
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$wechatUser->official()->create([
|
||||
'openid' => $this->payload->FromUserName,
|
||||
'subscribe' => 1,
|
||||
'subscribed_at' => Carbon::parse($user->subscribe_time)->toDateTimeString(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//插入关注数据
|
||||
$this->setUserSubscribe($user->unionid, $this->payload->FromUserName, 1);
|
||||
|
||||
} else {
|
||||
// 先查找用户是否存在,不存在再注册
|
||||
$officialUsers = UserWechatOfficial::query()
|
||||
->where('openid', $this->payload->FromUserName)
|
||||
->get();
|
||||
|
||||
//设置总表uniond
|
||||
if ($officialUsers->isNotEmpty()) {
|
||||
foreach ($officialUsers as $officialUser) {
|
||||
if (! $officialUser->userWechat->unionid && $user->unionid) {
|
||||
$officialUser->userWechat->update([
|
||||
'unionid' => $user->unionid
|
||||
]);
|
||||
}
|
||||
|
||||
if ($officialUser->subscribe != 1) {
|
||||
$officialUser->update([
|
||||
'subscribe' => 1,
|
||||
'subscribed_at' => Carbon::parse($user->subscribe_time)->toDateTimeString(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 用户取消关注公众号
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/11/1 14:25
|
||||
*/
|
||||
public function userUnSubOfficial()
|
||||
{
|
||||
$officialUsers = UserWechatOfficial::where('openid', $this->payload->FromUserName)->get();
|
||||
if ($officialUsers->isNotEmpty()) {
|
||||
foreach ($officialUsers as $officialUser) {
|
||||
$officialUser->update([
|
||||
'subscribe' => 0,
|
||||
'subscribed_at' => null,
|
||||
]);
|
||||
|
||||
//设置取消关注
|
||||
if ($officialUser->userWechat && $officialUser->userWechat->unionid) {
|
||||
$this->setUserSubscribe($officialUser->userWechat->unionid, $officialUser->openid, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user