Sale.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Sale 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['lists'] = model('Sale')->where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(10);
  23. return view()->assign($view);
  24. }
  25. /**
  26. * 添加
  27. */
  28. public function add(){
  29. if(request()->isAjax()){
  30. $data = [
  31. 'category_id' => input('post.category_id/d'),
  32. 'types' => input('post.types/d'),
  33. 'title' => input('post.title/s','','htmlspecialchars'),
  34. 'sale_nums' => input('post.sale_nums/d'),
  35. 'item_id' => input('post.item_id/d'),
  36. 'cost_price' => input('post.cost_price/f'),
  37. 'market_price' => input('post.market_price/f'),
  38. 'sale_price' => input('post.sale_price/f'),
  39. 'gift' => input('post.gift/a'),
  40. 'start_time' => input('post.start_time/s'),
  41. 'end_time' => input('post.end_time/s'),
  42. 'is_vip' => input('post.is_vip/d',0),
  43. 'is_newuser' => input('post.is_newuser/d',0),
  44. 'is_fusion' => input('post.is_fusion/d',0),
  45. ];
  46. $validate = $this->validate($data,'Sale.save');
  47. if(true !== $validate){
  48. return json(['code'=>0,'msg'=>$validate]);
  49. }
  50. //处理产品数据
  51. $gift = [];
  52. foreach ($data['gift'] as $key => $value) {
  53. foreach ($value as $k => $v) {
  54. $gift[$k][$key] = $v;
  55. }
  56. }
  57. foreach ($gift as $key => $value) {
  58. if(empty($value['item_id']) || empty($value['cost_price']) || empty($value['sale_price']) || empty($value['market_price'])){
  59. return json(['code'=>0,'msg'=>'价格信息填写不全']);
  60. }else{
  61. $gift[$key]['item_id'] = $value['item_id'];
  62. $gift[$key]['cost_price'] = $value['cost_price']*100;
  63. $gift[$key]['sale_price'] = $value['sale_price']*100;
  64. $gift[$key]['market_price'] = $value['market_price']*100;
  65. }
  66. }
  67. $data['gift'] = $gift;
  68. $result = model('Sale')->edit($this->member_miniapp_id,$data);
  69. if($result){
  70. return json(['code'=>200,'url' => url('fastshop/sale/index'),'msg'=>'操作成功']);
  71. }else{
  72. return json(['code'=>0,'msg'=>'操作失败']);
  73. }
  74. }else{
  75. $view['i'] = 0;
  76. $view['times'] = model('Times')->where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->select();
  77. return view()->assign($view);
  78. }
  79. }
  80. /**
  81. * 编辑
  82. */
  83. public function edit(){
  84. if(request()->isAjax()){
  85. $data = [
  86. 'id' => input('post.id/d'),
  87. 'category_id' => input('post.category_id/d'),
  88. 'types' => input('post.types/d'),
  89. 'title' => input('post.title/s','','htmlspecialchars'),
  90. 'sale_nums' => input('post.sale_nums/d'),
  91. 'item_id' => input('post.item_id/d'),
  92. 'cost_price' => input('post.cost_price/f'),
  93. 'market_price' => input('post.market_price/f'),
  94. 'sale_price' => input('post.sale_price/f'),
  95. 'gift' => input('post.gift/a'),
  96. 'start_time' => input('post.start_time/s'),
  97. 'end_time' => input('post.end_time/s'),
  98. 'is_vip' => input('post.is_vip/d',0),
  99. 'is_newuser' => input('post.is_newuser/d',0),
  100. 'is_fusion' => input('post.is_fusion/d',0),
  101. ];
  102. $validate = $this->validate($data,'Sale.save');
  103. if(true !== $validate){
  104. return json(['code'=>0,'msg'=>$validate]);
  105. }
  106. //处理产品数据
  107. $gift = [];
  108. foreach ($data['gift'] as $key => $value) {
  109. foreach ($value as $k => $v) {
  110. $gift[$k][$key] = $v;
  111. }
  112. }
  113. foreach ($gift as $key => $value) {
  114. if(empty($value['item_id']) || empty($value['cost_price']) || empty($value['sale_price']) || empty($value['market_price'])){
  115. return json(['code'=>0,'msg'=>'价格信息填写不全']);
  116. }else{
  117. $gift[$key]['item_id'] = $value['item_id'];
  118. $gift[$key]['cost_price'] = $value['cost_price']*100;
  119. $gift[$key]['sale_price'] = $value['sale_price']*100;
  120. $gift[$key]['market_price'] = $value['market_price']*100;
  121. }
  122. }
  123. $data['gift'] = $gift;
  124. $result = model('Sale')->edit($this->member_miniapp_id,$data);
  125. if($result){
  126. return json(['code'=>200,'url' => url('fastshop/sale/index'),'msg'=>'操作成功']);
  127. }else{
  128. return json(['code'=>0,'msg'=>'操作失败']);
  129. }
  130. }else{
  131. $view['id'] = input('get.id/d');
  132. $view['times'] = model('Times')->where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->select();
  133. $view['info'] = model('Sale')->get($view['id']);
  134. $view['info']['gift'] = json_decode($view['info']['gift'],true);
  135. $i = count($view['info']['gift']);
  136. $view['i'] = $i ? $i-1 : 0;
  137. return view()->assign($view);
  138. }
  139. }
  140. /**
  141. * 删除
  142. */
  143. public function delete(){
  144. $id = input('get.id');
  145. $rel = model('Order')->where(['member_miniapp_id' => $this->member_miniapp_id,'sale_id' => $id])->find();
  146. if($rel){
  147. return json(['code'=>403,'msg'=>'当前活动已有订单,禁止删除,建议下架操作']);
  148. }
  149. $result = model('sale')->destroy($id);
  150. if($result){
  151. return json(['code'=>200,'msg'=>'操作成功','data'=>[]]);
  152. }else{
  153. return json(['code'=>403,'msg'=>'操作失败']);
  154. }
  155. }
  156. /**
  157. * 上架,下架,从回收站恢复
  158. */
  159. public function ids_action(){
  160. if(request()->isAjax()){
  161. $issale = input('get.issale/d');
  162. $ids = input('post.ids/s');
  163. if(empty($ids)){
  164. return json(['code'=>403,'msg'=>'没有选择任何要操作商品']);
  165. }else{
  166. model('Item')->ids_action($issale,$ids);
  167. return json(['code'=>200,'msg'=>'操作成功','data'=>[]]);
  168. }
  169. }
  170. }
  171. /**
  172. * 排序
  173. */
  174. public function sort(){
  175. if(request()->isAjax()){
  176. $data = [
  177. 'sort' => input('post.sort/d'),
  178. 'id' => input('post.id/d'),
  179. ];
  180. $validate = $this->validate($data,'Category.sort');
  181. if(true !== $validate){
  182. return json(['code'=>0,'msg'=>$validate]);
  183. }
  184. $result = model('Sale')->where(['id' => $data['id'],'member_miniapp_id' => $this->member_miniapp_id])->update(['sort'=>$data['sort']]);
  185. if($result){
  186. return json(['code'=>200,'msg'=>'操作成功']);
  187. }else{
  188. return json(['code'=>0,'msg'=>'操作失败']);
  189. }
  190. }
  191. }
  192. }