6
0
Fork 0

添加商家端消息发送

release
vine_liutk 2022-01-04 20:38:40 +08:00
parent 2cad4e9a11
commit 0c4781012a
9 changed files with 276 additions and 6 deletions

View File

@ -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());

View File

@ -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);
}
}
});
}

View File

@ -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([

View File

@ -0,0 +1,19 @@
<?php
namespace App\Endpoint\Api\Http\Controllers;
use App\Endpoint\Api\Http\Resources\Merchant\MessageResource;
use App\Helpers\Paginator as PaginatorHelper;
use App\Models\MerchantMessage as Message;
use Illuminate\Http\Request;
class MessageController extends Controller
{
public function index(Request $request)
{
$list = Message::userMessages($request->user())
->orderBy('created_at', 'desc')
->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50));
return MessageResource::collection($list);
}
}

View File

@ -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) {

View File

@ -0,0 +1,28 @@
<?php
namespace App\Endpoint\Api\Http\Resources\Merchant;
use Illuminate\Http\Resources\Json\JsonResource;
class MessageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'id' => $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(),
];
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Models;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MerchantMessage extends Model
{
use HasFactory;
use HasDateTimeFormatter;
public const JUMP_NO = 0;
public const JUMP_MIINE = 1;
public const JUMP_WAP = 2;
protected $casts = [
'ext'=>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,
]);
}
}

View File

@ -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,
]);
}
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMerchantMessagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('merchant_messages', function (Blueprint $table) {
$table->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');
}
}