Index.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare (strict_types = 1);
  11. namespace app\admin\controller\user;
  12. use app\common\controller\Backend;
  13. use think\annotation\route\Group;
  14. use app\admin\traits\Actions;
  15. use app\common\model\User;
  16. use app\common\model\UserLog;
  17. use think\annotation\route\Route;
  18. use think\facade\Db;
  19. #[Group("user/index")]
  20. class Index extends Backend
  21. {
  22. protected $noNeedRight = ['index'];
  23. use Actions;
  24. protected function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new User();
  28. }
  29. /**
  30. * 添加
  31. */
  32. #[Route('GET,POST','add')]
  33. public function add()
  34. {
  35. if (false === $this->request->isPost()) {
  36. return $this->fetch();
  37. }
  38. $params = array_merge($this->request->post("row/a"),$this->postParams);
  39. if (empty($params)) {
  40. $this->error(__('提交的参数不能为空'));
  41. }
  42. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  43. $this->error(__('token错误,请刷新页面重试'));
  44. }
  45. foreach ($params as &$value){
  46. if(is_array($value)){
  47. $value=implode(',',$value);
  48. }
  49. if($value===''){
  50. $value=null;
  51. }
  52. }
  53. $result = false;
  54. Db::startTrans();
  55. try {
  56. $params['salt'] = str_rand(4);
  57. $params['password']= md5(md5($params['password'].$params['salt']));
  58. $params['avatar'] = empty($params['avatar'])? request()->domain().'/assets/img/logo.jpg': $params['avatar'];//默认头像
  59. $result = $this->model->save($params);
  60. if($this->callback){
  61. $callback=$this->callback;
  62. $callback($this->model);
  63. }
  64. Db::commit();
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. $this->error($e->getMessage());
  68. }
  69. if ($result === false) {
  70. $this->error(__('没有新增任何数据'));
  71. }
  72. $this->success();
  73. }
  74. /**
  75. * 编辑
  76. */
  77. #[Route('GET,POST','edit')]
  78. public function edit(mixed $row=null)
  79. {
  80. $ids = $this->request->get('ids');
  81. if(!$row || is_array($row)){
  82. $row = $this->model->find($ids);
  83. }
  84. if (!$row) {
  85. $this->error(__('没有找到记录'));
  86. }
  87. if(count($this->volidateFields)>0){
  88. foreach ($this->volidateFields as $field=>$value){
  89. if($row[$field]!=$value){
  90. $this->error(__('没有操作权限'));
  91. }
  92. }
  93. }
  94. if (false === $this->request->isPost()) {
  95. $this->assign('row', $row);
  96. return $this->fetch();
  97. }
  98. $params = array_merge($this->request->post("row/a"),$this->postParams);
  99. if (empty($params)) {
  100. $this->error(__('提交的参数不能为空'));
  101. }
  102. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  103. $this->error(__('token错误,请刷新页面重试'));
  104. }
  105. foreach ($params as &$value){
  106. if(is_array($value)){
  107. $value=implode(',',$value);
  108. }
  109. if($value===''){
  110. $value=null;
  111. }
  112. }
  113. $result = false;
  114. Db::startTrans();
  115. try {
  116. if(!empty($params['password'])) {
  117. $params['salt'] = str_rand(4);
  118. $params['password']= md5(md5($params['password'].$params['salt'])) ;
  119. }else{
  120. unset($params['password']);
  121. }
  122. $params['avatar'] = empty($params['avatar'])? request()->domain().'/assets/img/logo.jpg': $params['avatar'];//默认头像
  123. $result = $row->save($params);
  124. if($this->callback){
  125. $callback=$this->callback;
  126. $callback($row);
  127. }
  128. Db::commit();
  129. } catch (\Exception $e) {
  130. Db::rollback();
  131. $this->error($e->getMessage());
  132. }
  133. if (false === $result) {
  134. $this->error(__('没有数据被更新'));
  135. }
  136. $this->success();
  137. }
  138. #[Route('POST,GET','recharge')]
  139. public function recharge($ids)
  140. {
  141. if($this->request->isPost()){
  142. $module_type=$this->request->post('row.module_type');
  143. $change_type=$this->request->post('row.recharge_type');
  144. $change=$this->request->post('row.change/d');
  145. $remark=$this->request->post('row.remark');
  146. $order_no=time().rand(1000,9999);
  147. switch ($module_type){
  148. case 'score':
  149. UserLog::addScoreLog($ids,$change_type,$change,$order_no,$remark);
  150. break;
  151. case 'balance':
  152. UserLog::addBalanceLog($ids,$change_type,$change,$order_no,$remark);
  153. break;
  154. }
  155. $this->success();
  156. }else{
  157. $user=User::find($ids);
  158. $this->assign('moduletype',UserLog::TYPE);
  159. $this->assign('user',$user);
  160. return $this->fetch();
  161. }
  162. }
  163. #[Route('GET,JSON','detail')]
  164. public function detail($ids)
  165. {
  166. if($this->request->isAjax()){
  167. $this->model=new UserLog();
  168. $where=[];
  169. $where[]=['type','=',$this->request->get('type')];
  170. $where[]=['user_id','=',$ids];
  171. [$where, $order, $limit, $with] = $this->buildparams($where);
  172. $list = $this->model
  173. ->where($where)
  174. ->order($order)
  175. ->paginate($limit);
  176. $result = ['total' => $list->total(), 'rows' => $list->items()];
  177. return json($result);
  178. }else{
  179. $user=User::find($ids);
  180. $this->assign('moduletype',UserLog::TYPE);
  181. $this->assign('user',$user);
  182. return $this->fetch();
  183. }
  184. }
  185. }