BrandController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\backend\modules\goods\controllers;
  3. use app\backend\modules\goods\models\Brand;
  4. use app\backend\modules\goods\services\BrandService;
  5. use app\backend\modules\uploadVerificate\UploadVerificationBaseController;
  6. use app\common\components\BaseController;
  7. use app\common\helpers\PaginationHelper;
  8. use app\common\helpers\Url;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * Created by PhpStorm.
  12. * Author: 芸众商城 www.yunzshop.com
  13. * Date: 2017/2/27
  14. * Time: 上午9:17
  15. */
  16. class BrandController extends UploadVerificationBaseController
  17. {
  18. /**
  19. * 商品品牌列表
  20. */
  21. public function index()
  22. {
  23. return view('goods.brand.list')->render();
  24. }
  25. public function getBrandData(){
  26. $where = [];
  27. if (request()->search_brand_keyword){
  28. $where[] = ['name','like','%'.trim(request()->search_brand_keyword).'%'];
  29. }
  30. $data = \app\common\models\Brand::getBrands()->where($where)->select('id', 'name')->get();
  31. if ($data->isNotEmpty()){
  32. $data->each(function (&$v){
  33. if (mb_strlen($v->name,'utf-8') > 40){
  34. $v->name = mb_substr($v->name,0,40,'utf-8').'...';
  35. }
  36. });
  37. }
  38. return $this->successJson('获取成功',$data);
  39. }
  40. public function brandData()
  41. {
  42. $search = request()->search;
  43. $pageSize = 10;
  44. $list = Brand::getBrands($search)->orderBy('id','desc')->paginate($pageSize);
  45. foreach ($list as &$item){
  46. $item['logo_url'] = yz_tomedia($item['logo']);
  47. }
  48. return $this->successJson('ok',$list);
  49. }
  50. /**
  51. * 添加品牌
  52. */
  53. public function add()
  54. {
  55. $brandModel = new Brand();
  56. $requestBrand = request()->brand;
  57. if($requestBrand) {
  58. //将数据赋值到model
  59. $brandModel->setRawAttributes($requestBrand);
  60. //其他字段赋值
  61. $brandModel->uniacid = \YunShop::app()->uniacid;
  62. //字段检测
  63. $validator = $brandModel->validator($brandModel->getAttributes());
  64. if ($validator->fails()) {//检测失败
  65. $this->errorJson($validator->messages());
  66. } else {
  67. //数据保存
  68. if ($brandModel->save()) {
  69. //显示信息并跳转
  70. return $this->successJson('品牌创建成功');
  71. }else{
  72. $this->errorJson('品牌创建失败');
  73. }
  74. }
  75. }
  76. $this->title = '创建品牌';
  77. $this->breadcrumbs = [
  78. '品牌管理'=>['url'=>$this->createWebUrl('goods.brand.index'),'icon'=>'icon-dian'],
  79. $this->title,
  80. ];
  81. return $this->successJson('ok',$brandModel);
  82. }
  83. public function editViwe()
  84. {
  85. return view('goods.brand.info', [
  86. 'id' => request()->id
  87. ])->render();
  88. }
  89. /**
  90. * 编辑商品品牌
  91. */
  92. public function edit()
  93. {
  94. $brandModel = Brand::getBrand(request()->id);
  95. if(!$brandModel){
  96. return $this->errorJson('无此记录或已被删除');
  97. }
  98. $requestBrand = request()->brand;
  99. if($requestBrand) {
  100. //将数据赋值到model
  101. $brandModel->setRawAttributes($requestBrand);
  102. //字段检测
  103. $validator = $brandModel->validator($brandModel->getAttributes());
  104. if ($validator->fails()) {//检测失败
  105. $this->errorJson($validator->messages());
  106. } else {
  107. //数据保存
  108. if ($brandModel->save()) {
  109. //显示信息并跳转
  110. return $this->successJson('品牌保存成功');
  111. }else{
  112. $this->errorJson('品牌保存失败');
  113. }
  114. }
  115. }
  116. $brandModel->logo_url = yz_tomedia($brandModel->logo);
  117. $brandModel->desc = html_entity_decode($brandModel->desc);
  118. return $this->successJson('ok',$brandModel);
  119. }
  120. /**
  121. * 删除商品品牌
  122. */
  123. public function deletedBrand()
  124. {
  125. if (request()->ids) {
  126. $brand = Brand::whereIn('id', request()->ids);
  127. $result = $brand->delete();
  128. } else {
  129. $brand = Brand::getBrand(request()->id);
  130. if(!$brand) {
  131. return $this->errorJson('无此品牌或已经删除');
  132. }
  133. $result = Brand::deletedBrand(request()->id);
  134. }
  135. if($result) {
  136. return $this->successJson('删除品牌成功');
  137. }else{
  138. return $this->errorJson('删除品牌失败');
  139. }
  140. }
  141. /**
  142. * 商品品牌
  143. */
  144. public function searchBrand()
  145. {
  146. $keyword = request()->keyword;
  147. if (!$keyword)
  148. {
  149. return $this->errorJson('请输入关键字!!');
  150. }
  151. $brand = Brand::keywordGetBrand($keyword)->limit(20)->get()->toArray();
  152. return $this->successJson('ok',$brand);
  153. }
  154. /**
  155. * 批量更新品牌推荐状态
  156. */
  157. public function batchRecommend()
  158. {
  159. if (is_array(request()->ids) && isset(request()->is_recommend)){
  160. Brand::uniacid()->whereIn('id', request()->ids)->update(['is_recommend'=> request()->is_recommend]);
  161. return $this->successJson("批量更新成功");
  162. }
  163. return $this->errorJson('修改失败, 请检查参数');
  164. }
  165. }