$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('添加套餐失败'); } $fileName = md5($package_id); $fileUrl = '/uploads/package/'.$fileName.'.png'; Db::name('package')->where('id',$package_id)->update(['mini_code'=>$fileUrl]); 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->getMiniCode($package_id,[],$this->auth->company_id); $this->success('添加成功'); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } //上下架 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(); $fileName = md5($id); $fileUrl = '/uploads/package/'.$fileName.'.png'; $data['mini_code'] = $fileUrl; $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->getMiniCode($id,$check,$this->auth->company_id); $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('删除成功'); } //仅返回套餐二维码 private function getMiniCode($id,$package = [],$companyid) { $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST']; if (empty($package) || empty($package['mini_code'])) { $client = new Client(); $tk = getAccessToken(); $miniCodeConfig = config('param.mini_code'); $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); $miniCode = $httpStr.$fileUrl; } else { $miniCode = $httpStr.$package['mini_code']; } return $miniCode; } //生成视频分享海报 public function showposter() { $id = input('id', 0, 'intval'); //id if (!$id) { $this->error('参数缺失'); } $info = Db::name('package')->where('id',$id)->find(); if (!$info) { $this->error('套餐不存在'); } $inviteimage = $this->getMiniCode($id,$info,$this->auth->company_id); $package_image = explode(',',$info['images'])[0]; $data = [ [ "left"=> "0px", "top"=> "0px", "type"=> "img", "width"=> "320px", "height"=> "290px", "src"=> one_domain_image($package_image)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg" ], [ "left"=> "10px", "top"=> "370px", "type"=> "img", "width"=> "45px", "height"=> "45px", "src"=> one_domain_image($this->auth->company->image)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg" ], [ "left"=> "10px", "top"=> "330px", "type"=> "nickname", "width"=> "166px", "height"=> "38px", "size"=> "11px", "color"=> "#333333", "content" => (iconv_strlen($info['title'], 'utf-8') <= 12 ? $info['title'] : mb_substr($info['title'], 0, 12) ) ], [ "left"=> "60px", "top"=> "385px", "type"=> "nickname", "width"=> "166px", "height"=> "38px", "size"=> "10px", "color"=> "#666666", "content" => (iconv_strlen($this->auth->company->name, 'utf-8') <= 8 ? $this->auth->company->name : mb_substr($this->auth->company->name, 0, 8)) ], [ "left"=> "195px", "top"=> "300px", "type"=> "img", "width"=> "110px", "height"=> "110px", "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg" ], [ "left"=> "200px", "top"=> "420px", "type"=> "nickname", "width"=> "166px", "height"=> "38px", "size"=> "9px", "color"=> "#666666", "content" => '长按扫码查看详情', ], ]; $data = json_encode($data, 320); $poster = [ 'id' => 1, 'title' => '测试', 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...', 'bg_image' => '/assets/img/posterbg.png', 'data' => $data, 'status' => 'normal', 'weigh' => 0, 'createtime' => 1653993709, 'updatetime' => 1653994259, ]; $image = new \addons\poster\library\Image(); $imgurl = $image->createPosterImage($poster, $this->auth->getUser()); if (!$imgurl) { $this->error('生成海报出错'); } $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl; //echo '';exit; $this->success('', $imgurl); } }