User.php 6.6 KB

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