From 0c4781012ae115a0bd175130c309f6c392edc2a1 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Tue, 4 Jan 2022 20:38:40 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=AE=B6?= =?UTF-8?q?=E7=AB=AF=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/HomeController.php | 2 +- .../Distribution/PreIncomeJobCommand.php | 25 ++++++ app/Console/Commands/PushMessageCommand.php | 7 ++ .../Merchant/MessageController.php | 19 +++++ .../Api/Http/Controllers/PushController.php | 12 ++- .../Resources/Merchant/MessageResource.php | 28 ++++++ app/Models/MerchantMessage.php | 85 +++++++++++++++++++ app/Services/Push/MerchantUnipushService.php | 65 +++++++++++++- ..._192819_create_merchant_messages_table.php | 39 +++++++++ 9 files changed, 276 insertions(+), 6 deletions(-) create mode 100644 app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php create mode 100644 app/Endpoint/Api/Http/Resources/Merchant/MessageResource.php create mode 100644 app/Models/MerchantMessage.php create mode 100644 database/migrations/2022_01_04_192819_create_merchant_messages_table.php diff --git a/app/Admin/Controllers/HomeController.php b/app/Admin/Controllers/HomeController.php index 985c04ed..5945434e 100644 --- a/app/Admin/Controllers/HomeController.php +++ b/app/Admin/Controllers/HomeController.php @@ -18,7 +18,7 @@ class HomeController extends Controller { return $content ->header('首页') - ->description('开发中...') + ->description('首页') ->body(function (Row $row) { $row->column(6, function (Column $column) { $column->row(Dashboard::title()); diff --git a/app/Console/Commands/Distribution/PreIncomeJobCommand.php b/app/Console/Commands/Distribution/PreIncomeJobCommand.php index 0911b4a0..aebd4433 100644 --- a/app/Console/Commands/Distribution/PreIncomeJobCommand.php +++ b/app/Console/Commands/Distribution/PreIncomeJobCommand.php @@ -3,7 +3,10 @@ namespace App\Console\Commands\Distribution; use App\Exceptions\BizException; +use App\Models\DistributionPreIncome; use App\Models\DistributionPreIncomeJob; +use App\Models\MerchantMessage; +use App\Models\Order; use App\Services\DistributionPreIncomeJobService; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; @@ -53,6 +56,28 @@ class PreIncomeJobCommand extends Command ]); } } + try { + DB::beginTransaction(); + switch (get_class($job->jobable)) { + case Order::class://如果是订单类型,则发送预收益消息 + $order = $job->jobable; + $incomesLogs = DistributionPreIncome::where('order_id', $order->id)->get(); + foreach ($incomesLogs as $log) { + MerchantMessage::createDistributionMessage($log->user_id, [ + 'title'=>'恭喜收入'.$log->total_revenue.'元', + 'content'=>'您有新的预收益产生,共'.$log->total_revenue.'元。', + ]); + } + break; + default: + break; + } + DB::commit(); + } catch (Throwable $e) { + DB::rollBack(); + + report($e); + } } }); } diff --git a/app/Console/Commands/PushMessageCommand.php b/app/Console/Commands/PushMessageCommand.php index f02fa0ae..27bd5288 100644 --- a/app/Console/Commands/PushMessageCommand.php +++ b/app/Console/Commands/PushMessageCommand.php @@ -2,9 +2,11 @@ namespace App\Console\Commands; +use App\Models\MerchantMessage; use App\Models\Message; use App\Models\PushMessageTask; use App\Services\Push\MallUnipushService; +use App\Services\Push\MerchantUnipushService; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; use Throwable; @@ -34,6 +36,7 @@ class PushMessageCommand extends Command { PushMessageTask::with('message')->where('is_pushed', 0)->chunkById(100, function ($tasks) { $mallPushService = new MallUnipushService(); + $merchantPushService = new MerchantUnipushService(); foreach ($tasks as $task) { try { DB::beginTransaction(); @@ -43,6 +46,10 @@ class PushMessageCommand extends Command if ((bool) app_settings('unipush.is_use')) {//如果开启了推送 $status = $mallPushService->push($task->sn, $task->message) ? 1 : 2; } + } elseif ($task->message instanceof MerchantMessage) { + if ((bool) app_settings('unipush.is_use')) {//如果开启了推送 + $status = $merchantPushService->push($task->sn, $task->message) ? 1 : 2; + } } $task->update([ diff --git a/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php b/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php new file mode 100644 index 00000000..1d0565b4 --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php @@ -0,0 +1,19 @@ +user()) + ->orderBy('created_at', 'desc') + ->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); + return MessageResource::collection($list); + } +} diff --git a/app/Endpoint/Api/Http/Controllers/PushController.php b/app/Endpoint/Api/Http/Controllers/PushController.php index 4c1e5d0d..1803d3f0 100644 --- a/app/Endpoint/Api/Http/Controllers/PushController.php +++ b/app/Endpoint/Api/Http/Controllers/PushController.php @@ -6,6 +6,7 @@ use App\Exceptions\BizException; use App\Models\UserCid; use App\Services\Push\MallUnipushService; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use Throwable; @@ -25,16 +26,19 @@ class PushController extends Controller $input = $request->validate([ 'cid' => ['bail', 'required', 'string', 'max:64'], + 'type' => ['bail', 'string', 'max:1'], ]); try { DB::beginTransaction(); - + $filed = Arr::get($input, 'type', 'u').'_cid'; //查询目前有没有人已绑定这个cid, 有就解绑 - UserCid::where('u_cid', $input['cid'])->where('user_id', '<>', $request->user()->id)->update([ - 'u_cid' => null, + UserCid::where($filed, $input['cid'])->where('user_id', '<>', $request->user()->id)->update([ + $filed => null, ]); $request->user()->cid()->updateOrCreate([ - 'u_cid'=>$input['cid'], + 'user_id' => $request->user()->id, + ], [ + $filed=>$input['cid'], ]); DB::commit(); } catch (Throwable $th) { diff --git a/app/Endpoint/Api/Http/Resources/Merchant/MessageResource.php b/app/Endpoint/Api/Http/Resources/Merchant/MessageResource.php new file mode 100644 index 00000000..a5a90ce1 --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/Merchant/MessageResource.php @@ -0,0 +1,28 @@ + $this->id, + 'title' => $this->title, + 'content' => $this->content, + 'type' => $this->type, + 'ext' => $this->ext, + 'jump_type' => $this->jump_type, + 'jump_link' => (string) $this->jump_link, + 'created_at'=> $this->created_at->toDateTimeString(), + ]; + } +} diff --git a/app/Models/MerchantMessage.php b/app/Models/MerchantMessage.php new file mode 100644 index 00000000..39eac7de --- /dev/null +++ b/app/Models/MerchantMessage.php @@ -0,0 +1,85 @@ +JsonArray::class, + 'is_push'=>'bool', + ]; + + protected $fillable = [ + 'title', 'content', 'type', 'ext', 'jump_type', 'jump_link', 'user_id', 'is_push', + ]; + + public function user() + { + return $this->belongsTo(User::class); + } + + /** + * 此消息的推送任务 + */ + public function pushTasks() + { + return $this->morphMany(PushMessageTask::class, 'message'); + } + + public static function userMessages(User $user) + { + return self::where('created_at', '>=', $user->created_at->subDays(7))->where(function ($q) use ($user) { + $q->where('user_id', $user->id)->orWhere('user_id', 0); + }); + } + + /** + * 发送预收益消息 + * + * @return void + */ + public static function createDistributionMessage($userId, $params) + { + $message = self::create([ + 'type' => 1, + 'title' => $params['title'], + 'user_id' => $userId, + 'content' => $params['content'], + 'jump_type'=> self::JUMP_NO, + 'is_push' => 1, + ]); + + if ($message && $message->needPush()) { + $message->pushMessage(); + } + + return $message; + } + + public function needPush() + { + return $this->is_push; + } + + public function pushMessage() + { + //填入推送内容 + PushMessageTask::create([ + 'sn' => serial_number(), + 'message_id' => $this->id, + 'message_type' => $this::class, + ]); + } +} diff --git a/app/Services/Push/MerchantUnipushService.php b/app/Services/Push/MerchantUnipushService.php index 34a22799..be0dd885 100644 --- a/app/Services/Push/MerchantUnipushService.php +++ b/app/Services/Push/MerchantUnipushService.php @@ -2,9 +2,72 @@ namespace App\Services\Push; -class MerchantUnipushService +use App\Models\MerchantMessage; + +class MerchantUnipushService extends UniPushService { + protected $appId; + + protected $appKey; + + protected $appSecret; + + protected $masterSecret; + public function __construct() { + $this->appId = app_settings('unipush.merchant_app_id', ''); + $this->appKey = app_settings('unipush.merchant_app_key', ''); + $this->appSecret = app_settings('unipush.merchant_app_secret', ''); + $this->masterSecret = app_settings('unipush.merchant_master_secret', ''); + parent::__construct($this->appId, $this->appKey, $this->appSecret, $this->masterSecret); + } + + public function push(string $sn, MerchantMessage $message) + { + $res = false; + if ($message->user_id > 0) { + $res = $this->pushMessage($sn, $message); + } else { + $res = $this->pushAllMessage($sn, $message); + } + return $res; + } + + /** + * 推送公告消息 + * + * @param [type] $message + * @return void + */ + public function pushAllMessage(string $sn, MerchantMessage $message) + { + return $this->pushAll($sn, $message->title, $message->content, [ + 'jump_type' => $message->jump_type == 0 ? 1 : $message->jump_type, + 'jump_link' => $message->type == 1 || empty($message->jump_link) ? '/pages/news/index' : $message->jump_link, + ]); + } + + /** + * 推送单条消息 + * + * @param Message $message + * @return void + */ + public function pushMessage(string $sn, MerchantMessage $message) + { + //如果不是指定消息,直接退出; + if (is_null($message->user)) { + return false; + } + //如果拿不到m_cid直接退出 + if (!$message->user->cid->m_cid) { + return false; + } + + return $this->pushCid($sn, $message->user->cid->m_cid, $message->title, $message->content, [ + 'jump_type' => $message->jump_type == 0 ? 1 : $message->jump_type, + 'jump_link' => $message->type == 1 || empty($message->jump_link) ? '/pages/news/index' : $message->jump_link, + ]); } } diff --git a/database/migrations/2022_01_04_192819_create_merchant_messages_table.php b/database/migrations/2022_01_04_192819_create_merchant_messages_table.php new file mode 100644 index 00000000..c65189af --- /dev/null +++ b/database/migrations/2022_01_04_192819_create_merchant_messages_table.php @@ -0,0 +1,39 @@ +id(); + $table->string('title')->comment('消息标题'); + $table->text('content')->comment('消息内容'); + $table->unsignedTinyInteger('type')->default(0)->comment('消息类型:0公告,1预收益提醒'); + $table->unsignedBigInteger('user_id')->default(0)->comment('指定用户'); + $table->json('ext')->nullable()->comment('消息扩展内容'); + $table->tinyInteger('jump_type')->default(0)->comment('跳转类型:0不跳转,1跳转应用内页,2H5链接'); + $table->string('jump_link')->nullable()->comment('跳转地址'); + $table->unsignedTinyInteger('is_push')->default(0)->comment('是否推送'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('merchant_messages'); + } +} From c54f16f5152a85e297e734be9f696d77ddb41fc5 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Wed, 5 Jan 2022 11:39:02 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=AE=B6?= =?UTF-8?q?=E7=AB=AF=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/UserController.php | 1 + app/Admin/Forms/Settings/Unipush.php | 6 +++--- .../Commands/Distribution/PreIncomeJobCommand.php | 2 ++ app/Console/Commands/PushMessageCommand.php | 2 +- .../Http/Controllers/Merchant/MessageController.php | 3 ++- app/Endpoint/Api/routes.php | 2 ++ app/Models/MerchantMessage.php | 1 + database/seeders/AppSettingSeeder.php | 10 +++++----- 8 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/Admin/Controllers/UserController.php b/app/Admin/Controllers/UserController.php index f875afcd..dc174549 100644 --- a/app/Admin/Controllers/UserController.php +++ b/app/Admin/Controllers/UserController.php @@ -119,6 +119,7 @@ class UserController extends AdminController $grid->filter(function (Grid\Filter $filter) { $filter->panel(); $filter->like('phone')->width(3); + $filter->like('userInfo.nickname')->width(3); $filter->equal('userInfo.agent_level')->select(UserInfo::$agentLevelTexts)->width(3); $filter->between('created_at')->dateTime()->width(7); // $filter->between('userInfo.growth_value')->width(6); diff --git a/app/Admin/Forms/Settings/Unipush.php b/app/Admin/Forms/Settings/Unipush.php index 95312e78..444f8e3c 100644 --- a/app/Admin/Forms/Settings/Unipush.php +++ b/app/Admin/Forms/Settings/Unipush.php @@ -39,9 +39,9 @@ class Unipush extends Form $this->switch('is_use', '是否启用')->value($appSettings['is_use'] ?? 0); $this->text('mall_app_id', '商城APP_ID')->value($appSettings['mall_app_id'] ?? ''); - $this->text('mall_app_key', '商城APP_KEY')->value($appSettings['mall_app_id'] ?? ''); - $this->text('mall_app_secret', '商城APP_SECRET')->value($appSettings['mall_app_id'] ?? ''); - $this->text('mall_master_secret', '商城APP_MASTER_SECRET')->value($appSettings['mall_app_id'] ?? ''); + $this->text('mall_app_key', '商城APP_KEY')->value($appSettings['mall_app_key'] ?? ''); + $this->text('mall_app_secret', '商城APP_SECRET')->value($appSettings['mall_app_secret'] ?? ''); + $this->text('mall_master_secret', '商城APP_MASTER_SECRET')->value($appSettings['mall_master_secret'] ?? ''); $this->divider(); $this->switch('merchant_is_use', '是否启用')->value($appSettings['merchant_is_use'] ?? 0); diff --git a/app/Console/Commands/Distribution/PreIncomeJobCommand.php b/app/Console/Commands/Distribution/PreIncomeJobCommand.php index aebd4433..e8b6b4aa 100644 --- a/app/Console/Commands/Distribution/PreIncomeJobCommand.php +++ b/app/Console/Commands/Distribution/PreIncomeJobCommand.php @@ -56,6 +56,8 @@ class PreIncomeJobCommand extends Command ]); } } + + //发送商家端预收益进帐消息 try { DB::beginTransaction(); switch (get_class($job->jobable)) { diff --git a/app/Console/Commands/PushMessageCommand.php b/app/Console/Commands/PushMessageCommand.php index 27bd5288..f03ab6df 100644 --- a/app/Console/Commands/PushMessageCommand.php +++ b/app/Console/Commands/PushMessageCommand.php @@ -47,7 +47,7 @@ class PushMessageCommand extends Command $status = $mallPushService->push($task->sn, $task->message) ? 1 : 2; } } elseif ($task->message instanceof MerchantMessage) { - if ((bool) app_settings('unipush.is_use')) {//如果开启了推送 + if ((bool) app_settings('unipush.merchant_is_use')) {//如果开启了推送 $status = $merchantPushService->push($task->sn, $task->message) ? 1 : 2; } } diff --git a/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php b/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php index 1d0565b4..8e313473 100644 --- a/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php +++ b/app/Endpoint/Api/Http/Controllers/Merchant/MessageController.php @@ -1,7 +1,8 @@ 'JX33P0wP8bAQprI953hpN6', 'mall_app_secret' => 'a3u3B6lXjq6fPTBlOGiOc9', 'mall_master_secret' => 'MAxmqomwo597xJeDuMCvx1', - 'merchant_is_use' => false, - 'merchant_app_id' => '', - 'merchant_app_key' => '', - 'merchant_app_secret' => '', - 'merchant_master_secret' => '', + 'merchant_is_use' => true, + 'merchant_app_id' => '5bvlKxO8RK7lCTc9DINcx5', + 'merchant_app_key' => 'qrPLowCchvArbpMKoPlMV1', + 'merchant_app_secret' => '5k36Jo49EAAGBDvR0cFqG3', + 'merchant_master_secret' => 'HCB87uFoF18iWsKHf7VY57', ], 'remarks' => '个推配置', ], From ab24d61baed9909ca0aa3aa7fff8ce29bb548fd9 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Wed, 5 Jan 2022 11:44:06 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E5=AD=97=E4=BD=93=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Metrics/StatisticsTotal.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/Admin/Metrics/StatisticsTotal.php b/app/Admin/Metrics/StatisticsTotal.php index ee577b20..16a095a4 100644 --- a/app/Admin/Metrics/StatisticsTotal.php +++ b/app/Admin/Metrics/StatisticsTotal.php @@ -19,7 +19,7 @@ class StatisticsTotal extends RadialBar parent::init(); $this->title('统计预览'); - $this->height(320); + $this->height(300); $this->contentWidth(12, 0); } @@ -57,29 +57,29 @@ class StatisticsTotal extends RadialBar