123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- /**
- * Created by PhpStorm.
- * User: vowkin
- * Date: 2017/5/22
- * Time: 21:49
- */
- namespace app\common\service;
- use app\common\model\OrderModel;
- use app\common\model\UserModel;
- use think\Config;
- class ServiceSmsV1 extends BaseService
- {
- private $chuanglan_config='';
- public function __construct()
- {
- //创蓝发送短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
- $this->chuanglan_config['api_send_url'] = 'http://smssh1.253.com/msg/send/json';
- //创蓝变量短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
- $this->chuanglan_config['API_VARIABLE_URL'] = 'http://xxx/msg/variable/json';
- //创蓝短信余额查询接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
- $this->chuanglan_config['api_balance_query_url'] = 'http://xxx/msg/balance/json';
- //创蓝账号 替换成你自己的账号
- $this->chuanglan_config['api_account'] = 'N5737774';
- //创蓝密码 替换成你自己的密码
- $this->chuanglan_config['api_password'] = 'we7da5QAZ0a3ec';
- }
- /**南哥,下面的方法就是你需要发送短信的dome 可直接调用发送短信***************************************************************************/
- //短信验证码发送
- public static function sendCode(){
- $clapi = new ServiceSmsV1();
- $code = mt_rand(1000,9999);//这是短信验证码。发送成功后返回验证码,需要验证时使用,存session
- $mobile='15329884885';//这是接收者手机号码
- $message='亲爱的用户,您的短信验证码为'.$code.',1分钟内有效,若非本人操作请忽略。';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>['code'=>$code]]);
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- //订单物流派送
- public static function sendLogistics($order_no){
- $clapi = new ServiceSmsV1();
- $orderModel=new OrderModel();
- $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
- $logistics_name=$orderInfo['logistics_name'];
- $logistics_no=$orderInfo['logistics_no'];
- $mobile=$orderInfo['mobile'];
- $message='您购买的商品已发货,配送方式:'.$logistics_name.',配送单号:'.$logistics_no.',请留意查收!';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return true;
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- //订单物流后台重新发送
- public static function sendLogisticsReset($order_no){
- $clapi = new ServiceSmsV1();
- $orderModel=new OrderModel();
- $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
- $logistics_name=$orderInfo['logistics_name'];
- $logistics_no=$orderInfo['logistics_no'];
- $mobile=$orderInfo['mobile'];
- $message='亲,您的订单'.$order_no.'已重新发货,物流已在路上,'.$logistics_name.':'.$logistics_no.'';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- //订单物流价格更改,重新设置订单价格
- public static function sendLogisticsPrice($order_no){
- $clapi = new ServiceSmsV1();
- $orderModel=new OrderModel();
- $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
- $mobile=$orderInfo['mobile'];
- $message='尊敬的会员,您的订单'.$order_no.'已经更改物流价格,请及时付款,30分钟后系统将自动取消订单。';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- //订单退款发送通知短信
- public static function sendOrderRefund($order_no){
- $clapi = new ServiceSmsV1();
- $orderModel=new OrderModel();
- $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
- $mobile=$orderInfo['mobile'];
- // $message='尊敬的会员,'.$order_no.'退货单已通过退款审核,请在微信中查询退款信息。';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $message='编号为'.$order_no.'的订单被系统退款,详情请到微信中“我的消息“中查看详情,给您带来不便深表歉意,谢谢!';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- //订单锁单操作后,变成取消订单短信发送
- public static function sendOrderCancel($order_no,$money){
- $clapi = new ServiceSmsV1();
- $orderModel=new OrderModel();
- $orderInfo=$orderModel->getOrderOne(['order.order_no'=>$order_no]);
- $mobile=$orderInfo['mobile'];
- $message='尊敬的用户您好,编号为'.$order_no.'的订单被系统取消,系统在3个工作日内将'.$money.'元原路退换,详情请到微信中“我的消息“中查看详情,给您带来不便深表歉意,谢谢!';
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- //售后订单操作退款
- public static function sendOrderSalesRefunds($order_no,$money)
- {
- $clapi = new ServiceSmsV1();
- $orderModel = new OrderModel();
- $orderInfo = $orderModel->getOrderOne(['order.order_no' => $order_no]);
- $mobile = $orderInfo['mobile'];
- $message='尊敬的会员您好,您的订单已成功退款,退款金额为:'.$money.',您的订单号为'.$order_no.',请在微信中查看。如有疑问,请联系我们的售后电话:'.Config::get('service_mobile');
- $result = $clapi->sendSMS($mobile, $message);
- if (!is_null(json_decode($result))) {
- $output = json_decode($result, true);
- if (isset($output['code']) && $output['code'] == '0') {
- return helperService::returnJson(['code' => 200, 'msg' => '发送成功', 'data' => []]);
- } else {
- echo $output['errorMsg'];
- }
- } else {
- echo $result;
- }
- }
- //售后订单操作退款 没有同意
- public static function sendOrderSalesRefundsNo($order_no,$money)
- {
- $clapi = new ServiceSmsV1();
- $orderModel = new OrderModel();
- $orderInfo = $orderModel->getOrderOne(['order.order_no' => $order_no]);
- $mobile = $orderInfo['mobile'];
- $message = '尊敬的会员您好,您的售后订单系统不接受退款操作,请见谅。如有疑问请联系客户,tel:'.Config::get('service_mobile');
- $result = $clapi->sendSMS($mobile, $message);
- if (!is_null(json_decode($result))) {
- $output = json_decode($result, true);
- if (isset($output['code']) && $output['code'] == '0') {
- return helperService::returnJson(['code' => 200, 'msg' => '发送成功', 'data' => []]);
- } else {
- echo $output['errorMsg'];
- }
- } else {
- echo $result;
- }
- }
- //短信模板信息发送
- public static function sendMessage(){
- $clapi = new ServiceSmsV1();
- $mobile='15329884885';//这是接收者手机号码
- $name='南哥';
- $time=date("Y-m-d H:i:s");
- $message='你好'.$name.',我正在测试,时间是'.$time;//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
- }else{
- echo $output['errorMsg'];
- }
- }else{
- echo $result;
- }
- }
- /**南哥,下面的方法不用管,是短信发送的内部方法***************************************************************************/
- /**
- * 发送短信
- * @param $mobile
- * @param $msg
- * @param string $needstatus
- * @return mixed
- */
- public function sendSMS( $mobile, $msg, $needstatus = 'true') {
- //创蓝接口参数
- $postArr = array (
- 'account' => $this->chuanglan_config['api_account'],
- 'password' => $this->chuanglan_config['api_password'],
- 'msg' => urlencode($msg),
- 'phone' => $mobile,
- 'report' => $needstatus
- );
- $result = $this->curlPost( $this->chuanglan_config['api_send_url'] , $postArr);
- return $result;
- }
- /**
- *
- * 发送变量短信
- * @param $msg
- * @param $params
- * @return mixed
- */
- public function sendVariableSMS( $msg, $params) {
- //创蓝接口参数
- $postArr = array (
- 'account' => $this->chuanglan_config['api_account'],
- 'password' => $this->chuanglan_config['api_password'],
- 'msg' => $msg,
- 'params' => $params,
- 'report' => 'true'
- );
- $result = $this->curlPost( $this->chuanglan_config['API_VARIABLE_URL'], $postArr);
- return $result;
- }
- /**
- * 查询额度
- *
- * 查询地址
- */
- public function queryBalance() {
- //查询参数
- $postArr = array (
- 'account' => $this->chuanglan_config['api_account'],
- 'password' => $this->chuanglan_config['api_password'],
- );
- $result = $this->curlPost($this->chuanglan_config['api_balance_query_url'], $postArr);
- return $result;
- }
- /**
- * 通过CURL发送HTTP请求
- * @param string $url //请求URL
- * @param array $postFields //请求参数
- * @return mixed
- */
- private function curlPost($url,$postFields){
- $postFields = json_encode($postFields);
- $ch = curl_init ();
- curl_setopt( $ch, CURLOPT_URL, $url );
- curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8'
- )
- );
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt( $ch, CURLOPT_POST, 1 );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
- curl_setopt( $ch, CURLOPT_TIMEOUT,1);
- curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
- $ret = curl_exec ( $ch );
- if (false == $ret) {
- $result = curl_error( $ch);
- } else {
- $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
- if (200 != $rsp) {
- $result = "请求状态 ". $rsp . " " . curl_error($ch);
- } else {
- $result = $ret;
- }
- }
- curl_close ( $ch );
- return $result;
- }
- }
|