User.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Coupon;
  4. use app\admin\model\OrderInfo;
  5. use app\common\controller\Backend;
  6. use app\common\library\Auth;
  7. use app\common\model\Programme;
  8. use app\common\model\SysConfig;
  9. use app\common\service\DiscountService;
  10. /**
  11. * 会员管理
  12. *
  13. * @icon fa fa-user
  14. */
  15. class User extends Backend
  16. {
  17. protected $relationSearch = true;
  18. protected $searchFields = 'id,username,nickname';
  19. /**
  20. * @var \app\admin\model\User
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('User');
  27. $this->assign('levels',$this->model::getLevels());
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $list = $this->model
  43. ->with(['group','admin'])
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->paginate($limit);
  47. foreach ($list as $k => $v) {
  48. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  49. $v->hidden(['password', 'salt']);
  50. }
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. public function show($ids){
  57. $user=$this->model->find($ids);
  58. $programmes=Programme::where('user_id',$user['id'])->order('id','desc')->select();
  59. $this->assign('user',$user);
  60. $this->assign('programmes',$programmes);
  61. $buyNum=$user->orderInfo()->sum('num');
  62. $buyAmount=$user->orders()->payed()->sum('amount_pay');
  63. $this->assign('buyNum',$buyNum);
  64. $this->assign('buyAmount',$buyAmount);
  65. #产品统计
  66. $buyGoods=$user->orderInfo()->payed()->group('goods_id')->column('sum(amount_pay) as amount,sum(num) as num,goods_name');
  67. $this->assign('buyGoods',$buyGoods);
  68. #分类统计
  69. $bugCategory=$user->orderInfo()->payed()->group('category_id')->column('sum(amount_pay) as amount,sum(num) as num,category_name');
  70. $this->assign('bugCategory',$bugCategory);
  71. return $this->fetch();
  72. }
  73. /**
  74. * 添加
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isPost()) {
  79. $this->token();
  80. }
  81. if($this->request->isGet()){
  82. return $this->fetch();
  83. }
  84. $data=input('row/a');
  85. $this->validate($data,[
  86. 'username'=>['require'],
  87. 'mobile|手机号'=>['require','mobile'],
  88. ]);
  89. if($this->model->where('username',$data['username'])->find()){
  90. $this->error('用户名已存在');
  91. }
  92. if($this->model->where('mobile',$data['mobile'])->find()){
  93. $this->error('手机号已存在');
  94. }
  95. if(!empty($data['email']) && $this->model->where('email',$data['email'])->find()){
  96. $this->error('邮箱已存在');
  97. }
  98. $this->model->allowField(true)->create($data);
  99. $this->success();
  100. }
  101. /**
  102. * 编辑
  103. */
  104. public function edit($ids = null)
  105. {
  106. if ($this->request->isPost()) {
  107. $this->token();
  108. }
  109. $row = $this->model->get($ids);
  110. $this->modelValidate = true;
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  115. return parent::edit($ids);
  116. }
  117. /**
  118. * 删除
  119. */
  120. public function del($ids = "")
  121. {
  122. if (!$this->request->isPost()) {
  123. $this->error(__("Invalid parameters"));
  124. }
  125. $ids = $ids ? $ids : $this->request->post("ids");
  126. $row = $this->model->get($ids);
  127. $this->modelValidate = true;
  128. if (!$row) {
  129. $this->error(__('No Results were found'));
  130. }
  131. Auth::instance()->delete($row['id']);
  132. $this->success();
  133. }
  134. public function level_discount(){
  135. if ($this->request->isAjax()) {
  136. $levels=$this->model::getLevels();;
  137. $list=[];
  138. $config=DiscountService::getById();
  139. foreach ($levels as $id=>$level){
  140. $list[]=[
  141. 'id'=>$id,
  142. 'name'=>$level,
  143. 'discount'=>$config[$id]??'',
  144. ];
  145. }
  146. $result = array("total" => count($list), "rows" => $list);
  147. return json($result);
  148. }
  149. return $this->view->fetch();
  150. }
  151. public function level_discount_edit($ids){
  152. if($this->request->isGet()){
  153. $this->assign('row',DiscountService::getById($ids));
  154. $this->assign('id',$ids);
  155. return $this->fetch();
  156. }else{
  157. $data=input('row/a');
  158. $this->validate($data,[
  159. 'discount|折扣'=>['require','gt:0','lt:10'],
  160. ]);
  161. DiscountService::saveDiscount($ids,$data['discount']);
  162. $this->success();
  163. }
  164. }
  165. #赠送优惠券
  166. public function send_coupon($ids){
  167. if($this->request->isGet()) {
  168. $coupon = Coupon::column('name','id');
  169. $this->assign('coupon',$coupon);
  170. return $this->fetch();
  171. }else{
  172. $data=input('row/a');
  173. $this->validate($data,[
  174. 'coupon|券ID'=>['require','integer'],
  175. 'num|数量'=>['require','integer','gt:0'],
  176. ]);
  177. $coupon=Coupon::where('id',$data['coupon'])->find();
  178. if(!$coupon){
  179. $this->error('优惠券不存在');
  180. }
  181. $coupon->sendToUser([$ids],$data['num']);
  182. $this->success();
  183. }
  184. }
  185. }