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