作者 何书鹏
1 个管道 的构建 通过 耗费 5 秒

合并分支 'heshupeng' 到 'master'

Heshupeng



查看合并请求 !186
  1 +<?php
  2 +
  3 +namespace app\admin\controller\mobile\course;
  4 +
  5 +use app\common\controller\Backend;
  6 +use think\Db;
  7 +
  8 +/**
  9 + * 课程兑换码管理
  10 + *
  11 + * @icon fa fa-circle-o
  12 + */
  13 +class CourseCode extends Backend
  14 +{
  15 +
  16 + /**
  17 + * CourseCode模型对象
  18 + * @var \app\admin\model\mobile\course\CourseCode
  19 + */
  20 + protected $model = null;
  21 +
  22 + public function _initialize()
  23 + {
  24 + parent::_initialize();
  25 + $this->model = new \app\admin\model\mobile\course\CourseCode;
  26 + $this->view->assign("statusList", $this->model->getStatusList());
  27 + }
  28 +
  29 + /**
  30 + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33 + */
  34 +
  35 +
  36 + /**
  37 + * 查看
  38 + */
  39 + public function index()
  40 + {
  41 + $this->checkCodeMaturity(); //检测兑换码到期时间
  42 + $course_id = $this->request->request('course_id');
  43 + //当前是否为关联查询
  44 + $this->relationSearch = false;
  45 + //设置过滤方法
  46 + $this->request->filter(['strip_tags', 'trim']);
  47 + if ($this->request->isAjax())
  48 + {
  49 + //如果发送的来源是Selectpage,则转发到Selectpage
  50 + if ($this->request->request('keyField'))
  51 + {
  52 + return $this->selectpage();
  53 + }
  54 + list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55 + $total = $this->model
  56 + ->where('course_id',$course_id)
  57 + ->where($where)
  58 + ->order($sort, $order)
  59 + ->count();
  60 +
  61 + $list = $this->model
  62 + ->where('course_id',$course_id)
  63 + ->where($where)
  64 + ->order($sort, $order)
  65 + ->limit($offset, $limit)
  66 + ->select();
  67 +
  68 + foreach ($list as $row) {
  69 + $row->visible(['id','code','status','expire_time','createtime']);
  70 +
  71 + }
  72 + $list = collection($list)->toArray();
  73 + $result = array("total" => $total, "rows" => $list);
  74 +
  75 + return json($result);
  76 + }
  77 + return $this->view->fetch();
  78 + }
  79 +
  80 + /**
  81 + * 生成非销售兑换码
  82 + */
  83 + public function createCode()
  84 + {
  85 + if(request()->isAjax()){
  86 + $course_id = $this->request->request('course_id');
  87 + $saveData = [];
  88 + for($i=0;$i<100;$i++){
  89 + $saveData[] = [
  90 + 'course_id' => $course_id,
  91 + 'code' => $this->randStr(),
  92 + 'expire_time' => strtotime('+1 month'),
  93 + ];
  94 + }
  95 + $res = $this->model->saveAll($saveData);
  96 + if($res){
  97 + $this->success('创建成功');
  98 + }else{
  99 + $this->error('创建失败');
  100 + }
  101 + }
  102 +
  103 + }
  104 +
  105 + /**
  106 + * 生成不重复的兑换码
  107 + */
  108 + public function randStr($randLength=6)
  109 + {
  110 + $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789";
  111 + $rand_str = '';
  112 + for ($i=0;$i<$randLength;$i++){
  113 + $rand_str .= $chars[rand(0,strlen($chars)-1)];
  114 + }
  115 + $info = $this->model->field('id')->where('code',$rand_str)->find();
  116 + if($info){
  117 + $rand_str = $this->randStr($randLength);
  118 + }
  119 + return $rand_str;
  120 + }
  121 +
  122 + /**
  123 + * 检测兑换码到期时间
  124 + */
  125 + public function checkCodeMaturity()
  126 + {
  127 + $time = time();
  128 + $this->model->where("expire_time < {$time}")->setField('status', '2');
  129 + }
  130 +
  131 +}
  1 +<?php
  2 +
  3 +return [
  4 + 'Id' => 'ID',
  5 + 'Course_id' => '课程ID',
  6 + 'Code' => '兑换码',
  7 + 'Status' => '状态',
  8 + 'Status 0' => '未使用',
  9 + 'Status 1' => '已使用',
  10 + 'Status 2' => '已失效',
  11 + 'Expire_time' => '到期时间',
  12 + 'Createtime' => '创建时间',
  13 + 'Updatetime' => '修改时间'
  14 +];
  1 +<?php
  2 +
  3 +namespace app\admin\model\mobile\course;
  4 +
  5 +use think\Model;
  6 +
  7 +
  8 +class CourseCode extends Model
  9 +{
  10 +
  11 +
  12 +
  13 +
  14 +
  15 + // 表名
  16 + protected $name = 'mobile_course_code';
  17 +
  18 + // 自动写入时间戳字段
  19 + protected $autoWriteTimestamp = 'int';
  20 +
  21 + // 定义时间戳字段名
  22 + protected $createTime = 'createtime';
  23 + protected $updateTime = 'updatetime';
  24 + protected $deleteTime = false;
  25 +
  26 + // 追加属性
  27 + protected $append = [
  28 + 'status_text',
  29 + 'expire_time_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 getStatusTextAttr($value, $data)
  41 + {
  42 + $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  43 + $list = $this->getStatusList();
  44 + return isset($list[$value]) ? $list[$value] : '';
  45 + }
  46 +
  47 +
  48 + public function getExpireTimeTextAttr($value, $data)
  49 + {
  50 + $value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
  51 + return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  52 + }
  53 +
  54 + protected function setExpireTimeAttr($value)
  55 + {
  56 + return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  57 + }
  58 +
  59 +
  60 +}
  1 +<?php
  2 +
  3 +namespace app\admin\validate\mobile\course;
  4 +
  5 +use think\Validate;
  6 +
  7 +class CourseCode 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">{:__('Course_id')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-course_id" data-rule="required" data-source="course/index" class="form-control selectpage" name="row[course_id]" type="text" value="">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Code')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-code" data-rule="required" class="form-control" name="row[code]" type="text" value="">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 +
  19 + <div class="radio">
  20 + {foreach name="statusList" item="vo"}
  21 + <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>
  22 + {/foreach}
  23 + </div>
  24 +
  25 + </div>
  26 + </div>
  27 + <div class="form-group">
  28 + <label class="control-label col-xs-12 col-sm-2">{:__('Expire_time')}:</label>
  29 + <div class="col-xs-12 col-sm-8">
  30 + <input id="c-expire_time" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[expire_time]" type="text" value="{:date('Y-m-d H:i:s')}">
  31 + </div>
  32 + </div>
  33 + <div class="form-group layer-footer">
  34 + <label class="control-label col-xs-12 col-sm-2"></label>
  35 + <div class="col-xs-12 col-sm-8">
  36 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  37 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  38 + </div>
  39 + </div>
  40 +</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">{:__('Course_id')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-course_id" data-rule="required" data-source="course/index" class="form-control selectpage" name="row[course_id]" type="text" value="{$row.course_id|htmlentities}">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Code')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-code" data-rule="required" class="form-control" name="row[code]" type="text" value="{$row.code|htmlentities}">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 +
  19 + <div class="radio">
  20 + {foreach name="statusList" item="vo"}
  21 + <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>
  22 + {/foreach}
  23 + </div>
  24 +
  25 + </div>
  26 + </div>
  27 + <div class="form-group">
  28 + <label class="control-label col-xs-12 col-sm-2">{:__('Expire_time')}:</label>
  29 + <div class="col-xs-12 col-sm-8">
  30 + <input id="c-expire_time" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[expire_time]" type="text" value="{:$row.expire_time?datetime($row.expire_time):''}">
  31 + </div>
  32 + </div>
  33 + <div class="form-group layer-footer">
  34 + <label class="control-label col-xs-12 col-sm-2"></label>
  35 + <div class="col-xs-12 col-sm-8">
  36 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  37 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  38 + </div>
  39 + </div>
  40 +</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="active"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
  7 + {foreach name="statusList" item="vo"}
  8 + <li><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 create-code {:$auth->check('mobile/course/courseCode/createCode')?'':'hide'}" title="{:__('生成兑换码')}" ><i class="fa fa-plus"></i> {:__('生成兑换码')}</a>
  21 +
  22 + </div>
  23 + <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
  24 + data-operate-edit="false"
  25 + data-operate-del="{:$auth->check('mobile/course/course_code/del')}"
  26 + width="100%">
  27 + </table>
  28 + </div>
  29 + </div>
  30 +
  31 + </div>
  32 + </div>
  33 +</div>
1 <?php 1 <?php
2 namespace app\mobile\controller; 2 namespace app\mobile\controller;
3 3
4 -use think\Validate; 4 +use Exception;
  5 +use think\exception\PDOException;
5 use think\Db; 6 use think\Db;
6 use app\common\controller\Api; 7 use app\common\controller\Api;
7 use app\mobile\model\CourseBanner; 8 use app\mobile\model\CourseBanner;
@@ -607,4 +608,59 @@ class Course extends Api @@ -607,4 +608,59 @@ class Course extends Api
607 $payment = Service::submitOrder($model['pay_price'], $model['order_sn'], $param['pay_type'], '课程', $notifyurl, null, 'app'); 608 $payment = Service::submitOrder($model['pay_price'], $model['order_sn'], $param['pay_type'], '课程', $notifyurl, null, 'app');
608 $this->success('成功',$payment); 609 $this->success('成功',$payment);
609 } 610 }
  611 +
  612 + /**
  613 + * @ApiTitle (兑换)
  614 + * @ApiSummary (兑换)
  615 + * @ApiMethod (POST)
  616 + *
  617 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  618 + * @ApiParams (name="course_id", type="int", required=true, description="课程ID")
  619 + * @ApiParams (name="code", type="string", description="兑换码")
  620 + *
  621 + * @ApiReturn({
  622 + "code": 1,
  623 + "msg": "成功",
  624 + "time": "1599046220",
  625 + "data": null
  626 + })
  627 + */
  628 + public function exchange()
  629 + {
  630 + $param = $this->request->param();
  631 + empty($param['course_id']) && $this->error('缺少必要参数');
  632 + empty($param['code']) && $this->error('请输入兑换码');
  633 + $course = $this->model->get($param['course_id']);
  634 + empty($course) && $this->error('课程信息不存在');
  635 + $course_code = Db::name('mobile_course_code')
  636 + ->where('code',$param['code'])
  637 + ->where('course_id',$param['course_id'])
  638 + ->find();
  639 + empty($course_code) && $this->error('您输入的兑换码不存在');
  640 + $course_code['expire_time'] < time() && $this->error('您输入的兑换码已失效');
  641 + Db::startTrans();
  642 + try {
  643 + // 创建订单
  644 + $model = new CourseOrder;
  645 + $model->save([
  646 + 'user_id' => $this->auth->id,
  647 + 'course_id' => $param['course_id'],
  648 + 'course_code_id' => $course_code['id'],
  649 + 'order_sn' => get_order_sn(),
  650 + 'pay_price' => 0,
  651 + 'pay_type' => 'code',
  652 + 'course_price' => $course['current_price'],
  653 + ]);
  654 + (new Notify)->notifyCourseZero($model['order_sn'],$model['pay_price'],$model['pay_type']);
  655 + Db::name('mobile_course_code')->where('id',$course_code['id'])->update(['status'=>'1']); //兑换码状态变为:已兑换
  656 + Db::commit();
  657 + } catch (PDOException $e) {
  658 + Db::rollback();
  659 + $this->error($e->getMessage());
  660 + } catch (Exception $e) {
  661 + Db::rollback();
  662 + $this->error($e->getMessage());
  663 + }
  664 + $this->success('成功');
  665 + }
