model = model('Goods'); $this->view->assign("typeList", $this->model->getTypeList()); $this->view->assign("showTypeList", $this->model->getShowTypeList()); $this->categorylist = model('category')->order('weigh desc,id desc')->where('pid', 'in', $this->category_pid)->select(); $this->categorylist = $this->model->getCategoryList(); foreach ($this->categorylist as $k => $v) { $this->categorydata[$v['id']] = ['name'=>$v['name'],'nickname'=>$v['nickname']]; } $this->view->assign("parentList", $this->categorydata); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ public function index() { if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->with('category') ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]); return json($result); } $this->assignconfig('category_list',model('Goods')->getCategoryList()); $this->assignconfig('business',PayConstants::getBusinessLine()); return $this->view->fetch(); } public function select() { $params = $this->request->get(); $goodsCategory = $params['goods_category'] ?? PayConstants::GOODS_CATEGORY_RECHARGE; $businessLine = $params['business_line'] ?? '-1'; if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL); $adminId = $this->auth->id; $selectedIds = explode('_', $params['selected_ids']); if ($goodsCategory == PayConstants::GOODS_CATEGORY_RECHARGE) { $goodsTypes = [PayConstants::GOODS_TYPE_KD]; if ($businessLine == PayConstants::BUSINESS_APP) { $goodsTypes[] = PayConstants::GOODS_TYPE_VIP; } if ($businessLine == PayConstants::BUSINESS_APP) { $goodsCategory = [$goodsCategory, 'test']; } } elseif ($goodsCategory == PayConstants::GOODS_CATEGORY_ACTIVITY) { $goodsTypes = [PayConstants::GOODS_TYPE_KD, PayConstants::GOODS_TYPE_VIP]; } else { throw new Exception('参数错误'); } $maps = []; if ($businessLine != '-1') { $maps[] = ['exp', "FIND_IN_SET('$businessLine', business_line)"]; } //增加从充值渠道弹出商品列表逻辑,如果没有筛选条件&&是从商品列表tab弹出的框 //如果recharge_show_type=-1 为 筛选已充值列表(包括已充值,全部,新用户,老用户); //recharge_show_type=-2 为 筛选未充值列表(包括未充值,全部,新用户,老用户) $rechargeShowType = $this->request->get("recharge_show_type", null); $filter = $this->request->get("filter", ''); $filter = json_decode($filter, TRUE); $showType = $filter['show_type']??null; if(!$showType && $rechargeShowType){ if($rechargeShowType == -1) { $maps['show_type'] =['neq',2]; } elseif($rechargeShowType == -2) { $maps['show_type'] =['neq',1]; } } List($list, $total) = GoodsService::instance()->getSelectGoodsList($adminId, $selectedIds, $where, $offset, $limit, $goodsCategory, $goodsTypes, $maps); $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]); return json($result); } $this->assignconfig('category_list',model('Goods')->getCategoryList()); $this->assignconfig('business',PayConstants::getBusinessLine()); $this->assignconfig('goods_category', $goodsCategory); $this->assignconfig('business_line', $businessLine); return $this->view->fetch(); } public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if(empty($params['business_line']) || empty($params['category_id'])){ $this->error("业务线或者分类不能为空"); } if ($params['type'] == PayConstants::GOODS_TYPE_VIP) { $params['kandian'] = 0; $params['free_kandian'] = 0; } elseif ($params['type'] == PayConstants::GOODS_TYPE_KD) { $params['day'] = 0; } $params['business_line'] = implode(',',$params['business_line']); $params['category_id'] = implode(',',$params['category_id']); $result = $this->model->validate('goods.add')->save($params); if ($result === false) { $this->error($this->model->getError()); } $this->success(); } $this->error(); } $this->assign('business',PayConstants::getBusinessLine()); return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = NULL) { $row = $this->model->get(['id' => $ids]); if (!$row) $this->error(__('No Results were found')); if ($this->request->isPost()) { $params = $this->request->post("row/a"); if(empty($params['business_line']) || empty($params['category_id'])){ $this->error("业务线或者分类不能为空"); } //如果原来的商品为VIP,现在改成不是VIP if($params['type'] != PayConstants::GOODS_TYPE_VIP && $row['type'] == PayConstants::GOODS_TYPE_VIP){ $this->clearChannelVipConfig($row['id']); } //修改业务线的时候,不是微信商品 if( !in_array(strval(PayConstants::BUSINESS_WECHAT),$params['business_line']) && in_array(strval(PayConstants::BUSINESS_WECHAT),explode(',',$row['business_line'])) ){ $this->clearChannelVipConfig($row['id']); } if($ids = model('Goods')->getGoodsCategoryIds([PayConstants::GOODS_CATEGORY_RECHARGE])){ //修改分类,不是充值页 if( !in_array(strval(current($ids)),$params['category_id']) && in_array(strval(current($ids)),explode(',',$row['category_id'])) ){ $this->clearChannelVipConfig($row['id']); } } $params['business_line'] = implode(',',$params['business_line']); $params['category_id'] = implode(',',$params['category_id']); if ($params['type'] == PayConstants::GOODS_TYPE_VIP) { $params['kandian'] = 0; $params['free_kandian'] = 0; } elseif ($params['type'] == PayConstants::GOODS_TYPE_KD) { $params['day'] = 0; } try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; $row->validate($validate); } GoodsService::instance()->removeCacheCustomGoods($row['id']); $result = $row->allowField(true)->save($params); if ($result !== false) { //清理下缓存吧 Redis::instance()->del(CacheConstants::getGoodsInfoKey($row['id'])); $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $act_cate_id = model('Category')->where('type','default')->where('nickname','activity')->value('id'); $row['business_line'] = explode(',',$row['business_line']); $row['category_id'] = explode(',',$row['category_id']); $is_show_free_day = (in_array($act_cate_id,$row['category_id']) && $row['type'] != PayConstants::GOODS_TYPE_VIP); $this->assign('is_show_free_day',$is_show_free_day); $this->assign('business',PayConstants::getBusinessLine()); $this->view->assign("row", $row); return $this->view->fetch(); } public function categorylist() { foreach ($this->categorydata as $key => $value) { $searchlist[] = ['id' => $key, 'name' => $value['name']]; } $data = ['searchlist' => $searchlist]; $this->success('', null, $data); return json($data); } private function clearChannelVipConfig($goods_id){ //清空渠道的商品ID&关闭VIP状态 $admin_ids = model('AdminConfig')->where('vip_goods_id',$goods_id)->column('admin_id'); if($admin_ids){ foreach($admin_ids as $admin_id){ model('AdminConfig')->where('admin_id',$admin_id)->update(['vip_goods_id'=>0,'vip_state'=>0]); model('AdminConfig')->delAdminInfoAllCache($admin_id); } } } /** * 删除 */ public function del($ids = "") { if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; foreach ($list as $k => $v) { //清空渠道的商品ID&关闭VIP状态 $this->clearChannelVipConfig($v['id']); GoodsService::instance()->delCustomGoodsId($v['id']); $count += $v->delete(); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } }