Soldier.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\AdminBaseController;
  4. use app\common\model\SoldierModel;
  5. use app\common\model\SoldierVideoModel;
  6. use app\common\model\SoldierVideoSeriesModel;
  7. use app\common\model\SoldierVideoWatchModel;
  8. use app\common\validate\SoldierUserValidate;
  9. use app\common\validate\SoldierVideoSeriesValidate;
  10. use app\common\validate\SoldierVideoValidate;
  11. use think\exception\ValidateException;
  12. class Soldier extends AdminBaseController
  13. {
  14. /**
  15. * 用户列表
  16. */
  17. public function user()
  18. {
  19. return view('', [
  20. 'status_list' => SoldierModel::STATUS,
  21. ]);
  22. }
  23. public function listUser()
  24. {
  25. $map = $this->dealEqualInput(['status'], $this->dealLikeInput(['keywords' => 'name|mobile']));
  26. $list = SoldierModel::where($map)
  27. ->order('id', 'desc')
  28. ->limit(input('limit'))
  29. ->page(input('page'))
  30. ->append(['status_text'])->select();
  31. $count = SoldierModel::where($map)->count();
  32. if ($count == 0) {
  33. ajax_return(1, '未查询到数据');
  34. }
  35. list_return($list, $count);
  36. }
  37. public function delUser()
  38. {
  39. $id = input('id/d', 0);
  40. if (empty($id)) {
  41. ajax_return(1, '未查询到数据');
  42. }
  43. SoldierModel::destroy($id);
  44. SoldierVideoWatchModel::destroy(['user_id' => $id]);
  45. ajax_return();
  46. }
  47. /**
  48. * 编辑用户
  49. */
  50. public function userForm()
  51. {
  52. $id = input('id/d', 0);
  53. $info = SoldierModel::find($id);
  54. return view('', [
  55. 'info' => $info,
  56. 'status_list' => SoldierModel::STATUS,
  57. ]);
  58. }
  59. public function editUser()
  60. {
  61. $data = input('post.');
  62. try {
  63. validate(SoldierUserValidate::class)->check($data);
  64. } catch (ValidateException $e) {
  65. ajax_return(1, $e->getError());
  66. }
  67. //手机号
  68. $check_mobile_where = [['mobile', '=', $data['mobile']]];
  69. if (!empty($data['id'])) {
  70. $check_mobile_where[] = ['id', '<>', $data['id']];
  71. }
  72. $check_mobile = SoldierModel::where($check_mobile_where)->find();
  73. if (!empty($check_mobile)) {
  74. ajax_return(1, '手机号已存在');
  75. }
  76. //密码
  77. if (empty($data['id']) && empty($data['password'])) {
  78. ajax_return(1, '请输入一个初始密码');
  79. }
  80. if (empty($data['password'])) {
  81. unset($data['password']);
  82. } else {
  83. $data['salt'] = rand_str();
  84. $data['password'] = md5(md5($data['salt']) . $data['password']);
  85. }
  86. if (empty($data['id'])) {
  87. SoldierModel::create($data);
  88. } else {
  89. SoldierModel::update($data, ['id' => $data['id']]);
  90. }
  91. ajax_return();
  92. }
  93. /**
  94. * 用户导入
  95. */
  96. public function importUser()
  97. {
  98. return view('public/import', [
  99. 'url' => url('soldier/importUserPost'),
  100. 'last_table' => 'lay-soldier-user-table',
  101. 'template_file' => '/static/common/exl/soldier_user.xls',
  102. ]);
  103. }
  104. /**
  105. * 用户导入提交
  106. */
  107. public function importUserPost()
  108. {
  109. $file_url = input('file_url/s', "");
  110. if (!file_exists($file_url)) {
  111. ajax_return(1, '文件不存在');
  112. }
  113. //初始化数据
  114. $data = ['name', 'mobile', 'password'];
  115. $list = import_exl($file_url, $data, 1);
  116. if (empty($list)) {
  117. ajax_return(1, '请上传有数据的文件');
  118. }
  119. $empty_check = [
  120. 'name' => '姓名',
  121. 'mobile' => '手机号',
  122. 'password' => '密码',
  123. ];
  124. //获取手机号
  125. $mobile_list = array_column($list, 'mobile');
  126. $mobile_check_list = SoldierModel::where('mobile', 'in', $mobile_list)->column('mobile');
  127. //错误判断
  128. $validate = \think\facade\Validate::rule('mobile', 'mobile');
  129. foreach ($list as $k => $v) {
  130. foreach ($empty_check as $key => $value) {
  131. if (empty($v[$key])) {
  132. return ajax_return(1, '第' . ($k + 2) . '行的' . $value . '不能为空');
  133. }
  134. }
  135. if (!$validate->check($v)) {
  136. return ajax_return(1, '第' . ($k + 2) . '行的手机号格式不对');
  137. }
  138. if (!empty(in_array($v['mobile'], $mobile_check_list))) {
  139. return ajax_return(1, '第' . ($k + 2) . '行的手机号已存在');
  140. }
  141. $list[$k]['salt'] = rand_str();
  142. $list[$k]['password'] = md5(md5($list[$k]['salt']) . $v['password']);
  143. }
  144. SoldierModel::insertAll($list);
  145. ajax_return(0);
  146. }
  147. /**
  148. * 视频系列列表
  149. */
  150. public function videoSeries()
  151. {
  152. return view('', [
  153. 'status_list' => SoldierVideoSeriesModel::STATUS,
  154. ]);
  155. }
  156. public function listVideoSeries()
  157. {
  158. $map = $this->dealEqualInput(['status'], $this->dealLikeInput(['title']));
  159. $list = SoldierVideoSeriesModel::where($map)
  160. ->order('priority desc,id desc')
  161. ->limit(input('limit'))
  162. ->page(input('page'))
  163. ->append(['status_text'])->select();
  164. $count = SoldierVideoSeriesModel::where($map)->count();
  165. if ($count == 0) {
  166. ajax_return(1, '未查询到数据');
  167. }
  168. list_return($list, $count);
  169. }
  170. public function delVideoSeries()
  171. {
  172. $id = input('id/d', 0);
  173. if (empty($id)) {
  174. ajax_return(1, '未查询到数据');
  175. }
  176. $check = SoldierVideoModel::where('series_id', $id)->find();
  177. if (!empty($check)) {
  178. ajax_return(1, '该系列下有视频,无法删除');
  179. }
  180. SoldierVideoSeriesModel::destroy($id);
  181. ajax_return();
  182. }
  183. /**
  184. * 编辑视频系列
  185. */
  186. public function videoSeriesForm()
  187. {
  188. $id = input('id/d', 0);
  189. $info = SoldierVideoSeriesModel::find($id);
  190. return view('', [
  191. 'info' => $info,
  192. 'status_list' => SoldierVideoSeriesModel::STATUS,
  193. ]);
  194. }
  195. public function editVideoSeries()
  196. {
  197. $data = input('post.');
  198. try {
  199. validate(SoldierVideoSeriesValidate::class)->check($data);
  200. } catch (ValidateException $e) {
  201. ajax_return(1, $e->getError());
  202. }
  203. if (empty($data['id'])) {
  204. SoldierVideoSeriesModel::create($data);
  205. } else {
  206. SoldierVideoSeriesModel::update($data);
  207. }
  208. ajax_return();
  209. }
  210. /**
  211. * 视频列表
  212. */
  213. public function video()
  214. {
  215. $series_list = SoldierVideoSeriesModel::where('status', SoldierVideoSeriesModel::STATUS_SHOW)->order('priority desc')->select();
  216. return view('', [
  217. 'status_list' => SoldierVideoModel::STATUS,
  218. 'series_list' => $series_list,
  219. ]);
  220. }
  221. public function listVideo()
  222. {
  223. $map = $this->dealEqualInput(['status'], $this->dealLikeInput(['title']));
  224. $list = SoldierVideoModel::with(['series'])
  225. ->where($map)
  226. ->order('priority desc,id desc')
  227. ->limit(input('limit'))
  228. ->page(input('page'))
  229. ->append(['status_text'])->select();
  230. $count = SoldierVideoModel::where($map)->count();
  231. if ($count == 0) {
  232. ajax_return(1, '未查询到数据');
  233. }
  234. list_return($list, $count);
  235. }
  236. public function delVideo()
  237. {
  238. $id = input('id/d', 0);
  239. if (empty($id)) {
  240. ajax_return(1, '未查询到数据');
  241. }
  242. $check = SoldierVideoWatchModel::where('video_id', $id)->find();
  243. if (!empty($check)) {
  244. ajax_return(1, '该视频下有观看记录,无法删除');
  245. }
  246. SoldierVideoModel::destroy($id);
  247. ajax_return();
  248. }
  249. /**
  250. * 编辑视频
  251. */
  252. public function videoForm()
  253. {
  254. $id = input('id/d', 0);
  255. $series_list = SoldierVideoSeriesModel::where('status', SoldierVideoSeriesModel::STATUS_SHOW)->order('priority desc')->select();
  256. $info = SoldierVideoModel::find($id);
  257. return view('', [
  258. 'info' => $info,
  259. 'status_list' => SoldierVideoModel::STATUS,
  260. 'series_list' => $series_list,
  261. ]);
  262. }
  263. public function editVideo()
  264. {
  265. $data = input('post.');
  266. try {
  267. validate(SoldierVideoValidate::class)->check($data);
  268. } catch (ValidateException $e) {
  269. ajax_return(1, $e->getError());
  270. }
  271. if (empty($data['id'])) {
  272. SoldierVideoModel::create($data);
  273. } else {
  274. SoldierVideoModel::update($data);
  275. }
  276. ajax_return();
  277. }
  278. /**
  279. * 观看记录
  280. */
  281. public function videoWatch()
  282. {
  283. $id = input('id/d', 0);
  284. if (empty($id)) {
  285. return '请选择视频';
  286. }
  287. return view('', [
  288. 'id' => $id,
  289. ]);
  290. }
  291. public function listVideoWatch()
  292. {
  293. $id = input('id/d', 0);
  294. if (empty($id)) {
  295. ajax_return(1, '请选择视频');
  296. }
  297. $list = SoldierVideoWatchModel::with(['user'])
  298. ->where('video_id', $id)
  299. ->limit(input('limit'))
  300. ->page(input('page'))
  301. ->append(['status_text'])
  302. ->select();
  303. $count = SoldierVideoWatchModel::where('video_id', $id)->count();
  304. if ($count == 0) {
  305. ajax_return(1, '未查询到数据');
  306. }
  307. list_return($list, $count);
  308. }
  309. public function exportVideoWatch()
  310. {
  311. $id = input('id/d', 0);
  312. if (empty($id)) {
  313. ajax_return(1, '请选择视频');
  314. }
  315. $video = SoldierVideoModel::find($id);
  316. $list = SoldierVideoWatchModel::with(['user'])->where('video_id', $id)->append(['status_text'])->select();
  317. foreach ($list as $v) {
  318. $v['user_name'] = $v['user']['name'];
  319. }
  320. $xlsCell = [
  321. ['id', '表ID'],
  322. ['user_name', '姓名'],
  323. ['status_text', '状态'],
  324. ['create_time', '首次学习时间'],
  325. ['update_time', '最后学习时间'],
  326. ];
  327. export_exl($video['title']."的观看记录", $xlsCell, $list);
  328. }
  329. }