610 } 666 }
@@ -90,6 +90,14 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin @@ -90,6 +90,14 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
90 classname: 'btn btn-xs btn-primary btn-dialog', 90 classname: 'btn btn-xs btn-primary btn-dialog',
91 icon: 'fa fa-list', 91 icon: 'fa fa-list',
92 url: 'mobile/course/course_spec?course_id={id}', 92 url: 'mobile/course/course_spec?course_id={id}',
  93 + },
  94 + {
  95 + name: 'code',
  96 + text: '兑换码',
  97 + title: '兑换码',
  98 + classname: 'btn btn-xs btn-primary btn-dialog',
  99 + icon: 'fa fa-list',
  100 + url: 'mobile/course/course_code?course_id={id}',
93 } 101 }
94 ], 102 ],
95 formatter: Table.api.formatter.operate 103 formatter: Table.api.formatter.operate
  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: 'mobile/course/course_code/index' + location.search,
  9 + add_url: 'mobile/course/course_code/add',
  10 + edit_url: 'mobile/course/course_code/edit',
  11 + del_url: 'mobile/course/course_code/del',
  12 + multi_url: 'mobile/course/course_code/multi',
  13 + create_code_url: 'mobile/course/course_code/createCode' + location.search,
  14 + table: 'mobile_course_code',
  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: 'code', title: __('Code')},
  30 + {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
  31 + {field: 'expire_time', title: __('Expire_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  32 + {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  33 + {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  34 + ]
  35 + ]
  36 + });
  37 +
  38 + // 为表格绑定事件
  39 + Table.api.bindevent(table);
  40 +
  41 + // 生成兑换码
  42 + $(document).on('click', ".create-code", function () {
  43 + Layer.confirm('确认生成兑换码吗', {
  44 + btn: ['确认','取消'] //按钮
  45 + }, function(index){
  46 + Backend.api.ajax({
  47 + url: $.fn.bootstrapTable.defaults.extend.create_code_url,
  48 + }, function () {
  49 + table.bootstrapTable('refresh');
  50 + Layer.close(index);
  51 + });
  52 + });
  53 + });
  54 + },
  55 + add: function () {
  56 + Controller.api.bindevent();
  57 + },
  58 + edit: function () {
  59 + Controller.api.bindevent();
  60 + },
  61 + api: {
  62 + bindevent: function () {
  63 + Form.api.bindevent($("form[role=form]"));
  64 + }
  65 + }
  66 + };
  67 + return Controller;
  68 +});
此 diff 太大无法显示。