assignconfig(["admin" => $this->auth->getUserInfo()]); $this->model = model('User'); } /** * 查看 */ public function index() { //设置过滤方法 $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(); $list = $this->model ->with(['group','store', 'parent']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $k => $v) { $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname); $v->hidden(['password', 'salt']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $this->token(); } return parent::add(); } /** * 编辑 */ public function edit($ids = null) { if ($this->request->isPost()) { $this->token(); } $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker'])); return parent::edit($ids); } /** * 删除 */ public function del($ids = "") { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } Auth::instance()->delete($row['id']); $this->success(); } public function wallet($id = null) { if (!$id) $this->error("ID 为空!"); $w = $this->model->get($id); if (!$w) $this->error("用户不存在!"); $wallet = (new \app\api\model\user\Wallet())->getUserWallet($id); $bill = (new \app\api\model\user\Bill())->where("user_id", $id)->limit(0, 100)->order("createtime", "desc")->select(); $this->assign("wallet", $wallet); $this->assign("rows", $bill); return $this->view->fetch(); } /** * 充值 */ public function recharge($ids = null) { $admin = $this->auth->getUserInfo(); if (!$admin || $admin["id"] !== 1) $this->error("您没有权限充值余额!"); $user = $this->model->get($ids); if (!$user) $this->error("不存在!"); if ($this->request->post()) { $params = $this->request->param(); if ($admin["recharge_password"] !== md5(md5($params["recharge_password"]) . $admin["salt"])) $this->error("密码错误,充值失败!"); $change = 0; if (isset($params["change"])) { $change = $params["change"]; if ($change < -100000 || $change > 100000) $this->error("充值的金额必须在 -100000 ~ 100000 之前!"); } if ($change == 0) $this->error("无意义的操作!"); $uWalletModel = new \app\api\model\user\Wallet(); $redLock = RedLock::of(); $lock = $redLock->lock(\app\api\model\user\Wallet::UWKey($user["id"])); if (!is_array($lock)) $this->error("资源竞争中, 请稍后再试!"); $uWallet = $uWalletModel->getUserWallet($user["id"]); $total_money = $uWallet["money"] + $uWallet["give_money"]; if ($total_money + $change < 0) { $this->error("用户余额剩余:{$total_money}"); } Db::startTrans(); try { if ($change > 0) { // + $uWalletModel->where("id", $uWallet["id"])->setInc("give_money", $change); (new \app\admin\model\user\Bill())->save([ "user_id" => $user["id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::AdminRecharge[0], "change" => $change, "before" => $uWallet->money + $uWallet->give_money, "after" => ($uWallet->money + $uWallet->give_money) + $change, "reason" => \E_USER_BILL_CHANGE_TYPE::AdminRecharge[1], "money" => 0, "give_money" => $change, "createtime" => time() ]); } else { // - $abs_change = abs($change); $log = [ "user_id" => $user["id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::AdminReduce[0], "change" => $change, "before" => $uWallet->money + $uWallet->give_money, "after" => ($uWallet->money + $uWallet->give_money) + $change, "reason" => \E_USER_BILL_CHANGE_TYPE::AdminReduce[1], "money" => 0, "give_money" => 0, "createtime" => time() ]; if ($uWallet["money"] > $abs_change) { $log["money"] = $change; $uWalletModel->where("id", $uWallet["id"])->setDec("money", $abs_change); } else { $log["money"] = -$uWallet["money"]; $log["give_money"] = $change + $uWallet["money"]; $uWalletModel->where("id", $uWallet["id"])->setDec("money", $uWallet["money"]); $uWalletModel->where("id", $uWallet["id"])->setDec("give_money", $abs_change - $uWallet["money"]); } (new \app\admin\model\user\Bill())->save($log); } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } finally { $redLock->unlock($lock); } $this->success(); } $this->view->assign([ "user" => $user ]); return $this->view->fetch(); } public function deduct($id) { } }