分类 Laravel 下的文章

这个费了点时间,所以记录下:

App\Exceptions\Handler.php中重写两个方法如下:

/**
 * Convert an authentication exception into a response.
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Auth\AuthenticationException  $exception
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    return $request->expectsJson()
        ? response()->json(['message' => $exception->getMessage()], 401)
        : redirect()->guest($this->redirectTo($exception->guards()) ?? route('login'));
}
/**
 * 重定向跳转至会员中心
 * @param array $guardArr
 * @return string
 */
protected function redirectTo($guardArr = [])
{
    if(in_array('member',$guardArr)){
        return route('member.login');
    }
}

其中关键点是拿到guards()里的门卫名称,根据此来判断:
$exception->guards()得到数据类似['admin','user']再据此判断。

一、前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>开户注册</title>
</head>
<link rel="stylesheet" href="/layui/css/layui.css"  media="all">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
    #captcha{
        float: left;
    }
    #code{
        float: left;
    }
    #text{
        line-height:20px;
    }
</style>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>开户注册</legend>
</fieldset>

<form class="layui-form" action="save" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">开户银行</label>
        <div class="layui-input-block">
            <input type="text" name="bank" lay-verify="required|bank" autocomplete="off" placeholder="请输入银行" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开户账号</label>
        <div class="layui-input-block">
            <input type="text" name="accout" lay-verify="required|number" autocomplete="off" placeholder="请输入账号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required|name" lay-reqtext="用户名是必填项,岂能为空?" placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号码</label>
        <div class="layui-input-block">
            <input type="text" name="idcard" lay-verify="required|identity" placeholder="请输入身份证号码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <br>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">图形验证码</label>
            <div class="layui-input-inline">
                <input type="tel"  name="captcha" lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
            </div>
            <span><div id="captcha">{:captcha_img()}</div></span><span id="text"><a href="">看不清楚!点击图片换一张?</a></span>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="tel" id="phone" name="phone" lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
            <input type="button" id="btn" value="免费获取验证码" class="layui-btn" onclick="settime(this)" />
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">短信验证码</label>
            <div class="layui-input-inline">
                <input type="tel" id="code" name="code" lay-verify="required|code" placeholder="请输入接收的验证码" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <br><br>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="save" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
</html>
<script src="/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '银行名称至少得5个字';
                }
            },
            accout: function(value){
                if(value.length < 5){
                    return '账户至少得5个字符啊';
                }
            },
            code: function(value){
                if(value.length < 3){
                    return '验证码格式不正确';
                }
            },
            name: function(value) {
                if(!new RegExp("^[\u4e00-\u9fa5\]+$").test(value)){
                    return '姓名只能是中文';
                }
            },
            bank: function(value) {
                if(!new RegExp("^[\u4e00-\u9fa5\]+$").test(value)){
                    return '银行只能是中文';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位,且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var data = form.val('example');
            alert(JSON.stringify(data));
        });
    });

</script>
<script type="text/javascript">
    var countdown=60;
    function settime(val) {
        if (countdown == 0) {
            val.removeAttribute("disabled");
            val.value="免费获取验证码";
            countdown = 60;
        } else {
            val.setAttribute("disabled", true);
            val.value="重新发送(" + countdown + ")";
            countdown--;
            setTimeout(function() {
                settime(val)
            },1000)
        }
    }
    layui.use('layer', function(){
        var layer = layui.layer;
        $(function (){
            //获取手机号 发送验证码
            $('#btn').click(function (){
                let phone=$('#phone').val();
                $.post('/api/send',{phone:phone},function (res){
                    console.log(res.msg)
                    if (res.code==200){
                        layer.msg(res.msg);
                        alert(res.msg)
                    }

                })
            })
        })
    });

</script>

二、后端代码

/**
 * 保存新建的资源
 *
 * @param  \think\Request  $request
 * @return \think\Response
 */
public function save(Request $request)
{
    $data=$request->param();

    $validate = \think\facade\Validate::rule([
        'phone|手机号'  => 'require|mobile',
        'captcha|验证码'=>'require|captcha',
        'name|真实姓名'=>'require|chs',
        'bank|银行名称'=>'require|chs',
        'code|短信验证码'=>'require|alphaDash',
        'idcard|身份证'=>'require|idCard',
    ]);
    //验证参数
    if (!$validate->check($data)) {
        die($validate->getError());
    }
}

