作者 Richer

功能完成

<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* grid 列表 modal 类:家庭-成员 modal 类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Grids\Modal
* @package App\Admin\Grids\Modal
* @author Richer <yangzi1028@163.com>
* @date 2022年11月1日16:03:27
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Grids\Modal\User;
use App\Admin\Grids\Modal\BaseModal;
use App\Models\User\TimesRecord;
use App\Models\User\User;
use App\Transformers\Activity\ActivityUserTransformer;
use App\Transformers\User\TimesRecordTransformer;
use App\Transformers\User\UserTransformer;
/**
* Class TeamModal
*
* @category App\Admin\Grids\User
* @package App\Admin\Grids\User
* @author Richer <yangzi1028@163.com>
* @date 2022年3月15日09:22:19
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class TeamModal extends BaseModal
{
protected $type = '';
/**
* TeamModal constructor.
* @param $type
*/
public function __construct($type = '')
{
$this->type = $type;
}
public function render($key = null)
{
// dd($this->type );
$user = User::find($key);
if ($user->partner == 1) {
$list = User::where(function ($query) use($key) {
$query->where('pid', $key)->orWhere('gid', $key);
})->latest()->paginate(request('per_page', 10));
} else {
$list = User::where('pid', $key)->latest()->paginate(request('per_page', 10));
}
request()->offsetSet('user_id', $key);
$paginator = $this->renderPaginator($key, $list, app(UserTransformer::class));
// 定义需要展示的字段
$columns = [
[
'name' => __('avatar'),
'field' => 'avatar',
'type' => 'image',
],
[
'name' => __('nickname'),
'field' => 'nickname',
],
[
'name' => __('mobile'),
'field' => 'mobile',
],
[
'name' => __('关系'),
'field' => 'level_show',
],
[
'name' => __('加入时间'),
'field' => 'created_at',
],
];
$paginator['columns'] = $columns;
return view(
'admin::grid.modal',
$paginator
);
}
}
... ...
... ... @@ -18,6 +18,7 @@ namespace App\Admin\Grids\User;
use App\Admin\Extensions\CustomActions;
use App\Admin\Grids\Modal\User\IncomeRecordModal;
use App\Admin\Grids\Modal\User\TeamModal;
use App\Admin\Grids\Modal\User\TimesRecordModal;
use App\Admin\Grids\Modal\User\WithdrawnRecordModal;
use App\Models\User\User;
... ... @@ -91,7 +92,7 @@ trait UserGrid
{
parent::setGridQuery();
$this->grid->model()->with(['wallet']);
$this->grid->model()->with(['wallet']);//->withCount(['allChildren']);
}
/**
... ... @@ -132,7 +133,6 @@ trait UserGrid
$url = $actions->getResource();
$actions->append(CustomActions::renderAddUserTimes($id, $url));
}
/**
... ... @@ -142,16 +142,25 @@ trait UserGrid
*/
public function renderGridFields()
{
$this->grid->fixColumns(1, -1);
$this->gridRowNo();
$this->gridUser();
// $this->gridBoolField('partner');
$this->gridSwitchField('partner', '', config('constants.STATUS1_STATE_OPTIONS'));
$this->gridNumberField('children_count', '团队')->display(function () {
if ($this->partner == 1) {
return $this->children()->count() + $this->children1()->count();
}
return $this->children()->count();
})->modal('团队明细记录', TeamModal::class);//->sortable();
$this->gridTextField('wallet.income', '总收入')->modal('收入明细记录', IncomeRecordModal::class);
$this->gridTextField('wallet.balance', '余额');
$this->gridTextField('wallet.withdrawn', '提现')->modal('提现明细记录', WithdrawnRecordModal::class);
$this->gridTextField('wallet.frozen', '冻结');
// $this->gridTextField('wallet.withdrawn', '提现')->modal('提现明细记录', WithdrawnRecordModal::class);
// $this->gridTextField('wallet.frozen', '冻结');
// $this->gridSingleImageField('avatar', __('wx_avatar'));
//// $this->gridTextField('username');
//// $this->gridTextField('name', __('truename'));
... ... @@ -159,8 +168,8 @@ trait UserGrid
// $this->gridSingleSelectField('gender', __('gender'), User::GENDER_OPTIONS, User::GENDER_COLOR_OPTIONS);
$this->gridNumberField('times')->modal('次数明细记录', TimesRecordModal::class)->sortable();
$this->gridTextField('login_times')->sortable();
$this->gridDateField('last_login_time');
// $this->gridDateField('last_login_time');
$this->gridDateField('created_at', __('registered_at'));
$this->gridSingleImageField('invite_qr_code', '小程序码');
// $this->gridSingleImageField('invite_qr_code', '小程序码');
}
}
... ...
... ... @@ -16,8 +16,12 @@
*/
namespace App\Factories\Wechat;
use App\Models\User\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* Trait Official
... ... @@ -98,7 +102,7 @@ trait Official
Log::info($message);
//按事件类型处理
$event = \Str::camel('event_'. $message['Event']);
$event = Str::camel('event_'. $message['Event']);
if (method_exists($this, $event)) {
return call_user_func_array([$this, $event], [$message]);
... ... @@ -116,8 +120,8 @@ trait Official
protected function eventSubscribe($message)
{
// 设置了场景
if (\Arr::get($message, 'EventKey')) {
$message['EventKey'] = str_replace('qrscene_', '', \Arr::get($message, 'EventKey'));
if (Arr::get($message, 'EventKey')) {
$message['EventKey'] = str_replace('qrscene_', '', Arr::get($message, 'EventKey'));
$user_id = str_replace('INVITE', '', $message['EventKey']);
$user = User::findOrFail($user_id);
if ($user) {
... ... @@ -139,8 +143,8 @@ trait Official
*/
protected function eventSCAN($message)
{
if (\Arr::get($message, 'EventKey')) {
$message['EventKey'] = str_replace('qrscene_', '', \Arr::get($message, 'EventKey'));
if (Arr::get($message, 'EventKey')) {
$message['EventKey'] = str_replace('qrscene_', '', Arr::get($message, 'EventKey'));
$user_id = str_replace('INVITE', '', $message['EventKey']);
$user = User::findOrFail($user_id);
if ($user) {
... ...
... ... @@ -96,14 +96,14 @@ class AuthController extends BaseController
/**
* 用户登录:微信收取登录
*
* @param LoginRequest $request
* @param Request $request
* @return Response
*/
public function wechatAuth(LoginRequest $request)
public function wechatAuth(Request $request)
{
$result = $this->service->wechatAuth($request);
if ($result) {
return $this->success($result, __('login') . ' ' . __('succeeded'), 0);
return $this->success($result, __('login') . ' ' . __('succeeded'), 0, $result->token);
} else {
return $this->error($this->service->getMessage(), 2006);
}
... ...
... ... @@ -190,7 +190,7 @@ trait ChatTrait
* @param string $type
* @return string
*/
public static function sendRequest($open_ai, $send_data, $type = 'chunked')
public static function sendRequest($open_ai, $send_data, $type = 'stream')
{
$answer = '';
if ($type === 'chunked') {
... ... @@ -245,9 +245,8 @@ trait ChatTrait
header('Content-type: text/event-stream');
header('Cache-Control: no-cache');
ob_end_flush();
$answer = "";
$complete = $open_ai->chat($send_data, function ($curl_info, $data) use (&$answer) {
record_log($this->log_channel, '开始请求' );
// record_log($this->log_channel, '开始请求' );
$deltas = explode("\n", $data);
$content = '';
... ... @@ -266,11 +265,11 @@ trait ChatTrait
} else {
$content = "对不起,我不知道怎么去回答。";
}
echo "data: " .str_replace("\n", "\\n", $content )."\n\n";
echo str_replace("\n", "\\n", $content )."\n\n";
flush();
}
record_log($this->log_channel, '内容: ' . $content );
// record_log($this->log_channel, '内容: ' . $content );
if (connection_aborted()) {
return 0;
... ...
... ... @@ -87,10 +87,34 @@ trait UserTrait
*/
public function children(): HasMany
{
return $this->hasMany(get_class($this), 'pid', 'id');
}
/**
* @return HasMany
*/
public function children1(): HasMany
{
return $this->hasMany(get_class($this), 'gid', 'id');
}
/**
* @return hasMany
*/
public function allChildren()
{
if ($this->partner == 1) {
return $this->children()->with('allChildren');
}
return $this->children();
}
// public function team
/**
* 我的详情
*
* @return hasOne
... ...
... ... @@ -391,6 +391,75 @@ class WechatService extends BaseService
}
}
public function authLoginWithOfficialAccount1($request)
{
$config = [
'app_id' => config('wechat.official_account.app_id'),
'secret' => config('wechat.official_account.secret'),
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '/oauth_callback',
],
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
];
$app = Factory::officialAccount($config);
$oauth = $app->oauth;
$targetUrl = urldecode($request->input('target_url'));
$response = [
'errcode' => 0,
'errmsg' => '操作成功',
'result' => [],
];
if (empty($targetUrl)) {
$this->message = '参数错误:target_url';
return false;
}
// 判断用户是否已经授权
if (empty(session('wechat.oauth_user'))) {
// 用户未授权,则开始授权
session(['wechat.oauth_target_url' => $targetUrl]);
return $oauth->redirect();
} else {
// 用户已经授权
$user = session('wechat.oauth_user');
$response['result'] = $user;
return $response;
}
}
public function oauthCallback()
{
$config = [
'app_id' => config('wechat.official_account.app_id'),
'secret' => config('wechat.official_account.secret'),
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '/oauth_callback',
],
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
];
$app = Factory::officialAccount($config);
$oauth = $app->oauth;
$user = $oauth->user()->getOriginal();
$targetUrl = session('wechat.oauth_target_url');
// 将用户信息保存到session中
session(['wechat.oauth_user' => $user->toArray()]);
return redirect($targetUrl);
}
/**
* 登录:公众号收取登录
* "openid" => "oemt65wMNi7XXo76PUIrsJuoJUeA"
... ... @@ -411,9 +480,18 @@ class WechatService extends BaseService
*/
public function authLoginWithOfficialAccount($request)
{
record_log('wechat-auth', '微信公众号授权登录' , 'begin');
record_log('wechat-auth', '请求信息:' .json_encode($request->all()));
$code = $request->input('code');
if (!$code) {
$this->message = '授权失败,code不能为空。';
record_log('wechat-auth', '微信公众号授权登录' , 'end');
return false;
}
// 正在申请
$user_info = session('wechat.oauth_user.default')->original; // 拿到授权用户资料
record_log('wechat-auth', '授权用户信息:' .json_encode($user_info));
// $factory = app(WechatFactory::class);
// // 公众号好手气
... ... @@ -440,18 +518,35 @@ class WechatService extends BaseService
}
if ($user_info) {
$pid = $request->inviter_id ? : 0; // 邀请人id
$gid = 0;
if ($pid) {
$inviter = User::find($pid);
$gid = optional($inviter)->pid;
}
$user->avatar = Arr::get($user_info, 'avatarUrl');
$user->nickname = Arr::get($user_info, 'nickName');
$user->gender = Arr::get($user_info, 'gender');
$user->country = Arr::get($user_info, 'country');
$user->province = Arr::get($user_info, 'province');
$user->city = Arr::get($user_info, 'city');
$user->times = 10; // 新用户注册赠送 10 条
$user->invite_qr_code = 'mini_program_code/'.$user->id .'.png';
$user->pid = $pid;
$user->gid = $gid;
$user->openid = $openid;
$user->password = bcrypt(config('constants.PASSWORD'));
$user->role = Role::GENERAL;
$user->password = bcrypt(config('constants.PASSWORD'));
$user->registered_at = date('Y-m-d H:i:s');
$user->registered_ip = $request->ip();
}
$user->token = JWTAuth::fromuser($user);
$user->save();
// 记录登陆日志
$this->recordLoginRecord($request, $user);
record_log('wechat-auth', '微信公众号授权登录' , 'end');
return $user;
}
... ...
... ... @@ -18,7 +18,9 @@ namespace App\Transformers\User;
use App\Models\User\Role;
use App\Models\User\User;
use App\Models\User\WalletRecord;
use App\Transformers\BaseTransformer;
use Illuminate\Support\Arr;
/**
* Class UserTransformer.
... ... @@ -42,10 +44,10 @@ class UserTransformer extends BaseTransformer
*/
public function transform(User $model)
{
return [
$output = [
'id' => (int) $model->id,
'username' => (string)$model->username,
'name' => (string)$model->name,
// 'username' => (string)$model->username,
// 'name' => (string)$model->name,
'nickname' => (string)$model->nickname,
'avatar' => (string)$model->avatar,
'mobile' => (string)$model->mobile,
... ... @@ -62,5 +64,17 @@ class UserTransformer extends BaseTransformer
'created_at' => format_date($model->created_at),
'updated_at' => format_date($model->updated_at),
];
if (request('user_id')) {
if ($model->pid == request('user_id')) {
$output['level'] = 1;
$output['level_show'] = __(Arr::get(WalletRecord::EVENT_OPTIONS, WalletRecord::DIRECT));
} elseif ($model->gid == request('user_id')) {
$output['level'] = 2;
$output['level_show'] = __(Arr::get(WalletRecord::EVENT_OPTIONS, WalletRecord::INDIRECT));
}
}
return $output;
}
}
... ...
... ... @@ -164,6 +164,14 @@ return [
'days' => 14,
'permission'=> 0777,
],
// 微信支付回调日志
'wechat-auth' => [
'driver' => 'daily',
'path' => storage_path('logs/wechat-auth/wechat-auth.log'),
'level' => 'info',
'days' => 14,
'permission'=> 0777,
],
],
];
... ...
<!DOCTYPE html><html lang=zh-CN><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><title>chatApp</title><script>var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')</script><link rel=stylesheet href=./static/index.b0707a6a.css></head><body><noscript><strong>Please enable JavaScript to continue.</strong></noscript><div id=app></div><script src=./static/js/chunk-vendors.fc0b6f42.js></script><script src=./static/js/index.293a48ba.js></script></body></html>
... ...
... ... @@ -11,7 +11,8 @@
|
*/
Route::get('/', function () {
return view('welcome');
return view('index');
});
Route::group(['middleware' => ['cors']], function () {
... ...