123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace app\controller\admin;
- use laytp\controller\Backend;
- use think\facade\Config;
- use laytp\library\CommonFun;
- use laytp\library\Str;
- use laytp\library\UploadDomain;
- use think\facade\Db;
- use app\service\BillServiceFacade;
- /**
- * 会员管理
- */
- class User extends Backend
- {
- /**
- * member模型对象
- * @var \app\model\Member
- */
- protected $model;
- protected $hasSoftDel=1;//是否拥有软删除功能
- protected $noNeedLogin = []; // 无需登录即可请求的方法
- protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法
- public function _initialize()
- {
- $this->model = new \app\model\User();
- }
- //查看和搜索列表
- public function index(){
- global $_W;
- $where = $this->buildSearchParams();
- // $where[] = ['nickname','<>',''];
- $where[] = ['uniacid','=',$_W['uniacid']];
- $order = $this->buildOrder();
- $data = $this->model->where($where)->order($order)->with(['avatar_pic_file'])->withCount(['question','commission1','commission2','commission3']);
- $paging = $this->request->param('paging', false);
- if ($paging) {
- $limit = $this->request->param('limit', Config::get('paginate.limit'));
- $data = $data->paginate($limit)->toArray();
- $data['data'] = $this->getSelectedData($data['data']);
- } else {
- $data = $data->select()->toArray();
- }
- return $this->success('数据获取成功', $data);
- }
- //添加
- public function add()
- {
- $post = CommonFun::filterPostData($this->request->post());
- $validate = new Add();
- if(!$validate->check($post)){
- return $this->error($validate->getError());
- }
- if(isset($post['password']) && $post['password']) $post['password'] = Str::createPassword($post['password']);
- try {
- if ($this->model->create($post)) {
- return $this->success('添加成功', $post);
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //查看详情
- public function info()
- {
- $id = $this->request->param('id');
- $info = $this->model->with(['avatar_pic_file'])->find($id);
- return $this->success('获取成功', $info);
- }
- //编辑
- public function edit(){
- $id = $this->request->param('id');
- $info = $this->model->find($id);
- $post = CommonFun::filterPostData($this->request->post());
- $validate = new Edit();
- if(!$validate->check($post)){
- return $this->error($validate->getError());
- }
- if(!$post['password']){
- unset($post['password']);
- }else{
- $post['password'] = Str::createPassword($post['password']);
- }
- foreach ($post as $k => $v) {
- $info->$k = $v;
- }
- try {
- $updateRes = $info->save();
- if ($updateRes) {
- return $this->success('编辑成功');
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //设置账号状态
- public function setStatus()
- {
- $id = $this->request->post('id');
- $fieldVal = $this->request->post('field_val');
- $isRecycle = $this->request->post('is_recycle');
- $update['status'] = $fieldVal;
- try {
- if($isRecycle) {
- $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
- } else {
- $updateRes = $this->model->where('id', '=', $id)->update($update);
- }
- if ($updateRes) {
- return $this->success('操作成功');
- } else if ($updateRes === 0) {
- return $this->success('未作修改');
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->error('数据库异常,操作失败');
- }
- }
- public function recharge()
- {
- global $_W;
- $pm = 0;//0支出,1获得
- $id = $this->request->post('id');
- $status = $this->request->post('status',0);
- $number = $this->request->post('number',0);
- $mark = $this->request->post('mark','');
- if(!$id){
- return $this->error('未获得用户id');
- }
- $info = $this->model->find($id);
- if(!$info){
- return $this->error('未查询到此用户信息');
- }
- if($status == 0){
- $pm = 1;
- $balance = $info['coin'] + $number;
- $change = $balance;
- }elseif($status == 1){
- if($number > $info['coin']){
- return $this->error('请填写小于当前剩余次数' . $number .'的数字');
- }
- $balance = $info['coin'] - $number;
- $change = $balance;
- }else{
- $balance = $number;
- if($number == $info['coin']){
- return $this->error('请填写不等于当前剩余次数的数字');
- }
- if($number > $info['coin']){
- $pm = 1;
- $change = $number - $info['coin'];
- }else{
- $pm = 0;
- $change = $info['coin'] - $number;
- }
- // 以下为写入Bill需要赋的值
- $number = $change;
- }
- $update['coin'] = $balance;
- Db::startTrans();
- try{
- $updateRes = $this->model->where('id', '=', $id)->update($update);
- if (!$updateRes) return $this->error('操作失败');
- Db::commit();
- BillServiceFacade::record($pm,$number,'pay_member','后台充值(变更)积分'.$mark,$id,$_W['uniacid'],true,$balance);
- return $this->success('操作成功');
- }catch (\Exception $e) {
- Db::rollback();
- return $this->error('数据库异常,操作失败');
- }
- }
- }
|