model = new \app\admin\model\JunkaPurchcardLog; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 * * @return string|Json * @throws \think\Exception * @throws DbException */ public function index() { $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->where($where) ->with('store,code,reject') ->order($sort, $order) ->paginate($limit); foreach ($list as $k => $v) { $card_data = json_decode($v->data, true); $str = ''; if($card_data) { foreach ($card_data as $ck => $cd) { $str .= '卡号:' . $cd['card_number'] . ' 密码:' . $cd['card_password'] . '
'; $card = \app\admin\model\JunkaCard::where(['card_number' => $cd['card_number']])->find(); if($card && $card->status == 3) { $list[$k]->is_suo = 1; }else { $list[$k]->is_suo = 0; } } } $list[$k]->data = $str; $list[$k]->product_code = $list[$k]->product_code . ' (' .$v->code->name. ')'; $list[$k]->is_reject = 1; // 显示退货按钮 if($v->reject) { if($v->reject->status == 0){ $list[$k]->is_examine = 1; $list[$k]->is_reject = 2; }elseif ($v->reject->status == 1) { $list[$k]->is_reject = 2; } }else { $list[$k]->is_examine = 2; } } $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 编辑 * * @param $ids * @return string * @throws DbException * @throws \think\Exception */ 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) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $reject = \app\admin\model\JunkaReject::where(['purchcard_log_id' => $row->id, 'status' => 0])->find(); if(!$reject) { $is_reject = 10; }else{ if($reject->status == 0) { // 审核中 $is_reject = $reject->status; }elseif ($reject->status == 1) { // 通过 $is_reject = $reject->status; }elseif ($reject->status == 2) { // 拒绝 $is_reject = $reject->status; } $reject->createtime = date('Y-m-d H:i:s', $reject->createtime); } $this->view->assign('reject', $reject); $this->view->assign('is_reject', $is_reject); $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $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 . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } /** * 退货 */ public function subreject($ids) { $row = $this->model->get($ids); if($row) { if(!in_array($row->status, [1, 4])) { $this->error('状态错误'); } $ject = \app\admin\model\JunkaReject::where(['purchcard_log_id' => $ids])->find(); if(!$ject) { \app\admin\model\JunkaReject::create([ 'store_id' => $row->store_id, 'purchcard_log_id' => $row->id, 'admin_id' => $this->auth->id, ]); $row->status = 2; $row->save(); $this->success('申请成功,等待审核'); }else { $this->error('你不能再申请退货了'); } }else { $this->error('状态错误'); } } /** * 锁卡 */ public function sk($ids) { $log = $this->model->get($ids); if(!$log) { $this->error('记录不存在'); } $card_data = json_decode($log->data, true); foreach ($card_data as $k => $v) { $junka_card[] = [ 'card_number' => $v['card_number'], // 卡号 'card_price' => $log->card_price, // 卡密 ]; $card_number_list[] = $v['card_number']; } // 锁卡 $order = new \app\common\model\Order(); $res = $order->cardlock($junka_card, $log->bill_id); // if($res['money_save_card']) { // $amount = 0; // foreach ($card_number_list as $k => $v) { // foreach ($res['money_save_card'] as $ak => $av) { // if($v == $av) { // $amount += 1; // } // } // } // $store = \app\admin\model\JunkaStore::get($log->store_id); // // // 全部退成功了 // if($amount == $log->product_num) { // // 返还商家余额 // JunkaStore::moneychange($store, $log->t_price, 1, '锁卡-返还', $log->bill_id); // }else { // if($log->product_num >= $amount) { // // 返还商家余额 // JunkaStore::moneychange($store, ($log->t_price / $log->product_num) * $amount, 1, '锁卡-返还', $log->bill_id); // } // } // // } $this->success($res['msg']); } /** * 解锁 */ public function js($ids) { $log = $this->model->get($ids); if(!$log) { $this->error('记录不存在'); } $card_data = json_decode($log->data, true); foreach ($card_data as $k => $v) { $junka_card[] = [ 'card_number' => $v['card_number'], // 卡号 'card_price' => $log->card_price, // 卡密 ]; $card_number_list[] = $v['card_number']; } // 解锁 $order = new \app\common\model\Order(); $res = $order->jx($junka_card, $log->bill_id); // if($res['money_save_card']) { // $amount = 0; // foreach ($card_number_list as $k => $v) { // foreach ($res['money_save_card'] as $ak => $av) { // if($v == $av) { // $amount += 1; // } // } // } // $store = \app\admin\model\JunkaStore::get($log->store_id); // // 全部解锁了 // if($amount == $log->total_num) { // // 扣除商家余额 // JunkaStore::moneychange($store, $log->t_price, 2, '解锁卡-扣除', $log->bill_id); // }else { // if($log->total_num >= $amount) { // // 扣除商家余额 // JunkaStore::moneychange($store, ($log->t_price / $log->total_num) * $amount, 2, '解锁卡-扣除', $log->bill_id); // } // } // } $this->success($res['msg']); } }