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'); + } +}