| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- /**
- * Created by PhpStorm.
- * User: chenkuan
- * Date: 2017/11/13
- * Time: 下午1:54
- */
- namespace backend\helpers;
- use backend\models\Config;
- use backend\models\MailRecord;
- use common\helpers\HashMapHelper;
- use yii\helpers\VarDumper;
- use yii\httpclient\Client;
- use Yii;
- class MailHelper
- {
- const DEF_CONFIG_ID = 1;
- const CACHE_CODE = 'cache_code';
- const CACHE_TIME = 'cache_time';
- /**
- * 发送简单邮件
- * @param string $subject 主题/标题 如:'找回密码验证'
- * @param string $to 接收方邮箱地址
- * @param array $paramArray 需要替换的模版内容参数,如:找回密码中的${code} 数组传递['code' => $code]
- * @param string $fromName 发送人姓名
- * @param string $content 内容模板
- * @return bool 成功/失败
- */
- public static function sendMail($subject, $to, $paramArray, $fromName, $content)
- {
- $cache = \Yii::$app->cache;
- $config = $cache->get('config');
- if ($config == null) {
- $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
- $cache->set('config', $config, 86400);
- }
- // 本地和测试环境目前禁止发邮件
- return self::sendSimpleMail($subject, $to, $config->smtp_from_mail, $content, $paramArray, $fromName);
- }
- /**
- * 发送短信
- * @param $mobile
- * @return null|string
- */
- public static function sendCodeSms($mobile)
- {
- $cache = \Yii::$app->cache;
- $lastCode = $cache->get($mobile.self::CACHE_CODE);
- if (!empty($lastCode)) {
- $time = $cache->get($mobile.self::CACHE_TIME);
- if ($time != null && (DateTimeHelper::microtime_float() - $time <= 120000)) {
- return null;
- }
- }
- $code = RandomHelper::getRandomNo(6);
- $cache->set($mobile.self::CACHE_CODE, $code);
- $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float());
- return $code;
- // 本地和测试环境目前禁止发短信
- /*
- $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
- $temp = self::sendBasicSms($mobile, $config->juhe_sms_tpl_id, $code);
- if ($temp) {
- $cache->set($mobile.self::CACHE_CODE, $code);
- $cache->set($mobile.self::CACHE_TIME, DateTimeHelper::microtime_float());
- return $code;
- } else {
- return null;
- }
- */
- }
- /**
- * 验证短信code
- * @param $mobile
- * @param $code
- * @return bool
- */
- public static function validSmsCode($mobile, $code)
- {
- $cache = \Yii::$app->cache;
- $lastCode = $cache->get($mobile.self::CACHE_CODE);
- if (!empty($lastCode)) {
- if ($lastCode != $code) {
- return false;
- } else {
- return true;
- }
- }
- return false;
- }
- /**
- * 发送MT4sms/RegSms
- * @param $mobile
- * @param $tplId
- * @param $name
- * @param $login
- * @param $pwd
- * @return \yii\httpclient\Response
- */
- public static function sendSms($mobile, $tplId, $name, $login, $pwd = '')
- {
- $map = new HashMapHelper();
- $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]);
- $map->put('mobile', $mobile);
- $map->put('tpl_id', $tplId."");
- $tplValue = "#name#=".$name."&#login#=".$login;
- if ($pwd) {
- $tplValue .= "&#pwd#=".$pwd;
- }
- $a = urlencode("#");
- $b = urlencode("&");
- $c = urlencode("=");
- $tplValue = str_replace("#", $a, $tplValue);
- $tplValue = str_replace("&", $b, $tplValue);
- $tplValue = str_replace("=", $c, $tplValue);
- $map->put('tpl_value', $tplValue);
- $map->put('dtype', "json");
- $map->put('key', $config->juhe_sms_key);
- return true;
- $url = "http://v.juhe.cn/sms/send";
- $client = (new Client(['baseUrl' => $url]));
- $json = $client->get($url, self::buildParams($map->H_table))->send();
- return $json;
- }
- /**
- * @param string $mobile
- * @return array
- */
- public static function getCodeSms($mobile)
- {
- $result = [
- 'code' => '',
- 'created' => 0,
- ];
- $code = \Yii::$app->getCache()->get($mobile . self::CACHE_CODE);
- $created = \Yii::$app->getCache()->get($mobile . self::CACHE_TIME);
- if ($code && $created) {
- $result['code'] = $code;
- $result['created'] = $created;
- }
- return $result;
- }
- /**
- * @param $mobile
- * @param $tplId
- * @param $code
- * @return mixed
- */
- private static function sendBasicSms($mobile, $tplId, $code)
- {
- $map = new HashMapHelper();
- $config = Config::findOne([['id' => self::DEF_CONFIG_ID]]);
- $map->put('mobile', $mobile);
- $map->put('tpl_id', $tplId."");
- $tplValue = "#code#=".$code;
- $map->put('tpl_value', urlencode($tplValue));
- $map->put('dtype', "json");
- $map->put('key', $config->juhe_sms_key);
- $url = "http://v.juhe.cn/sms/send";
- $client = (new Client(['baseUrl' => $url]));
- $json = $client->get($url, self::buildParams($map->H_table))->send();
- return $json;
- }
- /**
- * 生成token
- * @param array $param
- * @return mixed
- */
- private static function buildParams($param)
- {
- ksort($param);//升序排序
- $strOauth = "";
- foreach ($param as $key => $val) {
- if ($key == 'token' || is_array($val) || $val === null) {
- continue;
- }
- $val = strval($val);
- $strOauth .= $key . '=' . $val . '&';
- }
- $strOauth = rtrim($strOauth, '&');
- $param['token'] = md5($strOauth);
- return $param;
- }
- /**
- * 发送简单邮件封装方法
- * @param $subject
- * @param $to
- * @param $from
- * @param $content
- * @param $paramArray
- * @param $fromName
- * @return bool
- */
- private static function sendSimpleMail($subject, $to, $from, $content, $paramArray, $fromName)
- {
- $isSuccess = false;
- $replace_content = self::replaceVariable($content, $paramArray);
- $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
- /*
- $smtp_username = $config->smtp_username;
- $smtp_password = $config->smtp_password;
- \Yii::$app->mail->apiUser = $smtp_username;
- \Yii::$app->mail->apiKey = $smtp_password;
- $result = \Yii::$app->mail->sendNormalMail($subject, '<html>'.$replace_content, [$to], $from, $fromName);
- if ($result['result'] === true) {
- self::saveRecord($result['result'], $to, $subject, $replace_content);
- $isSuccess = true;
- }
- */
- $mailid = self::saveRecord($isSuccess, $to, $subject, $replace_content);
- $mail = \Yii::$app->mailer->compose();
- $mail->setTo($to);
- $mail->setSubject($subject);
- $mail->setHtmlBody( '<html>'.$replace_content);
- $result = $mail->send();
- if ($result) {
- $isSuccess = true;
- } else {
- Yii::warning('邮件发送失败' . VarDumper::dumpAsString($result), __METHOD__);
- }
- $status = ($isSuccess) ? 1 : 2;
- MailRecord::updateAll(['status' => $status], "id={$mailid}" );
- return $isSuccess;
- }
- /**
- * 将模板中的变量替换为入参
- * @param $content string
- * @param $paramArray array
- * @return string
- */
- public static function replaceVariable($content, $paramArray)
- {
- if (!empty($content) && !empty($paramArray)) {
- foreach ($paramArray as $key => $value) {
- $variable='${'.$key.'}';
- $variable1 = '#{'.$key.'}';
- $variable2 = '#'.$key.'#';
- if (is_null($value)) {
- continue;
- }
- while (strpos($content, $variable, 0) > -1) {
- $content = str_replace($variable, $value, $content);
- }
- while (strpos($content, $variable1, 0) > -1) {
- $content = str_replace($variable1, $value, $content);
- }
- while (strpos($content, $variable2, 0) > -1) {
- $content = str_replace($variable2, $value, $content);
- }
- }
- }
- return $content;
- }
- /**
- * 保存发送的邮件记录到crm_mail_record表
- * @param $status
- * @param $receiver
- * @param $subject
- * @param $content
- * @return bool
- */
- private static function saveRecord($status, $receiver, $subject, $content)
- {
- $mailRecord = new MailRecord();
- $mailRecord->content = $content;
- $mailRecord->receiver = $receiver;
- if ($status) {
- $mailRecord->status = 1;
- } else {
- $mailRecord->status = 0;
- }
- $mailRecord->subject = $subject;
- $mailRecord->in_time = DateTimeHelper::microtime_float();
- $mailRecord->save();
- return $mailRecord->id;
- }
- /**
- * 发送email邮件
- * @param $subject
- * @param $to
- * @param $from
- * @param $body
- * @param $mid
- * @return bool
- */
- public static function sendButNotSaveRecord($subject, $to, $from, $body, $mid)
- {
- $isSuccess = false;
- $config = Config::findOne(['id' => self::DEF_CONFIG_ID]);
- /*
- $smtp_username = $config->smtp_username;
- $smtp_password = $config->smtp_password;
- \Yii::$app->mail->apiUser = $smtp_username;
- \Yii::$app->mail->apiKey = $smtp_password;
- $result = \Yii::$app->mail->sendNormalMail($subject, '<html>'.$body, $to, $from);
- if ($result['result'] === true) {
- $isSuccess = true;
- }
- return $isSuccess;
- */
- $mail = \Yii::$app->mailer->compose();
- $mail->setTo($to);
- $mail->setSubject($subject);
- $mail->setHtmlBody( '<html>'.$body);
- if ($mail->send()) {
- MailRecord::updateAll(['status' => 1], "id={$mid}" );
- return true;
- }
- return false;
- }
- }
|