PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/service/mobile/srv/PwMobileService.php

https://github.com/cuijinquan/nextwind
PHP | 158 lines | 104 code | 13 blank | 41 comment | 17 complexity | d49d5524862edb4788137bbcb5dd01fc MD5 | raw file
  1. <?php
  2. /**
  3. * 手机短信服务
  4. *
  5. * @author jinlong.panjl <jinlong.panjl@aliyun-inc.com>
  6. * @copyright ©2003-2103 phpwind.com
  7. * @license http://www.phpwind.com
  8. * @version $Id$
  9. * @package wind
  10. */
  11. class PwMobileService {
  12. protected $plat;
  13. protected $sendNumDay = 3; //每天发送手机验证码次数
  14. public $platUrl;
  15. public function __construct() {
  16. $this->setPlat();
  17. $this->platUrl = $this->plat->platUrl;
  18. }
  19. /**
  20. * 获取剩余短信数量
  21. *
  22. * @return int
  23. */
  24. public function getRestMobileMessage() {
  25. if (!$this->plat) {
  26. return new PwError('USER:mobile.plat.choose.error');
  27. }
  28. return $this->plat->getRestMobileMessage();
  29. }
  30. /**
  31. * 发送短信
  32. *
  33. * @return bool
  34. */
  35. public function sendMobileMessage($mobile) {
  36. if (!$this->plat) {
  37. return new PwError('USER:mobile.plat.choose.error');
  38. }
  39. $code = $this->_buildCode();
  40. $content = $this->_buildContent($code);
  41. $number = $this->checkTodayNum($mobile);
  42. if ($number instanceof PwError) {
  43. return $number;
  44. }
  45. $result = $this->plat->sendMobileMessage($mobile, $content);
  46. if ($result instanceof PwError) return $result;
  47. Wind::import('SRV:user.dm.PwUserMobileDm');
  48. $dm = new PwUserMobileDm();
  49. $dm->setMobile($mobile)
  50. ->setCode($code)
  51. ->setNumber($number);
  52. $result = $this->_getDs()->addMobileVerify($dm);
  53. if ($result instanceof PwError) return $result;
  54. return true;
  55. }
  56. /**
  57. * 验证验证码
  58. *
  59. */
  60. public function checkVerify($mobile, $inputCode) {
  61. if (!$mobile || !$inputCode) return new PwError('USER:mobile.code.mobile.empty');
  62. $info = $this->_getDs()->getMobileVerify($mobile);
  63. if (!$info) return new PwError('USER:mobile.code.error');
  64. if ($info['expired_time'] < Pw::getTime()) return new PwError('USER:mobile.code.expired_time.error');
  65. if ($inputCode !== $info['code']) return new PwError('USER:mobile.code.error');
  66. // 手机验证通过后扩展
  67. PwSimpleHook::getInstance('PwMobileService_checkVerify')->runDo($mobile);
  68. return true;
  69. }
  70. /**
  71. * 获取验证码
  72. *
  73. */
  74. public function getVerify($mobile) {
  75. $code = $this->_buildCode(4);
  76. Wind::import('SRV:user.dm.PwUserMobileDm');
  77. $dm = new PwUserMobileDm();
  78. $dm->setMobile($mobile)
  79. ->setCode($code);
  80. $this->_getDs()->addMobileVerify($dm);
  81. return $code;
  82. }
  83. /**
  84. * 用户连续天数的行为记录&&用户累计行为记录
  85. *
  86. * @param int $uid
  87. * @param string $behavior 行为标记
  88. * @param int $time 当前时间,为0则为累计行为记录,否则为连续行为记录(每天)
  89. */
  90. public function replaceBehavior(PwUserMobileDm $dm) {
  91. $mobile = $dm->getField('mobile');
  92. $number = $this->checkTodayNum($mobile);
  93. if ($number instanceof PwError) {
  94. return $number;
  95. }
  96. $dm->setNumber($number);
  97. return $this->_getDs()->addMobileVerify($dm);
  98. }
  99. public function checkTodayNum($mobile) {
  100. $info = $this->_getDs()->getMobileVerify($mobile);
  101. $number = 1;
  102. $tdtime = Pw::getTdtime();
  103. if ($info) {
  104. $number = $info['number'];
  105. if ($info['create_time'] < $tdtime + 86400 && $info['create_time'] > $tdtime) {
  106. $number++;
  107. } else {
  108. $number = 1;
  109. }
  110. }
  111. if ($number > $this->sendNumDay) {
  112. return new PwError('USER:mobile.code.send.num.error');
  113. }
  114. return $number;
  115. }
  116. private function _buildCode($len = 4) {
  117. $str = '123456789';
  118. $_tmp = Pw::strlen($str)-1;
  119. $code = '';
  120. $_num = 0;
  121. for($i = 0;$i < $len;$i++){
  122. $_num = mt_rand(0, $_tmp);
  123. $code .= $str[$_num];
  124. }
  125. return $code;
  126. }
  127. protected function _buildContent($code) {
  128. $search = array('{mobilecode}', '{sitename}');
  129. $replace = array($code, Wekit::C('site', 'info.name'));
  130. $content = str_replace($search, $replace, Wekit::C('register', 'mobile.message.content'));
  131. return $content;
  132. }
  133. /**
  134. * 设置平台类型
  135. */
  136. public function setPlat() {
  137. $this->plat = Wind::getComponent('mobileplat');
  138. }
  139. /**
  140. * @return PwUserMobileVerify
  141. */
  142. protected function _getDs() {
  143. return Wekit::load('user.PwUserMobileVerify');
  144. }
  145. }