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

课程购买次数

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace app\admin\controller\mobile\course; 3 namespace app\admin\controller\mobile\course;
4 4
5 use app\common\controller\Backend; 5 use app\common\controller\Backend;
  6 +use think\Db;
6 7
7 /** 8 /**
8 * 精品课程管理 9 * 精品课程管理
@@ -53,20 +54,20 @@ class Course extends Backend @@ -53,20 +54,20 @@ class Course extends Backend
53 $total = $this->model 54 $total = $this->model
54 55
55 ->where($where) 56 ->where($where)
56 - ->order('is_top desc') 57 + ->order(['is_top' => 'desc','top_time' => 'desc', 'createtime' => 'desc'])
57 ->order($sort, $order) 58 ->order($sort, $order)
58 ->count(); 59 ->count();
59 60
60 $list = $this->model 61 $list = $this->model
61 62
62 ->where($where) 63 ->where($where)
63 - ->order('is_top desc') 64 + ->order(['is_top' => 'desc','top_time' => 'desc', 'createtime' => 'desc'])
64 ->order($sort, $order) 65 ->order($sort, $order)
65 ->limit($offset, $limit) 66 ->limit($offset, $limit)
66 ->select(); 67 ->select();
67 68
68 foreach ($list as $row) { 69 foreach ($list as $row) {
69 - $row->visible(['id','title','cover','current_price','original_price','study_num_rate','study_num_real','teacher_avatar','teacher_name','teacher_desc','is_top']); 70 + $row->visible(['id','title','cover','current_price','original_price','study_num_rate','study_num_real','teacher_avatar','teacher_name','teacher_desc','is_top','pay_times']);
70 71
71 } 72 }
72 $list = collection($list)->toArray(); 73 $list = collection($list)->toArray();
@@ -77,6 +78,45 @@ class Course extends Backend @@ -77,6 +78,45 @@ class Course extends Backend
77 return $this->view->fetch(); 78 return $this->view->fetch();
78 } 79 }
79 80
  81 + /**
  82 + * 置顶
  83 + */
  84 + public function top($ids = null)
  85 + {
  86 + if($ids) {
  87 + $row = $this->model->get($ids);
  88 + if (!$row) {
  89 + $this->error(__('No Results were found'));
  90 + }
  91 + $adminIds = $this->getDataLimitAdminIds();
  92 + if (is_array($adminIds)) {
  93 + if (!in_array($row[$this->dataLimitField], $adminIds)) {
  94 + $this->error(__('You have no permission'));
  95 + }
  96 + }
  97 + $params = $this->request->param();
  98 + $params['top_time'] = $params['is_top'] == '1' ? time() : 0;
  99 + Db::startTrans();
  100 + try {
  101 + $result = $row->allowField(true)->save($params);
  102 + Db::commit();
  103 + } catch (ValidateException $e) {
  104 + Db::rollback();
  105 + $this->error($e->getMessage());
  106 + } catch (PDOException $e) {
  107 + Db::rollback();
  108 + $this->error($e->getMessage());
  109 + } catch (\think\Exception $e) {
  110 + Db::rollback();
  111 + $this->error($e->getMessage());
  112 + }
  113 + if(!$result) {
  114 + $this->error('操作失败');
  115 + }
  116 + $this->success('操作成功');
  117 + }
  118 + }
  119 +
80 public function selectpage() 120 public function selectpage()
81 { 121 {
82 return parent::selectpage(); // TODO: Change the autogenerated stub 122 return parent::selectpage(); // TODO: Change the autogenerated stub
@@ -18,5 +18,6 @@ return [ @@ -18,5 +18,6 @@ return [
18 'Is_top 1' => '是', 18 'Is_top 1' => '是',
19 'Top_time' => '置顶时间', 19 'Top_time' => '置顶时间',
20 'Createtime' => '创建时间', 20 'Createtime' => '创建时间',
21 - 'Updatetime' => '修改时间' 21 + 'Updatetime' => '修改时间',
  22 + 'Pay_times' => '购买次数'
22 ]; 23 ];
@@ -81,7 +81,7 @@ class Course extends Api @@ -81,7 +81,7 @@ class Course extends Api
81 $page_num = $this->request->param('page_num', 10, 'intval'); 81 $page_num = $this->request->param('page_num', 10, 'intval');
82 switch ($order) { 82 switch ($order) {
83 case 0: 83 case 0:
84 - $order = ['is_top' => 'desc','top_time' => 'desc']; 84 + $order = ['is_top' => 'desc','top_time' => 'desc', 'createtime' => 'desc'];
85 break; 85 break;
86 case 1: 86 case 1:
87 $order = ['study_num_rate' => 'desc']; 87 $order = ['study_num_rate' => 'desc'];
@@ -44,6 +44,8 @@ class Notify extends Api @@ -44,6 +44,8 @@ class Notify extends Api
44 $order = CourseOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]); 44 $order = CourseOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
45 if($order && $order['pay_status'] != '1'){ 45 if($order && $order['pay_status'] != '1'){
46 $order->save(['pay_status'=>'1','pay_time'=>time()]); 46 $order->save(['pay_status'=>'1','pay_time'=>time()]);
  47 + // 购买次数加1
  48 + Db::name('mobile_course')->where('id',$order['course_id'])->setInc('pay_times');
47 } 49 }
48 } catch (Exception $e) { 50 } catch (Exception $e) {
49 } 51 }