123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- namespace app\admin\controller;
- use app\admin\service\LogService;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use app\main\constants\PayConstants;
- use app\main\service\GoodsService;
- use think\Controller;
- use think\Db;
- use think\Request;
- use think\Exception;
- /**
- * 商品管理
- *
- * @icon fa fa-circle-o
- */
- class Goods extends Backend
- {
- /**
- * Goods模型对象
- */
- protected $model = null;
- protected $relationSearch = true;
- protected $categorylist = [];
- protected $categorydata = [];
- protected $category_pid = 14;//支付分类父级ID
- protected $searchFields = 'category_id';
- public function _initialize()
- {
- parent::_initialize();
- $this->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'));
- }
- }
|