Ems.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. {
  67. $statusStr = array(
  68. "0" => "短信发送成功",
  69. "-1" => "参数不全",
  70. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
  71. "30" => "密码错误",
  72. "40" => "账号不存在",
  73. "41" => "余额不足",
  74. "42" => "帐户已过期",
  75. "43" => "IP地址限制",
  76. "50" => "内容含有敏感词"
  77. );
  78. $mobile = input('phone');
  79. $code = input('code', rand(1000, 9999));
  80. $smsapi = "http://api.smsbao.com/";
  81. $user = "zhangbin1"; //短信平台帐号
  82. $pass = md5("zhangbin12"); //短信平台密码
  83. $content = "【咕咕网络】您的验证码是" . $code . "。如非本人操作,请忽略本短信";//要发送的短信内容
  84. $phone = $mobile;//要发送短信的手机号码
  85. $sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" . urlencode($content);
  86. $result = file_get_contents($sendurl);
  87. if ($result == 0) {
  88. $sms = \app\common\model\Sms::where(['mobile' => $mobile])->delete();
  89. $sms = \app\common\model\Sms::create(['event' => 'register', 'mobile' => $mobile, 'code' => $code, 'ip' => request()->ip(), 'createtime' => time()]);
  90. echo json_encode(['ok' => '1', 'data' => $code, 'msg' => '短信发送成功']);
  91. } else {
  92. echo json_encode(['ok' => '1', 'data' => $code, 'msg' => $statusStr[$result]]);
  93. }
  94. }
  95. /**
  96. * 发送验证码
  97. *
  98. * @param string $email 邮箱
  99. * @param string $event 事件名称
  100. */
  101. public function send()
  102. {
  103. $email = $this->request->request("email");
  104. $event = $this->request->request("event");
  105. $event = $event ? $event : 'register';
  106. $last = Emslib::get($email, $event);
  107. if ($last && time() - $last['createtime'] < 60) {
  108. $this->error(__('发送频繁'));
  109. }
  110. if ($event) {
  111. $userinfo = User::getByEmail($email);
  112. if ($event == 'register' && $userinfo) {
  113. //已被注册
  114. $this->error(__('已被注册'));
  115. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  116. //被占用
  117. $this->error(__('已被占用'));
  118. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  119. //未注册
  120. $this->error(__('未注册'));
  121. }
  122. }
  123. $ret = Emslib::send($email, null, $event);
  124. if ($ret) {
  125. $this->success(__('发送成功'));
  126. } else {
  127. $this->error(__('发送失败'));
  128. }
  129. }
  130. /**
  131. * 检测验证码
  132. *
  133. * @param string $email 邮箱
  134. * @param string $event 事件名称
  135. * @param string $captcha 验证码
  136. */
  137. public function check()
  138. {
  139. $email = $this->request->request("email");
  140. $event = $this->request->request("event");
  141. $event = $event ? $event : 'register';
  142. $captcha = $this->request->request("captcha");
  143. if ($event) {
  144. $userinfo = User::getByEmail($email);
  145. if ($event == 'register' && $userinfo) {
  146. //已被注册
  147. $this->error(__('已被注册'));
  148. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  149. //被占用
  150. $this->error(__('已被占用'));
  151. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  152. //未注册
  153. $this->error(__('未注册'));
  154. }
  155. }
  156. $ret = Emslib::check($email, $captcha, $event);
  157. if ($ret) {
  158. $this->success(__('成功'));
  159. } else {
  160. $this->error(__('验证码不正确'));
  161. }
  162. }
  163. /**
  164. * 请求接口返回内容
  165. * @param string $url [请求的URL地址]
  166. * @param string $params [请求的参数]
  167. * @param int $ipost [是否采用POST形式]
  168. * @return string
  169. */
  170. function juhecurl($url, $params = false, $ispost = 0)
  171. {
  172. $httpInfo = array();
  173. $ch = curl_init();
  174. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  175. 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');
  176. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  177. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  178. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  179. if ($ispost) {
  180. curl_setopt($ch, CURLOPT_POST, true);
  181. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  182. curl_setopt($ch, CURLOPT_URL, $url);
  183. } else {
  184. if ($params) {
  185. curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
  186. } else {
  187. curl_setopt($ch, CURLOPT_URL, $url);
  188. }
  189. }
  190. $response = curl_exec($ch);
  191. if ($response === FALSE) {
  192. //echo "cURL Error: " . curl_error($ch);
  193. return false;
  194. }
  195. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  196. $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
  197. curl_close($ch);
  198. return $response;
  199. }
  200. }