123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * Created by PhpStorm.
- * User: vowkin
- * Date: 2017/5/22
- * Time: 21:49
- */
- namespace app\common\service;
- use app\common\model\OrderModel;
- use think\Session;
- 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($mobile){
- $clapi = new ServiceSmsV1();
- $code = mt_rand(1000,9999);//这是短信验证码。发送成功后返回验证码,需要验证时使用,存session
- // $mobile='18355173015';//这是接收者手机号码
- $message='您的动态验证码为'.$code.'请在页面输入完成验证。如非本人操作请忽略。';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $result = $clapi->sendSMS($mobile, $message);
- // print_r($result);exit;
- if(!is_null(json_decode($result))){
- $output=json_decode($result,true);
- if(isset($output['code']) && $output['code']=='0'){
- Session::set('code',$code);
- return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>['code'=>$code]]);
- }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;
- }
- }
- //订单付款成功发送短信
- public static function sendPayMessage($orderNo){
- $clapi = new ServiceSmsV1();
- $orderModel = new OrderModel();
- $userInfo = $orderModel->getOrderOne(['order_no'=>$orderNo]);
- $mobile=$userInfo['mobile'];//这是接收者手机号码
- $message='您好,您的订单'.$orderNo.'已付款成功,请等待发货,如有疑问,请咨询客服';//这里是需要发送的短信模板,参数传递拼接在字符串中
- $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;
- }
- }
|