作者 何书鹏

优惠券管理

  1 +<?php
  2 +
  3 +namespace app\admin\controller;
  4 +
  5 +use app\common\controller\Backend;
  6 +
  7 +/**
  8 + * 用户优惠券
  9 + *
  10 + * @icon fa fa-circle-o
  11 + */
  12 +class UserCoupon extends Backend
  13 +{
  14 +
  15 + /**
  16 + * UserCoupon模型对象
  17 + * @var \app\admin\model\UserCoupon
  18 + */
  19 + protected $model = null;
  20 +
  21 + public function _initialize()
  22 + {
  23 + parent::_initialize();
  24 + $this->model = new \app\admin\model\UserCoupon;
  25 + $this->view->assign("statusList", $this->model->getStatusList());
  26 + }
  27 +
  28 + public function import()
  29 + {
  30 + parent::import();
  31 + }
  32 +
  33 + /**
  34 + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37 + */
  38 +
  39 +
  40 + /**
  41 + * 查看
  42 + */
  43 + public function index()
  44 + {
  45 + //当前是否为关联查询
  46 + $this->relationSearch = true;
  47 + //设置过滤方法
  48 + $this->request->filter(['strip_tags', 'trim']);
  49 + if ($this->request->isAjax()) {
  50 + //如果发送的来源是Selectpage,则转发到Selectpage
  51 + if ($this->request->request('keyField')) {
  52 + return $this->selectpage();
  53 + }
  54 + list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55 +
  56 + $list = $this->model
  57 + ->with(['user'])
  58 + ->where($where)
  59 + ->order($sort, $order)
  60 + ->paginate($limit);
  61 +
  62 + foreach ($list as $row) {
  63 + $row->visible(['id','name','price','full_price','createtime','endtime','status']);
  64 + $row->visible(['user']);
  65 + $row->getRelation('user')->visible(['nickname']);
  66 + }
  67 +
  68 + $result = array("total" => $list->total(), "rows" => $list->items());
  69 +
  70 + return json($result);
  71 + }
  72 + return $this->view->fetch();
  73 + }
  74 +
  75 +}
  1 +<?php
  2 +
  3 +return [
  4 + 'User_id' => '用户id',
  5 + 'Coupon_id' => '优惠券id',
  6 + 'Name' => '优惠券名称',
  7 + 'Price' => '优惠券金额',
  8 + 'Full_price' => '满减金额限制',
  9 + 'Createtime' => '创建时间',
  10 + 'Endtime' => '过期时间',
  11 + 'Status' => '状态',
  12 + 'Status 0' => '待使用',
  13 + 'Status 1' => '已使用',
  14 + 'Status 2' => '已过期',
  15 + 'User.nickname' => '所属用户'
  16 +];
  1 +<?php
  2 +
  3 +namespace app\admin\model;
  4 +
  5 +use think\Model;
  6 +
  7 +
  8 +class UserCoupon extends Model
  9 +{
  10 +
  11 +
  12 +
  13 +
  14 +
  15 + // 表名
  16 + protected $name = 'user_coupon';
  17 +
  18 + // 自动写入时间戳字段
  19 + protected $autoWriteTimestamp = 'int';
  20 +
  21 + // 定义时间戳字段名
  22 + protected $createTime = 'createtime';
  23 + protected $updateTime = false;
  24 + protected $deleteTime = false;
  25 +
  26 + // 追加属性
  27 + protected $append = [
  28 + 'endtime_text',
  29 + 'status_text'
  30 + ];
  31 +
  32 +
  33 +
  34 + public function getStatusList()
  35 + {
  36 + return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
  37 + }
  38 +
  39 +
  40 + public function getEndtimeTextAttr($value, $data)
  41 + {
  42 + $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
  43 + return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  44 + }
  45 +
  46 +
  47 + public function getStatusTextAttr($value, $data)
  48 + {
  49 + $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  50 + $list = $this->getStatusList();
  51 + return isset($list[$value]) ? $list[$value] : '';
  52 + }
  53 +
  54 + protected function setEndtimeAttr($value)
  55 + {
  56 + return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  57 + }
  58 +
  59 +
  60 + public function user()
  61 + {
  62 + return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  63 + }
  64 +}
  1 +<?php
  2 +
  3 +namespace app\admin\validate;
  4 +
  5 +use think\Validate;
  6 +
  7 +class UserCoupon extends Validate
  8 +{
  9 + /**
  10 + * 验证规则
  11 + */
  12 + protected $rule = [
  13 + ];
  14 + /**
  15 + * 提示消息
  16 + */
  17 + protected $message = [
  18 + ];
  19 + /**
  20 + * 验证场景
  21 + */
  22 + protected $scene = [
  23 + 'add' => [],
  24 + 'edit' => [],
  25 + ];
  26 +
  27 +}
  1 +<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('User_id')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-user_id" data-rule="required" min="0" data-source="user/user/index" data-field="nickname" class="form-control selectpage" name="row[user_id]" type="text" value="">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Coupon_id')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-coupon_id" data-rule="required" min="0" data-source="coupon/index" class="form-control selectpage" name="row[coupon_id]" type="text" value="">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 + <input id="c-name" data-rule="required" class="form-control" name="row[name]" type="text" value="">
  19 + </div>
  20 + </div>
  21 + <div class="form-group">
  22 + <label class="control-label col-xs-12 col-sm-2">{:__('Price')}:</label>
  23 + <div class="col-xs-12 col-sm-8">
  24 + <input id="c-price" data-rule="required" class="form-control" step="0.01" name="row[price]" type="number" value="0.00">
  25 + </div>
  26 + </div>
  27 + <div class="form-group">
  28 + <label class="control-label col-xs-12 col-sm-2">{:__('Full_price')}:</label>
  29 + <div class="col-xs-12 col-sm-8">
  30 + <input id="c-full_price" data-rule="required" class="form-control" step="0.01" name="row[full_price]" type="number" value="0.00">
  31 + </div>
  32 + </div>
  33 + <div class="form-group">
  34 + <label class="control-label col-xs-12 col-sm-2">{:__('Endtime')}:</label>
  35 + <div class="col-xs-12 col-sm-8">
  36 + <input id="c-endtime" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[endtime]" type="text" value="{:date('Y-m-d H:i:s')}">
  37 + </div>
  38 + </div>
  39 + <div class="form-group">
  40 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  41 + <div class="col-xs-12 col-sm-8">
  42 +
  43 + <div class="radio">
  44 + {foreach name="statusList" item="vo"}
  45 + <label for="row[status]-{$key}"><input id="row[status]-{$key}" name="row[status]" type="radio" value="{$key}" {in name="key" value="0"}checked{/in} /> {$vo}</label>
  46 + {/foreach}
  47 + </div>
  48 +
  49 + </div>
  50 + </div>
  51 + <div class="form-group layer-footer">
  52 + <label class="control-label col-xs-12 col-sm-2"></label>
  53 + <div class="col-xs-12 col-sm-8">
  54 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  55 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  56 + </div>
  57 + </div>
  58 +</form>
  1 +<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('User_id')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-user_id" data-rule="required" min="0" data-source="user/user/index" data-field="nickname" class="form-control selectpage" name="row[user_id]" type="text" value="{$row.user_id|htmlentities}">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Coupon_id')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-coupon_id" data-rule="required" min="0" data-source="coupon/index" class="form-control selectpage" name="row[coupon_id]" type="text" value="{$row.coupon_id|htmlentities}">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 + <input id="c-name" data-rule="required" class="form-control" name="row[name]" type="text" value="{$row.name|htmlentities}">
  19 + </div>
  20 + </div>
  21 + <div class="form-group">
  22 + <label class="control-label col-xs-12 col-sm-2">{:__('Price')}:</label>
  23 + <div class="col-xs-12 col-sm-8">
  24 + <input id="c-price" data-rule="required" class="form-control" step="0.01" name="row[price]" type="number" value="{$row.price|htmlentities}">
  25 + </div>
  26 + </div>
  27 + <div class="form-group">
  28 + <label class="control-label col-xs-12 col-sm-2">{:__('Full_price')}:</label>
  29 + <div class="col-xs-12 col-sm-8">
  30 + <input id="c-full_price" data-rule="required" class="form-control" step="0.01" name="row[full_price]" type="number" value="{$row.full_price|htmlentities}">
  31 + </div>
  32 + </div>
  33 + <div class="form-group">
  34 + <label class="control-label col-xs-12 col-sm-2">{:__('Endtime')}:</label>
  35 + <div class="col-xs-12 col-sm-8">
  36 + <input id="c-endtime" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[endtime]" type="text" value="{:$row.endtime?datetime($row.endtime):''}">
  37 + </div>
  38 + </div>
  39 + <div class="form-group">
  40 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  41 + <div class="col-xs-12 col-sm-8">
  42 +
  43 + <div class="radio">
  44 + {foreach name="statusList" item="vo"}
  45 + <label for="row[status]-{$key}"><input id="row[status]-{$key}" name="row[status]" type="radio" value="{$key}" {in name="key" value="$row.status"}checked{/in} /> {$vo}</label>
  46 + {/foreach}
  47 + </div>
  48 +
  49 + </div>
  50 + </div>
  51 + <div class="form-group layer-footer">
  52 + <label class="control-label col-xs-12 col-sm-2"></label>
  53 + <div class="col-xs-12 col-sm-8">
  54 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  55 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  56 + </div>
  57 + </div>
  58 +</form>
  1 +<div class="panel panel-default panel-intro">
  2 +
  3 + <div class="panel-heading">
  4 + {:build_heading(null,FALSE)}
  5 + <ul class="nav nav-tabs" data-field="status">
  6 + <li class="{:$Think.get.status === null ? 'active' : ''}"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
  7 + {foreach name="statusList" item="vo"}
  8 + <li class="{:$Think.get.status === (string)$key ? 'active' : ''}"><a href="#t-{$key}" data-value="{$key}" data-toggle="tab">{$vo}</a></li>
  9 + {/foreach}
  10 + </ul>
  11 + </div>
  12 +
  13 +
  14 + <div class="panel-body">
  15 + <div id="myTabContent" class="tab-content">
  16 + <div class="tab-pane fade active in" id="one">
  17 + <div class="widget-body no-padding">
  18 + <div id="toolbar" class="toolbar">
  19 + <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
  20 + <!--<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('user_coupon/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
  21 + <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('user_coupon/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>-->
  22 + <a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('user_coupon/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
  23 + <!--<a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('user_coupon/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
  24 +
  25 + <div class="dropdown btn-group {:$auth->check('user_coupon/multi')?'':'hide'}">
  26 + <a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
  27 + <ul class="dropdown-menu text-left" role="menu">
  28 + <li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
  29 + <li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
  30 + </ul>
  31 + </div>-->
  32 +
  33 +
  34 + </div>
  35 + <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
  36 + data-operate-edit=""
  37 + data-operate-del="{:$auth->check('user_coupon/del')}"
  38 + width="100%">
  39 + </table>
  40 + </div>
  41 + </div>
  42 +
  43 + </div>
  44 + </div>
  45 +</div>
  1 +define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2 +
  3 + var Controller = {
  4 + index: function () {
  5 + // 初始化表格参数配置
  6 + Table.api.init({
  7 + extend: {
  8 + index_url: 'user_coupon/index' + location.search,
  9 + add_url: 'user_coupon/add',
  10 + edit_url: 'user_coupon/edit',
  11 + del_url: 'user_coupon/del',
  12 + multi_url: 'user_coupon/multi',
  13 + import_url: 'user_coupon/import',
  14 + table: 'user_coupon',
  15 + }
  16 + });
  17 +
  18 + var table = $("#table");
  19 +
  20 + // 初始化表格
  21 + table.bootstrapTable({
  22 + url: $.fn.bootstrapTable.defaults.extend.index_url,
  23 + pk: 'id',
  24 + sortName: 'id',
  25 + columns: [
  26 + [
  27 + {checkbox: true},
  28 + {field: 'id', title: __('Id')},
  29 + {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
  30 + {field: 'name', title: __('Name'), operate: 'LIKE'},
  31 + {field: 'price', title: __('Price'), operate:'BETWEEN'},
  32 + {field: 'full_price', title: __('Full_price'), operate:'BETWEEN'},
  33 + {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  34 + {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  35 + {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
  36 + {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  37 + ]
  38 + ]
  39 + });
  40 +
  41 + // 为表格绑定事件
  42 + Table.api.bindevent(table);
  43 + },
  44 + add: function () {
  45 + Controller.api.bindevent();
  46 + },
  47 + edit: function () {
  48 + Controller.api.bindevent();
  49 + },
  50 + api: {
  51 + bindevent: function () {
  52 + Form.api.bindevent($("form[role=form]"));
  53 + }
  54 + }
  55 + };
  56 + return Controller;
  57 +});