Account.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. $page = isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) : 1;
  23. $size = isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :10;
  24. $status = isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']) :"";
  25. $where=[];
  26. if($status!==""){
  27. $where['status'] = $status;
  28. }
  29. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  30. if($username!=""){
  31. $where['username'] = ["like","%{$username}%"];
  32. }
  33. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  34. if($nickname!=""){
  35. $where['nickname'] = ["like"=>"%{$nickname}%"];
  36. }
  37. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  38. if($mobile!=""){
  39. $where['mobile'] = ["like"=>"%{$mobile}%"];
  40. }
  41. $count= Db::name("account_list")->where($where)->count();
  42. $total = ceil($count/$size);
  43. $page = $page>=$total? $total:$page;
  44. $list = Db::name("account_list")->where($where)->page($page,$size)->order("addtime desc")->select();
  45. $i=[];
  46. foreach ($list as $vus ){
  47. $vi = Db::name('rela_video')->join('fc_video a','a.id=rela_video.video_id','left')->field('a.video_sn,a.video_name,a.video_url,a.video_img')->where(['accountid'=>$vus['id'],'is_del'=>0,])->select();
  48. if(empty($vi)){
  49. $vi=[];
  50. }
  51. $vus['info']=$vi;
  52. $i[]=$vus;
  53. }
  54. return app_show(0,"获取成功",["list"=>$i,"count"=>$count]);
  55. }
  56. /**
  57. * @param username
  58. * @param password
  59. * @param starttime
  60. * @param expiretime
  61. * @param nickname
  62. * @param remark
  63. * @param video
  64. */
  65. public function Create(){
  66. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  67. if($username==""){
  68. return error_show(1004,"参数username 不能为空");
  69. }
  70. if(!checkAccount($username)){
  71. return error_show(1004,"账户格式不正确");
  72. }
  73. $isT= Db::name("account")->where(["is_del"=>0,"username"=>$username])->find();
  74. if($isT){
  75. return error_show(1004,"账户名已存在");
  76. }
  77. $pasword = isset($this->post['password'])&&$this->post['password']!=="" ? trim($this->post['password']) :"";
  78. if($pasword==""){
  79. return error_show(1004,"参数password 不能为空");
  80. }
  81. if(!checkPasswd($pasword)){
  82. return error_show(1004,"密码格式不正确");
  83. }
  84. $starttime = isset($this->post['starttime'])&&$this->post['starttime']!=="" ? $this->post['starttime'] :"";
  85. if($starttime==""){
  86. return error_show(1004,"参数starttime 不能为空");
  87. }
  88. $expiretime = isset($this->post['expiretime'])&&$this->post['expiretime']!=="" ? $this->post['expiretime'] :"";
  89. if($expiretime==""){
  90. return error_show(1004,"参数expiretime 不能为空");
  91. }
  92. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  93. // if($nickname==""){
  94. // return error_show(1004,"参数nickname 不能为空");
  95. // }
  96. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  97. // if($mobile==""){
  98. // return error_show(1004,"参数mobile 不能为空");
  99. // }
  100. $remark = isset($this->post['remark'])&&$this->post['remark']!=="" ? trim($this->post['remark']) :"";
  101. $video = isset($this->post['video'])&&$this->post['video']!=="" ? trim($this->post['video']) :"";
  102. Db::startTrans();
  103. try {
  104. $salt = makeSalt();
  105. $pas = sha1($pasword.$salt);
  106. $data=[
  107. "username"=>$username,
  108. "password"=>$pas,
  109. "pwd"=>$pasword,
  110. "salt"=>$salt,
  111. "status"=>0,
  112. "is_del"=>0,
  113. "starttime"=>$starttime,
  114. "expiretime"=>$expiretime,
  115. "addtime"=>date("Y-m-d H:i:s"),
  116. "updatetime"=>date("Y-m-d H:i:s")
  117. ];
  118. $acccount = Db::name("account")->insert($data,false,true);
  119. if($acccount>0){
  120. $user=[
  121. "nickname"=>$nickname,
  122. "mobile"=>$mobile,
  123. "avatar"=>"",
  124. "remark"=>$remark,
  125. "sex"=>"",
  126. "addtime"=>date("Y-m-d H:i:s"),
  127. "updatetime"=>date("Y-m-d H:i:s")
  128. ];
  129. $info = Db::name("account_info")->insert($user,false,true);
  130. if($info>0){
  131. $rela=["accountid"=>$acccount,"account_info"=>$info];
  132. $rela_acc=Db::name("rela_account")->insert($rela);
  133. // $rele = [["video_id"=>$video,"accountid"=>$video,"addtime"=>$video]];
  134. $l=[];
  135. foreach ($video as $value){
  136. $temp=["video_id"=>$value,"accountid"=>$acccount,"addtime"=>date("Y-m-d H:i:s")];
  137. $l[]=$temp;
  138. }
  139. $rele_a= Db::name("rela_video")->insertAll($l);
  140. if($rele_a==false){
  141. Db::rollback();
  142. return error_show(1002,"绑定失败");
  143. }else{
  144. write_log("视频绑定成功",$this->userinfo,"account","add");
  145. }
  146. if($rela_acc){
  147. write_log("账户{$username}新建成功",$this->userinfo,"account","add");
  148. Db::commit();
  149. return app_show(0,"账户新建成功");
  150. }
  151. }
  152. }
  153. Db::rollback();
  154. return error_show(1005,"账户新建失败");
  155. }catch (\Exception $e){
  156. Db::rollback();
  157. return error_show(1003,$e->getMessage());
  158. }
  159. }
  160. /**@param id 账户id
  161. * @return \think\response\Json|void
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. */
  167. public function Read(){
  168. $id=isset($this->post['id'])&&$this->post["id"]!="" ? intval($this->post['id']):"";
  169. if($id==""){
  170. return error_show(1004,"参数id 不能为空");
  171. }
  172. $info = Db::name("account_list")->where(["id"=>$id])->find();
  173. if(empty($info)){
  174. return error_show(1005,"未找到数据");
  175. }
  176. if($info["is_del"]==1){
  177. return error_show(1005,"账户已被删除");
  178. }
  179. $info['status_n'] = $info['status']==0 ? "未激活": $info['status']==1? "已激活":"已失效";
  180. return app_show(0,"获取成功",$info);
  181. }
  182. /**
  183. * @param id
  184. * @param username
  185. * @param password
  186. * @param starttime
  187. * @param expiretime
  188. * @param nickname
  189. * @param remark
  190. * @param video
  191. */
  192. public function Save(){
  193. $id=isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']) :"";
  194. if($id==""){
  195. return error_show(1004,"参数id 不能为空");
  196. }
  197. $info= Db::name("account")->where(["is_del"=>0,"id"=>$id])->find();
  198. if(empty($info)){
  199. return error_show(1004,"未找到数据");
  200. }
  201. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  202. if($username!=""){
  203. $isT= Db::name("account")->where(["is_del"=>0,"username"=>$username,"id"=>["<>",$id]])->find();
  204. if($isT){
  205. return error_show(1004,"账户名已存在");
  206. }
  207. $info['username'] = $username;
  208. }
  209. $pasword = isset($this->post['password'])&&$this->post['password']!=="" ? trim($this->post['password']) :"";
  210. if($pasword!=""){
  211. $salt=makeSalt();
  212. $info['password']=sha1($pasword.$salt);
  213. $info['pwd']=$pasword;
  214. }
  215. $starttime = isset($this->post['starttime'])&&$this->post['starttime']!=="" ? $this->post['starttime'] :"";
  216. if($starttime!=""){
  217. $info['starttime'] = $starttime;
  218. }
  219. $expiretime = isset($this->post['expiretime'])&&$this->post['expiretime']!=="" ? $this->post['expiretime'] :"";
  220. if($expiretime!=""){
  221. $expire = strtotime($expiretime);
  222. if($expire>time()){
  223. $info['status'] = $info['activetime']==""? 0:1;
  224. }else{
  225. $info['status'] =2;
  226. }
  227. $info['expiretime'] = $expiretime;
  228. }
  229. $info['updatetime'] = date("Y-m-d H:i:s");
  230. $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();
  231. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  232. if($nickname!=""){
  233. $rela['nickname']=$nickname;
  234. }
  235. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  236. if($mobile!=""){
  237. $rela['mobile']=$mobile;
  238. }
  239. $rela['remark'] = isset($this->post['remark'])&&$this->post['remark']!=="" ? trim($this->post['remark']) :"";
  240. $video = isset($this->post['video'])&&$this->post['video']!=="" ? $this->post['video'] :"";
  241. $rela['updatetime'] = date("Y-m-d H:i:s");
  242. Db::startTrans();
  243. try {
  244. $acccount = Db::name("account")->update($info);
  245. if($acccount){
  246. $infoacc = Db::name("account_info")->update($rela);
  247. $del = Db::name('rela_video')->where(["is_del"=>0,"accountid"=>$id])->select();
  248. if($del==true){
  249. $dl = Db::name('rela_video')->where(["is_del"=>0,"accountid"=>$id])->update(["updatetime"=>date("Y-m-d H:i:s"),"is_del"=>1]);
  250. }
  251. //$relo=["video_id"=>$video,"accountid"=>$video,"addtime"=>date("Y-m-d H:i:s")];
  252. $k=[];
  253. foreach ($video as $valu){
  254. $temp=["video_id"=>$valu,"accountid"=>$id,"addtime"=>date("Y-m-d H:i:s")];
  255. $k[]=$temp;
  256. }
  257. $rele_a= Db::name("rela_video")->insertAll($k);
  258. if($rele_a==false){
  259. Db::rollback();
  260. return error_show(1002,"绑定失败");
  261. }else{
  262. write_log("视频绑定成功",$this->userinfo,"account","edit");
  263. }
  264. if($infoacc){
  265. write_log("账户{$username}新建成功",$this->userinfo,"account","edit");
  266. Db::commit();
  267. return app_show(0,"账户编辑成功");
  268. }else{
  269. Db::rollback();
  270. return error_show(1005,"账户编辑失败");
  271. }
  272. }
  273. Db::rollback();
  274. return error_show(1005,"账户编辑失败");
  275. }catch (\Exception $e){
  276. Db::rollback();
  277. return error_show(1003,$e->getMessage());
  278. }
  279. }
  280. }