Store.php 5.8 KB
<?php

namespace app\admin\controller\store;

use app\admin\model\Admin;
use app\admin\model\AuthGroupAccess;
use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 商家入驻订单管理
 *
 * @icon fa fa-circle-o
 */
class Store extends Backend
{
    
    /**
     * Store模型对象
     * @var \app\admin\model\store\Store
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\store\Store;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有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', 'trim']);
        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(['admin','user','industry'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['admin','user','industry'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['nickname']);
				$row->getRelation('user')->visible(['nickname']);
				$row->getRelation('industry')->visible(['name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 审核
     */
    public function examine($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $admin_model = new Admin();
                $auth_group_model = new AuthGroupAccess();
                if($params['status'] == 3) {
                    $salt = Random::alnum();
                    $password = md5(md5($params['password']) . $salt);
                    $admin_data = [
                        'username' => $params['username'],
                        'nickname' => $params['nickname'],
                        'salt' => $salt,
                        'password' => $password
                    ];
                    $auth_group_data = [
                        'group_id' => 4,
                    ];
                }
                $result = false;
                $result_admin = $result_auth_group = true;
                Db::startTrans();
                try {
                    $result = $row->allowField(true)->save($params);
                    // 判断审核状态
                    if($params['status'] == 3) {
                        // 审核通过,新增商家管理员
                        $result_admin = $admin_model->isUpdate(false)->save($admin_data);
                        $auth_group_data['uid'] = $admin_model->id;
                        $result_auth_group = $auth_group_model->isUpdate(false)->save($auth_group_data);
                    }
                    if($params['status'] == 4) {
                        // 审核不通过,执行退款操作
                        $refund = [
                            'order_id' => $row->id,
                            'user_id' => $row->user_id,
                            'order_sn' => $row->order_sn,
                            'transaction_id' => $row->transaction_id,
                            'out_refund_no' => date('Ymd').substr($row->order_sn,-8),
                            'price' => $row->money
                        ];
                        $this->refund($refund);
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false && $result_admin !== false && $result_auth_group !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }
}