SendSms.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 发送手机短信
  4. */
  5. namespace app\api\controller\v1;
  6. use app\api\controller\Api;
  7. use app\common\model\Members;
  8. use app\common\model\MailLog;
  9. use app\common\library\Sms;
  10. class SendSms extends Api
  11. {
  12. public function _initialize()
  13. {
  14. //不是通过ajax请求的需要进行数据验签
  15. if(!$this->request->isAjax())
  16. {
  17. parent::_initialize();
  18. }
  19. }
  20. /**
  21. * 用户注册时,发送验证码
  22. */
  23. public function index()
  24. {
  25. $membersModel = new Members;
  26. $mailLogModel = new MailLog;
  27. $sms = new Sms;
  28. $mobile = $this->input('mobile');
  29. $action = $this->input('action'); //操作类型
  30. if (isset($this->input['captcha'], $this->input['random']) && $this->input['captcha'] && $this->input['random']) {
  31. $captcha = new \app\common\library\Captcha();
  32. if (!$captcha->check($this->input['captcha'], $this->input['random'])) {
  33. $this->jsonResult('', 0, '图形验证码错误', 'json');
  34. }
  35. }
  36. switch($action) {
  37. case 'login': // 手机验证码登录
  38. if ( empty($mobile) ) {
  39. $this->jsonResult('',0,'手机号码不能为空');
  40. } elseif (empty($membersModel->where(['username'=>$mobile])->find()) ) {
  41. $this->jsonResult('',0,'您的手机号尚未注册,请先去注册');
  42. }
  43. break;
  44. case 'register': // 注册
  45. if(isVirtualMobile($mobile)){
  46. $this->jsonResult('', 0, '禁止使用虚拟手机号!');
  47. }
  48. if ( empty($mobile) ) {
  49. $this->jsonResult('' ,0, '手机号码不能为空');
  50. } elseif ( ! empty($membersModel->where(['username'=>$mobile])->find()) ) {
  51. $this->jsonResult('', 0, '您的手机号已经注册,请直接登录');
  52. }
  53. break;
  54. case 'password_find': // 找回密码
  55. $member_id = session('to_reset');
  56. if($member_id)
  57. {
  58. $memberInfo = $membersModel->where(['id'=>$member_id])->find();
  59. if ( empty($memberInfo) || empty($memberInfo['mobile']) ) {
  60. $this->jsonResult('',0,'用户不存在或被冻结');
  61. } else{
  62. $mobile = $memberInfo['mobile'];
  63. }
  64. } else{
  65. $this->jsonResult('',0,'用户不存在或被冻结');
  66. }
  67. break;
  68. default:
  69. $this->jsonResult('',0,'非法操作');
  70. break;
  71. }
  72. $result = $sms->sendCode($mobile);
  73. if ( $result['status'] ) {
  74. $mailLogModel->insert(['content'=>$sms->_validate_content, 'target'=>$mobile, 'create_time'=>time(), 'client_ip'=>request()->ip(), 'type'=>'sms']);
  75. $this->jsonResult('', 1, '发送成功,请查收');
  76. } else {
  77. $this->jsonResult('', 0, $result['msg']);
  78. }
  79. }
  80. }