Account.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * 用户账户管理
  4. */
  5. namespace app\Admin\controller;
  6. use think\Db;
  7. class Account extends Base
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. /**
  14. * @param status
  15. * @param username
  16. * @param mobile
  17. * @param nickname
  18. * @param page
  19. * @param size
  20. */
  21. public function List()
  22. {
  23. $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
  24. $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
  25. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  26. $where = ['a.is_del'=>0];
  27. if ($status !== "") {
  28. $where['a.status'] = $status;
  29. }
  30. $username = isset($this->post['username']) && $this->post['username'] !== "" ? trim($this->post['username']) : "";
  31. if ($username != "") {
  32. $where['username'] = ["like", "%{$username}%"];
  33. }
  34. $nickname = isset($this->post['nickname']) && $this->post['nickname'] !== "" ? trim($this->post['nickname']) : "";
  35. if ($nickname != "") {
  36. $where['nickname'] = ["like" => "%{$nickname}%"];
  37. }
  38. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
  39. if ($mobile != "") {
  40. $where['c.mobile'] = ["like" => "%{$mobile}%"];
  41. }
  42. $count = Db::name("account")->alias('a')
  43. ->join("fc_rela_account b", "a.id = b.accountid", "left")
  44. ->join("fc_account_info c", "b.account_info= c.id", "left")
  45. ->where($where)->count();
  46. $total = ceil($count / $size);
  47. $page = $page >= $total ? $total : $page;
  48. $list = Db::name("account")->alias('a')->where($where)->page($page, $size)
  49. ->join("fc_rela_account b", "a.id = b.accountid", "left")
  50. ->join("fc_account_info c", "b.account_info= c.id", "left")
  51. ->field("`a`.`id` AS `id`,
  52. `a`.`username` AS `username`,
  53. `a`.`password` AS `password`,
  54. `a`.`salt` AS `salt`,
  55. `a`.`status` AS `status`,
  56. `a`.`is_del` AS `is_del`,
  57. `a`.`starttime` AS `starttime`,
  58. `a`.`expiretime` AS `expiretime`,
  59. `a`.`activetime` AS `activetime`,
  60. `a`.`addtime` AS `addtime`,
  61. `c`.`nickname` AS `nickname`,
  62. `c`.`avatar` AS `avatar`,
  63. `c`.`mobile` AS `mobile`,
  64. `c`.`remark` AS `remark`,
  65. `c`.`sex` AS `sex`")
  66. ->order("a.id desc")->select();
  67. $i = [];
  68. foreach ($list as $vus) {
  69. $vi = Db::name('rela_video')->join('fc_video a', 'a.id=fc_rela_video.video_id', 'left')->field('a.video_sn,a.video_name,a.video_url,a.video_img')->where(['accountid' => $vus['id'], 'a.is_del' => 0,])->select();
  70. if (empty($vi)) {
  71. $vi = [];
  72. }
  73. $vus['info'] = $vi;
  74. $i[] = $vus;
  75. }
  76. return app_show(0, "获取成功", ["list" => $i, "count" => $count]);
  77. }
  78. /**
  79. * @param username
  80. * @param password
  81. * @param starttime
  82. * @param expiretime
  83. * @param nickname
  84. * @param remark
  85. * @param video
  86. */
  87. public function Create()
  88. {
  89. $username = isset($this->post['username']) && $this->post['username'] !== "" ? trim($this->post['username']) : "";
  90. if ($username == "") {
  91. return error_show(1004, "参数username 不能为空");
  92. }
  93. if (!checkAccount($username)) {
  94. return error_show(1004, "账户格式不正确");
  95. }
  96. $isT = Db::name("account")->where(["is_del" => 0, "username" => $username])->find();
  97. if ($isT) {
  98. return error_show(1004, "账户名已存在");
  99. }
  100. $pasword = isset($this->post['password']) && $this->post['password'] !== "" ? trim($this->post['password']) : "";
  101. if ($pasword == "") {
  102. return error_show(1004, "参数password 不能为空");
  103. }
  104. if (!checkPasswd($pasword)) {
  105. return error_show(1004, "密码格式不正确");
  106. }
  107. $starttime = isset($this->post['starttime']) && $this->post['starttime'] !== "" ? $this->post['starttime'] : "";
  108. if ($starttime == "") {
  109. return error_show(1004, "参数starttime 不能为空");
  110. }
  111. $expiretime = isset($this->post['expiretime']) && $this->post['expiretime'] !== "" ? $this->post['expiretime'] : "";
  112. if ($expiretime == "") {
  113. return error_show(1004, "参数expiretime 不能为空");
  114. }
  115. $nickname = isset($this->post['nickname']) && $this->post['nickname'] !== "" ? trim($this->post['nickname']) : "";
  116. // if($nickname==""){
  117. // return error_show(1004,"参数nickname 不能为空");
  118. // }
  119. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
  120. // if($mobile==""){
  121. // return error_show(1004,"参数mobile 不能为空");
  122. // }
  123. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  124. $video = isset($this->post['video']) && $this->post['video'] !== "" ? $this->post['video'] : "";
  125. if ($video == "") {
  126. return error_show(1004, "参数video 不能为空");
  127. }
  128. Db::startTrans();
  129. try {
  130. $salt = makeSalt();
  131. $pas = sha1($pasword . $salt);
  132. $data = [
  133. "username" => $username,
  134. "password" => $pas,
  135. "pwd" => $pasword,
  136. "salt" => $salt,
  137. "status" => 0,
  138. "is_del" => 0,
  139. "starttime" => $starttime,
  140. "expiretime" => $expiretime,
  141. "addtime" => date("Y-m-d H:i:s"),
  142. "updatetime" => date("Y-m-d H:i:s")
  143. ];
  144. $acccount = Db::name("account")->insert($data, false, true);
  145. if ($acccount > 0) {
  146. $user = [
  147. "nickname" => $nickname,
  148. "mobile" => $mobile,
  149. "avatar" => "",
  150. "remark" => $remark,
  151. "sex" => "",
  152. "addtime" => date("Y-m-d H:i:s"),
  153. "updatetime" => date("Y-m-d H:i:s")
  154. ];
  155. $info = Db::name("account_info")->insert($user, false, true);
  156. if ($info > 0) {
  157. $rela = ["accountid" => $acccount, "account_info" => $info];
  158. $rela_acc = Db::name("rela_account")->insert($rela);
  159. // $rele = [["video_id"=>$video,"accountid"=>$video,"addtime"=>$video]];
  160. $l = [];
  161. foreach ($video as $value) {
  162. $temp = ["video_id" => $value, "accountid" => $acccount, "addtime" => date("Y-m-d H:i:s")];
  163. $l[] = $temp;
  164. }
  165. $rele_a = Db::name("rela_video")->insertAll($l);
  166. if ($rele_a == false) {
  167. Db::rollback();
  168. return error_show(1002, "绑定失败");
  169. } else {
  170. write_log("视频绑定成功", $this->userinfo, "account", "add");
  171. }
  172. if ($rela_acc) {
  173. write_log("账户{$username}新建成功", $this->userinfo, "account", "add");
  174. Db::commit();
  175. return app_show(0, "账户新建成功");
  176. }
  177. }
  178. }
  179. Db::rollback();
  180. return error_show(1005, "账户新建失败");
  181. } catch (\Exception $e) {
  182. Db::rollback();
  183. return error_show(1003, $e->getMessage());
  184. }
  185. }
  186. /**@param id 账户id
  187. * @return \think\response\Json|void
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @throws \think\exception\DbException
  192. */
  193. public function Read()
  194. {
  195. $id = isset($this->post['id']) && $this->post["id"] != "" ? intval($this->post['id']) : "";
  196. if ($id == "") {
  197. return error_show(1004, "参数id 不能为空");
  198. }
  199. $info = Db::name("account")->alias('a')->where(["a.id" => $id,'a.is_del'=>0])
  200. ->join("fc_rela_account b", "a.id = b.accountid", "left")
  201. ->join("fc_account_info c", "b.account_info= c.id", "left")
  202. ->field("`a`.`id` AS `id`,
  203. `a`.`username` AS `username`,
  204. `a`.`password` AS `password`,
  205. `a`.`salt` AS `salt`,
  206. `a`.`status` AS `status`,
  207. `a`.`is_del` AS `is_del`,
  208. `a`.`starttime` AS `starttime`,
  209. `a`.`expiretime` AS `expiretime`,
  210. `a`.`activetime` AS `activetime`,
  211. `a`.`addtime` AS `addtime`,
  212. `c`.`nickname` AS `nickname`,
  213. `c`.`avatar` AS `avatar`,
  214. `c`.`mobile` AS `mobile`,
  215. `c`.`remark` AS `remark`,
  216. `c`.`sex` AS `sex`")
  217. ->find();
  218. if (empty($info)) {
  219. return error_show(1005, "未找到数据");
  220. }
  221. if ($info["is_del"] == 1) {
  222. return error_show(1005, "账户已被删除");
  223. }
  224. $info['status_n'] = $info['status'] == 0 ? "未激活" : $info['status'] == 1 ? "已激活" : "已失效";
  225. $vi = Db::name('rela_video')->join('fc_video a', 'a.id=fc_rela_video.video_id', 'left')
  226. ->field('a.video_sn,a.video_name,a.video_url,a.video_img,fc_rela_video.video_id,a.status')
  227. ->where(['accountid' => $id, 'a.is_del' => 0, 'fc_rela_video.is_del' => 0])->select();
  228. //var_dump(Db::name('rela_video')->getLastSql());
  229. $info['info'] = $vi;
  230. return app_show(0, "获取成功", $info);
  231. }
  232. /**
  233. * @param id
  234. * @param username
  235. * @param password
  236. * @param starttime
  237. * @param expiretime
  238. * @param nickname
  239. * @param remark
  240. * @param video
  241. */
  242. public function Save()
  243. {
  244. $id = isset($this->post['id']) && $this->post['id'] != "" ? intval($this->post['id']) : "";
  245. if ($id == "") {
  246. return error_show(1004, "参数id 不能为空");
  247. }
  248. $info = Db::name("account")->where(["is_del" => 0, "id" => $id])->find();
  249. if (empty($info)) {
  250. return error_show(1004, "未找到数据");
  251. }
  252. $username = isset($this->post['username']) && $this->post['username'] !== "" ? trim($this->post['username']) : "";
  253. if ($username != "") {
  254. $isT = Db::name("account")->where(["is_del" => 0, "username" => $username, "id" => ["<>", $id]])->find();
  255. if ($isT) {
  256. return error_show(1004, "账户名已存在");
  257. }
  258. $info['username'] = $username;
  259. }
  260. //
  261. // $pasword = isset($this->post['password']) && $this->post['password'] !== "" ? trim($this->post['password']) : "";
  262. // if ($pasword != "" && $info['password'] != sha1($pasword.$info['salt'])) {
  263. // $salt = makeSalt();
  264. // $info['password'] = sha1($pasword . $salt);
  265. // $info['pwd'] = $pasword;
  266. // }
  267. $starttime = isset($this->post['starttime']) && $this->post['starttime'] !== "" ? $this->post['starttime'] : "";
  268. if ($starttime != "") {
  269. $info['starttime'] = $starttime;
  270. }
  271. $expiretime = isset($this->post['expiretime']) && $this->post['expiretime'] !== "" ? $this->post['expiretime'] : "";
  272. if ($expiretime != "") {
  273. $expire = strtotime($expiretime);
  274. if ($expire > time()) {
  275. $info['status'] = $info['activetime'] == "" ? 0 : 1;
  276. } else {
  277. $info['status'] = 2;
  278. }
  279. $info['expiretime'] = $expiretime;
  280. }
  281. $info['updatetime'] = date("Y-m-d H:i:s");
  282. $rela = Db::name("account_info")->alias("a")->Join("fc_rela_account b", "b.account_info=a.id", "left")->where(["b.accountid" => $id])->field("a.*")->find();
  283. $nickname = isset($this->post['nickname']) && $this->post['nickname'] !== "" ? trim($this->post['nickname']) : "";
  284. $rela['nickname'] = $nickname;
  285. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
  286. if ($mobile != "") {
  287. $rela['mobile'] = $mobile;
  288. }
  289. $rela['remark'] = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  290. $video = isset($this->post['video']) && $this->post['video'] !== "" ? $this->post['video'] : "";
  291. if ($video == "") {
  292. return error_show(1004, "参数video 不能为空");
  293. }
  294. $rela['updatetime'] = date("Y-m-d H:i:s");
  295. Db::startTrans();
  296. try {
  297. $acccount = Db::name("account")->update($info);
  298. if ($acccount) {
  299. $infoacc = Db::name("account_info")->update($rela);
  300. $del = Db::name('rela_video')->where(["is_del" => 0, "accountid" => $id])->select();
  301. if ($del == true) {
  302. $dl = Db::name('rela_video')->where(["is_del" => 0, "accountid" => $id])->update(["addtime" => date("Y-m-d H:i:s"), "is_del" => 1]);
  303. }
  304. $k = [];
  305. $vb = Db::name('video')->where(['status' => 0, 'id' => ["in", $video]])->select();
  306. if (!empty($vb)) {
  307. return error_show(1004, "存在已禁用的视频");
  308. }
  309. foreach ($video as $valu) {
  310. $temp = ["video_id" => $valu, "accountid" => $id, "addtime" => date("Y-m-d H:i:s")];
  311. $k[] = $temp;
  312. }
  313. $rele_a = Db::name("rela_video")->insertAll($k);
  314. if ($rele_a == false) {
  315. Db::rollback();
  316. return error_show(1002, "绑定失败");
  317. } else {
  318. write_log("视频绑定成功", $this->userinfo, "account", "edit");
  319. }
  320. if ($infoacc) {
  321. write_log("账户{$username}新建成功", $this->userinfo, "account", "edit");
  322. Db::commit();
  323. return app_show(0, "账户编辑成功");
  324. } else {
  325. Db::rollback();
  326. return error_show(1005, "账户编辑失败");
  327. }
  328. }
  329. Db::rollback();
  330. return error_show(1005, "账户编辑失败");
  331. } catch (\Exception $e) {
  332. Db::rollback();
  333. return error_show(1003, $e->getMessage());
  334. }
  335. }
  336. public function checkPwd(){
  337. $id= isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  338. if($id===''){
  339. return error_show(1004, "参数id 不能为空");
  340. }
  341. $info = Db::name("account")->where(["is_del" => 0, "id" => $id])->find();
  342. if (empty($info)) {
  343. return error_show(1004, "未找到数据");
  344. }
  345. $pasword = isset($this->post['password']) && $this->post['password'] !== "" ? trim($this->post['password']) : "";
  346. if($pasword===''){
  347. return error_show(1004, "参数password 不能为空");
  348. }
  349. if ($info['pwd']==$pasword) {
  350. return error_show(1004, "新密码不能与原密码相同");
  351. }
  352. // if (!checkPasswd($pasword)) {
  353. // return error_show(1004, "密码格式不正确");
  354. // }
  355. $salt = makeSalt();
  356. $info['password'] = sha1($pasword . $salt);
  357. $info['pwd'] = $pasword;
  358. $info['salt'] = $salt;
  359. $info['updatetime'] = date("Y-m-d H:i:s");
  360. $acc= Db::name("account")->update($info);
  361. return $acc ?app_show(0,"账户密码修改成功"): error_show(1005, "账户密码修改失败");
  362. }
  363. }