SmsService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\message\notice;
  12. use app\jobs\notice\SmsJob;
  13. use app\jobs\TaskJob;
  14. use app\model\system\SystemNotification;
  15. use app\services\message\NoticeService;
  16. use app\services\kefu\service\StoreServiceServices;
  17. use app\services\message\SystemNotificationServices;
  18. use app\services\serve\ServeServices;
  19. use crmeb\exceptions\ApiException;
  20. use crmeb\services\CacheService;
  21. use think\facade\Log;
  22. /**
  23. * 短信发送消息列表
  24. * Created by PhpStorm.
  25. * User: xurongyao <763569752@qq.com>
  26. * Date: 2021/9/22 1:23 PM
  27. */
  28. class SmsService extends NoticeService
  29. {
  30. /**
  31. * 短信类型
  32. * @var string[]
  33. */
  34. private $smsType = ['yihaotong', 'aliyun', 'tencent'];
  35. const url = 'https://api-shss.zthysms.com/v2/sendSms';
  36. private static $username = 'binfeirlyong';
  37. private static $password = 'T17L7T10';
  38. private static $sign = '玉环斌飞日用品商行';
  39. /**
  40. * 发送短信消息
  41. * @param $phone
  42. * @param array $data
  43. * @return bool|void
  44. */
  45. public function sendSms($phone, array $data)
  46. {
  47. try {
  48. if ($this->noticeInfo['is_sms'] == 1) {
  49. try {
  50. $this->send(true, $phone, $data, $this->noticeInfo['mark']);
  51. return true;
  52. } catch (\Throwable $e) {
  53. Log::error('发送短信失败,失败原因:' . $e->getMessage());
  54. }
  55. }
  56. } catch (\Exception $e) {
  57. Log::error($e->getMessage());
  58. return true;
  59. }
  60. }
  61. public function send(bool $switch, $phone, array $data, string $mark)
  62. {
  63. if ($switch && $phone) {
  64. //获取发送短信驱动类型
  65. $type = $this->smsType[sys_config('sms_type', 0)];
  66. if ($type == 'tencent') {
  67. $data = $this->handleTencent($mark, $data);
  68. }
  69. $sms_txt = SystemNotification::where('mark',$mark)->value('sms_text');
  70. foreach ($data as $key=>$v)
  71. {
  72. $arr[] = '{$'.$key.'}';
  73. $arr1[] = $v;
  74. }
  75. $sms_txt = str_replace($arr,$arr1,$sms_txt);
  76. $data1['username'] = self::$username;
  77. $data1['tKey'] = time();
  78. $data1['password'] = md5(md5(self::$password).$data1['tKey']);
  79. $data1['mobile'] = $phone;
  80. $sign = self::$sign;
  81. $data1['content'] = "【{$sign}】".$sms_txt;
  82. $rs =$this->do_request(self::url,$data1,[
  83. 'Content-Type: application/json'
  84. ],true,true,0);
  85. $rs = json_decode($rs,true);
  86. if ($rs['code'] !=200) {
  87. throw new ApiException($rs['msg']);
  88. }
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. private function do_request($url, $data, $header = null, $post = true, $json = false, $format = 0, $form = false)
  95. {
  96. $curl = curl_init();
  97. curl_setopt($curl, CURLOPT_URL, $url);
  98. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  99. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  100. if ($post) {
  101. curl_setopt($curl, CURLOPT_POST, 1);
  102. if (!$json && !$form) {
  103. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  104. } else if ($json && !$form) {
  105. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
  106. } else {
  107. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  108. }
  109. }
  110. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  111. if ($header) {
  112. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  113. curl_setopt($curl, CURLOPT_HEADER, 0);
  114. }
  115. $result = curl_exec($curl);
  116. @file_put_contents("do.txt",$result.'---'.$url);
  117. if (curl_errno($curl)) {
  118. return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
  119. }
  120. curl_close($curl);
  121. return $result;
  122. }
  123. /**
  124. * 发送短信
  125. * @param bool $switch
  126. * @param $phone
  127. * @param array $data
  128. * @param string $mark
  129. * @return bool
  130. */
  131. public function sendold(bool $switch, $phone, array $data, string $mark)
  132. {
  133. if ($switch && $phone) {
  134. //获取发送短信驱动类型
  135. $type = $this->smsType[sys_config('sms_type', 0)];
  136. if ($type == 'tencent') {
  137. $data = $this->handleTencent($mark, $data);
  138. }
  139. $smsMake = app()->make(ServeServices::class)->sms($type);
  140. $smsId = $mark == 'verify_code' ? app()->make(SystemNotificationServices::class)->value(['mark' => 'verify_code'], 'sms_id') : $this->noticeInfo['sms_id'];
  141. //发送短信
  142. $res = $smsMake->send($phone, $smsId, $data);
  143. if ($res === false) {
  144. throw new ApiException($smsMake->getError());
  145. }
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * 退款发送管理员消息任务
  153. * @param $order
  154. * @return bool
  155. */
  156. public function sendAdminRefund($order)
  157. {
  158. if ($this->noticeInfo['is_sms'] == 1) {
  159. /** @var StoreServiceServices $StoreServiceServices */
  160. $StoreServiceServices = app()->make(StoreServiceServices::class);
  161. $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
  162. foreach ($adminList as $item) {
  163. $data = ['order_id' => $order['order_id'], 'admin_name' => $item['nickname']];
  164. $this->sendSms($item['phone'], $data);
  165. }
  166. }
  167. return true;
  168. }
  169. /**
  170. * 用户确认收货管理员短信提醒
  171. * @param $switch
  172. * @param $adminList
  173. * @param $order
  174. * @return bool
  175. */
  176. public function sendAdminConfirmTakeOver($order)
  177. {
  178. if ($this->noticeInfo['is_sms'] == 1) {
  179. /** @var StoreServiceServices $StoreServiceServices */
  180. $StoreServiceServices = app()->make(StoreServiceServices::class);
  181. $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
  182. foreach ($adminList as $item) {
  183. $data = ['order_id' => $order['order_id'], 'admin_name' => $item['nickname']];
  184. $this->sendSms($item['phone'], $data);
  185. }
  186. }
  187. return true;
  188. }
  189. /**
  190. * 下单成功给客服管理员发送短信
  191. * @param $switch
  192. * @param $adminList
  193. * @param $order
  194. * @return bool
  195. */
  196. public function sendAdminPaySuccess($order)
  197. {
  198. if ($this->noticeInfo['is_sms'] == 1) {
  199. /** @var StoreServiceServices $StoreServiceServices */
  200. $StoreServiceServices = app()->make(StoreServiceServices::class);
  201. $adminList = $StoreServiceServices->getStoreServiceOrderNotice();
  202. foreach ($adminList as $item) {
  203. $data = ['order_id' => $order['order_id'], 'admin_name' => $item['nickname']];
  204. $this->sendSms($item['phone'], $data);
  205. }
  206. }
  207. return true;
  208. }
  209. /**
  210. * 处理腾讯云参数
  211. * @param $mark
  212. * @param $data
  213. * @return array
  214. */
  215. public function handleTencent($mark, $data)
  216. {
  217. $result = [];
  218. switch ($mark) {
  219. case 'verify_code':
  220. $result = [(string)$data['code'], (string)$data['time']];
  221. break;
  222. case 'send_order_refund_no_status':
  223. case 'order_pay_false':
  224. $result = [$data['order_id']];
  225. break;
  226. case 'price_revision':
  227. $result = [$data['order_id'], (string)$data['pay_price']];
  228. break;
  229. case 'order_pay_success':
  230. $result = [(string)$data['pay_price'], $data['order_id']];
  231. break;
  232. case 'order_take':
  233. $result = [$data['order_id'], $data['store_name']];
  234. break;
  235. case 'send_order_apply_refund':
  236. case 'admin_pay_success_code':
  237. case 'send_admin_confirm_take_over':
  238. $result = [$data['admin_name'], $data['order_id']];
  239. break;
  240. case 'order_deliver_success':
  241. case 'order_postage_success':
  242. $result = [$data['nickname'], $data['store_name'], $data['order_id']];
  243. break;
  244. case 'order_refund':
  245. $result = [$data['order_id'], $data['refund_price']];
  246. break;
  247. case 'recharge_success':
  248. $result = [$data['price'], $data['now_money']];
  249. break;
  250. case 'sign_remind':
  251. $result = [$data['site_name']];
  252. break;
  253. }
  254. return $result;
  255. }
  256. }