Ems.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems as Emslib;
  5. use app\common\model\User;
  6. /**
  7. * 邮箱验证码接口
  8. */
  9. class Ems extends Api
  10. {
  11. protected $noNeedLogin = '*';
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. \think\Hook::add('ems_send', function ($params) {
  17. $obj = \app\common\library\Email::instance();
  18. $result = $obj
  19. ->to($params->email)
  20. ->subject('验证码')
  21. ->message("你的验证码是:" . $params->code)
  22. ->send();
  23. return $result;
  24. });
  25. }
  26. public function index()
  27. {
  28. if($_POST){
  29. $mobile=input('phone');
  30. $code=input('code',rand(1000, 9999)) ;
  31. $site=config('site');
  32. if(!$site['dxjk']['key']){
  33. echo json_encode(['ok' => '1','data'=>$code,'msg' => '短信模拟发送成功 验证码:'.$code]);
  34. exit();
  35. }
  36. $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
  37. $smsConf = array(
  38. 'key' => $site['dxjk']['key'],//'f5cd3c466161918a823ed1c5d58cec58', //您申请的APPKEY
  39. 'mobile' => $mobile, //接受短信的用户手机号码
  40. 'tpl_id' => $site['dxjk']['tpl_id'], //您申请的短信模板ID,根据实际情况修改
  41. 'tpl_value' =>'#code#='.$code //您设置的模板变量,根据实际情况修改
  42. );
  43. $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
  44. if($content){
  45. $result = json_decode($content,true);
  46. $error_code = $result['error_code'];
  47. if($error_code == 0){
  48. //状态为0,说明短信发送成功
  49. //Cookie::set('yzmcode',md5($code));//验证码保存到SESSION中
  50. $sms = \app\common\model\Sms::where(['mobile' => $mobile])->delete();
  51. $sms = \app\common\model\Sms::create(['event' => 'register', 'mobile' => $mobile, 'code' => $code, 'ip' => request()->ip(), 'createtime' => time()]);
  52. echo json_encode(['ok' => '1','data'=>$code,'msg' => '短信发送成功']);
  53. //echo json_encode(['ok' => '1','data'=>md5($code.'6db'),'msg' => '短信发送成功,短信ID:'.$result['result']['sid']]);
  54. }else{
  55. //状态非0,说明失败
  56. $msg = $result['reason'];
  57. echo json_encode(['ok' => '2','data'=>md5($code), 'msg' => '短信发送失败('.$error_code.'):'.$msg]);
  58. }
  59. }else{
  60. //返回内容异常,以下可根据业务逻辑自行修改
  61. echo json_encode(['ok' => '2','data'=>md5($code), 'msg' => '请求发送短信失败']);
  62. }
  63. }
  64. }
  65. public function dxb(){
  66. $statusStr = array(
  67. "0" => "短信发送成功",
  68. "-1" => "参数不全",
  69. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
  70. "30" => "密码错误",
  71. "40" => "账号不存在",
  72. "41" => "余额不足",
  73. "42" => "帐户已过期",
  74. "43" => "IP地址限制",
  75. "50" => "内容含有敏感词"
  76. );
  77. $mobile=input('phone');
  78. $code=input('code',rand(1000, 9999)) ;
  79. $smsapi = "http://api.smsbao.com/";
  80. $user = "zhangbin1"; //短信平台帐号
  81. $pass = md5("zhangbin12"); //短信平台密码
  82. $content="【咕咕网络】您的验证码是".$code."。如非本人操作,请忽略本短信";//要发送的短信内容
  83. $phone = $mobile;//要发送短信的手机号码
  84. $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
  85. $result =file_get_contents($sendurl) ;
  86. if($result==0){
  87. $sms = \app\common\model\Sms::where(['mobile' => $mobile])->delete();
  88. $sms = \app\common\model\Sms::create(['event' => 'register', 'mobile' => $mobile, 'code' => $code, 'ip' => request()->ip(), 'createtime' => time()]);
  89. echo json_encode(['ok' => '1','data'=>$code,'msg' => '短信发送成功']);
  90. }else{
  91. echo json_encode(['ok' => '1','data'=>$code,'msg' => $statusStr[$result]]);
  92. }
  93. }
  94. /**
  95. * 发送验证码
  96. *
  97. * @param string $email 邮箱
  98. * @param string $event 事件名称
  99. */
  100. public function send()
  101. {
  102. $email = $this->request->request("email");
  103. $event = $this->request->request("event");
  104. $event = $event ? $event : 'register';
  105. $last = Emslib::get($email, $event);
  106. if ($last && time() - $last['createtime'] < 60) {
  107. $this->error(__('发送频繁'));
  108. }
  109. if ($event) {
  110. $userinfo = User::getByEmail($email);
  111. if ($event == 'register' && $userinfo) {
  112. //已被注册
  113. $this->error(__('已被注册'));
  114. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  115. //被占用
  116. $this->error(__('已被占用'));
  117. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  118. //未注册
  119. $this->error(__('未注册'));
  120. }
  121. }
  122. $ret = Emslib::send($email, null, $event);
  123. if ($ret) {
  124. $this->success(__('发送成功'));
  125. } else {
  126. $this->error(__('发送失败'));
  127. }
  128. }
  129. /**
  130. * 检测验证码
  131. *
  132. * @param string $email 邮箱
  133. * @param string $event 事件名称
  134. * @param string $captcha 验证码
  135. */
  136. public function check()
  137. {
  138. $email = $this->request->request("email");
  139. $event = $this->request->request("event");
  140. $event = $event ? $event : 'register';
  141. $captcha = $this->request->request("captcha");
  142. if ($event) {
  143. $userinfo = User::getByEmail($email);
  144. if ($event == 'register' && $userinfo) {
  145. //已被注册
  146. $this->error(__('已被注册'));
  147. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  148. //被占用
  149. $this->error(__('已被占用'));
  150. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  151. //未注册
  152. $this->error(__('未注册'));
  153. }
  154. }
  155. $ret = Emslib::check($email, $captcha, $event);
  156. if ($ret) {
  157. $this->success(__('成功'));
  158. } else {
  159. $this->error(__('验证码不正确'));
  160. }
  161. }
  162. /**
  163. * 请求接口返回内容
  164. * @param string $url [请求的URL地址]
  165. * @param string $params [请求的参数]
  166. * @param int $ipost [是否采用POST形式]
  167. * @return string
  168. */
  169. function juhecurl($url,$params=false,$ispost=0){
  170. $httpInfo = array();
  171. $ch = curl_init();
  172. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  173. curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
  174. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
  175. curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
  176. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  177. if( $ispost )
  178. {
  179. curl_setopt( $ch , CURLOPT_POST , true );
  180. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  181. curl_setopt( $ch , CURLOPT_URL , $url );
  182. }
  183. else
  184. {
  185. if($params){
  186. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  187. }else{
  188. curl_setopt( $ch , CURLOPT_URL , $url);
  189. }
  190. }
  191. $response = curl_exec( $ch );
  192. if ($response === FALSE) {
  193. //echo "cURL Error: " . curl_error($ch);
  194. return false;
  195. }
  196. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  197. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  198. curl_close( $ch );
  199. return $response;
  200. }
  201. }