本文转自:https://blog.csdn.net/array_sum/article/details/117608907

比如说余额更新,防止重复

悲观锁性能很差,我目前用的是乐观锁,但是用的是 `User::where (‘id’, $user->id)->where (‘version’, $user->version)->update ([]);`
这种方式感觉不是很优雅,看手册,发现有一个原子锁,`Cache::lock ()` 这个方案你们觉得怎么样?

回答处理

  1. 放一段一直在用的代码仅供参考,这段代码是所有加减款都必须经过这个方法,所以只在加入这个 lock,下面的代码类似于乐观锁,利用缓存键加入一个钱包 ID,只会在这个钱包 ID 业务下 lock,其它钱包 ID 是另一个 lock 键锁互不影响
  2. if 判断不那么优雅,按照文档说法应该放到 catch 里,但没啥问题,我也不想去改这个逻辑了。又不是不能用(图先欠着)
      $out_purse = null;
      $out_lock = Cache::lock('EBank@_transfer:' . $out_purse_id);
      try {
          $out_purse = $out_lock->block(50, function () use ($out_purse_id, $amount) {
              $var = FundPurse::where(['id' => $out_purse_id, 'status' => 1])->where('balance', '>=', $amount)->decrement('balance', $amount);
              // 未修改返回修改行数为0
              if (!$var) {
                  return false;
              }
              return FundPurse::find($out_purse_id);
          });
          optional($out_lock)->release();
      } catch (LockTimeoutException $e) {

      } finally {
          optional($out_lock)->release();
      }
      if ($out_purse === null) {
          abort(422, '转出钱包查询超时');
      }
      if ($out_purse === false) {
          abort(422, '转出钱包扣款失败,余额不足或账户被禁用');
      }

转自:https://learnku.com/laravel/t/61510

1、获取上传的文件

$file=$request->file(‘file’);

2、获取上传文件的文件名(带后缀,如 abc.png)

$filename=$file->getClientOriginalName();

3、获取上传文件的后缀(如 abc.png,获取到的为 png)

$fileextension=$file->getClientOriginalExtension();

4、获取上传文件的大小

$filesize=$file->getClientSize();

5、获取缓存在 tmp 目录下的文件名(带后缀,如 php8933.tmp)

$filaname=$file->getFilename();

6、获取上传的文件缓存在 tmp 文件夹下的绝对路径

$realpath=$file->getRealPath();

7、将缓存在 tmp 目录下的文件移到某个位置,返回的是这个文件移动过后的路径

$path=$file->move(path,newname);

move () 方法有两个参数,第一个参数是文件移到哪个文件夹下的路径,第二个参数是将上传的文件重新命名的文件名

8、检测上传的文件是否合法,返回值为 true 或 false

$file->isValid();

转自:https://learnku.com/articles/61077

1、xls
application/vnd.ms-excel
2、xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
3、ppt
application/vnd.ms-powerpoint
4、pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
5、doc
application/msword
6、docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
7、zip
application/x-zip-compressed
8、rar
application/x-zip-compressed
9、wmv
video/x-ms-wmv
10、mp3
audio/mpeg
11、mp4
video/mp4
12、gif
image/gif
13、jpg
image/jpeg
14、png
image/png
15、bmp
image/bmp
16、psd
application/octet-stream
17、ico
image/x-icon
18、7z
application/octet-stream
19、exe
application/octet-stream
20、avi
video/avi
21、rmvb
application/vnd.rn-realmedia-vbr
22、3gp
application/octet-stream
23、flv
application/octet-stream
24、wav
audio/wav
25、krc
application/octet-stream
26、lrc
application/octet-stream
27、txt
text/plain
28、pdf
application/pdf
29、chm
application/octet-stream
30、mdb
application/msaccess
31、sql
application/octet-stream
32、con
application/octet-stream
33、log
text/plain
34、dat
application/octet-stream
35、ini
application/octet-stream
36、php
application/octet-stream
37、html 和 htmhtm
text/html
38、ttf
application/octet-stream
39、fon
application/octet-stream
40、js
application/x-javascript
41、xml
text/xml
42、dll
application/octet-stream

转自:https://learnku.com/articles/60802