| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 | <?phpnamespace app\api\controller\company;use app\common\controller\Apic;use think\Db;use think\Exception;use GuzzleHttp\Client;/** * 套餐管理 */class Package extends Apic{    protected $noNeedLogin = [];    protected $noNeedRight = '*';    //列表    public function lists(){        $status = input('status',1);        $keyword = input('keyword','');        $where = [            'p.company_id' => $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 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('删除成功');    }    //仅返回套餐二维码    private function getMiniCode($id,$package,$companyid)    {        $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'];        }        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);        $data = [            [                "left"=> "10px",                "top"=> "250px",                "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"=> "205px",                "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"=> "270px",                "type"=> "nickname",                "width"=> "166px",                "height"=> "38px",                "size"=> "10px",                "color"=> "#666666",                "content" => (iconv_strlen($this->auth->company->name, 'utf-8') <= 8 ? $this->auth->nickname : mb_substr($this->auth->nickname, 0, 8))            ],            [                "left"=> "200px",                "top"=> "190px",                "type"=> "img",                "width"=> "110px",                "height"=> "110px",                "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"            ],            [                "left"=> "205px",                "top"=> "300px",                "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 '<html><body><img src="'.$imgurl.'"></body></html>';exit;        $this->success('', $imgurl);    }}
 |