User.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\system\controller\passport;
  9. use app\common\controller\Manage;
  10. use app\common\model\SystemUser;
  11. use app\common\model\SystemUserLevel;
  12. use think\facade\Validate;
  13. use think\facade\Request;
  14. class User extends Manage
  15. {
  16. public function initialize(){
  17. parent::initialize();
  18. if(!$this->member_miniapp_id){
  19. $this->error('未找到所属应用,请先开通应用。');
  20. }
  21. $this->assign('pathMaps', [['name'=>$this->member_miniapp->appname,'url'=>'javascript:;'],['name'=>'用户管理','url'=>'javascript:;']]);
  22. }
  23. /**
  24. * 会员列表
  25. */
  26. public function index($types = 0){
  27. $keyword = Request::param('keyword/s');
  28. $condition['is_lock'] = $types ? 1 : 0;
  29. $condition['is_delete'] = 0;
  30. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  31. $search = [];
  32. if(!empty($keyword)){
  33. if(Validate::isMobile($keyword)){
  34. $condition['phone_uid'] = $keyword;
  35. }else{
  36. $search[] = ['nickname','like','%'.$keyword.'%'];
  37. }
  38. }
  39. $view['keyword'] = $keyword;
  40. $view['list'] = SystemUser::where($condition)->where($search)->order('id desc')->paginate(20,false,['query'=>['keyword' => $keyword,'types' => $types]]);
  41. $view['types'] = $types;
  42. return view()->assign($view);
  43. }
  44. /**
  45. * 邀请关系
  46. */
  47. public function level(int $id){
  48. $user = SystemUser::field('id,nickname,face,phone_uid,invite_code,create_time')->where(['id' => $id])->find();
  49. if(!$user){
  50. return $this->error("404 NOT FOUND");
  51. }
  52. $data['nickname'] = $user['nickname'];
  53. $data['invite_code'] = $user['invite_code'];
  54. $data['phone_uid'] = $user['phone_uid'];
  55. $data['face'] = $user['face'];
  56. $data['level'] = 0;
  57. $view['user'] = $data;
  58. $view['level'] = SystemUserLevel::children_user($id);
  59. return view()->assign($view);
  60. }
  61. /**
  62. * 伞下
  63. */
  64. public function pyramid(int $id){
  65. $view['pathMaps'] = [['name'=>'用户管理','url'=>url('passport.user/index')],['name'=>'伞下用户','url'=>'javascript:;']];
  66. $view['uid'] = $id;
  67. $view['people_num'] = SystemUserLevel::where(['parent_id' => $id])->count();
  68. return view()->assign($view);
  69. }
  70. /**
  71. * 目录树
  72. * @param integer $uid
  73. * @return void
  74. */
  75. public function ztree(int $uid){
  76. if (request()->isAjax()) {
  77. $id = $this->request->param('id/d',0);
  78. $parent_uid = $id ? $id : $uid;
  79. $info = SystemUserLevel::pyramid($parent_uid);
  80. if(empty($info)){
  81. $info = [['id' => $uid,'name'=>'无用户']];
  82. }
  83. return json($info);
  84. }
  85. }
  86. /**
  87. * 选择所属用户
  88. */
  89. public function selectUser(){
  90. $keyword = Request::param('keyword/s');
  91. $input = Request::param('input/s');
  92. $condition = [];
  93. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
  94. if(!empty($keyword)){
  95. if(Validate::isMobile($keyword)){
  96. $condition[] = ['phone_uid','=',$keyword];
  97. }else{
  98. $condition[] = ['nickname','like','%'.$keyword.'%'];
  99. }
  100. }
  101. $view['list'] = SystemUser::where($condition)->order('id desc')->paginate(10,false,['query'=>['keyword' => $keyword,'input' => $input]]);
  102. $view['keyword'] = $keyword;
  103. $view['input'] = $input;
  104. $view['id'] = $this->member_miniapp_id;
  105. return view()->assign($view);
  106. }
  107. /**
  108. * 用户预览
  109. */
  110. public function review(int $uid){
  111. $view['user'] = SystemUser::where(['id' => $uid])->find();
  112. return view()->assign($view);
  113. }
  114. /**
  115. * 编辑用户
  116. * @access public
  117. */
  118. public function edit(){
  119. if($this->user->parent_id){
  120. $this->error('仅创始人有权限访问.');
  121. }
  122. if(request()->isAjax()){
  123. $data = [
  124. 'id' => Request::param('id/d'),
  125. 'password' => Request::param('password/s'),
  126. 'safepassword' => Request::param('safepassword/s'),
  127. ];
  128. $updata = [];
  129. if ($data['safepassword']) {
  130. $validate = $this->validate($data, 'User.safepassword');
  131. if (true !== $validate) {
  132. return json(['code'=>0,'msg'=>$validate]);
  133. }
  134. $updata['safe_password'] = password_hash(md5($data['safepassword']),PASSWORD_DEFAULT);
  135. }
  136. if($data['password']){
  137. $updata['password'] = password_hash(md5($data['password']),PASSWORD_DEFAULT);
  138. }
  139. if(empty($data['password']) && empty($data['password'])){
  140. return json(['code'=>0,'msg'=>'操作失败']);
  141. }
  142. $result = SystemUser::edit($updata,$data['id']);
  143. if(!$result){
  144. return json(['code'=>0,'msg'=>'操作失败']);
  145. }else{
  146. return json(['code'=>200,'msg'=>'操作成功','url' => url('passport.user/index')]);
  147. }
  148. }else{
  149. $id = $this->request->param('id/d');
  150. $view['info'] = SystemUser::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->find();
  151. if(!$view['info']){
  152. return $this->error("404 NOT FOUND");
  153. }
  154. return view('edit',$view);
  155. }
  156. }
  157. /**
  158. * 锁定
  159. * @param integer $id 用户ID
  160. */
  161. public function islock(int $id){
  162. if($this->user->parent_id){
  163. return json(['code'=>0,'msg'=>'仅创始人有权限访问.']);
  164. }
  165. $result = SystemUser::lock($this->member_miniapp_id,$id);
  166. if(!$result){
  167. return json(['code'=>0,'message'=>'操作失败']);
  168. }else{
  169. return json(['code'=>200,'message'=>'操作成功']);
  170. }
  171. }
  172. /**
  173. * 作废
  174. * @param integer $id 用户ID
  175. */
  176. public function delete(int $id){
  177. if($this->user->parent_id){
  178. return json(['code'=>0,'msg'=>'仅创始人有权限访问.']);
  179. }
  180. $result = SystemUser::isDelete($this->member_miniapp_id,$id);
  181. if(!$result){
  182. return json(['code'=>0,'message'=>'操作失败']);
  183. }else{
  184. return json(['code'=>200,'message'=>'操作成功']);
  185. }
  186. }
  187. }