Account.php 17 KB

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