Item.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商品管理
  7. */
  8. namespace app\popupshop\controller;
  9. use app\common\controller\Manage;
  10. use app\popupshop\model\Category;
  11. use app\popupshop\model\Item as AppItem;
  12. use think\facade\Request;
  13. class Item extends Manage{
  14. public function initialize() {
  15. parent::initialize();
  16. $this->assign('pathMaps',[['name'=>'商品管理','url'=>url("popupshop/item/index")]]);
  17. }
  18. /**
  19. * 列表
  20. */
  21. public function index(){
  22. $view['keyword'] = Request::param('keyword');
  23. $view['status'] = Request::param('status',0);
  24. $view['page'] = Request::param('page/d',0);
  25. $view['lists'] = AppItem::list($this->member_miniapp_id,$view['status'],$view['keyword']);
  26. return view()->assign($view);
  27. }
  28. /**
  29. * 添加
  30. */
  31. public function add(){
  32. if(request()->isAjax()){
  33. $data = [
  34. 'category_id' => Request::param('category_id/d'),
  35. 'category_path_id' => Request::param('category_path_id/s'),
  36. 'name' => Request::param('name/s'),
  37. 'note' => Request::param('note/s'),
  38. 'sell_price' => Request::param('sell_price/f'),
  39. 'market_price' => Request::param('market_price/f'),
  40. 'cost_price' => Request::param('cost_price/f'),
  41. 'types' => Request::param('types/d'),
  42. 'points' => Request::param('points/d'),
  43. 'repoints' => Request::param('repoints/d'),
  44. 'store_nums' => Request::param('store_nums/d'),
  45. 'weight' => Request::param('weight/f'),
  46. 'unit' => Request::param('unit/s'),
  47. 'imgs' => Request::param('imgs/a'),
  48. 'img' => Request::param('img/s'),
  49. 'content' => Request::param('content/s'),
  50. ];
  51. $validate = $this->validate($data,'item.save');
  52. if(true !== $validate){
  53. return json(['code'=>0,'msg'=>$validate]);
  54. }
  55. $result = AppItem::edit($this->member_miniapp_id,$data);
  56. if($result){
  57. return json(['code'=>200,'url' => url('popupshop/item/index'),'msg'=>'操作成功']);
  58. }else{
  59. return json(['code'=>0,'msg'=>'操作失败']);
  60. }
  61. }else{
  62. return view();
  63. }
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit(){
  69. if(request()->isAjax()){
  70. $data = [
  71. 'id' => Request::param('id/d'),
  72. 'category_id' => Request::param('category_id/d'),
  73. 'category_path_id' => Request::param('category_path_id/s'),
  74. 'types' => Request::param('types/d'),
  75. 'points' => Request::param('points/d'),
  76. 'repoints' => Request::param('repoints/d'),
  77. 'name' => Request::param('name/s'),
  78. 'note' => Request::param('note/s'),
  79. 'sell_price' => Request::param('sell_price/f'),
  80. 'market_price' => Request::param('market_price/f'),
  81. 'cost_price' => Request::param('cost_price/f'),
  82. 'store_nums' => Request::param('store_nums/d'),
  83. 'weight' => Request::param('weight/f'),
  84. 'unit' => Request::param('unit/s'),
  85. 'imgs' => Request::param('imgs/a'),
  86. 'img' => Request::param('img/s'),
  87. 'content' => Request::param('content/s'),
  88. ];
  89. $validate = $this->validate($data,'item.save');
  90. if(true !== $validate){
  91. return json(['code'=>0,'msg'=>$validate]);
  92. }
  93. $result = AppItem::edit($this->member_miniapp_id,$data);
  94. if($result){
  95. return json(['code'=>200,'url' => url('popupshop/item/index'),'msg'=>'操作成功']);
  96. }else{
  97. return json(['code'=>0,'msg'=>'操作失败']);
  98. }
  99. }else{
  100. $view['id'] = Request::param('id/d');
  101. $view['info'] = AppItem::where(['id' => $view['id']])->find();
  102. $view['imgs'] = json_decode($view['info']->imgs,true);
  103. $view['status'] = Request::param('status/d',0);
  104. $view['page'] = Request::param('page/d',0);
  105. //当前商品目录
  106. $category = Category::getPath($this->member_miniapp_id,$view['info']->category_id);
  107. $category_path = null;
  108. foreach ($category as $value) {
  109. $category_path .= $value['title'].' / ';
  110. }
  111. $view['category_path'] = $category_path;
  112. return view()->assign($view);
  113. }
  114. }
  115. /**
  116. * 删除
  117. */
  118. public function delete(){
  119. $id = Request::param('id/d',0);
  120. $ids = Request::param('ids/s');
  121. $result = AppItem::spu_delete($id,$ids);
  122. if($result){
  123. return json(['code'=>200,'msg'=>'操作成功','data'=>[]]);
  124. }else{
  125. return json(['code'=>403,'msg'=>'操作失败']);
  126. }
  127. }
  128. /**
  129. * 上架,下架,从回收站恢复
  130. */
  131. public function ids_action(){
  132. if(request()->isAjax()){
  133. $issale = Request::param('issale/d',0);
  134. $ids = Request::param('ids/s');
  135. if(empty($ids)){
  136. return json(['code'=>403,'msg'=>'没有选择任何要操作商品']);
  137. }else{
  138. AppItem::ids_action($issale,$ids);
  139. return json(['code'=>200,'msg'=>'操作成功','data'=>[]]);
  140. }
  141. }
  142. }
  143. /**
  144. * 商品栏目
  145. */
  146. public function category(){
  147. if(request()->isAjax()){
  148. $parent_id = Request::param('parent_id/d',0);
  149. $info = Category::where(['member_miniapp_id' => $this->member_miniapp_id,'parent_id' => $parent_id])->field('id,parent_id,title')->order(['sort'=>'desc','id'=>'desc'])->select();
  150. return json(['code'=>200,'msg'=>'操作成功','data'=>$info]);
  151. }else{
  152. $view['input'] = Request::param('input');
  153. $view['path'] = Request::param('path');
  154. return view('category',$view);
  155. }
  156. }
  157. /**
  158. * 及时返回当前路径
  159. */
  160. public function category_path(){
  161. $parent_id = Request::param('parent_id/d',0);
  162. $info = Category::getPath($this->member_miniapp_id,$parent_id);
  163. if($info){
  164. $category = [];
  165. foreach ($info as $value) {
  166. $category[] = $value['id'];
  167. }
  168. $category_id = implode(',',$category);
  169. return json(['code'=>200,'msg'=>'操作成功','data'=>$info,'category_id' => $category_id]);
  170. }
  171. return json(['code'=>403,'msg'=>'读取商品分类路径失败']);
  172. }
  173. }