template
?并配置了對應的模板IDuser
?用戶,通過配置?NOTIFICATION_ACCOUNT
?實現(xiàn),以逗號分割多個用戶NOTIFICATION_ACCOUNT = "1,2"
示例代碼
$invoice = [
'type' => InvoicePaid::NOTIFICATION_TYPE_DEAL,
'title' => '恭喜您!購買的商品已支付成功,我們會盡快安排發(fā)貨哦!么么噠!~~',
'list' => [
[
'keyword' => '訂單編號',
'data' => $parameter['identification']
],
[
'keyword' => '商品名稱',
'data' => $parameter['name']
],
[
'keyword' => '訂單總額',
'data' => sprintf("%01.2f", $parameter['total'] / 100),
],
[
'keyword' => '支付方式',
'data' => $parameter['type']
],
[
'keyword' => '訂單狀態(tài)',
'data' => '已支付'
],
[
'keyword' => '下單時間',
'data' => $parameter['time']
]
],
'price' => $parameter['total'],
'url' => '/pages/indent/detail?id=' . $parameter['id'],
'parameter' => $parameter,
'prefers' => ['database', 'mail', 'wechat']
];
$user = User::find($parameter['user_id']);
$user->notify(new InvoicePaid($invoice));
屬性 | 類型 | 默認值 | 必填 | 說明 |
---|---|---|---|---|
title | string | 是 | 標題,不要太長,太長引響排版 | |
type | number | 1 | 是 | 通知類型:1系統(tǒng)消息(默認)2交易 |
url | string | 否 | 跳轉地址,格式如:/pages/finance/bill_show?id=1 | |
image | string | 否 | 圖片地址 | |
price | number | 否 | 金額,分,如100(1元),類型為2時必填 | |
list | array | 否 | 列表 | |
remark | string | 否 | 備注 | |
admin | Boole | 否 | 是否為后臺通知,true:是,為空:否 | |
user_id | string | 否 | 用戶ID,如admin為true時,不用傳 | |
parameter | array | 否 | 不需要傳 | |
template | string | 否 | 通知模板標識,目前只有微信公眾號才需要,見下表 | |
prefers | array | ['database'] | 否 | 通知途徑:默認為站內信 |
屬性 | 類型 | 默認值 | 必填 | 說明 |
---|---|---|---|---|
keyword | string | 是 | 鍵名,一般為4個字符,不然引響排版美觀 | |
data | string | 是 | 鍵值,一般為20個字符以內,不然引響排版美觀 | |
copy | boolean | 是 | 是否支持拷貝,true:是,false:否 |
值 | 說明 | 微信公眾號模版編號 |
---|---|---|
finish_payment | 訂單支付成功通知 | OPENTM416836000 |
order_confirm_receipt | 訂單確認收貨通知 | OPENTM202314085 |
delivery_release | 發(fā)貨通知 | OPENTM414956350 |
refund_success | 退款成功通知 | OPENTM414889350 |
admin_order_send_good | 待發(fā)貨提醒 | OPENTM409521500 |
admin_order_completion | 訂單完成通知 | OPENTM412598101 |
WECHAT_SUBSCRIPTION_INFORMATION_FINISH_PAYMENT=
WECHAT_SUBSCRIPTION_INFORMATION_ORDER_CONFIRM_RECEIPT=
WECHAT_SUBSCRIPTION_INFORMATION_SHIPMENTS=
WECHAT_SUBSCRIPTION_INFORMATION_REFUND_SUCCESS=
WECHAT_SUBSCRIPTION_INFORMATION_ADMIN_ORDER_SEND_GOOD=
WECHAT_SUBSCRIPTION_INFORMATION_ADMIN_ORDER_COMPLETION=
值 | 說明 |
---|---|
database | 站內信,默認 |
sms | 短信 |
郵箱 | |
miniweixin | 微信小程序訂閱消息 |
微信公眾號模板消息 |
Notifications/InvoicePaid.php
?,?prefers
?中添加或刪除通知途徑即可以下以微信公眾號通知為例
命名規(guī)則:以下劃線拼接,如是管理員通知,名稱前面加?admin_
?,如?finish_payment
?、?admin_subscribe
?
#api\config\notification.php
'wechat'=>[ //微信公眾號
'finish_payment'=>env('WECHAT_SUBSCRIPTION_INFORMATION_FINISH_PAYMENT',''), //訂單支付成功通知
],
命名規(guī)則:根據上一步配置的常量名去掉下劃線,并采用駝峰命名法命名,如?finishPayment
?、?adminSubscribe
?
#api\app\Channels\WechatChannel.php
protected function finishPayment($notifiable, $message)
{
$data = [
'template_id' => $this->information[$message['template']],
'touser' => $notifiable->wechat,
'data' => [
'first' => '恭喜您!購買的商品已支付成功,我們會盡快安排發(fā)貨哦!么么噠!~~',
'keyword1' => $message['identification'],
'keyword2' => $message['name'],
'keyword3' => sprintf("%01.2f", $message['total'] / 100),
'keyword4' => '已支付',
'keyword5' => $message['time'],
'remark' => '歡迎您的到來!',
],
];
if ($this->miniweixin) {
$data['miniprogram'] = [
'appid' => $this->miniweixin,
'pagepath' => '/pages/indent/detail?id=' . $message['id'],
];
} else {
$data['url'] = request()->root() . '/h5/#/pages/indent/detail?id=' . $message['id'];
}
//發(fā)送記錄
$send = $this->app->template_message->send($data);
$NotificationLog = new NotificationLog();
$NotificationLog->user_id = $message['user_id'];
$NotificationLog->type = NotificationLog::NOTIFICATION_LOG_TYPE_MINIWEIXIN;
$NotificationLog->msg = json_encode($data);
$NotificationLog->feedback = json_encode($send);
$NotificationLog->state = $send['errcode'] == 0 ? NotificationLog::NOTIFICATION_LOG_STATE_OK : NotificationLog::NOTIFICATION_LOG_STATE_ERROR;
$NotificationLog->save();
}
$user = App\User::find(1);
$user->notifications->toArray(); //所用該用戶下的通知
$user->unreadNotifications->toArray(); //所用該用戶下未讀的通知
$user->unreadNotifications->where('id','8b9e0bc4-eb3f-4d36-aad9-2dce24a495c6')->markAsRead(); //對某個信息進行已讀標記
$user->unreadNotifications()->update(['read_at' => now()]); //所有的通知標為已讀
更多建議: