GoodsController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020-08-28
  6. * Time: 14:58
  7. */
  8. namespace app\back\controller;
  9. use app\back\model\GoodsCategoryModel;
  10. use app\back\model\GoodsModel;
  11. use cmf\controller\AdminBaseController;
  12. class GoodsController extends AdminBaseController
  13. {
  14. public function index()
  15. {
  16. $where = [];
  17. $pw = [];
  18. $page = $this->request->param('page',1);
  19. $list = GoodsModel::with('category')->where($where)->order('list_order','asc')->paginate(30,false,array($page,url('index'),'query'=>$pw));
  20. $this->assign('list',$list);
  21. return $this->fetch();
  22. }
  23. public function add()
  24. {
  25. $goodsCategoryList = GoodsCategoryModel::where('is_show',1)->order('list_order','asc')->select();
  26. $this->assign('goodsCategoryList',$goodsCategoryList);
  27. return $this->fetch();
  28. }
  29. public function addPost()
  30. {
  31. $data = $this->request->param();
  32. if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
  33. $data['banner'] = [];
  34. foreach ($data['photo_urls'] as $key => $url) {
  35. $photoUrl = cmf_asset_relative_url($url);
  36. array_push( $data['banner'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
  37. }
  38. }
  39. unset($data['photo_urls']);
  40. unset($data['photo_names']);
  41. $goodsModel = new GoodsModel();
  42. $goodsModel->allowField(true)->isUpdate(false)->save($data);
  43. $this->success('添加成功');
  44. }
  45. public function edit()
  46. {
  47. $id = $this->request->param('id');
  48. $info = GoodsModel::get($id,'category');
  49. $this->assign('info',$info);
  50. $goodsCategoryList = GoodsCategoryModel::where('is_show',1)->where('id','<>',$info->category_id)->order('list_order','asc')->select();
  51. $this->assign('goodsCategoryList',$goodsCategoryList);
  52. return $this->fetch();
  53. }
  54. public function editPost()
  55. {
  56. $data = $this->request->param();
  57. if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
  58. $data['banner'] = [];
  59. foreach ($data['photo_urls'] as $key => $url) {
  60. $photoUrl = cmf_asset_relative_url($url);
  61. array_push( $data['banner'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
  62. }
  63. }
  64. unset($data['photo_urls']);
  65. unset($data['photo_names']);
  66. $goodsModel = new GoodsModel();
  67. $goodsModel->allowField(true)->isUpdate(true)->save($data);
  68. $this->success('编辑成功');
  69. }
  70. public function delete()
  71. {
  72. $id = $this->request->param('id');
  73. GoodsModel::destroy($id);
  74. $this->success('删除成功');
  75. }
  76. public function shelves()
  77. {
  78. $ids = $this->request->param('ids/a');
  79. $shelves = $this->request->param('shelves');
  80. GoodsModel::where('id','in',$ids)->update(['shelves'=>$shelves]);
  81. $this->success('操作成功');
  82. }
  83. public function is_sell()
  84. {
  85. $ids = $this->request->param('ids/a');
  86. $is_sell = $this->request->param('is_sell');
  87. GoodsModel::where('id','in',$ids)->update(['is_sell'=>$is_sell]);
  88. $this->success('操作成功');
  89. }
  90. public function listOrder()
  91. {
  92. $GoodsModel = new GoodsModel();
  93. parent::listOrders($GoodsModel);
  94. $this->success("排序更新成功!");
  95. }
  96. public function select()
  97. {
  98. $id = $this->request->param('id');
  99. $list = GoodsModel::where('shelves',1)->order('list_order','asc')->field('id,goods_name,price,seckill_price,repertory')->select();
  100. $this->assign('list',$list);
  101. $this->assign('id',$id);
  102. return $this->fetch();
  103. }
  104. }