SmsCaptcha.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\service\passport;
  13. use app\api\validate\passport\SmsCaptcha as ValidateSmsCaptcha;
  14. use app\common\service\BaseService;
  15. use app\common\service\Message as MessageService;
  16. use edward\captcha\facade\CaptchaApi;
  17. /**
  18. * 服务类:发送短信验证码
  19. * Class SmsCaptcha
  20. * @package app\api\service\passport
  21. */
  22. class SmsCaptcha extends BaseService
  23. {
  24. /**
  25. * 发送短信验证码
  26. * @param array $data
  27. * @return bool
  28. */
  29. public function sendSmsCaptcha(array $data)
  30. {
  31. // 数据验证
  32. if (!$this->validate($data)) return false;
  33. // 生成验证码
  34. $smsCaptcha = CaptchaApi::createSMS($data['mobile']);
  35. // 发送短信
  36. $status = MessageService::send('passport.captcha', [
  37. 'code' => $smsCaptcha['code'],
  38. 'mobile' => $smsCaptcha['key']
  39. ], $this->storeId);
  40. if (!$status) {
  41. $this->error = '短信发送失败';
  42. return false;
  43. }
  44. return true;
  45. }
  46. /**
  47. * 数据验证
  48. * @param array $data
  49. * @return bool
  50. */
  51. private function validate(array $data)
  52. {
  53. // 数据验证
  54. $validate = new ValidateSmsCaptcha;
  55. if (!$validate->check($data)) {
  56. $this->error = $validate->getError();
  57. return false;
  58. }
  59. // 验证图形验证码
  60. if (!CaptchaApi::check($data['captchaCode'], $data['captchaKey'])) {
  61. $this->error = '图形验证码不正确';
  62. return false;
  63. }
  64. return true;
  65. }
  66. }