FooTask.php 3.66 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
<?php

namespace App\Task;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use App\Model\DevicesModel;
use App\Service\QueueService;
use App\Service\ALiSmsService;
use Hyperf\Utils\ApplicationContext;
class FooTask
{
    /**
     * @Inject
     * @var QueueService
     */
    protected $service;

    private $logger;

    private $DevicesModel;

    private $redisClient;

    /**
     * @Inject
     * @var ALiSmsService
     */
    protected $smsphone;

    public function __construct(LoggerFactory $loggerFactory, DevicesModel $devicesModel)
    {
        $this->logger = $loggerFactory->get('log', 'default');
        $container = ApplicationContext::getContainer();
        $this->redisClient = $container->get(\Redis::class);
        $this->DevicesModel = $devicesModel;
    }

    public function execute()
    {
        $data = $this->DevicesModel->devicephone();
        if ($data != '') {
            $this->jsondata($data);
        }

    }

    //解出 json 字符串
    public function jsondata($info)
    {
        $data = [];
        $infos = json_decode($info, true);
        foreach ($infos as $key => $value) {
            if (substr($value['phone'], 0, 1) == '[') {
                $newphone = json_decode($value['phone'], true);
                foreach ($newphone as $k => $v) {
                    $infodata = ['phone' => $v['cphone'], 'devicenum' => substr($value['devicenum'], -3), 'typedevicenum' => $value['devicenum']];
                    array_push($data, $infodata);
                }
            } else {
                $infodatas = ['phone' => $value['phone'], 'devicenum' => substr($value['devicenum'], -3), 'typedevicenum' => $value['devicenum']];
                array_push($data, $infodatas);
            }
        }
        return $this->toarray($data);

    }
//
    /*二维数组去重*/
    public function toarray($data)
    {
        $public_info = [];
        for ($i = 0; $i < count($data); $i++) {
            $a = $data[$i];
            unset($data[$i]);
            if (!in_array($a, $data)) {
                $public_info[] = $a;
            }
        }
        return $this->example($public_info);
    }
//
    /*数组总量进行判断*/
    public function example($data)
    {
        if (count($data) > 1) {
            foreach ($data as $key => $value) {
                $newdata = ['phone' => $value['phone'], 'devicenum' => $value['devicenum'], 'typedevicenum' => $value['typedevicenum']];
                $this->service->push($newdata);
            }
        } else {
            if(!$this->redisClient->get($data['typedevicenum'])){
                $smsdata = $data[0]['phone'];
                $template_param = ['cphone' => $data[0]['phone'], 'position' => '您的设备尾号为' . $data[0]['devicenum'], 'alarm' => '离线'];
                $typeinfo = $this->smsphone($template_param, $smsdata);
                if ($typeinfo['Message'] == 'OK' ||$typeinfo['Code'] == 'OK') {
                    $redisdata = ['data'=>$data,'sendSms'=>json_decode($typeinfo, true),'time'=>time()];
                    $this->redisClient->set($data[0]['typedevicenum'], json_encode($redisdata));
                }
            }
        }
    }

    public function smsphone($template_param, $phone)
    {
        $params = array(
            'SignName' => '泽宏云',
            'AccessKeyId' => 'LTAI2xiZNF3iV2aV',
            'TemplateCode' => 'SMS_169897307',
            'PhoneNumbers' => $phone,
            'TemplateParam' => json_encode($template_param, JSON_UNESCAPED_UNICODE),
            'RegionId' => 'cn-beijing',
        );
        return $this->smsphone->sendSms('bprEWwn1M0xgglRQCQEMYSPiYctDk4', $params);
    }
}