Index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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['salt'].$params['password'])) ;
  58. $result = $this->model->save($params);
  59. if($this->callback){
  60. $callback=$this->callback;
  61. $callback($this->model);
  62. }
  63. Db::commit();
  64. } catch (\Exception $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. }
  68. if ($result === false) {
  69. $this->error(__('没有新增任何数据'));
  70. }
  71. $this->success();
  72. }
  73. /**
  74. * 编辑
  75. */
  76. #[Route('GET,POST','edit')]
  77. public function edit(mixed $row=null)
  78. {
  79. $ids = $this->request->get('ids');
  80. if(!$row || is_array($row)){
  81. $row = $this->model->find($ids);
  82. }
  83. if (!$row) {
  84. $this->error(__('没有找到记录'));
  85. }
  86. if(count($this->volidateFields)>0){
  87. foreach ($this->volidateFields as $field=>$value){
  88. if($row[$field]!=$value){
  89. $this->error(__('没有操作权限'));
  90. }
  91. }
  92. }
  93. if (false === $this->request->isPost()) {
  94. $this->assign('row', $row);
  95. return $this->fetch();
  96. }
  97. $params = array_merge($this->request->post("row/a"),$this->postParams);
  98. if (empty($params)) {
  99. $this->error(__('提交的参数不能为空'));
  100. }
  101. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  102. $this->error(__('token错误,请刷新页面重试'));
  103. }
  104. foreach ($params as &$value){
  105. if(is_array($value)){
  106. $value=implode(',',$value);
  107. }
  108. if($value===''){
  109. $value=null;
  110. }
  111. }
  112. $result = false;
  113. Db::startTrans();
  114. try {
  115. if(!empty($params['password'])) {
  116. $params['salt'] = str_rand(4);
  117. $params['password']= md5(md5($params['salt'].$params['password'])) ;
  118. }
  119. $result = $row->save($params);
  120. if($this->callback){
  121. $callback=$this->callback;
  122. $callback($row);
  123. }
  124. Db::commit();
  125. } catch (\Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if (false === $result) {
  130. $this->error(__('没有数据被更新'));
  131. }
  132. $this->success();
  133. }
  134. #[Route('POST,GET','recharge')]
  135. public function recharge($ids)
  136. {
  137. if($this->request->isPost()){
  138. $module_type=$this->request->post('row.module_type');
  139. $change_type=$this->request->post('row.recharge_type');
  140. $change=$this->request->post('row.change/d');
  141. $remark=$this->request->post('row.remark');
  142. $order_no=time().rand(1000,9999);
  143. switch ($module_type){
  144. case 'score':
  145. UserLog::addScoreLog($ids,$change_type,$change,$order_no,$remark);
  146. break;
  147. case 'balance':
  148. UserLog::addBalanceLog($ids,$change_type,$change,$order_no,$remark);
  149. break;
  150. }
  151. $this->success();
  152. }else{
  153. $user=User::find($ids);
  154. $this->assign('moduletype',UserLog::TYPE);
  155. $this->assign('user',$user);
  156. return $this->fetch();
  157. }
  158. }
  159. #[Route('GET,JSON','detail')]
  160. public function detail($ids)
  161. {
  162. if($this->request->isAjax()){
  163. $this->model=new UserLog();
  164. $where=[];
  165. $where[]=['type','=',$this->request->get('type')];
  166. $where[]=['user_id','=',$ids];
  167. [$where, $order, $limit, $with] = $this->buildparams($where);
  168. $list = $this->model
  169. ->where($where)
  170. ->order($order)
  171. ->paginate($limit);
  172. $result = ['total' => $list->total(), 'rows' => $list->items()];
  173. return json($result);
  174. }else{
  175. $user=User::find($ids);
  176. $this->assign('moduletype',UserLog::TYPE);
  177. $this->assign('user',$user);
  178. return $this->fetch();
  179. }
  180. }
  181. }