Msg.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Traits;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Http\Request;
  5. trait Msg{
  6. //检测手机号码格式
  7. public function verifyPhone($phone){
  8. return preg_match('/^1[3456789][0-9]{9}$/',$phone);
  9. }
  10. //发送短信验证码
  11. public function sendMsg(Request $request,$phone=null)
  12. {
  13. if (empty($phone)){
  14. $phone = $request->get('phone');
  15. }
  16. if (!$this->verifyPhone($phone)){
  17. return response()->json(['code'=>1,'msg'=>'手机号码格式错误']);
  18. }
  19. //检测图形验证码是否正确
  20. if (!$this->verifyCaptcha($request)){
  21. return response()->json(['code'=>1,'msg'=>'图形验证码不正确']);
  22. }
  23. $code = mt_rand(100000,999999);
  24. $content = '【转丁丁】您的验证码为:'.$code.',有效期10分钟。';
  25. $msgApiUrl = "http://139.224.36.226:1082/wgws/BatchSubmit";
  26. $data = [
  27. 'apName' => 'bjdgg', //帐号
  28. 'apPassword' => 'bjdgg2016', //密码
  29. 'srcId' => '', //附加号(可置空)
  30. 'ServiceId' => '', //预留,可为空
  31. 'calledNumber' => $phone, //手机号码
  32. 'content' => $content, //内容
  33. 'sendTime' => '' //发送时间,为空表示立即发送,时间格式为:yyyyMMddHHmmss
  34. ];
  35. //错误码
  36. $statusCode = [
  37. 0=>"发送成功",
  38. 3=>"密码错误",
  39. 8=>"流量控制",
  40. 13=>"缺少被叫",
  41. 14=>"被叫数量太多",
  42. 15=>"端口号验证失败",
  43. 16=>"被叫连续下单限制",
  44. 80=>"用户已停用",
  45. 81=>"余额不足",
  46. 82=>"产品未定价",
  47. 83=>"上级产品未定价",
  48. 84=>"缺少签名",
  49. 85=>"网关没有报备签名",
  50. 86=>"内容不合法",
  51. 87=>"用户类型不匹配",
  52. 100=>"其他异常",
  53. 102=>"没有匹配到模板",
  54. 103=>"未知号码运营商",
  55. 104=>"模板没有配置运营商网关",
  56. 106=>"非法IP",
  57. 107=>"定时时间格式错误",
  58. 114=>"号码与提交协议不匹配"
  59. ];
  60. $client = new Client();
  61. $response = $client->post($msgApiUrl,['form_params'=>$data]);
  62. $res = simplexml_load_string($response->getBody());
  63. $datas = (array)$res->submitResp;
  64. if ($datas['error']==0){
  65. session([md5($request->get('phone')) => $code]);
  66. }
  67. return response()->json(['code'=>$datas['error'],'msg'=>$statusCode[$datas['error']]]);
  68. }
  69. //验证手机短信验证码
  70. public function verifyMsgCode(Request $request)
  71. {
  72. if ( $request->get('msgCode') == session(md5($request->get('phone'))) ){
  73. $request->session()->forget(md5($request->get('phone')));
  74. $request->session()->forget('captcha');
  75. return true;
  76. }
  77. return false;
  78. }
  79. //验证图形验证码
  80. public function verifyCaptcha(Request $request)
  81. {
  82. return captcha_check($request->get('captcha'));
  83. }
  84. }