ServiceSmsV1.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: vowkin
  5. * Date: 2017/5/22
  6. * Time: 21:49
  7. */
  8. namespace app\common\service;
  9. use app\common\model\OrderModel;
  10. use app\common\model\UserModel;
  11. use think\Config;
  12. class ServiceSmsV1 extends BaseService
  13. {
  14. private $chuanglan_config='';
  15. public function __construct()
  16. {
  17. //创蓝发送短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  18. $this->chuanglan_config['api_send_url'] = 'http://smssh1.253.com/msg/send/json';
  19. //创蓝变量短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  20. $this->chuanglan_config['API_VARIABLE_URL'] = 'http://xxx/msg/variable/json';
  21. //创蓝短信余额查询接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  22. $this->chuanglan_config['api_balance_query_url'] = 'http://xxx/msg/balance/json';
  23. //创蓝账号 替换成你自己的账号
  24. $this->chuanglan_config['api_account'] = 'N5737774';
  25. //创蓝密码 替换成你自己的密码
  26. $this->chuanglan_config['api_password'] = 'we7da5QAZ0a3ec';
  27. }
  28. /**南哥,下面的方法就是你需要发送短信的dome 可直接调用发送短信***************************************************************************/
  29. //短信验证码发送
  30. public static function sendCode(){
  31. $clapi = new ServiceSmsV1();
  32. $code = mt_rand(1000,9999);//这是短信验证码。发送成功后返回验证码,需要验证时使用,存session
  33. $mobile='15329884885';//这是接收者手机号码
  34. $message='亲爱的用户,您的短信验证码为'.$code.',1分钟内有效,若非本人操作请忽略。';//这里是需要发送的短信模板,参数传递拼接在字符串中
  35. $result = $clapi->sendSMS($mobile, $message);
  36. if(!is_null(json_decode($result))){
  37. $output=json_decode($result,true);
  38. if(isset($output['code']) && $output['code']=='0'){
  39. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>['code'=>$code]]);
  40. }else{
  41. echo $output['errorMsg'];
  42. }
  43. }else{
  44. echo $result;
  45. }
  46. }
  47. //订单物流派送
  48. public static function sendLogistics($order_no){
  49. $clapi = new ServiceSmsV1();
  50. $orderModel=new OrderModel();
  51. $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
  52. $logistics_name=$orderInfo['logistics_name'];
  53. $logistics_no=$orderInfo['logistics_no'];
  54. $mobile=$orderInfo['mobile'];
  55. $message='您购买的商品已发货,配送方式:'.$logistics_name.',配送单号:'.$logistics_no.',请留意查收!';//这里是需要发送的短信模板,参数传递拼接在字符串中
  56. $result = $clapi->sendSMS($mobile, $message);
  57. if(!is_null(json_decode($result))){
  58. $output=json_decode($result,true);
  59. if(isset($output['code']) && $output['code']=='0'){
  60. return true;
  61. }else{
  62. echo $output['errorMsg'];
  63. }
  64. }else{
  65. echo $result;
  66. }
  67. }
  68. //订单物流后台重新发送
  69. public static function sendLogisticsReset($order_no){
  70. $clapi = new ServiceSmsV1();
  71. $orderModel=new OrderModel();
  72. $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
  73. $logistics_name=$orderInfo['logistics_name'];
  74. $logistics_no=$orderInfo['logistics_no'];
  75. $mobile=$orderInfo['mobile'];
  76. $message='亲,您的订单'.$order_no.'已重新发货,物流已在路上,'.$logistics_name.':'.$logistics_no.'';//这里是需要发送的短信模板,参数传递拼接在字符串中
  77. $result = $clapi->sendSMS($mobile, $message);
  78. if(!is_null(json_decode($result))){
  79. $output=json_decode($result,true);
  80. if(isset($output['code']) && $output['code']=='0'){
  81. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  82. }else{
  83. echo $output['errorMsg'];
  84. }
  85. }else{
  86. echo $result;
  87. }
  88. }
  89. //订单物流价格更改,重新设置订单价格
  90. public static function sendLogisticsPrice($order_no){
  91. $clapi = new ServiceSmsV1();
  92. $orderModel=new OrderModel();
  93. $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
  94. $mobile=$orderInfo['mobile'];
  95. $message='尊敬的会员,您的订单'.$order_no.'已经更改物流价格,请及时付款,30分钟后系统将自动取消订单。';//这里是需要发送的短信模板,参数传递拼接在字符串中
  96. $result = $clapi->sendSMS($mobile, $message);
  97. if(!is_null(json_decode($result))){
  98. $output=json_decode($result,true);
  99. if(isset($output['code']) && $output['code']=='0'){
  100. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  101. }else{
  102. echo $output['errorMsg'];
  103. }
  104. }else{
  105. echo $result;
  106. }
  107. }
  108. //订单退款发送通知短信
  109. public static function sendOrderRefund($order_no){
  110. $clapi = new ServiceSmsV1();
  111. $orderModel=new OrderModel();
  112. $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
  113. $mobile=$orderInfo['mobile'];
  114. // $message='尊敬的会员,'.$order_no.'退货单已通过退款审核,请在微信中查询退款信息。';//这里是需要发送的短信模板,参数传递拼接在字符串中
  115. $message='编号为'.$order_no.'的订单被系统退款,详情请到微信中“我的消息“中查看详情,给您带来不便深表歉意,谢谢!';//这里是需要发送的短信模板,参数传递拼接在字符串中
  116. $result = $clapi->sendSMS($mobile, $message);
  117. if(!is_null(json_decode($result))){
  118. $output=json_decode($result,true);
  119. if(isset($output['code']) && $output['code']=='0'){
  120. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  121. }else{
  122. echo $output['errorMsg'];
  123. }
  124. }else{
  125. echo $result;
  126. }
  127. }
  128. //订单锁单操作后,变成取消订单短信发送
  129. public static function sendOrderCancel($order_no,$money){
  130. $clapi = new ServiceSmsV1();
  131. $orderModel=new OrderModel();
  132. $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
  133. $mobile=$orderInfo['mobile'];
  134. $message='尊敬的用户您好,编号为'.$order_no.'的订单被系统取消,系统在3个工作日内将'.$money.'元原路退换,详情请到微信中“我的消息“中查看详情,给您带来不便深表歉意,谢谢!';
  135. $result = $clapi->sendSMS($mobile, $message);
  136. if(!is_null(json_decode($result))){
  137. $output=json_decode($result,true);
  138. if(isset($output['code']) && $output['code']=='0'){
  139. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  140. }else{
  141. echo $output['errorMsg'];
  142. }
  143. }else{
  144. echo $result;
  145. }
  146. }
  147. //售后订单操作退款
  148. public static function sendOrderSalesRefunds($order_no,$money)
  149. {
  150. $clapi = new ServiceSmsV1();
  151. $orderModel = new OrderModel();
  152. $orderInfo = $orderModel->getOrderOne(['order.order_no' => $order_no]);
  153. $mobile = $orderInfo['mobile'];
  154. $message='尊敬的会员您好,您的订单已成功退款,退款金额为:'.$money.',您的订单号为'.$order_no.',请在微信中查看。如有疑问,请联系我们的售后电话:'.Config::get('service_mobile');
  155. $result = $clapi->sendSMS($mobile, $message);
  156. if (!is_null(json_decode($result))) {
  157. $output = json_decode($result, true);
  158. if (isset($output['code']) && $output['code'] == '0') {
  159. return helperService::returnJson(['code' => 200, 'msg' => '发送成功', 'data' => []]);
  160. } else {
  161. echo $output['errorMsg'];
  162. }
  163. } else {
  164. echo $result;
  165. }
  166. }
  167. //售后订单操作退款 没有同意
  168. public static function sendOrderSalesRefundsNo($order_no,$money)
  169. {
  170. $clapi = new ServiceSmsV1();
  171. $orderModel = new OrderModel();
  172. $orderInfo = $orderModel->getOrderOne(['order.order_no' => $order_no]);
  173. $mobile = $orderInfo['mobile'];
  174. $message = '尊敬的会员您好,您的售后订单系统不接受退款操作,请见谅。如有疑问请联系客户,tel:'.Config::get('service_mobile');
  175. $result = $clapi->sendSMS($mobile, $message);
  176. if (!is_null(json_decode($result))) {
  177. $output = json_decode($result, true);
  178. if (isset($output['code']) && $output['code'] == '0') {
  179. return helperService::returnJson(['code' => 200, 'msg' => '发送成功', 'data' => []]);
  180. } else {
  181. echo $output['errorMsg'];
  182. }
  183. } else {
  184. echo $result;
  185. }
  186. }
  187. //短信模板信息发送
  188. public static function sendMessage(){
  189. $clapi = new ServiceSmsV1();
  190. $mobile='15329884885';//这是接收者手机号码
  191. $name='南哥';
  192. $time=date("Y-m-d H:i:s");
  193. $message='你好'.$name.',我正在测试,时间是'.$time;//这里是需要发送的短信模板,参数传递拼接在字符串中
  194. $result = $clapi->sendSMS($mobile, $message);
  195. if(!is_null(json_decode($result))){
  196. $output=json_decode($result,true);
  197. if(isset($output['code']) && $output['code']=='0'){
  198. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  199. }else{
  200. echo $output['errorMsg'];
  201. }
  202. }else{
  203. echo $result;
  204. }
  205. }
  206. /**南哥,下面的方法不用管,是短信发送的内部方法***************************************************************************/
  207. /**
  208. * 发送短信
  209. * @param $mobile
  210. * @param $msg
  211. * @param string $needstatus
  212. * @return mixed
  213. */
  214. public function sendSMS( $mobile, $msg, $needstatus = 'true') {
  215. //创蓝接口参数
  216. $postArr = array (
  217. 'account' => $this->chuanglan_config['api_account'],
  218. 'password' => $this->chuanglan_config['api_password'],
  219. 'msg' => urlencode($msg),
  220. 'phone' => $mobile,
  221. 'report' => $needstatus
  222. );
  223. $result = $this->curlPost( $this->chuanglan_config['api_send_url'] , $postArr);
  224. return $result;
  225. }
  226. /**
  227. *
  228. * 发送变量短信
  229. * @param $msg
  230. * @param $params
  231. * @return mixed
  232. */
  233. public function sendVariableSMS( $msg, $params) {
  234. //创蓝接口参数
  235. $postArr = array (
  236. 'account' => $this->chuanglan_config['api_account'],
  237. 'password' => $this->chuanglan_config['api_password'],
  238. 'msg' => $msg,
  239. 'params' => $params,
  240. 'report' => 'true'
  241. );
  242. $result = $this->curlPost( $this->chuanglan_config['API_VARIABLE_URL'], $postArr);
  243. return $result;
  244. }
  245. /**
  246. * 查询额度
  247. *
  248. * 查询地址
  249. */
  250. public function queryBalance() {
  251. //查询参数
  252. $postArr = array (
  253. 'account' => $this->chuanglan_config['api_account'],
  254. 'password' => $this->chuanglan_config['api_password'],
  255. );
  256. $result = $this->curlPost($this->chuanglan_config['api_balance_query_url'], $postArr);
  257. return $result;
  258. }
  259. /**
  260. * 通过CURL发送HTTP请求
  261. * @param string $url //请求URL
  262. * @param array $postFields //请求参数
  263. * @return mixed
  264. */
  265. private function curlPost($url,$postFields){
  266. $postFields = json_encode($postFields);
  267. $ch = curl_init ();
  268. curl_setopt( $ch, CURLOPT_URL, $url );
  269. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  270. 'Content-Type: application/json; charset=utf-8'
  271. )
  272. );
  273. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  274. curl_setopt( $ch, CURLOPT_POST, 1 );
  275. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  276. curl_setopt( $ch, CURLOPT_TIMEOUT,1);
  277. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  278. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  279. $ret = curl_exec ( $ch );
  280. if (false == $ret) {
  281. $result = curl_error( $ch);
  282. } else {
  283. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  284. if (200 != $rsp) {
  285. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  286. } else {
  287. $result = $ret;
  288. }
  289. }
  290. curl_close ( $ch );
  291. return $result;
  292. }
  293. }