| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\message\notice;
- use app\jobs\notice\SmsJob;
- use app\jobs\TaskJob;
- use app\model\system\SystemNotification;
- use app\services\message\NoticeService;
- use app\services\kefu\service\StoreServiceServices;
- use app\services\message\SystemNotificationServices;
- use app\services\serve\ServeServices;
- use crmeb\exceptions\ApiException;
- use crmeb\services\CacheService;
- use think\facade\Log;
- /**
- * 短信发送消息列表
- * Created by PhpStorm.
- * User: xurongyao <763569752@qq.com>
- * Date: 2021/9/22 1:23 PM
- */
- class SmsService extends NoticeService
- {
- /**
- * 短信类型
- * @var string[]
- */
- private $smsType = ['yihaotong', 'aliyun', 'tencent'];
- const url = 'https://api-shss.zthysms.com/v2/sendSms';
- private static $username = 'binfeirlyong';
- private static $password = 'T17L7T10';
- private static $sign = '玉环斌飞日用品商行';
- /**
- * 发送短信消息
- * @param $phone
- * @param array $data
- * @return bool|void
- */
- public function sendSms($phone, array $data)
- {
- try {
- if ($this->noticeInfo['is_sms'] == 1) {
- try {
- $this->send(true, $phone, $data, $this->noticeInfo['mark']);
- return true;
- } catch (\Throwable $e) {
- Log::error('发送短信失败,失败原因:' . $e->getMessage());
- }
- }
- } catch (\Exception $e) {
- Log::error($e->getMessage());
- return true;
- }
- }
- public function send(bool $switch, $phone, array $data, string $mark)
- {
- if ($switch && $phone) {
- //获取发送短信驱动类型
- $type = $this->smsType[sys_config('sms_type', 0)];
- if ($type == 'tencent') {
- $data = $this->handleTencent($mark, $data);
- }
- $sms_txt = SystemNotification::where('mark',$mark)->value('sms_text');
- foreach ($data as $key=>$v)
- {
- $arr[] = '{$'.$key.'}';
- $arr1[] = $v;
- }
- $sms_txt = str_replace($arr,$arr1,$sms_txt);
- $data1['username'] = self::$username;
- $data1['tKey'] = time();
- $data1['password'] = md5(md5(self::$password).$data1['tKey']);
- $data1['mobile'] = $phone;
- $sign = self::$sign;
- $data1['content'] = "【{$sign}】".$sms_txt;
- $rs =$this->do_request(self::url,$data1,[
- 'Content-Type: application/json'
- ],true,true,0);
- $rs = json_decode($rs,true);
- if ($rs['code'] !=200) {
- throw new ApiException($rs['msg']);
- }
- return true;
- } else {
- return false;
- }
- }
- private function do_request($url, $data, $header = null, $post = true, $json = false, $format = 0, $form = false)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- if ($post) {
- curl_setopt($curl, CURLOPT_POST, 1);
- if (!$json && !$form) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
- } else if ($json && !$form) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
- } else {
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- if ($header) {
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- }
- $result = curl_exec($curl);
- @file_put_contents("do.txt",$result.'---'.$url);
- if (curl_errno($curl)) {
- return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
- }
- curl_close($curl);
- return $result;
- }
- /**
- * 发送短信
- * @param bool $switch
- * @param $phone
- * @param array $data
- * @param string $mark
- * @return bool
- */
- public function sendold(bool $switch, $phone, array $data, string $mark)
- {
- if ($switch && $phone) {
- //获取发送短信驱动类型
- $type = $this->smsType[sys_config('sms_type', 0)];
- if ($type == 'tencent') {
- $data = $this->handleTencent($mark, $data);
- }
- $smsMake = app()->make(ServeServices::class)->sms($type);
- $smsId = $mark == 'verify_code' ? app()->make(SystemNotificationServices::class)->value(['mark' => 'verify_code'], 'sms_id') : $this->noticeInfo['sms_id'];
- //发送短信
- $res = $smsMake->send($phone, $smsId, $data);
- if ($res === false) {
- throw new ApiException($smsMake->getError());
- }
- return true;
- } else {
- return false;
- }
- }
- /**
- * 退款发送管理员消息任务
- * @param $order
- * @return bool
- */
- public function sendAdminRefund($order)
- {
- if ($this->noticeInfo['is_sms'] == 1) {
- /** @var StoreServiceServices $StoreServiceServices */
- $StoreServiceServices = app()->make(StoreServiceServices::class);
- $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
- foreach ($adminList as $item) {
- $data = ['order_id' => $order['order_id'], 'admin_name' => $item['nickname']];
- $this->sendSms($item['phone'], $data);
- }
- }
- return true;
- }
- /**
- * 用户确认收货管理员短信提醒
- * @param $switch
- * @param $adminList
- * @param $order
- * @return bool
- */
- public function sendAdminConfirmTakeOver($order)
- {
- if ($this->noticeInfo['is_sms'] == 1) {
- /** @var StoreServiceServices $StoreServiceServices */
- $StoreServiceServices = app()->make(StoreServiceServices::class);
- $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
- foreach ($adminList as $item) {
- $data = ['order_id' => $order['order_id'], 'admin_name' => $item['nickname']];
- $this->sendSms($item['phone'], $data);
- }
- }
- return true;
- }
- /**
- * 下单成功给客服管理员发送短信
- * @param $switch
- * @param $adminList
- * @param $order
- * @return bool
- */
- public function sendAdminPaySuccess($order)
- {
- if ($this->noticeInfo['is_sms'] == 1) {
- /** @var StoreServiceServices $StoreServiceServices */
- $StoreServiceServices = app()->make(StoreServiceServices::class);
- $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
- foreach ($adminList as $item) {
- $data = ['order_id' => $order['order_id'], 'admin_name' => $item['nickname']];
- $this->sendSms($item['phone'], $data);
- }
- }
- return true;
- }
- /**
- * 处理腾讯云参数
- * @param $mark
- * @param $data
- * @return array
- */
- public function handleTencent($mark, $data)
- {
- $result = [];
- switch ($mark) {
- case 'verify_code':
- $result = [(string)$data['code'], (string)$data['time']];
- break;
- case 'send_order_refund_no_status':
- case 'order_pay_false':
- $result = [$data['order_id']];
- break;
- case 'price_revision':
- $result = [$data['order_id'], (string)$data['pay_price']];
- break;
- case 'order_pay_success':
- $result = [(string)$data['pay_price'], $data['order_id']];
- break;
- case 'order_take':
- $result = [$data['order_id'], $data['store_name']];
- break;
- case 'send_order_apply_refund':
- case 'admin_pay_success_code':
- case 'send_admin_confirm_take_over':
- $result = [$data['admin_name'], $data['order_id']];
- break;
- case 'order_deliver_success':
- case 'order_postage_success':
- $result = [$data['nickname'], $data['store_name'], $data['order_id']];
- break;
- case 'order_refund':
- $result = [$data['order_id'], $data['refund_price']];
- break;
- case 'recharge_success':
- $result = [$data['price'], $data['now_money']];
- break;
- case 'sign_remind':
- $result = [$data['site_name']];
- break;
- }
- return $result;
- }
- }
|