一、操作流程图
二、后端代码实现
1、题库实现
a、列表、所有、详情、保存、启禁用、导入答题
b、获取奖品信息、保存奖品信息、
class Question extends Base
{
// 列表
public function getList(){
$param = $this->request->param();
$where = [];
if(!empty($param['title']))$where[] = ['title','like','%'.$param['title'].'%'];
if(!empty($param['id']))$where[] = ['id','=',$param['id']];
if(!empty($param['status']))$where[] = ['status','=',$param['status'] == 1 ? 1 : 0];
$resList = AQBModel::getList($where, 'id,title,option_number,status,create_time,update_time',[$param['currentPage'], $param['currentSize']]);
$list = $resList['info'];
$res['list'] = $list;
$res['meat'] = $resList['page'];
return success($res);
}
// 所有
public function getAll(){
$param = $this->request->param();
$where = [];
if(!empty($param['title']))$where[] = ['title','like','%'.$param['title'].'%'];
$list = AQBModel::getAll($where, 'id,title','id DESC');
return success($list);
}
// 详情
public function getInfo(){
$id = $this->request->param('id',0);
if(empty($id)) return error('主键不能为空');
$data = AQBModel::dataFind([['id','=',$id]],'id,title,status',true);
if(!empty($data))$data['quest_opt'] = AQOModel::getAll(['bank_id' => $id],'content,is_correct','id DESC');
return success($data);
}
// 保存
public function saveInfo(){
$param = $this->request->param();
$validateBank = new \app\validate\anxun\admin\activity\QuestionBank;
if (!$validateBank->check($param)) return error($validateBank->getError());//验证参数
$validateOpt = new \app\validate\anxun\admin\activity\QuestionOpt;
$dataOpt = [];
foreach($param['quest_opt'] as $k => $v){
$dataOpt[$k]['content'] = $v['content'];
$dataOpt[$k]['is_correct'] = $v['is_correct'];
if (!$validateOpt->check($dataOpt[$k])) return error($validateOpt->getError());//验证参数
$dataOpt[$k]['opt'] = question_bank($k);
}
if(count($dataOpt) < 2) return error('题目选项最低两个选项');//验证参数
$where = empty($param['id']) ? [] : ['id' => $param['id']];
$data['title'] = $param['title'];
$data['status'] = $param['status'];
$data['update_time'] = date('Y-m-d H:i:s',time());
$data['option_number'] = count($dataOpt);
if(empty($param['id']))$data['create_time'] = date('Y-m-d H:i:s',time());
$bankId = AQBModel::save($data,$where);
if($param['id'] > 0)AQOModel::dataDetele(['bank_id' => $bankId]);
foreach ($dataOpt as $ks => $vs){
$dataOpt[$ks]['bank_id'] = $bankId;
}
AQOModel::dataAllSave($dataOpt);
return success([],"操作成功");
}
// 启禁用
public function saveStatus(){
$param = $this->request->param();
AQBModel::save(['status' => $param['status']],[['id','in',$param['id']]]);
return success([],"操作成功");
}
//获取奖品信息
public function getPrizeInfo(){
$data = AQPModel::dataFind(['id' => 1],'prize_name,prize_price,postage_price,prize_picture,status',true);
return success($data);
}
//保存产品信息
public function savePrizeInfo(){
$param = $this->request->param();
$validateBank = new \app\validate\anxun\admin\activity\QuestionPrize;
if (!$validateBank->check($param)) return error($validateBank->getError());//验证参数
$data['prize_name'] = $param['prize_name'];
$data['prize_price'] = $param['prize_price'];
$data['postage_price'] = $param['postage_price'];
$data['prize_picture'] = $param['prize_picture'];
$data['status'] = $param['status'];
$data['update_time'] = date('Y-m-d H:i:s',time());
AQPModel::save($data,['id' => 1]);
return success([],"操作成功");
}
//导入答题
public function saveImportInfo(){
$file = $this->request->file();
$savename = Filesystem::disk('public')->putFile('topic',$file['file'],'md5');
$reader = IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(true);
$filePath = app()->getRootPath().'public/storage/'.$savename;
$spreadsheet=$reader->load($filePath);//载入文件
$worksheet=$spreadsheet->getActiveSheet();
$highestRow=$worksheet->getHighestRow();//总行数
for($row=2;$row<=$highestRow;$row++){
$questionOpt[$row] = array_filter(explode('|', $worksheet->getCellByColumnAndRow(2, $row)->getValue()), 'remove_empty_values', ARRAY_FILTER_USE_BOTH);
$questionOpt[$row] = remove_array_values($questionOpt[$row]);
if(count($questionOpt[$row]) < 2)continue;
$title[$row] = trim($worksheet->getCellByColumnAndRow(1, $row)->getValue());
$bankId[$row] = AQBModel::dataFind([['title','=',$title[$row]]],'id',false);
if(empty($bankId[$row])){
//添加题目
$data[$row]['title'] = $title[$row];
$data[$row]['status'] = 1;
$data[$row]['option_number'] = count($questionOpt[$row]);
$data[$row]['create_time'] = date('Y-m-d H:i:s', time());
$data[$row]['update_time'] = date('Y-m-d H:i:s', time());
$bankId[$row] = AQBModel::save($data[$row]);
$correct[$row] = array_filter(explode(',', $worksheet->getCellByColumnAndRow(3, $row)->getValue()), 'remove_empty_values', ARRAY_FILTER_USE_BOTH);
//添加选项
foreach($questionOpt[$row] as $k => $v) {
$da[$row.$k]['bank_id'] = $bankId[$row];
$da[$row.$k]['opt'] = question_bank($k);
$da[$row.$k]['content'] = trim($v);
foreach ($correct[$row] as $ko => $vo) {
$da[$row.$k]['is_correct'] = $da[$row.$k]['opt'] == trim($vo) ? 1 : 0;
}
AQOModel::save($da[$row.$k]);
}
}
}
@unlink($filePath);
return success([],'导入成功');
}
}
2、机器人实现
a、列表、详情、保存、删除
class DigitizedHuman extends Base
{
//列表
public function getList(){
$param = $this->request->param();
$where = [];
if(!empty($param['digitized_human_name']))$where[] = ['digitized_human_name','like','%'.$param['digitized_human_name'].'%'];
if(!empty($param['digitized_human_id']))$where[] = ['digitized_human_id','=',$param['digitized_human_id']];
if(!empty($param['area_code'])){
if(count($param['area_code']) >= 1){
if(count($param['area_code']) == 1)$where[] = ['province_code','=',$param['area_code'][0]];
if(count($param['area_code']) >= 2)$where[] = ['municipality_code','=',$param['area_code'][1]];
if(count($param['area_code']) == 3)$where[] = ['district_code','=',$param['area_code'][2]];
}
}
$resList = ASDHModel::getList($where, 'id,digitized_human_code,digitized_human_name,digitized_human_id,province_code,municipality_code,district_code,address,update_time',[$param['currentPage'], $param['currentSize']]);
$list = $resList['info'];
foreach ($list as $k => $v) {
$areaTitle[$k] = PPModel::dataFind(['id' => $v['province_code']],'title',false);
if(!empty($v['municipality_code']))$areaTitle[$k] = $areaTitle[$k].'-'.PPModel::dataFind(['id' => $v['municipality_code']],'title',false);
if(!empty($v['district_code']))$areaTitle[$k] = $areaTitle[$k].'-'.PPModel::dataFind(['id' => $v['district_code']],'title',false);
$list[$k]['area_title'] = $areaTitle[$k];
unset($list[$k]['province_code']);
unset($list[$k]['municipality_code']);
unset($list[$k]['district_code']);
}
$res['list'] = $list;
$res['meat'] = $resList['page'];
return success($res);
}
//详情
public function getInfo(){
$id = $this->request->param('id');//接收参数
if(empty($id)) return error('主键不能为空');
$data = ASDHModel::dataFind(['id' => $id],'id,digitized_human_id,digitized_human_name,province_code,municipality_code,district_code,address',true);
return success($data);
}
//保存
public function saveInfo(){
$param = $this->request->param();
// 模板基础验证
$validate = new \app\validate\anxun\admin\shuziren\DigitizedHuman;
if (!$validate->check($param)) return error($validate->getError());//验证参数
if(count($param['area_code']) == 0 || count($param['area_code']) >= 4) return error('省市区至少选择一项');//验证参数
$time = date('Y-m-d H:i:s', time());
$data['digitized_human_name'] = $param['digitized_human_name'];
$data['digitized_human_id'] = $param['digitized_human_id'];
$data['province_code'] = $param['area_code'][0];
$data['municipality_code'] = count($param['area_code']) >= 2 ? $param['area_code'][1] : 0;
$data['district_code'] = count($param['area_code']) == 3 ? $param['area_code'][2] : 0;
$data['address'] = $param['address'];
$data['update_time'] = $time;
if($param['id'] == 0){
$data['status'] = 1;
$data['create_time'] = $time;
$data['digitized_human_code'] = create_order();
}
$where = empty($param['id']) ? [] : ['id' => $param['id']];
ASDHModel::save($data,$where);
return success([], '操作成功');
}
//删除
public function deleteInfo(){
$id = $this->request->param('id');//接收参数
if(empty($id)) return error('主键不能为空');
ASDHModel::dataDetele(['id' => 1]);
return success([], '操作成功');
}
}
3、订单实现(也就是发货记录)
a、列表、详情、发货、修改发货信息、导出信息
class QuestionOrder extends Base
{
//获取分页数据
public function getList(){
$param = $this->request->param();
$where = [];
if(!empty($param['express_company']))$where[] = ['express_company','like','%'.$param['express_company'].'%'];
if(!empty($param['express_number']))$where[] = ['express_number','like','%'.$param['express_number'].'%'];
if(!empty($param['receipt_status']))$where[] = ['receipt_status','=',$param['receipt_status']];
$resList = ASQOModel::getList($where, 'id,order_no,consignee_name,consignee_phone,prize_name,receipt_status,express_company,status,express_number',[$param['currentPage'], $param['currentSize']]);
$list = $resList['info'];
$res['list'] = $list;
$res['meat'] = $resList['page'];
return success($res);
}
//获取详情
public function getInfo(){
$id = $this->request->param('id');//接收参数
if(empty($id)) return error('主键不能为空');
$data = ASQOModel::dataFind(['id' => $id], 'id,order_no,consignee_name,consignee_phone,area,details_addr,receipt_status,express_company,express_number',true);
return success($data);
}
//发货
public function saveInfo(){
$param = $this->request->param();
// 模板基础验证
$validate = new \app\validate\anxun\admin\shuziren\QuestionOrder;
if (!$validate->check($param)) return error($validate->getError());//验证参数
$data['prize_name'] = $param['prize_name'];
$data['express_company'] = $param['express_company'];
$data['express_number'] = $param['express_number'];
$data['receipt_status'] = 3;
ASQOModel::save($data,['id' => $param['id'],'receipt_status' => 2]);
return success('操作成功');
}
//修改发货信息
public function saveDeliverGoods(){
$param = $this->request->param();
// 模板基础验证
$validate = new \app\validate\anxun\admin\shuziren\QuestionOrder;
if (!$validate->check($param)) return error($validate->getError());//验证参数
$data['prize_name'] = $param['prize_name'];
$data['express_company'] = $param['express_company'];
$data['express_number'] = $param['express_number'];
ASQOModel::save($data,['id' => $param['id'],'receipt_status' => 3]);
return success('操作成功');
}
//导出信息
public function exportInfo(){
$list = ASQOModel::getAll([],'receipt_status,order_no,consignee_name,consignee_phone,area,details_addr,express_company,express_number','id DESC');
if(!$list) return error('暂无可导出');
// 处理标题
$title = ['发货状态', '订单号', '收货人','收货手机号','所在地区','详细地址','快递公司','快递单号'];
$filename = '发货记录';
$spreadsheet = new Spreadsheet();
$spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //设置水平默认对齐方式
$spreadsheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); //设置垂直默认对齐方式
$sheet = $spreadsheet->getActiveSheet();
$everyDay = date('Y-m-d', time()). '_' .$this->user_id;
foreach ($title as $key => $value) {
$sheet->setCellValueByColumnAndRow($key+1, 1, $value);
}
$row = 2;
foreach ($list as $item) {
$column = 1;
foreach ($item as $k => $value) {
$sheet->setCellValueByColumnAndRow($column, $row, $value);
$column++;
}
$row++;
}
$savename = '/storage/'.$filename . '_' . $everyDay . '.xlsx';
$object = date('Y-m-d',time()).'/' . $filename . '_' . $everyDay . '.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('.' . $savename);
$UploadFactory = new UploadFactory();
$uploadHandle = $UploadFactory->getUploadHandle();
$res = $uploadHandle->uploadFile($object,'.' . $savename);
$data['url'] = $res;
$filePath = app()->getRuntimePath() . $savename;
@unlink($filePath);
return success($data,'上传成功');
}
}
4、题库参数配置
a、获取配置、保存配置
class QuestionParam extends Base
{
//获取配置
public function getInfo(){
$data = ASQPModel::dataFind(['id' => 1],'answer_number,answer_accuracy,answer_interval_period,postage_price');
return success($data, '操作成功');
}
//保存配置
public function saveInfo(){
$param = $this->request->param();
$validate = new \app\validate\anxun\admin\shuziren\QuestionParam;
if(!$validate->check($param)) return error($validate->getError());
ASQPModel::save([
'answer_number' => $param['answer_number'],
'answer_accuracy' => $param['answer_accuracy'],
'answer_interval_period' => $param['answer_interval_period'],
'postage_price' => $param['postage_price']
],[['id','=',1]]);
return success([], '操作成功');
}
}
5、统计
a、列表、详情、导出
class Statistics extends Base
{
//列表
public function getList(){
$param = $this->request->param();
if(empty($param['currentPage']))return error('狗日的,到底是第几页呀。不要瞎闹');
if(empty($param['currentSize']))return error('狗日的,一页我给你拉取多少条合适呀。不要瞎闹');
//初始化工厂
$statisticsFactory = new StatisticsFactory($param['types']);
//选择通道
$statisticsHandle = $statisticsFactory->statisticsHandle();
$res = $statisticsHandle->getList($param);
return success($res);
}
//详情
public function getInfo(){
$param = $this->request->param();//接收参数
if(empty($param['id'])) return error('主键不能为空');
//初始化工厂
$statisticsFactory = new StatisticsFactory($param['types']);
//选择通道
$statisticsHandle = $statisticsFactory->statisticsHandle();
$res = $statisticsHandle->getInfo($param['id']);
return success($res);
}
//导出
public function exportInfo(){
$param = $this->request->param();//接收参数
//初始化工厂
$statisticsFactory = new StatisticsFactory($param['types']);
//选择通道
$statisticsHandle = $statisticsFactory->statisticsHandle();
$res = $statisticsHandle->exportInfo($param);
return success($res,'导出成功');
}
}