2021年10月

动态渲染select表单

    form.on('radio(xf_step_type)', function (data) {
        let radioVal = $('#IsPurchased input[name="processor_type"]:checked ').val();
        var param;
        if (radioVal === "member") {
            param = 'member';
        }else if(radioVal === "department"){
            param = 'department';
        }else{
            param = 'position';
        }

        //请求数据
        $.ajax({
            url:'/manage/public/select_table/' + param,
            dataType:'json',
            type:'post',
            async:false,
            headers:{"X-CSRF-TOKEN" : $('meta[name="csrf-token"]').attr('content')},
            success:function(res){
                //先清空select列表数据
                $("#processor").empty();
                $.each(res.data,function(index,item){
                    //方法一
                    $('#processor').append(new Option(item.title,item.id));
                    //方法二 往下拉菜单里添加元素
                    //$("#processor").append("<option value='"+item.id+"'>"+item.title+"</option>");
                })
                form.render();//菜单渲染 把内容加载进去
            }
        });
        //form.render();
    });

一、前端代码

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