89 lines
2.0 KiB
PHP
89 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\User\Traits;
|
|
|
|
use Modules\User\Models\UserSubscribe;
|
|
|
|
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, $subscribe = 1)
|
|
{
|
|
UserSubscribe::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);
|
|
}
|
|
});
|
|
}
|
|
|
|
} |