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

Language PHP Lines 158
MD5 Hash d49d5524862edb4788137bbcb5dd01fc
Repository https://github.com/cuijinquan/nextwind.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

/**
 * 手机短信服务
 *
 * @author jinlong.panjl <jinlong.panjl@aliyun-inc.com>
 * @copyright ©2003-2103 phpwind.com
 * @license http://www.phpwind.com
 * @version $Id$
 * @package wind
 */
class PwMobileService {
	protected $plat;	
	protected $sendNumDay = 3;  //每天发送手机验证码次数
	public $platUrl;
	
	public function __construct() {
		$this->setPlat();
		$this->platUrl = $this->plat->platUrl;
	}
	
	/**
	 * 获取剩余短信数量
	 *
	 * @return int
	 */
	public function getRestMobileMessage() {
		if (!$this->plat) {
			return new PwError('USER:mobile.plat.choose.error');
		}
		return $this->plat->getRestMobileMessage();
	}

	/**
	 * 发送短信
	 *
	 * @return bool
	 */
	public function sendMobileMessage($mobile) {
		if (!$this->plat) {
			return new PwError('USER:mobile.plat.choose.error');
		}
		$code = $this->_buildCode();
		$content = $this->_buildContent($code);
		$number = $this->checkTodayNum($mobile);
		if ($number instanceof PwError) {
			return $number;
		}
		$result = $this->plat->sendMobileMessage($mobile, $content);
		if ($result instanceof PwError) return $result;
		Wind::import('SRV:user.dm.PwUserMobileDm');
		$dm = new PwUserMobileDm();
		$dm->setMobile($mobile)
			->setCode($code)
			->setNumber($number);
		$result = $this->_getDs()->addMobileVerify($dm);
		if ($result instanceof PwError) return $result;
		return true;
	}

	/**
	 * 验证验证码
	 * 
	 */
	public function checkVerify($mobile, $inputCode) {
		if (!$mobile || !$inputCode) return new PwError('USER:mobile.code.mobile.empty');
		$info = $this->_getDs()->getMobileVerify($mobile);
		if (!$info) return new PwError('USER:mobile.code.error');
		if ($info['expired_time'] < Pw::getTime()) return new PwError('USER:mobile.code.expired_time.error');
		if ($inputCode !== $info['code']) return new PwError('USER:mobile.code.error');
		// 手机验证通过后扩展
		PwSimpleHook::getInstance('PwMobileService_checkVerify')->runDo($mobile);
		return true;
	}

	/**
	 * 获取验证码
	 * 
	 */
	public function getVerify($mobile) {
		$code = $this->_buildCode(4);
		Wind::import('SRV:user.dm.PwUserMobileDm');
		$dm = new PwUserMobileDm();
		$dm->setMobile($mobile)
			->setCode($code);
		
		$this->_getDs()->addMobileVerify($dm);
		return $code;
	}

	/**
	 * 用户连续天数的行为记录&&用户累计行为记录
	 * 
	 * @param int $uid
	 * @param string $behavior 行为标记
	 * @param int $time 当前时间,为0则为累计行为记录,否则为连续行为记录(每天)
	 */
	public function replaceBehavior(PwUserMobileDm $dm) {
		$mobile = $dm->getField('mobile');
		$number = $this->checkTodayNum($mobile);
		if ($number instanceof PwError) {
			return $number;
		}
		$dm->setNumber($number);
		return $this->_getDs()->addMobileVerify($dm);
	}
	
	public function checkTodayNum($mobile) {
		$info = $this->_getDs()->getMobileVerify($mobile);
		$number = 1;
		$tdtime = Pw::getTdtime();
		if ($info) {
			$number = $info['number'];
			if ($info['create_time'] < $tdtime + 86400 && $info['create_time'] > $tdtime) {
				$number++;
			} else {
				$number = 1;
			}
		}
		if ($number > $this->sendNumDay) {
			return new PwError('USER:mobile.code.send.num.error');
		}
		return $number;
	}
	
	private function _buildCode($len = 4) {
		$str = '123456789';
		$_tmp = Pw::strlen($str)-1;
		$code = '';
	    $_num = 0;
	    for($i = 0;$i < $len;$i++){
	        $_num = mt_rand(0, $_tmp);
	        $code .= $str[$_num]; 
	    }
		return $code;
	}
	
	protected function _buildContent($code) {
		$search = array('{mobilecode}', '{sitename}');
		$replace = array($code, Wekit::C('site', 'info.name'));
		$content = str_replace($search, $replace, Wekit::C('register', 'mobile.message.content'));
		return $content;
	}
	
	/**
	 * 设置平台类型
	 */
	public function setPlat() {
		$this->plat = Wind::getComponent('mobileplat');
	}
	
	/**
	 * @return PwUserMobileVerify
	 */
	protected function _getDs() {
		return Wekit::load('user.PwUserMobileVerify');
	}
}
Back to Top