Users.php 4.8 KB
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Users extends Backend
{
    
    /**
     * Users模型对象
     * @var \app\admin\model\Users
     */
    protected $model = null;
    protected $searchFields = 'nickname,name,card,mobile';
    protected $multiFields = "type";

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Users;
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['third'])
                    ->where($where)
                    ->where(['type'=>['in',[1,2]]])
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['third'])
                    ->where($where)
                    ->where(['type'=>['in',[1,2]]])
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();
            foreach ($list as $key => $row) {
                $list[$key]['parent_nickname'] = Db::name('user')->where(['id'=>$row['parent_id']])->value('nickname');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 赠送、扣除余额
     */
    public function money(){
        $admin_id = $this->auth->id;
        $admin = Db::name('admin')->where(['id'=>$admin_id])->find();
        if($this->request->isPost()){
            $param = $this->request->param();
            $validate = new \think\Validate([
                'admin_id' => 'require',
                'money' => 'require',
                'type' => 'require',
            ]);
            $validate->message([
                'admin_id' => '商户id不能为空',
                'money' => '变动金额不能为空',
                'type' => '类型不能为空',
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            $admin = Db::name('admin')->where(['id'=>$admin_id])->find();
            if($param['type'] == 7){
                $after_money = $param['money'] + $admin['money'];
            }else if($param['type'] == 8){
                if($admin['money'] < $param['money']){
                    $this->error('用户余额不足');
                }
                $after_money = $admin['money'] - $param['money'];
            }else{
                $this->error('未知类型');
                $after_money = 0;
            }
            $arr['admin_id'] = $admin_id;
            $arr['before_money'] = $admin['money'];
            $arr['money'] = $param['money'];
            $arr['after_money'] = $after_money;
            $arr['type'] = $param['type'];
            $arr['createtime'] = time();
            Db::startTrans();
            $result1 = Db::name('user_money_log')->insert($arr);
            if(empty($result1)){
                Db::rollback();
                $this->error('sql执行失败');
            }
            if($param['type'] == 7){
                $result2 = Db::name('admin')->where(['id'=>$admin_id])->setInc('money',$param['money']);
            }else if($param['type'] == 8){
                $result2 = Db::name('admin')->where(['id'=>$admin_id])->setDec('money',$param['money']);
            }else{
                Db::rollback();
                $this->error('未知类型');
                $result2 = 0;
            }
            if(empty($result2)){
                Db::rollback();
                $this->error('sql执行失败');
            }
            Db::commit();
            $this->success();
        }else{
            $this->assign('admin',$admin);
            return $this->fetch();
        }
    }
}