252 lines
9.1 KiB
PHP
252 lines
9.1 KiB
PHP
<?php
|
||
|
||
namespace app\common\service;
|
||
|
||
use app\common\model\store\order\StoreGroupOrder;
|
||
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;
|
||
use think\facade\Log;
|
||
|
||
class JgPush
|
||
{
|
||
|
||
public $client;
|
||
public $push;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->client = new Client('b5f679f4357018605ea6fd2e', 'c4fb573758f8d7058d697c54');
|
||
$this->push = $this->client->push();
|
||
$this->setPlatform();
|
||
}
|
||
|
||
public function send($type, $data)
|
||
{
|
||
Log::info("sms ============= sms");
|
||
switch ($type) {
|
||
case 'ADMIN_PAY_SUCCESS_CODE':
|
||
$groupOrder = app()->make(StoreGroupOrderRepository::class)->get($data['id']);
|
||
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;
|
||
}
|
||
$msg = $order['pay_type'] == StoreGroupOrder::PAY_TYPE_CREDIT_BUY ? '您有新的订单已结算,请注意查看。' : '您有新的订单,请注意查看。';
|
||
$this->addRegistrationId($jgRegisterId);
|
||
$this->androidNotification($msg, ['extras' => ['route' => $route]]);
|
||
$this->iosNotification($msg, ['extras' => ['route' => $route]]);
|
||
$this->push->send();
|
||
}
|
||
}
|
||
break;
|
||
case 'DELIVER_GOODS_CODE':
|
||
case 'ORDER_DELIVER_SUCCESS':
|
||
$order = app()->make(StoreOrderRepository::class)->get($data['id']);
|
||
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)) {
|
||
$this->addRegistrationId($jgRegisterId);
|
||
$this->androidNotification('您的订单已发货,请注意查看。', ['extras' => ['route' => $route]]);
|
||
$this->iosNotification('您的订单已发货,请注意查看。', ['extras' => ['route' => $route]]);
|
||
$this->push->send();
|
||
}
|
||
}
|
||
break;
|
||
case 'MERCHANT_CREDIT_BUY_NOTICE':
|
||
$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;
|
||
}
|
||
$this->addRegistrationId($jgRegisterId);
|
||
$this->androidNotification('您的先货后款订单即将逾期,请尽快处理!', ['extras' => ['route' => $route]]);
|
||
$this->iosNotification('您的先货后款订单即将逾期,请尽快处理!', ['extras' => ['route' => $route]]);
|
||
$this->push->send();
|
||
break;
|
||
case 'ORDER_CREATE':
|
||
Log::info("sms ============= 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;
|
||
}
|
||
$this->addRegistrationId($jgRegisterId);
|
||
$this->androidNotification('您有新的订单,请注意查看111。', ['extras' => ['route' => $route, 'type' =>'ORDER_CREATE']]);
|
||
$this->iosNotification('您有新的订单,请注意查看111。', ['extras' => ['route' => $route, 'type' =>'ORDER_CREATE']]);
|
||
$this->push->send();
|
||
Log::info("ORDER_CREATE ============= 您有新的订单111,请注意查看。");
|
||
break;
|
||
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;
|
||
}
|
||
$this->addRegistrationId($jgRegisterId);
|
||
$this->androidNotification('您有新的订单,请注意查看222。', ['extras' => ['route' => $route, 'type' =>'ORDER_PAY_SUCCESS']]);
|
||
$this->iosNotification('您有新的订单,请注意查看222。', ['extras' => ['route' => $route, 'type' =>'ORDER_PAY_SUCCESS']]);
|
||
$this->push->send();
|
||
Log::info("ORDER_PAY_SUCCESS ============= 您有新的订单222,请注意查看。");
|
||
break;
|
||
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();
|
||
}
|
||
|
||
} |