Item.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\fastshop\controller;
  9. use app\common\controller\Manage;
  10. class Item extends Manage{
  11. public function initialize() {
  12. parent::initialize();
  13. if(!model('auth')->getAuth($this->user->id,1)){
  14. $this->error('无权限,你非【内容管理员】');
  15. }
  16. $this->assign('pathMaps',[['name'=>'商品管理','url'=>url("fastshop/item/index")]]);
  17. }
  18. /**
  19. * 列表
  20. */
  21. public function index(){
  22. $view['keyword'] = input('get.keyword');
  23. $view['status'] = input('?status') ? input('status') : 0;
  24. $view['page'] = input('?page') ? input('page') : 0;
  25. $view['lists'] = model('Item')->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' => input('post.category_id/d'),
  35. 'category_path_id' => input('post.category_path_id/s'),
  36. 'name' => input('post.name/s'),
  37. 'price' => input('post.price/f'),
  38. 'sell_price' => input('post.sell_price/f'),
  39. 'market_price' => input('post.market_price/f'),
  40. 'cost_price' => input('post.price/f'),
  41. 'types' => input('post.types/d'),
  42. 'points' => input('post.points/d'),
  43. 'repoints' => input('post.repoints/d'),
  44. 'store_nums' => input('post.store_nums/d'),
  45. 'weight' => input('post.weight/f'),
  46. 'unit' => input('post.unit/s'),
  47. 'imgs' => input('post.imgs/a'),
  48. 'img' => input('post.img/s'),
  49. 'content' => input('post.content/s'),
  50. ];
  51. $validate = $this->validate($data,'item.save');
  52. if(true !== $validate){
  53. return json(['code'=>0,'msg'=>$validate]);
  54. }
  55. $result = model('item')->edit($this->member_miniapp_id,$data);
  56. if($result){
  57. return json(['code'=>200,'url' => url('fastshop/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' => input('post.id/d'),
  72. 'category_id' => input('post.category_id/d'),
  73. 'category_path_id' => input('post.category_path_id/s'),
  74. 'types' => input('post.types/d'),
  75. 'points' => input('post.points/d'),
  76. 'repoints' => input('post.repoints/d'),
  77. 'name' => input('post.name/s','','htmlspecialchars'),
  78. 'price' => input('post.price/f'),
  79. 'sell_price' => input('post.sell_price/f'),
  80. 'market_price' => input('post.market_price/f'),
  81. 'cost_price' => input('post.price/f'),
  82. 'store_nums' => input('post.store_nums/d'),
  83. 'weight' => input('post.weight/f'),
  84. 'unit' => input('post.unit/s','','htmlspecialchars'),
  85. 'imgs' => input('post.imgs/a'),
  86. 'img' => input('post.img/s','','htmlspecialchars'),
  87. 'content' => input('post.content/s','','htmlspecialchars'),
  88. ];
  89. $validate = $this->validate($data,'item.save');
  90. if(true !== $validate){
  91. return json(['code'=>0,'msg'=>$validate]);
  92. }
  93. $result = model('item')->edit($this->member_miniapp_id,$data);
  94. if($result){
  95. return json(['code'=>200,'url' => url('fastshop/item/index'),'msg'=>'操作成功']);
  96. }else{
  97. return json(['code'=>0,'msg'=>'操作失败']);
  98. }
  99. }else{
  100. $view['id'] = input('get.id/d');
  101. $view['info'] = model('Item')->get($view['id']);
  102. $view['imgs'] = json_decode($view['info']['imgs'],true);
  103. $view['status'] = input('?status') ? input('status'): 0;
  104. $view['page'] = input('?page') ? input('page') : 0;
  105. //当前商品目录
  106. $category = model('Category')->getPath($this->member_miniapp_id,$view['info']['category_id']);
  107. $category_path = null;
  108. foreach ($category as $key => $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 = input('?get.id')?input('get.id/d'):0;
  120. $ids = input('?post.ids')?input('post.ids/s'):null;
  121. $result = model('Item')->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 = input('get.issale/d');
  134. $ids = input('post.ids/s');
  135. if(empty($ids)){
  136. return json(['code'=>403,'msg'=>'没有选择任何要操作商品']);
  137. }else{
  138. model('Item')->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 = input('?get.parent_id') ? input('get.parent_id/d'): 0;
  149. $info = model('Category')->field('id,parent_id,title')->where(['member_miniapp_id' => $this->member_miniapp_id,'parent_id' => $parent_id])->order(['sort'=>'desc','id'=>'desc'])->select();
  150. return json(['code'=>200,'msg'=>'操作成功','data'=>$info]);
  151. }else{
  152. $view['input'] = input('get.input');
  153. $view['path'] = input('get.path');
  154. return view('category',$view);
  155. }
  156. }
  157. /**
  158. * 及时返回当前路径
  159. */
  160. public function category_path(){
  161. $parent_id = input('?get.parent_id') ? input('get.parent_id/d'): 0;
  162. $info = model('Category')->getPath($this->member_miniapp_id,$parent_id);
  163. if($info){
  164. $category = [];
  165. foreach ($info as $key => $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. /**
  174. * 选择商品
  175. * @return void
  176. */
  177. public function select(){
  178. $view['keyword'] = input('get.keyword');
  179. $view['input'] = input('get.input');
  180. $view['status'] = 0;
  181. $view['lists'] = model('Item')->list($this->member_miniapp_id,$view['status'],$view['keyword'],$view['input']);
  182. $view['pathMaps'] = [['name' => '商品选择','url'=>'javascript:;']];
  183. return view()->assign($view);
  184. }
  185. /**
  186. * 选择商品
  187. * @return void
  188. */
  189. public function getView(){
  190. $id = input('get.id/d');
  191. $info = model('Item')->where(['id' => $id])->find();
  192. if(!empty($info)){
  193. $info['price'] = money($info['price']/100);
  194. $info['entrust_number'] = model('EntrustList')->where(['item_id' => $id,'is_rebate' => 0])->count();
  195. }
  196. return json(['code'=>200,'msg'=>'操作成功','data'=>$info]);
  197. }
  198. }