| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace app\backend\modules\goods\controllers;
- use app\backend\modules\goods\models\Brand;
- use app\backend\modules\goods\services\BrandService;
- use app\backend\modules\uploadVerificate\UploadVerificationBaseController;
- use app\common\components\BaseController;
- use app\common\helpers\PaginationHelper;
- use app\common\helpers\Url;
- use Illuminate\Support\Facades\DB;
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/2/27
- * Time: 上午9:17
- */
- class BrandController extends UploadVerificationBaseController
- {
- /**
- * 商品品牌列表
- */
- public function index()
- {
- return view('goods.brand.list')->render();
- }
- public function getBrandData(){
- $where = [];
- if (request()->search_brand_keyword){
- $where[] = ['name','like','%'.trim(request()->search_brand_keyword).'%'];
- }
- $data = \app\common\models\Brand::getBrands()->where($where)->select('id', 'name')->get();
- if ($data->isNotEmpty()){
- $data->each(function (&$v){
- if (mb_strlen($v->name,'utf-8') > 40){
- $v->name = mb_substr($v->name,0,40,'utf-8').'...';
- }
- });
- }
- return $this->successJson('获取成功',$data);
- }
- public function brandData()
- {
- $search = request()->search;
- $pageSize = 10;
- $list = Brand::getBrands($search)->orderBy('id','desc')->paginate($pageSize);
- foreach ($list as &$item){
- $item['logo_url'] = yz_tomedia($item['logo']);
- }
- return $this->successJson('ok',$list);
- }
- /**
- * 添加品牌
- */
- public function add()
- {
- $brandModel = new Brand();
- $requestBrand = request()->brand;
- if($requestBrand) {
- //将数据赋值到model
- $brandModel->setRawAttributes($requestBrand);
- //其他字段赋值
- $brandModel->uniacid = \YunShop::app()->uniacid;
- //字段检测
- $validator = $brandModel->validator($brandModel->getAttributes());
- if ($validator->fails()) {//检测失败
- $this->errorJson($validator->messages());
- } else {
- //数据保存
- if ($brandModel->save()) {
- //显示信息并跳转
- return $this->successJson('品牌创建成功');
- }else{
- $this->errorJson('品牌创建失败');
- }
- }
- }
- $this->title = '创建品牌';
- $this->breadcrumbs = [
- '品牌管理'=>['url'=>$this->createWebUrl('goods.brand.index'),'icon'=>'icon-dian'],
- $this->title,
- ];
- return $this->successJson('ok',$brandModel);
- }
- public function editViwe()
- {
- return view('goods.brand.info', [
- 'id' => request()->id
- ])->render();
- }
- /**
- * 编辑商品品牌
- */
- public function edit()
- {
- $brandModel = Brand::getBrand(request()->id);
- if(!$brandModel){
- return $this->errorJson('无此记录或已被删除');
- }
- $requestBrand = request()->brand;
- if($requestBrand) {
- //将数据赋值到model
- $brandModel->setRawAttributes($requestBrand);
- //字段检测
- $validator = $brandModel->validator($brandModel->getAttributes());
- if ($validator->fails()) {//检测失败
- $this->errorJson($validator->messages());
- } else {
- //数据保存
- if ($brandModel->save()) {
- //显示信息并跳转
- return $this->successJson('品牌保存成功');
- }else{
- $this->errorJson('品牌保存失败');
- }
- }
- }
- $brandModel->logo_url = yz_tomedia($brandModel->logo);
- $brandModel->desc = html_entity_decode($brandModel->desc);
- return $this->successJson('ok',$brandModel);
- }
- /**
- * 删除商品品牌
- */
- public function deletedBrand()
- {
- if (request()->ids) {
- $brand = Brand::whereIn('id', request()->ids);
- $result = $brand->delete();
- } else {
- $brand = Brand::getBrand(request()->id);
- if(!$brand) {
- return $this->errorJson('无此品牌或已经删除');
- }
- $result = Brand::deletedBrand(request()->id);
- }
- if($result) {
- return $this->successJson('删除品牌成功');
- }else{
- return $this->errorJson('删除品牌失败');
- }
- }
- /**
- * 商品品牌
- */
- public function searchBrand()
- {
- $keyword = request()->keyword;
- if (!$keyword)
- {
- return $this->errorJson('请输入关键字!!');
- }
- $brand = Brand::keywordGetBrand($keyword)->limit(20)->get()->toArray();
- return $this->successJson('ok',$brand);
- }
- /**
- * 批量更新品牌推荐状态
- */
- public function batchRecommend()
- {
- if (is_array(request()->ids) && isset(request()->is_recommend)){
- Brand::uniacid()->whereIn('id', request()->ids)->update(['is_recommend'=> request()->is_recommend]);
- return $this->successJson("批量更新成功");
- }
- return $this->errorJson('修改失败, 请检查参数');
- }
- }
|