* @day: 2017/11/11 */ namespace app\admin\controller\water; use app\admin\controller\AuthController; use crmeb\services\{ExpressService, JsonService, JsonService as Json, MiniProgramService, WechatService, FormBuilder as Form, CacheService, UtilService as Util}; use think\facade\Route as Url; use think\facade\Validate; Use app\admin\model\water\WaterMembershioCard as model; /** * 订单管理控制器 同一个订单表放在一个控制器 * Class StoreOrder * @package app\admin\controller\store */ class WaterMembershioCard extends AuthController { /** * @return mixed */ public function index() { return $this->fetch(); } public function list() { $where = Util::getMore([ ['page', 1], ['limit', 20], ['name', ''], ['role', ''] ]); return Json::successlayui(model::list($where)); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create($id = 0) { $f = []; $f[] = Form::input('title', '名称')->col(12); $f[] = Form::input('price', '价格'); $f[] = Form::input('ot_price', '原价'); $f[] = Form::input('time', '天数'); $form = Form::make_post_form('添加', $f, Url::buildUrl('save')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } public function save() { $model = new model; $data = Util::postMore([ 'title', 'price', 'ot_price', 'time', ]); $validate = Validate::rule([ 'title' => 'require', 'price' => 'require', 'ot_price' => 'require', 'time' => 'require', ]); $validate->message([ 'title.require' => '名称不能为空', 'price.require' => '请填写价格', 'ot_price.require' => '请填写原价', 'time.require' => '请填写时间', ]); if (!$validate->check($data)) { return Json::fail($validate->getError()); } $res = $model->save($data); if ($res) return Json::successful('添加成功'); return Json::fail('添加失败'); } /** * 显示创建资源表单页. * * @return \think\Response */ public function edit($id = 0) { $data = model::find($id); $f = []; $f[] = Form::input('title', '名称', $data['title'])->col(12); $f[] = Form::input('price', '价格', $data['price']); $f[] = Form::input('ot_price', '原价', $data['ot_price']); $f[] = Form::input('time', '天数', $data['time']); $f[] = Form::hidden('id', $id); $form = Form::make_post_form('修改', $f, Url::buildUrl('update')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 修改 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function update() { $model = new model; $data = Util::postMore([ 'title', 'price', 'ot_price', 'time', 'id' ]); $validate = Validate::rule([ 'title' => 'require', 'price' => 'require', 'ot_price' => 'require', 'time' => 'require', ]); $validate->message([ 'title.require' => '名称不能为空', 'price.require' => '请填写价格', 'ot_price.require' => '请填写原价', 'time.require' => '请填写时间', ]); $details = $model->find($data['id']); $details['name'] = $data['name']; $details['thickness'] = $data['thickness']; $res = $details->save(); if ($res) return Json::successful('修改成功'); return Json::fail('修改失败'); } /** * 删除 * @param $id * @return void * @throws \Exception */ public function delete($id) { if (!$id) Json::fail('删除失败'); $model = new model; $res = model::destroy($id); if ($res){ return Json::success('删除成功!'); }else{ return Json::fail($model->getErrorInfo()); } } }