Account.php 17 KB

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