shop-php/app/common/service/JgPush.php

340 lines
13 KiB
PHP
Raw Normal View History

2023-06-30 17:27:43 +08:00
<?php
namespace app\common\service;
2023-07-13 13:46:50 +08:00
use app\common\model\store\order\StoreGroupOrder;
2023-06-30 17:27:43 +08:00
use app\common\model\system\merchant\Merchant;
use app\common\model\user\User;
use app\common\repositories\store\order\StoreGroupOrderRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use JPush\Client;
2023-08-16 10:30:32 +08:00
use think\facade\Log;
2023-06-30 17:27:43 +08:00
class JgPush
{
public $client;
public $push;
2023-09-28 18:22:38 +08:00
public $iosKey;
public $iosSecret;
public $azKey;
public $azSecret;
2023-06-30 17:27:43 +08:00
public function __construct()
{
2023-09-28 18:22:38 +08:00
// ios
// $this->client = new Client('8a5efd65cda14fafa6e64ad3', 'daebe19b547c43128796a078');
$this->iosKey = '8a5efd65cda14fafa6e64ad3';
$this->iosSecret = 'daebe19b547c43128796a078';
// 安卓
// $this->client = new Client('b5f679f4357018605ea6fd2e', 'c4fb573758f8d7058d697c54');
$this->azKey = 'b5f679f4357018605ea6fd2e';
$this->azSecret = 'c4fb573758f8d7058d697c54';
// $this->push = $this->client->push();
// $this->setPlatform();
2023-06-30 17:27:43 +08:00
}
2023-07-01 16:14:09 +08:00
public function send($type, $data)
2023-06-30 17:27:43 +08:00
{
switch ($type) {
case 'ADMIN_PAY_SUCCESS_CODE':
2023-07-01 15:06:03 +08:00
$groupOrder = app()->make(StoreGroupOrderRepository::class)->get($data['id']);
2023-06-30 17:27:43 +08:00
if ($groupOrder) {
foreach ($groupOrder->orderList as $order) {
$route = "/pages/admin/orderDetail/index?id={$order['order_id']}&mer_id={$order['mer_id']}";
$merUserId = Merchant::where('mer_id', $order->mer_id)->value('uid');
$jgRegisterId = User::where('uid', $merUserId)->value('jg_register_id');
if (empty($jgRegisterId)) {
continue;
}
2023-08-16 17:19:30 +08:00
$msg = $order['pay_type'] == StoreGroupOrder::PAY_TYPE_CREDIT_BUY ? '您有新的订单已结算,请注意查看。' : '您有新的订单,请注意查看。';
2023-08-16 17:23:03 +08:00
$msgType = $order['pay_type'] == StoreGroupOrder::PAY_TYPE_CREDIT_BUY ? 'ORDER_SETTLEMENT' : 'ORDER_CREATE';
2023-09-28 18:22:38 +08:00
try {
$this->client = new Client($this->iosKey, $this->iosSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->iosNotification($msg, ['extras' => ['route' => $route, 'type' => $msgType]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
try {
$this->client = new Client($this->azKey, $this->azSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->androidNotification($msg, ['extras' => ['route' => $route, 'type' => $msgType]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
2023-06-30 17:27:43 +08:00
}
}
break;
case 'DELIVER_GOODS_CODE':
case 'ORDER_DELIVER_SUCCESS':
2023-07-01 15:06:03 +08:00
$order = app()->make(StoreOrderRepository::class)->get($data['id']);
2023-06-30 17:27:43 +08:00
if ($order) {
$route = "/pages/order_details/index?order_id={$order['order_id']}";
$jgRegisterId = User::where('uid', $order['uid'])->value('jg_register_id');
if (!empty($jgRegisterId)) {
2023-09-28 18:22:38 +08:00
$msg = '您的订单已发货,请注意查看。';
try {
$this->client = new Client($this->iosKey, $this->iosSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->iosNotification($msg, ['extras' => ['route' => $route]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
try {
$this->client = new Client($this->azKey, $this->azSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->androidNotification($msg, ['extras' => ['route' => $route]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
2023-06-30 17:27:43 +08:00
}
}
break;
case 'MERCHANT_CREDIT_BUY_NOTICE':
2023-09-07 15:04:19 +08:00
// 委托商品是否需要提醒信息
2023-07-12 15:41:41 +08:00
$route = "/pages/order_details/stay?order_id={$data['orderId']}&product_type=98";
$merUserId = Merchant::where('mer_id', $data['id'])->value('uid');
$jgRegisterId = User::where('uid', $merUserId)->value('jg_register_id');
if (empty($jgRegisterId)) {
break;
}
2023-09-28 18:22:38 +08:00
$msg = '您的先货后款订单即将逾期,请尽快处理!';
try {
$this->client = new Client($this->iosKey, $this->iosSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->iosNotification($msg, ['extras' => ['route' => $route]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
try {
$this->client = new Client($this->azKey, $this->azSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->androidNotification($msg, ['extras' => ['route' => $route]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
break;
2023-07-13 13:46:50 +08:00
case 'ORDER_CREATE':
$order = app()->make(StoreOrderRepository::class)->get($data['id']);
if ($order->pay_type != StoreGroupOrder::PAY_TYPE_CREDIT_BUY) {
break;
}
$route = "/pages/admin/orderDetail/index?id={$order['order_id']}&mer_id={$order['mer_id']}";
$merUserId = Merchant::where('mer_id', $order['mer_id'])->value('uid');
$jgRegisterId = User::where('uid', $merUserId)->value('jg_register_id');
if (empty($jgRegisterId)) {
break;
}
2023-09-28 18:22:38 +08:00
$msg = '您有新的订单,请注意查看。';
$type = 'ORDER_CREATE';
try {
$this->client = new Client($this->iosKey, $this->iosSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->iosNotification($msg, ['extras' => ['route' => $route, 'type' => $type]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
try {
$this->client = new Client($this->azKey, $this->azSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->androidNotification($msg, ['extras' => ['route' => $route, 'type' => $type]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
2023-07-13 13:46:50 +08:00
break;
2023-08-16 10:30:32 +08:00
case 'ORDER_PAY_SUCCESS':
$order = app()->make(StoreOrderRepository::class)->get($data['id']);
if ($order->pay_type != StoreGroupOrder::PAY_TYPE_CREDIT_BUY) {
break;
}
$route = "/pages/admin/orderDetail/index?id={$order['order_id']}&mer_id={$order['mer_id']}";
$merUserId = Merchant::where('mer_id', $order['mer_id'])->value('uid');
$jgRegisterId = User::where('uid', $merUserId)->value('jg_register_id');
if (empty($jgRegisterId)) {
break;
}
2023-09-28 18:22:38 +08:00
$msg = '您有新的订单,请注意查看。';
$type = 'ORDER_PAY_SUCCESS';
try {
$this->client = new Client($this->iosKey, $this->iosSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->iosNotification($msg, ['extras' => ['route' => $route, 'type' => $type]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
try {
$this->client = new Client($this->azKey, $this->azSecret);
$this->push = $this->client->push();
$this->setPlatform();
$this->addRegistrationId($jgRegisterId);
$this->androidNotification($msg, ['extras' => ['route' => $route, 'type' => $type]]);
$this->push->send();
unset($this->client, $this->push);
} catch (\Exception $e) {}
2023-08-16 10:30:32 +08:00
break;
2023-06-30 17:27:43 +08:00
default:
break;
}
}
/**
* 设置推送平台
* @param array $platform
*/
public function setPlatform(array $platform = ['android', 'ios'])
{
$this->push->setPlatform($platform);
}
/**
* 设置推送设备
* @param $registrationId
* @return void
*/
public function addRegistrationId($registrationId)
{
$this->push->addRegistrationId($registrationId);
}
/**
* 给所有平台推送相同的 alert 消息
* @param $alert
* @return void
*/
public function setNotificationAlert($alert)
{
$this->push->setNotificationAlert($alert);
}
/**
* ios平台通知
* @param $alert
* @param $extras
* @return void
* @example $extras = ['sound' => 'sound', 'badge' => '+1', 'extras' => ['key' => 'value']
*/
public function iosNotification($alert, $extras = [])
{
$this->push->iosNotification($alert, $extras);
}
/**
* android平台通知
* @param $alert
* @param $extras
* @return void
* @example $extras = ['alert' => 'alert', 'title' => 'title', 'extras' => ['key' => 'value']
*/
public function androidNotification($alert, $extras = [])
{
$this->push->androidNotification($alert, $extras);
}
/**
* iOS VOIP 功能
* @param $extras
* @return void
* @example $extras = ['key' => 'value'] //任意自定义 key/value 对,会透传给 APP
*/
public function voip($extras = [])
{
$this->push->voip($extras);
}
/**
* @param $msg_content
* @param array $extras
* @return void
* @example $extras = ['title' => 'title', 'content_type' => 'text', 'extras' => ['key' => 'value']
*/
public function message($msg_content, array $extras = [])
{
$this->push->message($msg_content, $extras);
}
/**
* 推送可选项
* @param array $options
* @return void
* @example $options = ['sendno' => 100, 'time_to_live' => 1, 'apns_production' => false, 'big_push_duration' => 1]
* sendno: 表示推送序号,纯粹用来作为 API 调用标识,
* time_to_live: 表示离线消息保留时长()
* apns_production: 表示APNs是否生产环境
* big_push_duration: 表示定速推送时长(分钟),又名缓慢推送
*/
public function options(array $options = [])
{
$this->push->options($options);
}
/**
* 发送短信通知
* @param int $delay 延迟时间,单位秒
* @param string $templateId 模板id
* @param array $param 模板参数
* @return void
*/
public function setSms(int $delay, string $templateId, array $param = [])
{
$this->push->setSms($delay, $templateId, $param);
}
/**
* 设备标签
* @param $tag
*/
public function addTag($tag)
{
$this->push->addTag($tag);
}
/**
* 设备标签AND
* @param $tag
*/
public function addTagAnd($tag)
{
$this->push->addTagAnd($tag);
}
/**
* 设备别名
* @param $alias
*/
public function addAlias($alias)
{
$this->push->addAlias($alias);
}
/**
* 所有用户
*/
public function addAllAudience()
{
$this->push->addAllAudience();
}
}