laravel里面关于阿里大于的短信验证码

来源:互联网 发布:乾隆自恋知乎 编辑:程序博客网 时间:2024/06/06 05:46
1,安装阿里大于服务composer require iscms/alisms-for-laravel2,注册服务(config/app.php)iscms/Alisms/AlidayuServiceProvider::class3,生成阿里大于()配置文件(config文件夹)php artisan vendor:publish4,修改阿里大于()配置文件(alisms.php)文件为:<?php  return [      'KEY' =>env('ALISMS_KEY',null),      'SECRETKEY'=>env('ALISMS_SECRETKEY',null)  ];5laravel根目录下找到.env文件ALISMS_KEY=234*****3ALISMS_SECRETKEY=****************
控制器里写方法:
use App\Api\BaseController;use Dingo\Api\Http\Request;use Illuminate\Support\Facades\Validator;use iscms\Alisms\SendsmsPusher as Sms;class CommonController extends BaseController {    /**     * 发送验证短信     *     * 创建人:AaronW     * 创建时间: 201792718:27:08     */    public function sendVerifyMessage(Request $request, Sms $sms) {        $inputData = $request->only(['phone']);        $validator = Validator::make($request->all(), [            'phone' => 'required|regex:/^1[34578][0-9]{9}$/',        ]);        if ($validator->fails()) {            return response()->json([                'errcode' => -4001,                'msg' => 'Param error',                'data' => $validator->errors(),            ]);        }//      已发送未过期不再发送短信验证码        if (get_phone_verify_code($inputData['phone'])) {            xlogger('info', 'request phone number:'.$inputData['phone'], 'sys');            return response()->json([                'errcode' => '-4001',                'msg' => 'msg already send',            ]);        }        $verifycode = random_num_generator(4);        $content = json_encode([            'code' => $verifycode        ]);        $result=$sms->send($inputData['phone'],'上信科技', $content,"SMS_55110001");        set_phone_verify_code($inputData['phone'], $verifycode);        xlogger('info', 'send phone verify code for '.$inputData['phone'].            " ---> result: ".json_encode($result));        return response()->json($result);    }}
公共方法里面:
use Illuminate\Support\Facades\Cache;use Illuminate\Support\Facades\Log;if (!function_exists('get_phone_verify_code')) {    /**     * 获取存储的验证码     * @param $phone     * @return mixed     * @throws Exception     */    function get_phone_verify_code($phone) {        if (strlen($phone) != 11) {            throw new Exception('Param error: param length must 11');        }        return Cache::get('verifycode:'.$phone, false);    }}if (!function_exists('set_phone_verify_code')) {    /**     * 存储验证码     * @param string $phone 手机号     * @param int $expire 单位minute     */    function set_phone_verify_code($phone, $expire = 1) {        Cache::put('verifycode:'.$phone, $expire);    }}if (!function_exists('random_num_generator')) {    /**     * 生成指定数量的随机数字符串     * @param $length     * @return string     * @throws Exception     */    function random_num_generator($length) {        if (!is_int($length)) {            throw new Exception('Type error: param need ini');        }        $numbers = [            0,1,2,3,4,5,6,7,8,9        ];        $result = "";        for ($i = 0; $i < $length; $i++) {            $result.=$numbers[rand(0, 9)];        }        return $result;    }}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 凌波城 凌波的意思 青玉案凌波不过横塘路 碧蓝航线凌波 凌波微步专解图 凌波微步罗袜生尘 凌波不过横塘路 2018年改版凌波城经脉怎么点109 舰娘凌波号为什么叫凌斩仙 专解红字 凌波微步 凌波仙子是什么花 2018年109级凌波城多少伤害达标 凌波鱼和清江鱼哪个好吃 2018凌波城69带碎星套开局秒5 凌波零 木叶之猿飞才藏 风掠凌波 穿成师徒恋的绊脚石 凌波燕 凌派4s店 凌派2013款 杰德 凌派 广本 凌派 凌派 优惠 凌派dvd导航 凌派led大灯 1.8l凌派 凌派1.5l 凌派1.5t 凌派1.8t 凌派 降价 凌派suv 凌派 减震 凌派 平台 凌派1.0t怎么样 凌派2019 缤智1.5 东风思铭 缤智多少钱 滨智suv报价 缤智车怎么样 锋范车怎么样 哥诗图