Blogger Information
Blog 20
fans 0
comment 0
visits 33320
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
在laravel中写的短信接口和邮箱接口
陈文鹏的博客
Original
1251 people have browsed it

<!--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>

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

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post