User.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\controller\admin;
  3. use laytp\controller\Backend;
  4. use think\facade\Config;
  5. use laytp\library\CommonFun;
  6. use laytp\library\Str;
  7. use laytp\library\UploadDomain;
  8. use think\facade\Db;
  9. use app\service\BillServiceFacade;
  10. /**
  11. * 会员管理
  12. */
  13. class User extends Backend
  14. {
  15. /**
  16. * member模型对象
  17. * @var \app\model\Member
  18. */
  19. protected $model;
  20. protected $hasSoftDel=1;//是否拥有软删除功能
  21. protected $noNeedLogin = []; // 无需登录即可请求的方法
  22. protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法
  23. public function _initialize()
  24. {
  25. $this->model = new \app\model\User();
  26. }
  27. //查看和搜索列表
  28. public function index(){
  29. global $_W;
  30. $where = $this->buildSearchParams();
  31. // $where[] = ['nickname','<>',''];
  32. $where[] = ['uniacid','=',$_W['uniacid']];
  33. $order = $this->buildOrder();
  34. $data = $this->model->where($where)->order($order)->with(['avatar_pic_file'])->withCount(['question','commission1','commission2','commission3']);
  35. $paging = $this->request->param('paging', false);
  36. if ($paging) {
  37. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  38. $data = $data->paginate($limit)->toArray();
  39. $data['data'] = $this->getSelectedData($data['data']);
  40. } else {
  41. $data = $data->select()->toArray();
  42. }
  43. return $this->success('数据获取成功', $data);
  44. }
  45. //添加
  46. public function add()
  47. {
  48. $post = CommonFun::filterPostData($this->request->post());
  49. $validate = new Add();
  50. if(!$validate->check($post)){
  51. return $this->error($validate->getError());
  52. }
  53. if(isset($post['password']) && $post['password']) $post['password'] = Str::createPassword($post['password']);
  54. try {
  55. if ($this->model->create($post)) {
  56. return $this->success('添加成功', $post);
  57. } else {
  58. return $this->error('操作失败');
  59. }
  60. } catch (\Exception $e) {
  61. return $this->exceptionError($e);
  62. }
  63. }
  64. //查看详情
  65. public function info()
  66. {
  67. $id = $this->request->param('id');
  68. $info = $this->model->with(['avatar_pic_file'])->find($id);
  69. return $this->success('获取成功', $info);
  70. }
  71. //编辑
  72. public function edit(){
  73. $id = $this->request->param('id');
  74. $info = $this->model->find($id);
  75. $post = CommonFun::filterPostData($this->request->post());
  76. $validate = new Edit();
  77. if(!$validate->check($post)){
  78. return $this->error($validate->getError());
  79. }
  80. if(!$post['password']){
  81. unset($post['password']);
  82. }else{
  83. $post['password'] = Str::createPassword($post['password']);
  84. }
  85. foreach ($post as $k => $v) {
  86. $info->$k = $v;
  87. }
  88. try {
  89. $updateRes = $info->save();
  90. if ($updateRes) {
  91. return $this->success('编辑成功');
  92. } else {
  93. return $this->error('操作失败');
  94. }
  95. } catch (\Exception $e) {
  96. return $this->exceptionError($e);
  97. }
  98. }
  99. //设置账号状态
  100. public function setStatus()
  101. {
  102. $id = $this->request->post('id');
  103. $fieldVal = $this->request->post('field_val');
  104. $isRecycle = $this->request->post('is_recycle');
  105. $update['status'] = $fieldVal;
  106. try {
  107. if($isRecycle) {
  108. $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
  109. } else {
  110. $updateRes = $this->model->where('id', '=', $id)->update($update);
  111. }
  112. if ($updateRes) {
  113. return $this->success('操作成功');
  114. } else if ($updateRes === 0) {
  115. return $this->success('未作修改');
  116. } else {
  117. return $this->error('操作失败');
  118. }
  119. } catch (\Exception $e) {
  120. return $this->error('数据库异常,操作失败');
  121. }
  122. }
  123. public function recharge()
  124. {
  125. global $_W;
  126. $pm = 0;//0支出,1获得
  127. $id = $this->request->post('id');
  128. $status = $this->request->post('status',0);
  129. $number = $this->request->post('number',0);
  130. $mark = $this->request->post('mark','');
  131. if(!$id){
  132. return $this->error('未获得用户id');
  133. }
  134. $info = $this->model->find($id);
  135. if(!$info){
  136. return $this->error('未查询到此用户信息');
  137. }
  138. if($status == 0){
  139. $pm = 1;
  140. $balance = $info['coin'] + $number;
  141. $change = $balance;
  142. }elseif($status == 1){
  143. if($number > $info['coin']){
  144. return $this->error('请填写小于当前剩余次数' . $number .'的数字');
  145. }
  146. $balance = $info['coin'] - $number;
  147. $change = $balance;
  148. }else{
  149. $balance = $number;
  150. if($number == $info['coin']){
  151. return $this->error('请填写不等于当前剩余次数的数字');
  152. }
  153. if($number > $info['coin']){
  154. $pm = 1;
  155. $change = $number - $info['coin'];
  156. }else{
  157. $pm = 0;
  158. $change = $info['coin'] - $number;
  159. }
  160. // 以下为写入Bill需要赋的值
  161. $number = $change;
  162. }
  163. $update['coin'] = $balance;
  164. Db::startTrans();
  165. try{
  166. $updateRes = $this->model->where('id', '=', $id)->update($update);
  167. if (!$updateRes) return $this->error('操作失败');
  168. Db::commit();
  169. BillServiceFacade::record($pm,$number,'pay_member','后台充值(变更)积分'.$mark,$id,$_W['uniacid'],true,$balance);
  170. return $this->success('操作成功');
  171. }catch (\Exception $e) {
  172. Db::rollback();
  173. return $this->error('数据库异常,操作失败');
  174. }
  175. }
  176. }