From b1b5bbd25980b34877f7b561f849d1321d9ef5a8 Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 23 Sep 2019 14:39:59 +0800 Subject: [PATCH] Update ExtendService.php --- application/store/service/ExtendService.php | 86 +++++++++++++++------ 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/application/store/service/ExtendService.php b/application/store/service/ExtendService.php index 6ff007b47..8e52d7ab2 100644 --- a/application/store/service/ExtendService.php +++ b/application/store/service/ExtendService.php @@ -53,20 +53,63 @@ class ExtendService public static function sendChinaSms($mid, $phone, $content, $productid = '676767') { $tkey = date("YmdHis"); - $data = [ - 'tkey' => $tkey, - 'mobile' => $phone, - 'content' => $content, - 'username' => sysconf('sms_zt_username'), - 'productid' => $productid, - 'password' => md5(md5(sysconf('sms_zt_password')) . $tkey), - ]; - $result = Http::post('http://www.ztsms.cn/sendNSms.do', $data); + $result = Http::post('http://www.ztsms.cn/sendNSms.do', [ + 'tkey' => $tkey, 'mobile' => $phone, 'content' => $content, + 'username' => sysconf('sms_zt_username'), 'productid' => $productid, + 'password' => md5(md5(sysconf('sms_zt_password')) . $tkey), + ]); list($code, $msg) = explode(',', $result . ','); Db::name('StoreMemberSmsHistory')->insert(['mid' => $mid, 'phone' => $phone, 'content' => $content, 'result' => $result]); return intval($code) === 1; } + /** + * 发送国内短信验证码 + * @param string $mid 会员ID + * @param string $phone 目标手机 + * @param integer $wait 等待时间 + * @param string $type 短信模板 + * @return array + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + public static function sendChinaSmsByCode($mid, $phone, $wait = 120, $type = 'sms_reg_template') + { + $cache = cache($ckey = "{$type}_{$phone}"); + if (is_array($cache) && isset($cache['time']) && $cache['time'] > time() - $wait) { + $dtime = ($cache['time'] + $wait < time()) ? 0 : ($wait - time() + $cache['time']); + return [1, '短信验证码已经发送!', ['time' => $dtime]]; + } + list($code, $content) = [rand(1000, 9999), sysconf($type)]; + if (empty($content) || stripos($content, '{code}') === false) { + $content = '您的验证码为{code},请在十分钟内完成操作!'; + } + cache($ckey, ['phone' => $phone, 'code' => $code, 'time' => time()], 600); + if (empty($content) || strpos($content, '{code}') === false) { + return [0, '获取短信模板失败,联系管理员配置!', []]; + } + $cache = cache($ckey); + if (self::sendChinaSms($mid, $phone, str_replace('{code}', $code, $content))) { + $dtime = ($cache['time'] + $wait < time()) ? 0 : ($wait - time() + $cache['time']); + return [1, '短信验证码发送成功!', ['time' => $dtime]]; + } else { + return [0, '短信发送失败,请稍候再试!', []]; + } + } + + /** + * 验证手机短信验证码 + * @param string $phone 目标手机 + * @param string $code 短信验证码 + * @param string $type 短信模板 + * @return boolean + */ + public static function checkChinaSmsByCode($phone, $code, $type = 'sms_reg_template') + { + $cache = cache($cachekey = "{$type}_{$phone}"); + return is_array($cache) && isset($cache['code']) && $cache['code'] == $code; + } + /** * 查询国内短信余额 * @return array @@ -76,12 +119,10 @@ class ExtendService public static function queryChinaSmsBalance() { $tkey = date("YmdHis"); - $data = [ - 'tkey' => $tkey, - 'username' => sysconf('sms_zt_username'), + $result = Http::post('http://www.ztsms.cn/balanceN.do', [ + 'username' => sysconf('sms_zt_username'), 'tkey' => $tkey, 'password' => md5(md5(sysconf('sms_zt_password')) . $tkey), - ]; - $result = Http::post('http://www.ztsms.cn/balanceN.do', $data); + ]); if ($result > -1) { return ['code' => 1, 'num' => $result, 'msg' => '获取短信剩余条数成功!']; } elseif ($result > -2) { @@ -122,15 +163,11 @@ class ExtendService public static function sendGlobeSms($mid, $code, $mobile, $content) { $tkey = date("YmdHis"); - $data = [ - 'tkey' => $tkey, - 'code' => $code, - 'mobile' => $mobile, - 'content' => $content, - 'username' => sysconf('sms_zt_username2'), + $result = Http::post('http://intl.zthysms.com/intSendSms.do', [ + 'tkey' => $tkey, 'code' => $code, 'mobile' => $mobile, + 'content' => $content, 'username' => sysconf('sms_zt_username2'), 'password' => md5(md5(sysconf('sms_zt_password2')) . $tkey), - ]; - $result = Http::post('http://intl.zthysms.com/intSendSms.do', $data); + ]); Db::name('StoreMemberSmsHistory')->insert([ 'mid' => $mid, 'region' => $code, 'phone' => $mobile, 'content' => $content, 'result' => $result, ]); @@ -146,11 +183,10 @@ class ExtendService public static function queryGlobeSmsBalance() { $tkey = date("YmdHis"); - $data = [ + $result = Http::post('http://intl.zthysms.com/intBalance.do', [ 'username' => sysconf('sms_zt_username2'), 'tkey' => $tkey, 'password' => md5(md5(sysconf('sms_zt_password2')) . $tkey), - ]; - $result = Http::post('http://intl.zthysms.com/intBalance.do', $data); + ]); if (!is_numeric($result) && ($state = intval($result)) && isset(self::$globeMessageMap[$state])) { return ['code' => 0, 'num' => 0, 'msg' => self::$globeMessageMap[$state]]; } else {