request->only([ 'type' ]); // 验证参数 $validate = new \app\api\validate\user\Account(); if (!$validate->scene('index')->check($params)) { $this->error($validate->getError()); } $user = auth_user(); $where = [ 'user_id' => $user->id ]; if (!empty($params['type'])) { $where['type'] = $params['type']; } $data = AccountModel::where($where)->order('updatetime desc')->find(); if (!$data) { $this->error(__('No Results were found')); } $this->success('获取成功', $data); } public function save() { $user = auth_user(); $params = $this->request->only([ 'type', 'account_name', 'account_header', 'account_no' ]); // 使用验证器验证基础参数 $validate = new \app\api\validate\user\Account(); if (!$validate->scene($params['type'])->check($params)) { $this->error($validate->getError()); } // 根据账户类型设置默认值 if ($params['type'] === 'alipay') { $params['account_header'] = '支付宝账户'; } if ($params['type'] === 'wechat') { $params['account_header'] = '微信账户'; $params['account_no'] = '-'; } $data = AccountModel::where(['user_id' => $user->id, 'type' => $params['type']])->find(); if (!$data) { $data = AccountModel::create([ 'user_id' => $user->id, 'type' => $params['type'], 'account_name' => $params['account_name'], 'account_header' => $params['account_header'], 'account_no' => $params['account_no'], ]); } else { $data->save($params); } $this->success('保存成功', $data); } }