调试极光推送

This commit is contained in:
luofei 2023-06-30 17:27:43 +08:00
parent 8f522e588d
commit 52ef67b76b
5 changed files with 235 additions and 1 deletions

View File

@ -34,7 +34,7 @@ class SystemNoticeConfigDao extends BaseDao
public function getNoticeStatusByConstKey(string $key)
{
$value = $this->getModel()::getDb()->where('const_key',$key)->field('notice_sys,notice_wechat,notice_routine,notice_sms')->find();
$value = $this->getModel()::getDb()->where('const_key',$key)->field('notice_sys,notice_wechat,notice_routine,notice_sms,notice_app')->find();
return $value;
}

View File

@ -0,0 +1,200 @@
<?php
namespace app\common\service;
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;
class JgPush
{
public $client;
public $push;
public function __construct()
{
$this->client = new Client('b5f679f4357018605ea6fd2e', 'c4fb573758f8d7058d697c54');
$this->push = $this->client->push();
}
public function init($type, $data)
{
switch ($type) {
case 'ADMIN_PAY_SUCCESS_CODE':
$groupOrder = app()->make(StoreGroupOrderRepository::class)->get($data['group_order_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;
}
$this->addRegistrationId($jgRegisterId);
$this->androidNotification('您有新的订单,请注意查看。', ['extras' => ['route' => $route]]);
$this->iosNotification('您有新的订单,请注意查看。', ['extras' => ['route' => $route]]);
}
}
break;
case 'DELIVER_GOODS_CODE':
case 'ORDER_DELIVER_SUCCESS':
$order = app()->make(StoreOrderRepository::class)->get($data['order_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]]);
}
}
break;
default:
break;
}
$this->setPlatform();
return $this;
}
/**
* 设置推送平台
* @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();
}
}

View File

@ -393,6 +393,25 @@ class Auth extends BaseController
return app('json')->success();
}
/**
* 绑定极光register_id
* @param UserRepository $repository
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function bindJg(UserRepository $repository)
{
$phone = $this->request->param('phone');
$jgRegisterId = $this->request->param('jg_register_id');
$user = $repository->accountByUser($phone);
if ($user->save(['jg_register_id' => $jgRegisterId]) === false) {
return app('json')->fail('绑定失败');
}
return app('json')->success();
}
public function getCaptcha()
{
$codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));

View File

@ -15,9 +15,11 @@ namespace crmeb\jobs;
use app\common\repositories\system\notice\SystemNoticeConfigRepository;
use app\common\service\JgPush;
use crmeb\interfaces\JobInterface;
use crmeb\services\SmsService;
use crmeb\services\WechatTemplateMessageService;
use crmeb\utils\DingTalk;
use think\facade\Log;
class SendSmsJob implements JobInterface
@ -26,6 +28,17 @@ class SendSmsJob implements JobInterface
public function fire($job, $data)
{
$status = app()->make(SystemNoticeConfigRepository::class)->getNoticeStatusByConstKey($data['tempId']);
if ($status['notice_app'] == 1) {
try {
/** @var JgPush $push */
$push = app()->make(JgPush::class);
Log::info('app推送发送数据' . var_export($data, 1));
$push->init($data['tempId'], $data)->push->send();
} catch (\Exception $e) {
Log::info('app推送消息发送失败' . var_export($data, 1) . $e->getMessage());
DingTalk::exception($e, 'app推送消息发送失败' . var_export($data, 1));
}
}
if ($status['notice_sms'] == 1) {
try {
SmsService::sendMessage($data);

View File

@ -602,6 +602,8 @@ Route::group('api/', function () {
Route::post('auth/mp', 'api.Auth/mpAuth');
//绑定小程序账号
Route::post('auth/bindMp', 'api.Auth/bindMp');
//绑定极光register_id
Route::post('auth/bindJg', 'api.Auth/bindJg');
//app授权
Route::post('auth/app', 'api.Auth/appAuth');
//apple授权