Account.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  46. }
  47. /**
  48. * @param username
  49. * @param password
  50. * @param starttime
  51. * @param expiretime
  52. * @param nickname
  53. * @param remark
  54. * @param video
  55. */
  56. public function Create(){
  57. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  58. if($username==""){
  59. return error_show(1004,"参数username 不能为空");
  60. }
  61. if(!checkAccount($username)){
  62. return error_show(1004,"账户格式不正确");
  63. }
  64. $isT= Db::name("account")->where(["is_del"=>0,"username"=>$username])->find();
  65. if($isT){
  66. return error_show(1004,"账户名已存在");
  67. }
  68. $pasword = isset($this->post['password'])&&$this->post['password']!=="" ? trim($this->post['password']) :"";
  69. if($pasword==""){
  70. return error_show(1004,"参数password 不能为空");
  71. }
  72. if(!checkPasswd($pasword)){
  73. return error_show(1004,"密码格式不正确");
  74. }
  75. $starttime = isset($this->post['starttime'])&&$this->post['starttime']!=="" ? $this->post['starttime'] :"";
  76. if($starttime==""){
  77. return error_show(1004,"参数starttime 不能为空");
  78. }
  79. $expiretime = isset($this->post['expiretime'])&&$this->post['expiretime']!=="" ? $this->post['expiretime'] :"";
  80. if($expiretime==""){
  81. return error_show(1004,"参数expiretime 不能为空");
  82. }
  83. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  84. // if($nickname==""){
  85. // return error_show(1004,"参数nickname 不能为空");
  86. // }
  87. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  88. // if($mobile==""){
  89. // return error_show(1004,"参数mobile 不能为空");
  90. // }
  91. $remark = isset($this->post['remark'])&&$this->post['remark']!=="" ? trim($this->post['remark']) :"";
  92. $video = isset($this->post['video'])&&$this->post['video']!=="" ? trim($this->post['video']) :"";
  93. Db::startTrans();
  94. try {
  95. $salt = makeSalt();
  96. $pas = sha1($pasword.$salt);
  97. $data=[
  98. "username"=>$username,
  99. "password"=>$pas,
  100. "pwd"=>$pasword,
  101. "salt"=>$salt,
  102. "status"=>0,
  103. "is_del"=>0,
  104. "starttime"=>$starttime,
  105. "expiretime"=>$expiretime,
  106. "addtime"=>date("Y-m-d H:i:s"),
  107. "updatetime"=>date("Y-m-d H:i:s")
  108. ];
  109. $acccount = Db::name("account")->insert($data,false,true);
  110. if($acccount>0){
  111. $user=[
  112. "nickname"=>$nickname,
  113. "mobile"=>$mobile,
  114. "avatar"=>"",
  115. "remark"=>$remark,
  116. "sex"=>"",
  117. "addtime"=>date("Y-m-d H:i:s"),
  118. "updatetime"=>date("Y-m-d H:i:s")
  119. ];
  120. $info = Db::name("account_info")->insert($user,false,true);
  121. if($info>0){
  122. $rela=["accountid"=>$acccount,"account_info"=>$info];
  123. $rela_acc=Db::name("rela_account")->insert($rela);
  124. if($rela_acc){
  125. write_log("账户{$username}新建成功",$this->userinfo,"account","add");
  126. Db::commit();
  127. return app_show(0,"账户新建成功");
  128. }
  129. }
  130. }
  131. Db::rollback();
  132. return error_show(1005,"账户新建失败");
  133. }catch (\Exception $e){
  134. Db::rollback();
  135. return error_show(1003,$e->getMessage());
  136. }
  137. }
  138. /**@param id 账户id
  139. * @return \think\response\Json|void
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public function Read(){
  146. $id=isset($this->post['id'])&&$this->post["id"]!="" ? intval($this->post['id']):"";
  147. if($id==""){
  148. return error_show(1004,"参数id 不能为空");
  149. }
  150. $info = Db::name("account_list")->where(["id"=>$id])->find();
  151. if(empty($info)){
  152. return error_show(1005,"未找到数据");
  153. }
  154. if($info["is_del"]==1){
  155. return error_show(1005,"账户已被删除");
  156. }
  157. $info['status_n'] = $info['status']==0 ? "未激活": $info['status']==1? "已激活":"已失效";
  158. return app_show(0,"获取成功",$info);
  159. }
  160. /**
  161. * @param id
  162. * @param username
  163. * @param password
  164. * @param starttime
  165. * @param expiretime
  166. * @param nickname
  167. * @param remark
  168. * @param video
  169. */
  170. public function Save(){
  171. $id=isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']) :"";
  172. if($id==""){
  173. return error_show(1004,"参数id 不能为空");
  174. }
  175. $info= Db::name("account")->where(["is_del"=>0,"id"=>$id])->find();
  176. if(empty($info)){
  177. return error_show(1004,"未找到数据");
  178. }
  179. $username = isset($this->post['username'])&&$this->post['username']!=="" ? trim($this->post['username']) :"";
  180. if($username!=""){
  181. $isT= Db::name("account")->where(["is_del"=>0,"username"=>$username,"id"=>["<>",$id]])->find();
  182. if($isT){
  183. return error_show(1004,"账户名已存在");
  184. }
  185. $info['username'] = $username;
  186. }
  187. $pasword = isset($this->post['password'])&&$this->post['password']!=="" ? trim($this->post['password']) :"";
  188. if($pasword!=""){
  189. $salt=makeSalt();
  190. $info['password']=sha1($pasword.$salt);
  191. $info['pwd']=$pasword;
  192. }
  193. $starttime = isset($this->post['starttime'])&&$this->post['starttime']!=="" ? $this->post['starttime'] :"";
  194. if($starttime!=""){
  195. $info['starttime'] = $starttime;
  196. }
  197. $expiretime = isset($this->post['expiretime'])&&$this->post['expiretime']!=="" ? $this->post['expiretime'] :"";
  198. if($expiretime!=""){
  199. $expire = strtotime($expiretime);
  200. if($expire>time()){
  201. $info['status'] = $info['activetime']==""? 0:1;
  202. }else{
  203. $info['status'] =2;
  204. }
  205. $info['expiretime'] = $expiretime;
  206. }
  207. $info['updatetime'] = date("Y-m-d H:i:s");
  208. $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();
  209. $nickname = isset($this->post['nickname'])&&$this->post['nickname']!=="" ? trim($this->post['nickname']) :"";
  210. if($nickname!=""){
  211. $rela['nickname']=$nickname;
  212. }
  213. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!=="" ? trim($this->post['mobile']) :"";
  214. if($mobile!=""){
  215. $rela['mobile']=$mobile;
  216. }
  217. $rela['remark'] = isset($this->post['remark'])&&$this->post['remark']!=="" ? trim($this->post['remark']) :"";
  218. $video = isset($this->post['video'])&&$this->post['video']!=="" ? trim($this->post['video']) :"";
  219. $rela['updatetime'] = date("Y-m-d H:i:s");
  220. Db::startTrans();
  221. try {
  222. $acccount = Db::name("account")->update($info);
  223. if($acccount){
  224. $infoacc = Db::name("account_info")->update($rela);
  225. if($infoacc){
  226. write_log("账户{$username}新建成功",$this->userinfo,"account","edit");
  227. Db::commit();
  228. return app_show(0,"账户编辑成功");
  229. }else{
  230. Db::rollback();
  231. return error_show(1005,"账户编辑失败");
  232. }
  233. }
  234. Db::rollback();
  235. return error_show(1005,"账户编辑失败");
  236. }catch (\Exception $e){
  237. Db::rollback();
  238. return error_show(1003,$e->getMessage());
  239. }
  240. }
  241. }