LivegoodsController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\LiveGoods;
  5. use app\model\Goods;
  6. use app\model\GoodsSku;
  7. class LivegoodsController extends Base
  8. {
  9. function index()
  10. {
  11. $weid = weid();
  12. $keyword = input('post.keyword', '', 'serach_in');
  13. $query = LiveGoods::where(['weid' => $weid]);
  14. if (!empty($keyword)) {
  15. $query->where('title', 'like', '%' . $keyword . '%');
  16. }
  17. if (!empty($this->sid)) {
  18. $query->where('sid', $this->sid);
  19. }
  20. $res = $query->order('sort asc,id desc')
  21. ->paginate(getpage())
  22. ->toArray();
  23. foreach ($res['data'] as &$vo) {
  24. $vo['begin_date'] = time_format($vo['begin_date']);
  25. $vo['end_date'] = time_format($vo['end_date']);
  26. if(!empty($vo['goods_id'])){
  27. $goods = Goods::find($vo['goods_id']);
  28. if(!empty($goods)){
  29. $vo['goods'] = $goods->toArray();
  30. $vo['image'] = toimg($vo['goods']['image']);
  31. }
  32. }
  33. }
  34. $data['data'] = $res;
  35. return $this->json($data);
  36. }
  37. function listUpdate()
  38. {
  39. $data = only('id,status,sort');
  40. if (!$data['id']) throw new ValidateException('参数错误');
  41. LiveGoods::update($data);
  42. return $this->json(['msg' => '操作成功']);
  43. }
  44. public function update()
  45. {
  46. $id = $this->request->post('id');
  47. $data = input('post.');
  48. unset($data['create_time']);
  49. $data['begin_date'] = strtotime($data['begin_date']);
  50. $data['end_date'] = strtotime($data['end_date']);
  51. if (empty($id)) {
  52. $data['weid'] = weid();
  53. if (!empty($this->sid)) {
  54. $data['sid'] = $this->sid;
  55. }
  56. try {
  57. $res = LiveGoods::create($data);
  58. if ($res->id && empty($data['sort'])) {
  59. LiveGoods::update(['sort' => $res->id, 'id' => $res->id]);
  60. }
  61. $data['id'] = $res->id;
  62. } catch (\Exception $e) {
  63. throw new ValidateException($e->getMessage());
  64. }
  65. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  66. } else {
  67. try {
  68. LiveGoods::update($data);
  69. } catch (\Exception $e) {
  70. throw new ValidateException($e->getMessage());
  71. }
  72. return $this->json(['msg' => '修改成功']);
  73. }
  74. }
  75. function getInfo()
  76. {
  77. $id = $this->request->post('id', '', 'serach_in');
  78. if (!$id) throw new ValidateException('参数错误');
  79. $data = LiveGoods::field('*')->find($id)->toArray();
  80. if(!empty($data['goods_id'])){
  81. $goods = Goods::find($data['goods_id']);
  82. if(!empty($goods)){
  83. $data['goods'] = $goods->toArray();
  84. }
  85. }
  86. $data['attribute'] = GoodsSku::get_goods_sku($data['goods_id']);
  87. $data['sourceAttribute'] = $data['attribute'];
  88. $data['begin_date'] = time_format($data['begin_date']);
  89. $data['end_date'] = time_format($data['end_date']);
  90. return $this->json(['data' => $data]);
  91. }
  92. function delete()
  93. {
  94. return $this->del(new LiveGoods());
  95. }
  96. }