Vip.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\allwin\controller;
  9. use app\allwin\model\VipCard;
  10. use app\allwin\model\Coupon;
  11. class Vip extends Common{
  12. public $mini_program = [];
  13. public function initialize() {
  14. parent::initialize();
  15. $this->assign('pathMaps',[['name'=>'会员设置','url'=>url("vip/index")]]);
  16. }
  17. /**
  18. * 列表
  19. */
  20. public function index(){
  21. $condition = [];
  22. $view['lists'] = VipCard::where($this->mini_program)->where($condition)->order('sort desc,id desc')->paginate(20);
  23. return view()->assign($view);
  24. }
  25. /**
  26. * 选择优惠券
  27. */
  28. public function coupon(int $id){
  29. $condition = [];
  30. $vip = VipCard::where(['id' => $id])->where($condition)->field('coupon_ids')->find();
  31. if(empty($vip)){
  32. $this->error('没有找到对应会员类型');
  33. }
  34. $view['lists'] = Coupon::where($this->mini_program)->whereIn('id',$vip->coupon_ids)->order('sort desc,id desc')->paginate(20,false,['query' => ['id' => $id]]);
  35. $view['vip_id'] = $id;
  36. return view()->assign($view);
  37. }
  38. /**
  39. * 弹出选择优惠券
  40. */
  41. public function winCoupon(int $vip_id){
  42. if(request()->isAjax()){
  43. $ids = $this->request->param('ids/s');
  44. if(empty($ids)){
  45. return json(['code'=>0,'msg'=>'请选择要关联的优惠券']);
  46. }
  47. $result = VipCard::editCoupon($vip_id,ids($ids,true));
  48. if($result){
  49. return json(['code'=>302,'msg'=>'关联优惠券成功','data' =>[]]);
  50. }else{
  51. return json(['code'=>0,'msg'=>'关联优惠券失败']);
  52. }
  53. }else{
  54. $coupon = VipCard::where($this->mini_program)->where(['id' => $vip_id])->field('coupon_ids')->find();
  55. $coupon_ids = [];
  56. if($coupon){
  57. $coupon_ids = explode(',',$coupon->coupon_ids);
  58. }
  59. $keyword = $this->request->param('keyword');
  60. if(!empty($keyword)){
  61. $sql = Coupon::where($this->mini_program)->whereLike('name','%'.$keyword.'%');
  62. }else{
  63. $sql = Coupon::where($this->mini_program);
  64. }
  65. $condition = [];
  66. $view['lists'] = $sql->whereNotIn('id',$coupon_ids)->where($condition)->order('is_platform desc,size desc,id desc')->paginate(20,false,['query' => ['vip_id' => $vip_id,'keyword'=>$keyword]]);
  67. $view['vip_id'] = $vip_id;
  68. $view['keyword'] = $keyword;
  69. return view()->assign($view);
  70. }
  71. }
  72. /**
  73. * 添加
  74. */
  75. public function add(){
  76. if(request()->isAjax()){
  77. $data = [
  78. 'member_miniapp_id' => $this->member_miniapp_id,
  79. 'name' => $this->request->param('name/s'),
  80. 'price' => $this->request->param('price/f'),
  81. 'coupon_num' => $this->request->param('coupon_num/d'),
  82. 'tips' => $this->request->param('tips/s'),
  83. 'store_workers' => $this->request->param('store_workers/f'),
  84. 'store' => $this->request->param('store/f'),
  85. 'company_workers' => $this->request->param('company_workers/f'),
  86. 'company' => $this->request->param('company/f'),
  87. 'stock' => $this->request->param('stock/f'),
  88. 'group' => $this->request->param('group/f'),
  89. 'rule' => $this->request->param('rule'),
  90. 'is_tax' => $this->request->param('is_tax/d'),
  91. 'nominate' => $this->request->param('nominate/d'),
  92. ];
  93. $validate = $this->validate($data,'Vipcard.edit');
  94. if(true !== $validate){
  95. return json(['code'=>0,'msg'=>$validate]);
  96. }
  97. $data['is_up'] = 1;
  98. $result = VipCard::edit($data);
  99. if($result){
  100. return json(['code'=>200,'url'=>url('vip/index'),'msg'=>'操作成功']);
  101. }else{
  102. return json(['code'=>0,'msg'=>'操作失败']);
  103. }
  104. }else{
  105. return view();
  106. }
  107. }
  108. //编辑
  109. public function edit(){
  110. if(request()->isAjax()){
  111. $data = [
  112. 'name' => $this->request->param('name/s'),
  113. 'price' => $this->request->param('price/f'),
  114. 'coupon_num' => $this->request->param('coupon_num/d'),
  115. 'tips' => $this->request->param('tips/s'),
  116. 'store_workers' => $this->request->param('store_workers/f'),
  117. 'store' => $this->request->param('store/f'),
  118. 'company_workers' => $this->request->param('company_workers/f'),
  119. 'company' => $this->request->param('company/f'),
  120. 'stock' => $this->request->param('stock/f'),
  121. 'group' => $this->request->param('group/f'),
  122. 'rule' => $this->request->param('rule'),
  123. 'is_tax' => $this->request->param('is_tax/d'),
  124. 'nominate' => $this->request->param('nominate/d'),
  125. ];
  126. $validate = $this->validate($data,'Vipcard.edit');
  127. if(true !== $validate){
  128. return json(['code'=>0,'msg'=>$validate]);
  129. }
  130. $id = $this->request->param('id/d');
  131. $result = VipCard::edit($data,$id);
  132. if($result){
  133. return json(['code'=>200,'url'=>url('vip/index'),'msg'=>'操作成功']);
  134. }else{
  135. return json(['code'=>0,'msg'=>'操作失败']);
  136. }
  137. }else{
  138. $view['info'] = VipCard::get(input('get.id/d'));
  139. return view()->assign($view);
  140. }
  141. }
  142. /**
  143. * 排序
  144. */
  145. public function sort(){
  146. if(request()->isAjax()){
  147. $data = [
  148. 'sort' => $this->request->param('sort/d'),
  149. 'id' => $this->request->param('id/d'),
  150. ];
  151. $validate = $this->validate($data,'Cate.sort');
  152. if(true !== $validate){
  153. return json(['code'=>0,'msg'=>$validate]);
  154. }
  155. $result = VipCard::where(['id' => $data['id']])->update(['sort'=>$data['sort']]);
  156. if($result){
  157. return json(['code'=>200,'msg'=>'操作成功']);
  158. }else{
  159. return json(['code'=>0,'msg'=>'操作失败']);
  160. }
  161. }
  162. }
  163. /**
  164. * 置顶/取消
  165. * @param integer $id 用户ID
  166. */
  167. public function isTop(int $id){
  168. VipCard::where(['member_miniapp_id' => $this->member_miniapp_id])->update(['is_up' => 0]);
  169. $result = VipCard::where(['member_miniapp_id' => $this->member_miniapp_id,'id' =>$id])->update(['is_up' => 1]);
  170. if(!$result){
  171. return json(['code'=>0,'message'=>'操作失败']);
  172. }else{
  173. return json(['code'=>200,'message'=>'操作成功']);
  174. }
  175. }
  176. //删除
  177. public function delete(int $id){
  178. $result = VipCard::where($this->mini_program)->where(['id'=>$id])->delete();
  179. if($result){
  180. return json(['code'=>200,'msg'=>'操作成功']);
  181. }else{
  182. return json(['code'=>403,'msg'=>'删除失败']);
  183. }
  184. }
  185. //删除
  186. public function delCoupon(int $vip_id,$coupon_id){
  187. $info = VipCard::where(['id' => $vip_id])->find();
  188. $result = false;
  189. if($info){
  190. $coupon_ids = ids(array_values_unset($coupon_id,explode(',',$info->coupon_ids)));
  191. $result = VipCard::where($this->mini_program)->where(['id' => $vip_id])->update(['coupon_ids' => $coupon_ids]);
  192. }
  193. if($result){
  194. return json(['code' =>200,'msg'=>'操作成功']);
  195. }else{
  196. return json(['code' => 0,'msg'=>'操作失败']);
  197. }
  198. }
  199. }