博客列表 >在laravel中写的短信接口和邮箱接口

在laravel中写的短信接口和邮箱接口

陈文鹏的博客
陈文鹏的博客原创
2018年01月23日 10:51:071249浏览

<!--ValidateController类的开始-->

<?php


namespace App\Http\Controllers;



use App\Entity\TempPhone;

use App\Models\Result;

use App\Tool\SME\SendEmail;

use App\Tool\SMS\SendMessage;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;


class ValidateController extends Controller

{


    // 发送短信验证码

    public function sendSMS(Request $request)

    {

        //设置时区

        date_default_timezone_set('Asia/Shanghai');

        $Api_result = new Result();

        $phoneNumber = $request->input('phoneNumber', '');

        if ($phoneNumber == '') {

            $Api_result->status = 1;

            $Api_result->message = 'Phone number is empty';

            return $Api_result->toJson();

        }

//        if (strlen($phoneNumber) != 11 || !preg_match("/^1[34578]\d{9}$/", $phoneNumber)) {

//            $Api_result->status = 2;

//            $Api_result->message = '手机格式不正确';

//            return $Api_result->toJson();

//        }


        $sendMessage = new SendMessage();

        $code = '';

        $code =substr(str_shuffle("012345678901234567890123456789"), 0,6);

        $result = $sendMessage->sendMessage($phoneNumber,"{$code}");

//        echo "<pre>";

//        print_r($result);exit;

        $result = json_decode($result, JSON_UNESCAPED_UNICODE);

        DB::table('code_message')->insert(['yzmlog' => $result["message"], 'senddata' => date('Y-m-d H:i:s', time())]);

//        echo "<pre>";

//        print_r($result);exit;

        if ($result['status'] == 1) {

            $tempPhone = TempPhone::where('phoneNumber', $phoneNumber)->first();

            if ($tempPhone == null) {

                $tempPhone = new TempPhone;

            }

            $tempPhone->phoneNumber = $phoneNumber;

            $tempPhone->code = $code;

            $tempPhone->deadline = date('Y-m-d H:i:s', time() + 3 * 60);

            $tempPhone->save();

        }

        return $result['status'];

    }

    //邮件发送验证码

    public function sendEmail(Request $request){

        //设置时区

        date_default_timezone_set('Asia/Shanghai');

        $Api_result = new Result();

        $email = $request->input('email', '');

        $code =substr(str_shuffle("012345678901234567890123456789"), 0,6);

        $sendMail=new SendEmail();

        $sendMail->sendEmail($email,$code);

        $tempPhone = TempPhone::where('phoneNumber', $email)->first();

        if ($tempPhone == null) {

            $tempPhone = new TempPhone;

        }

        $tempPhone->phoneNumber = $email;

        $tempPhone->code = $code;

        $tempPhone->deadline = date('Y-m-d H:i:s', time() + 30 * 60);

        $tempPhone->save();

        $Api_result->status = 2;

        $Api_result->message = $email;

        return $Api_result->toJson();

    }

}

<!--ValidateController类的结束-->

<!--SendEmail类的开始封装了一个方法-->

<?php

/**

 * Created by PhpStorm.

 * User: Android-Dev

 * Date: 2018/1/17

 * Time: 12:13

 */


namespace App\Tool\SME;

use App\Models\EmailModel;

use Mail;


class SendEmail

{

    public function sendEmail($email,$code)

    {

        $emailModel = new EmailModel();

        $emailModel->to = $email;

        $emailModel->subject = 'HLWCHAIN ICO';

        $emailModel->content =$code;

//        $emailModel->content = 'We\'ve received your message and you will get our reply soon.';

//        $code =substr(str_shuffle("012345678901234567890123456789"), 0,6);;

        Mail::send('sendemail',['emailModel' => $emailModel],function($m) use ($emailModel) {

            $m->to($emailModel->to, 'Dear user')

                ->subject($emailModel->subject);

        });

    }

}

<!--SendEmail类的结束的方法-->

<!--EmailModel 模板的开始-->

<?php 


namespace App\Models;


class EmailModel {


  public $from;  // 发件人邮箱

  public $to; // 收件人邮箱

  public $cc; // 抄送

  public $attach; // 附件

  public $subject; // 主题

  public $content; // 内容


}

<!--EmailModel 模板的结束-->

<!--

自己定义个路由,然后注意命名空间有没有引用到,下面我用一个ajax的请求方式进行调用一下我们的接口

-->

<!--发送邮件开始-->

<script>

   var flag = true;

    $('#sendEmail').click(function (event) {

        var email = $('input[name=email]').val();

        if(email==""){

            alert("Email is not allowed to be empty!");

            return false;

        }

        if(email!=""){

            var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;

            if (!reg.test(email)) {

                alert("Email format is not correct");

                return;

            }

        }

        if (flag == false) {

            return;

        }

        flag = false;

        var num = 180;


        var interval = setInterval(function () {

            $("#sendEmail").css("background-color", "silver");

            $('#sendEmail').html(--num + 's Resend');

            if (num == 0) {

                flag = true;

                clearInterval(interval);

                $("#sendEmail").css("background-color", "white");

                $('#sendEmail').html('Send');

            }

        }, 1000);

        $.ajax({

            url: '/HlwChain/sendEmail',

            type: 'POST',

            dataType: 'json',

            cache: false,

            data: {

                email: email,

                _token: "{{csrf_token()}}"

            },

            success: function (data) {

                console.log(data);

                layer.msg('Verifying code has been sent');

                return false;

            }


        })

    });

</script>

<!--发送邮件结束-->

上一条:nginx虚拟主机配置下一条:2018-1-23
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议