$this->auth->company_id, 'p.status' => $status, ]; if(!empty($keyword)){ $where['p.title|p.info'] = ['LIKE','%'.$keyword.'%']; } $list = Db::name('package')->alias('p') ->field('p.*,type.title as servicetype_title') ->join('servicetype type','p.servicetype_id = type.id','LEFT') ->where($where)->order('p.id desc')->autopage()->select(); //追加赠送 if(!empty($list)){ $package_ids = array_column($list,'id'); $gift = Db::name('package_gift')->alias('gift') ->field('gift.*,coupons.name,coupons.info,coupons.days') ->join('coupons','gift.coupon_id = coupons.id','LEFT') ->where('gift.package_id','IN',$package_ids) ->where('coupons.status',1) ->select(); foreach($list as $key => &$val){ $val['gift'] = []; foreach($gift as $k => $v){ if($val['id'] == $v['package_id']){ $val['gift'][] = $v; } } } } $list = list_domain_image($list,['images','content_images']); $this->success(1,$list); } //新增 public function add(){ Db::startTrans(); try { //验证 if($this->auth->type != 1){ throw new Exception('只有门店老板才能设置'); } $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images','type']; $data = request_post_hub($field); $data['company_id'] = $this->auth->company_id; $data['createtime'] = time(); $data['updatetime'] = time(); $data['status'] = 1; $package_id = Db::name('package')->insertGetId($data); if (!$package_id) { throw new Exception('添加套餐失败'); } if (isset($data['type']) && $data['type'] == 2) { $gift_data = input('gift_data','','trim'); $gift_data = json_decode(htmlspecialchars_decode($gift_data),true); if(is_array($gift_data) && !empty($gift_data)){ $package_gift = []; foreach($gift_data as $key => $val){ $package_gift[] = [ 'package_id' => $package_id, 'coupon_id' => $val['coupon_id'], 'number' => $val['number'], ]; } if(!empty($package_gift)){ $rs_gift = Db::name('package_gift')->insertAll($package_gift); if($rs_gift === false){ Db::rollback(); $this->error('添加失败'); } } } } Db::commit(); $this->success('添加成功'); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } public function showposter(){ $id = input('id',1); $this->getMiniCode($id,$this->auth->company_id); } private function getMiniCode($id,$companyid) { $package = Db::name('package')->where('id',$id)->find(); $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST']; if (empty($package['mini_code'])) { $client = new Client(); $tk = getAccessToken(); $miniCodeConfig = config('param.mini_code'); //$miniCodeConfig['env_version'] = 'trial'; //强制体验版 $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid; $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [ 'json' => $miniCodeConfig, ]); $fileName = md5($id); $fileUrl = '/uploads/package/'.$fileName.'.png'; $code = $res2->getBody()->getContents(); file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code); $companyRes = Db::name('package')->where('id',$id)->update(['mini_code'=>$fileUrl]); if ($companyRes === false) { $this->error('生成套餐小程序码失败'); } $miniCode = $httpStr.$fileUrl; } else { $miniCode = $httpStr.$package['mini_code']; } $this->success('success',$miniCode); } //上下架 public function changestatus(){ //验证 if($this->auth->type != 1){ $this->error('只有门店老板才能设置'); } $id = input('id',0); $status = Db::name('package')->where('id',$id)->value('status'); $new_status = $status == 1 ? 0 : 1; $info = Db::name('package')->where('id',$id)->update(['status'=>$new_status,'updatetime'=>time()]); $this->success(); } //详情 public function info(){ $id = input('id',0); $info = Db::name('package')->alias('p') ->field('p.*,type.title as servicetype_title') ->join('servicetype type','p.servicetype_id = type.id','LEFT') ->where('p.id',$id)->find(); //追加赠送 if(!empty($info)){ $gift = Db::name('package_gift')->alias('gift') ->field('gift.*,coupons.name,coupons.info,coupons.days') ->join('coupons','gift.coupon_id = coupons.id','LEFT') ->where('gift.package_id',$id) ->where('coupons.status',1) ->select(); $info['gift'] = []; if (!empty($gift)) { foreach($gift as $k => $v){ $info['gift'][] = $v; } } } $info = info_domain_image($info,['images','content_images']); $this->success(1,$info); } //编辑 public function edit(){ Db::startTrans(); try { //验证 if($this->auth->type != 1){ throw new Exception('只有门店老板才能设置'); } $id = input('id',''); $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find(); if(empty($check)){ throw new Exception('不存在的套餐'); } // $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images']; $data = request_post_hub($field); $data['updatetime'] = time(); $packageRes = Db::name('package')->where('id',$id)->update($data); if (!$packageRes) { throw new Exception('套餐编辑失败'); } if ($check['type'] == 2) { $packageGiftWhere['package_id'] = $id; $packageGiftData = Db::name('package_gift')->where($packageGiftWhere)->select(); $gift_data = input('gift_data','','trim'); $gift_data = json_decode(htmlspecialchars_decode($gift_data),true); if(is_array($gift_data) && !empty($gift_data)) { $packageGiftCouponIds = array_column($packageGiftData,'coupon_id'); $postCouponIds = array_column($gift_data,'coupon_id'); $package_gift = []; $same = []; $different = []; foreach ($gift_data as $key => $val) { if (in_array($val['coupon_id'], $packageGiftCouponIds)) { $same[] = [ 'package_id' => $id, 'coupon_id' => $val['coupon_id'], 'number' => $val['number'], ]; } else { $different[] = [ 'package_id' => $id, 'coupon_id' => $val['coupon_id'], 'number' => $val['number'], ]; } } if (!empty($same)) {//相同的更新 foreach ($same as $sameKey => $sameVal) { $sameWhere['package_id'] = $sameVal['package_id']; $sameWhere['coupon_id'] = $sameVal['coupon_id']; $sameData['number'] = $sameVal['number']; Db::name('package_gift')->where($sameWhere)->update($sameData); } } if (!empty($different)) {//新增的添加 $packageGiftRes = Db::name('package_gift')->where($sameWhere)->insertAll($different); if (!$packageGiftRes) { throw new Exception('套餐卡券添加失败'); } } //多余的删除 $delIds = array_diff($packageGiftCouponIds,$postCouponIds); if (!empty($delIds)) { $packageGiftDelWhere['package_id'] = $id; $packageGiftDelWhere['coupon_id'] = ['in',$delIds]; Db::name('package_gift')->where($packageGiftDelWhere)->delete(); } } else { if (!empty($packageGiftData)) { $packageGiftDelRes = Db::name('package_gift')->where($packageGiftWhere)->delete(); if (!$packageGiftDelRes) { throw new Exception('删除卡券失败'); } } } } Db::commit(); $this->success('编辑成功'); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } //删除 public function delete(){ //验证 if($this->auth->type != 1){ $this->error('只有门店老板才能设置'); } $id = input('id',''); $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->find(); if(empty($check)){ $this->error('不存在的套餐'); } Db::name('package')->where('id',$id)->delete(); Db::name('package_gift')->where('package_id',$id)->delete(); $this->success('删除成功'); } }