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

合并分支 'heshupeng' 到 'master'

Heshupeng



查看合并请求 !186
<?php
namespace app\admin\controller\mobile\course;
use app\common\controller\Backend;
use think\Db;
/**
* 课程兑换码管理
*
* @icon fa fa-circle-o
*/
class CourseCode extends Backend
{
/**
* CourseCode模型对象
* @var \app\admin\model\mobile\course\CourseCode
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\mobile\course\CourseCode;
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
$this->checkCodeMaturity(); //检测兑换码到期时间
$course_id = $this->request->request('course_id');
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField'))
{
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where('course_id',$course_id)
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where('course_id',$course_id)
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->visible(['id','code','status','expire_time','createtime']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
/**
* 生成非销售兑换码
*/
public function createCode()
{
if(request()->isAjax()){
$course_id = $this->request->request('course_id');
$saveData = [];
for($i=0;$i<100;$i++){
$saveData[] = [
'course_id' => $course_id,
'code' => $this->randStr(),
'expire_time' => strtotime('+1 month'),
];
}
$res = $this->model->saveAll($saveData);
if($res){
$this->success('创建成功');
}else{
$this->error('创建失败');
}
}
}
/**
* 生成不重复的兑换码
*/
public function randStr($randLength=6)
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789";
$rand_str = '';
for ($i=0;$i<$randLength;$i++){
$rand_str .= $chars[rand(0,strlen($chars)-1)];
}
$info = $this->model->field('id')->where('code',$rand_str)->find();
if($info){
$rand_str = $this->randStr($randLength);
}
return $rand_str;
}
/**
* 检测兑换码到期时间
*/
public function checkCodeMaturity()
{
$time = time();
$this->model->where("expire_time < {$time}")->setField('status', '2');
}
}
... ...
<?php
return [
'Id' => 'ID',
'Course_id' => '课程ID',
'Code' => '兑换码',
'Status' => '状态',
'Status 0' => '未使用',
'Status 1' => '已使用',
'Status 2' => '已失效',
'Expire_time' => '到期时间',
'Createtime' => '创建时间',
'Updatetime' => '修改时间'
];
... ...
<?php
namespace app\admin\model\mobile\course;
use think\Model;
class CourseCode extends Model
{
// 表名
protected $name = 'mobile_course_code';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'status_text',
'expire_time_text'
];
public function getStatusList()
{
return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getExpireTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setExpireTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
}
... ...
<?php
namespace app\admin\validate\mobile\course;
use think\Validate;
class CourseCode extends Validate
{
/**
* 验证规则
*/
protected $rule = [
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => [],
'edit' => [],
];
}
... ...
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Course_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-course_id" data-rule="required" data-source="course/index" class="form-control selectpage" name="row[course_id]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Code')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-code" data-rule="required" class="form-control" name="row[code]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
<div class="col-xs-12 col-sm-8">
<div class="radio">
{foreach name="statusList" item="vo"}
<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>
{/foreach}
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Expire_time')}:</label>
<div class="col-xs-12 col-sm-8">
<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')}">
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
... ...
<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Course_id')}:</label>
<div class="col-xs-12 col-sm-8">
<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}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Code')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-code" data-rule="required" class="form-control" name="row[code]" type="text" value="{$row.code|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
<div class="col-xs-12 col-sm-8">
<div class="radio">
{foreach name="statusList" item="vo"}
<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>
{/foreach}
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Expire_time')}:</label>
<div class="col-xs-12 col-sm-8">
<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):''}">
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
... ...
<div class="panel panel-default panel-intro">
<div class="panel-heading">
{:build_heading(null,FALSE)}
<ul class="nav nav-tabs" data-field="status">
<li class="active"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
{foreach name="statusList" item="vo"}
<li><a href="#t-{$key}" data-value="{$key}" data-toggle="tab">{$vo}</a></li>
{/foreach}
</ul>
</div>
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding">
<div id="toolbar" class="toolbar">
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
<a href="javascript:;" class="btn btn-success create-code {:$auth->check('mobile/course/courseCode/createCode')?'':'hide'}" title="{:__('生成兑换码')}" ><i class="fa fa-plus"></i> {:__('生成兑换码')}</a>
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-edit="false"
data-operate-del="{:$auth->check('mobile/course/course_code/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ...
<?php
namespace app\mobile\controller;
use think\Validate;
use Exception;
use think\exception\PDOException;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\CourseBanner;
... ... @@ -607,4 +608,59 @@ class Course extends Api
$payment = Service::submitOrder($model['pay_price'], $model['order_sn'], $param['pay_type'], '课程', $notifyurl, null, 'app');
$this->success('成功',$payment);
}
/**
* @ApiTitle (兑换)
* @ApiSummary (兑换)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
* @ApiParams (name="code", type="string", description="兑换码")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": null
})
*/
public function exchange()
{
$param = $this->request->param();
empty($param['course_id']) && $this->error('缺少必要参数');
empty($param['code']) && $this->error('请输入兑换码');
$course = $this->model->get($param['course_id']);
empty($course) && $this->error('课程信息不存在');
$course_code = Db::name('mobile_course_code')
->where('code',$param['code'])
->where('course_id',$param['course_id'])
->find();
empty($course_code) && $this->error('您输入的兑换码不存在');
$course_code['expire_time'] < time() && $this->error('您输入的兑换码已失效');
Db::startTrans();
try {
// 创建订单
$model = new CourseOrder;
$model->save([
'user_id' => $this->auth->id,
'course_id' => $param['course_id'],
'course_code_id' => $course_code['id'],
'order_sn' => get_order_sn(),
'pay_price' => 0,
'pay_type' => 'code',
'course_price' => $course['current_price'],
]);
(new Notify)->notifyCourseZero($model['order_sn'],$model['pay_price'],$model['pay_type']);
Db::name('mobile_course_code')->where('id',$course_code['id'])->update(['status'=>'1']); //兑换码状态变为:已兑换
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('成功');
}
}
\ No newline at end of file
... ...
... ... @@ -90,6 +90,14 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
classname: 'btn btn-xs btn-primary btn-dialog',
icon: 'fa fa-list',
url: 'mobile/course/course_spec?course_id={id}',
},
{
name: 'code',
text: '兑换码',
title: '兑换码',
classname: 'btn btn-xs btn-primary btn-dialog',
icon: 'fa fa-list',
url: 'mobile/course/course_code?course_id={id}',
}
],
formatter: Table.api.formatter.operate
... ...
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'mobile/course/course_code/index' + location.search,
add_url: 'mobile/course/course_code/add',
edit_url: 'mobile/course/course_code/edit',
del_url: 'mobile/course/course_code/del',
multi_url: 'mobile/course/course_code/multi',
create_code_url: 'mobile/course/course_code/createCode' + location.search,
table: 'mobile_course_code',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'code', title: __('Code')},
{field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
{field: 'expire_time', title: __('Expire_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
// 生成兑换码
$(document).on('click', ".create-code", function () {
Layer.confirm('确认生成兑换码吗', {
btn: ['确认','取消'] //按钮
}, function(index){
Backend.api.ajax({
url: $.fn.bootstrapTable.defaults.extend.create_code_url,
}, function () {
table.bootstrapTable('refresh');
Layer.close(index);
});
});
});
},
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
\ No newline at end of file
... ...
此 diff 太大无法显示。