AdminIndexController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use api\user\model\UserLikeModel;
  13. use app\common\Constant;
  14. use app\common\Excel;
  15. use app\common\Fun;
  16. use app\love\model\ActiveApplyModel;
  17. use app\love\model\UserFriendModel;
  18. use app\love\model\UserGiftModel;
  19. use app\love\model\UserInviteModel;
  20. use app\love\model\UserMatingModel;
  21. use app\love\model\UserMessageModel;
  22. use app\love\model\UserSelectLogModel;
  23. use app\love\model\UserSelectModel;
  24. use app\love\model\UserVisitModel;
  25. use app\user\model\UserFavoriteModel;
  26. use app\user\model\UserModel;
  27. use cmf\controller\AdminBaseController;
  28. use think\Db;
  29. use think\db\Query;
  30. use \think\facade\Request;
  31. /**
  32. * Class AdminIndexController
  33. * @package app\user\controller
  34. *
  35. * @adminMenuRoot(
  36. * 'name' =>'用户管理',
  37. * 'action' =>'default',
  38. * 'parent' =>'',
  39. * 'display'=> true,
  40. * 'order' => 10,
  41. * 'icon' =>'group',
  42. * 'remark' =>'用户管理'
  43. * )
  44. *
  45. * @adminMenuRoot(
  46. * 'name' =>'用户组',
  47. * 'action' =>'default1',
  48. * 'parent' =>'user/AdminIndex/default',
  49. * 'display'=> true,
  50. * 'order' => 10000,
  51. * 'icon' =>'',
  52. * 'remark' =>'用户组'
  53. * )
  54. */
  55. class AdminIndexController extends AdminBaseController
  56. {
  57. /**
  58. * 后台本站用户列表
  59. * @adminMenu(
  60. * 'name' => '本站用户',
  61. * 'parent' => 'default1',
  62. * 'display'=> true,
  63. * 'hasView'=> true,
  64. * 'order' => 10000,
  65. * 'icon' => '',
  66. * 'remark' => '本站用户',
  67. * 'param' => ''
  68. * )
  69. */
  70. public function index()
  71. {
  72. $content = hook_one('user_admin_index_view');
  73. if (!empty($content)) {
  74. return $content;
  75. }
  76. $count = Db::name('user')
  77. ->where(function (Query $query) {
  78. $data = $this->request->param();
  79. $query->where('user_type', 2);
  80. $query->where('is_complete', 1);
  81. if (!empty($data['uid'])) {
  82. $query->where('id', intval($data['uid']));
  83. }
  84. if (!empty($data['keyword'])) {
  85. $keyword = $data['keyword'];
  86. $query->where('realname', 'like', "%$keyword%");
  87. }
  88. if (!empty($data['sex'])) {
  89. $query->where('sex', $data['sex']);
  90. }
  91. if (!empty($data['check_status'])) {
  92. $query->where('check_status', $data['check_status']);
  93. }
  94. })
  95. ->count();
  96. $this->assign('total',$count);
  97. $list = Db::name('user')
  98. ->where(function (Query $query) {
  99. $data = $this->request->param();
  100. $query->where('user_type', 2);
  101. $query->where('is_complete', 1);
  102. if (!empty($data['uid'])) {
  103. $query->where('id', intval($data['uid']));
  104. }
  105. if (!empty($data['keyword'])) {
  106. $keyword = $data['keyword'];
  107. $query->where('realname', 'like', "%$keyword%");
  108. }
  109. if (!empty($data['sex'])) {
  110. $query->where('sex', $data['sex']);
  111. }
  112. if (!empty($data['check_status'])) {
  113. $query->where('check_status', $data['check_status']);
  114. }
  115. })
  116. ->order("check_status asc,create_time DESC")
  117. ->paginate(10, false, [
  118. 'query' => Request::param(),//不丢失已存在的url参数
  119. ]);
  120. $this->assign('sex', $this->request->param('sex') ?: 0);
  121. $this->assign('is_complete', $this->request->param('is_complete') ?: 0);
  122. $this->assign('check_status', $this->request->param('check_status') ?: 0);
  123. // 获取分页显示
  124. $page = $list->render();
  125. $this->assign('list', $list);
  126. $this->assign('page', $page);
  127. // 渲染模板输出
  128. return $this->fetch();
  129. }
  130. /**
  131. * 审核
  132. */
  133. public function checkPost()
  134. {
  135. $data = $this->request->post();
  136. UserModel::update($data, ['id' => $data['id']]);
  137. $this->success('操作成功');
  138. }
  139. /**
  140. * 详情
  141. */
  142. public function show()
  143. {
  144. //获取id
  145. $id = input('param.id');
  146. if (empty($id)) {
  147. $this->error(lang('信息不存在或已删除'));
  148. }
  149. //获取信息
  150. $info = UserModel::get($id);
  151. $info['family'] = $info['family'] ? implode(',', json_decode($info['family'], true)) : '未填写';
  152. $info['have_house'] = Constant::COND_TINYINT[$info['have_house']];
  153. $info['have_car'] = Constant::COND_TINYINT[$info['have_car']];
  154. $info['with_parent_live'] = Constant::COND_TINYINT[$info['with_parent_live']];
  155. if (empty($info)) {
  156. $this->error(lang('信息不存在或已删除'));
  157. }
  158. $this->assign('info', $info);
  159. $mating = UserMatingModel::get(['user_id' => $id]);
  160. $mating['have_house'] = Constant::COND_TINYINT[$mating['have_house']];
  161. $mating['have_car'] = Constant::COND_TINYINT[$mating['have_car']];
  162. $mating['with_parent_live'] = Constant::COND_TINYINT[$mating['with_parent_live']];
  163. $this->assign('mating', $mating);
  164. return $this->fetch();
  165. }
  166. /**
  167. * 本站用户拉黑
  168. * @adminMenu(
  169. * 'name' => '本站用户拉黑',
  170. * 'parent' => 'index',
  171. * 'display'=> false,
  172. * 'hasView'=> false,
  173. * 'order' => 10000,
  174. * 'icon' => '',
  175. * 'remark' => '本站用户拉黑',
  176. * 'param' => ''
  177. * )
  178. */
  179. public function ban()
  180. {
  181. $id = input('param.id', 0, 'intval');
  182. if ($id) {
  183. $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
  184. if ($result) {
  185. $this->success("会员拉黑成功!", "adminIndex/index");
  186. } else {
  187. $this->error('会员拉黑失败,会员不存在,或者是管理员!');
  188. }
  189. } else {
  190. $this->error('数据传入失败!');
  191. }
  192. }
  193. /**
  194. * 本站用户启用
  195. * @adminMenu(
  196. * 'name' => '本站用户启用',
  197. * 'parent' => 'index',
  198. * 'display'=> false,
  199. * 'hasView'=> false,
  200. * 'order' => 10000,
  201. * 'icon' => '',
  202. * 'remark' => '本站用户启用',
  203. * 'param' => ''
  204. * )
  205. */
  206. public function cancelBan()
  207. {
  208. $id = input('param.id', 0, 'intval');
  209. if ($id) {
  210. Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
  211. $this->success("会员启用成功!", '');
  212. } else {
  213. $this->error('数据传入失败!');
  214. }
  215. }
  216. /**
  217. * 删除用户
  218. */
  219. public function delete()
  220. {
  221. $id = input('param.id', 0, 'intval');
  222. if ($id > 1) {
  223. Db::startTrans();
  224. try {
  225. UserModel::destroy($id);
  226. ActiveApplyModel::where('user_id', $id)->delete();
  227. UserFavoriteModel::where('user_id|uid', '=', $id)->delete();
  228. UserFriendModel::where('user_id|friend_id', '=', $id)->delete();
  229. UserGiftModel::where('from_id|to_id', '=', $id)->delete();
  230. UserInviteModel::where('from_id|to_id', '=', $id)->delete();
  231. UserLikeModel::where('user_id', '=', $id)->delete();
  232. UserMatingModel::where('user_id', '=', $id)->delete();
  233. UserMessageModel::where('from_id|to_id', '=', $id)->delete();
  234. UserSelectModel::where('user_id|uid', '=', $id)->delete();
  235. UserSelectLogModel::where('user_id1|user_id2', '=', $id)->delete();
  236. Db::name('user_token')->where('user_id', '=', $id)->delete();
  237. UserVisitModel::where('from_id|to_id', '=', $id)->delete();
  238. Db::commit();
  239. } catch (\Exception $e) {
  240. //回滚事务
  241. Db::rollback();
  242. $this->error('会员删除失败,会员不存在,或者是管理员!');
  243. }
  244. $this->success("会员删除成功!", "adminIndex/index");
  245. } else {
  246. $this->error('数据传入失败!');
  247. }
  248. }
  249. /**
  250. * 导出
  251. */
  252. public function export()
  253. {
  254. $data = $this->request->param();
  255. $where = [
  256. ['user_type', '=', 2],
  257. ['is_complete', '=', 1],
  258. ];
  259. if (!empty($data['uid'])) {
  260. $where[] = ['id', '=', intval($data['uid'])];
  261. }
  262. if (!empty($data['keyword'])) {
  263. $where[] = ['realname', 'like', "%{$data['keyword']}%"];
  264. }
  265. if (!empty($data['sex'])) {
  266. $where[] = ['sex', '=', $data['sex']];
  267. }
  268. if (!empty($data['check_status'])) {
  269. $where[] = ['check_status', '=', $data['check_status']];
  270. }
  271. $list = UserModel::where($where)->order("check_status asc,create_time DESC")->select();
  272. $data = [];
  273. foreach ($list as $v) {
  274. $data[] = [
  275. 'name' => $v['realname'],
  276. 'sex' => $v['sex_text'],
  277. 'age' => Fun::getAgeByBirth($v['birthday']),
  278. 'mobile' => $v['mobile'],
  279. 'company' => $v['company'],
  280. 'native' => $v['native'],
  281. 'marry' => $v['marry'],
  282. ];
  283. }
  284. if (empty($data)) {
  285. return '暂无数据';
  286. }
  287. $excel = new Excel();
  288. $title = [
  289. ['name','姓名'],
  290. ['sex','性别'],
  291. ['age','年龄'],
  292. ['mobile','电话'],
  293. ['company','单位'],
  294. ['native','籍贯'],
  295. ['marry','婚姻状况'],
  296. ];
  297. $excel->export('本站用户',$title,$data,['mobile']);
  298. }
  299. }