SMS.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * 服务提供者
  5. * User: qissen
  6. * Date: 2017/6/7
  7. * Time: 7:36
  8. * 注意调用顺序,checkClientType,checkData必须先调用,才可以验证其他
  9. */
  10. namespace app\core\service;
  11. use think\Cache;
  12. use think\Validate;
  13. use Yunpian_SMS\SMS as Service;
  14. use tool\Common;
  15. class SMS
  16. {
  17. //发送验证码,手机号
  18. public function sendVerificationCode($mobile = '') {
  19. $validate = new Validate([
  20. 'mobile' => 'require|length:11'
  21. ],[
  22. 'name.require' => '手机号码必须填写',
  23. 'name.length' => '手机号码格式不正确'
  24. ]);
  25. $result = $validate->check([
  26. 'mobile'=>$mobile
  27. ]);
  28. if(!$result){
  29. return Common::rm(-101, $validate->getError());
  30. }
  31. $package = Cache::get('verificationCode'.$mobile);
  32. if(!empty($package)) {
  33. if ($package['outTime'] > THINK_START_TIME) {
  34. return Common::rm(-102, '您发送验证码过快,请等待' . (int)($package['outTime'] - THINK_START_TIME) . '后重新发送');
  35. }
  36. if ($package['times'] > 100) {
  37. return Common::rm(-103, '您发送的短信太频繁了,请稍后再次验证');
  38. }
  39. }
  40. else {
  41. $package = [
  42. 'times'=>0
  43. ];
  44. }
  45. /*$code = 1849970;*/
  46. $code = mt_rand(100000, 999999);
  47. $text = '【佳乾财富】您的验证码是'.$code.'。如非本人操作,请忽略本短信';
  48. $result = Service::send_sms($mobile, $text);
  49. if($result === true) {
  50. $package['times']++;
  51. $package['outTime'] = THINK_START_TIME + 30;
  52. $package['code'] = $code;
  53. Cache::set('verificationCode'.$mobile, $package, 600);
  54. return Common::rm(1, '操作成功');
  55. }
  56. return Common::rm(-102, $result);
  57. }
  58. //获取验证码
  59. public function checkVerificationCode($mobile = '', $code = '') {
  60. $result = Cache::get('verificationCode'.$mobile);
  61. if(!$result) {
  62. return Common::rm(-101, '验证码已经失效了');
  63. }
  64. if($result['code'] != $code) {
  65. return Common::rm(-102, '验证码失败');
  66. }
  67. return Common::rm(1, '验证码通过');
  68. }
  69. //发送短信
  70. public static function sendSMS($mobile, $text) {
  71. Service::send_sms($mobile, $text);
  72. }
  73. //还款发送短信
  74. public static function message_setCodeRepay($trueName, $title, $linkUrl) {
  75. return '【佳乾财富】您好,'.$trueName.',您在投资的'.$title.'所得本息已到账,请打开app注意查收,'.$linkUrl;
  76. }
  77. //还款发送短信
  78. public static function message_setJiaxiYuebiao($trueName, $yearExt, $linkUrl) {
  79. // 【佳乾财富】亲爱的#truename#,#year#%加息券已到账请打开app查收,新app #linkurl#。新年到来之际,祝您财运旺旺,万事如意!
  80. //【佳乾财富】亲爱的#truename#,#year#%已到账,请打开app查收,新app地址 #linkurl#。
  81. return '【佳乾财富】亲爱的'.$trueName.','.$yearExt.'%已到账,请打开app查收,新app地址 '.$linkUrl.'。';
  82. }
  83. }