model = new \app\admin\model\Game; $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("msgstatusList", $this->model->getMsgstatusList()); $this->view->assign("showstatusList", $this->model->getShowstatusList()); $this->view->assign("isAutoshowList", $this->model->getIsAutoshowList()); $this->view->assign("isBannerList", $this->model->getIsBannerList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['price']) || $params['price'] <= 0) { $this->error('请输入正确价格'); } if (strtotime($params['starttime']) < time()) { $this->error('活动开始时间不能小于当前时间'); } if (strtotime($params['starttime']) >= strtotime($params['endtime'])) { $this->error('活动结束时间必须大于活动开始时间'); } if ($params['num'] < 1) { $this->error('请输入正确每队最大人数'); } if (strtotime($params['signupendtime']) < time()) { $this->error('报名截止时间不能小于当前时间'); } if (strtotime($params['signupendtime']) >= strtotime($params['starttime'])) { $this->error('报名截止时间必须小于活动开始时间'); } // if (strtotime($params['refundendtime']) < time()) { // $this->error('退款截止时间不能小于当前时间'); // } // if (strtotime($params['refundendtime']) >= strtotime($params['signupendtime'])) { // $this->error('退款截止时间必须小于报名截止时间'); // } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } $result = $this->model->allowField(true)->save($params); $id = $this->model->id; Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { /*if ($params['is_banner'] == 1) { //在轮播图显示 $banner_data['title'] = $params['title']; $banner_data['image'] = $params['image']; $banner_data['url'] = '/pages/home/detail?id='.$id; $banner_data['createtime'] = time(); Db::name('banner')->insertGetId($banner_data); }*/ $this->success(); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); // p($row['status']);p($params);die; if ($row['status'] == 2 || $row['status'] == 3) { $this->error('活动已经结束或取消'); } if ($params['showstatus'] == 0) { //下架 $count = Db::name('game_people')->where(['game_id' => $ids, 'status' => 1])->count('id'); if ($count) { $this->error('已经有人报名,不能下架'); } } $result = false; Db::startTrans(); /*//是否在轮播图显示 if ($params['is_banner'] == 0) { //不显示 Db::name('banner')->where(['title' => $row['title']])->delete(); } elseif ($params['is_banner'] == 1) { //显示 $banner_count = Db::name('banner')->where(['title' => $row['title']])->count('id'); if (!$banner_count) { $banner_data['title'] = $row['title']; $banner_data['image'] = $row['image']; $banner_data['url'] = '/pages/home/detail?id=' . $ids; $banner_data['createtime'] = time(); Db::name('banner')->insertGetId($banner_data); } }*/ try { if (isset($params['status'])) { if ($params['status'] == 2) { //活动结束 //修改活动人员状态 $people_rs = Db::name('game_people')->where(['game_id' => $ids, 'status' => 1])->setField(['status' => 2, 'updatetime' => time()]); if ($people_rs === false) { Db::rollback(); return ['code' => 0, 'msg' => '操作失败']; } } elseif ($params['status'] == 3) { //活动取消 /*//查询报名订单 $sys_msg = Db::name('sys_msg'); //消息通知 $hu_game_people = Db::name('game_people'); //报名人员表 $active_order_list = $hu_game_people->where(['game_id' => $ids, 'status' => 1])->select(); if ($active_order_list) { //活动订单修改信息 $order_data['status'] = 3; //状态:0=待付款,1=待出行,2=已完成,3=已取消 $order_data['updatetime'] = time(); foreach ($active_order_list as &$v) { //退还支付金额 if ($v['price'] > 0) { $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败 $order_data['refundprice'] = $v['price'];//退款金额 $order_data['refundtime'] = time(); if ($v['paytype'] == 0) { //余额支付, 退回余额 $rs = create_log($v['price'], '活动取消返还', $v['user_id'], 3, $v['id']); if ($rs != 1) { $order_data['refundstatus'] = 2; } } elseif ($v['paytype'] == 1) { //微信支付, 退回微信 //扣除用户成长值 $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值 $balance = floor($paygrowth * $v['price']); if ($balance > 0) { $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $v['user_id'], 4, $v['id']); } //退款单号 $order_data['refund_no'] = $v['order_sn']; //调用微信退款 $wxData['transaction_id'] = $v['transaction_id']; $wxData['out_refund_no'] = $order_data['refund_no']; $wxData['total_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分 $wxData['refund_fee'] = (int)($v['price'] * 100);//1 微信支付 单位为分 $wxData['refund_desc'] = '活动取消返还'; // require_once("Plugins/WxPay/WxPay.php"); $wxPay = new wxpay\WxPay(config('wxchatpay')); $back = $wxPay->WxPayRefund($wxData); if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') { $order_data['refundstatus'] = 2; } } } //修改活动订单状态 $order_rs = $hu_game_people->where(['id' => $v['id'], 'status' => 1])->setField($order_data); if ($order_rs === false) { Db::rollback(); return ['code' => 0, 'msg' => '操作失败']; } //发送消息 $data = [ 'user_id' => $v['user_id'], 'type' => 1, 'title' => '活动通知', 'content' => '您报名的' . $row['title'] . '活动已取消', 'createtime' => time() ]; $sys_msg->insertGetId($data); } }*/ //修改活动人员状态 $people_rs = Db::name('game_people')->where(['game_id' => $ids, 'status' => 1])->setField(['status' => 3, 'updatetime' => time()]); if ($people_rs === false) { Db::rollback(); return ['code' => 0, 'msg' => '操作失败']; } } } //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 删除 */ public function del($ids = "") { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); $count = Db::name('game_people')->where(['game_id' => $ids, 'status' => ['in', [1, 2]]])->count('id'); if ($count) { $this->error('已经有人报名或未退款,不能删除'); } Db::name('game')->delete($ids); Db::name('game_people')->where(['game_id' => $ids])->delete(); $this->success(); } }