|
|
@@ -0,0 +1,165 @@
|
|
|
+<?php
|
|
|
+declare (strict_types = 1);
|
|
|
+
|
|
|
+namespace app\admin\controller\shop;
|
|
|
+
|
|
|
+use app\common\controller\Backend;
|
|
|
+use app\admin\traits\Actions;
|
|
|
+use think\annotation\route\Group;
|
|
|
+use think\annotation\route\Route;
|
|
|
+use app\common\model\Customer;
|
|
|
+use app\common\model\StockConfig;
|
|
|
+use think\facade\Db;
|
|
|
+use app\common\model\ProductConfig;
|
|
|
+use app\common\model\CustomerSpec as CustomerSpecModel;
|
|
|
+
|
|
|
+#[Group("shop/customer_spec")]
|
|
|
+class CustomerSpec extends Backend
|
|
|
+{
|
|
|
+ use Actions{
|
|
|
+ index as private _index;
|
|
|
+ add as private _add;
|
|
|
+ edit as private _edit;
|
|
|
+ del as private _del;
|
|
|
+ multi as private _multi;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function _initialize()
|
|
|
+ {
|
|
|
+ parent::_initialize();
|
|
|
+ $this->model = new CustomerSpecModel();
|
|
|
+ $this->assign('customerList', Customer::where('status', 1)->column('name','id'));
|
|
|
+ $this->assign('fieldList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
|
|
|
+ }
|
|
|
+
|
|
|
+ //查看
|
|
|
+ #[Route("GET,JSON","index")]
|
|
|
+ public function index()
|
|
|
+ {
|
|
|
+ return $this->_index();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 添加
|
|
|
+ */
|
|
|
+ #[Route('GET,POST','add')]
|
|
|
+ public function add()
|
|
|
+ {
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ $customer_id =$this->request->post('customer_id/d', 0);
|
|
|
+ $type_box =$this->request->post('type_box');
|
|
|
+ $type_list =$this->request->post('type_list');
|
|
|
+ if(empty($customer_id) || empty($type_box) || empty($type_list)) return resp_json(0,'请填写完整信息');
|
|
|
+ $specsList = [];
|
|
|
+ foreach ($type_box as $item) {
|
|
|
+ if(empty($item['price'])) return resp_json(0, '请填写发货价格');
|
|
|
+ $specsList[] = ['type_id'=> $item['type_id'], 'type_name'=> $item['type_name'],'specs_id'=>$item['value'],'specs_name'=>$item['name'], 'price'=> $item['price']];
|
|
|
+ }
|
|
|
+ if($this->model->where('customer_id', $customer_id)->count()>0) return resp_json(0,'客户信息已存在');
|
|
|
+ $result = false;
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+
|
|
|
+ $result = $this->model->save([
|
|
|
+ 'customer_id'=> $customer_id,
|
|
|
+ 'variety' => json_encode($type_list, JSON_UNESCAPED_UNICODE),
|
|
|
+ 'specs' => json_encode($type_box, JSON_UNESCAPED_UNICODE),
|
|
|
+ ]);
|
|
|
+ if($this->callback){
|
|
|
+ $callback=$this->callback;
|
|
|
+ $callback($this->model);
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+ if ($result === false) {
|
|
|
+ $this->error(__('没有新增任何数据'));
|
|
|
+ }
|
|
|
+ return resp_json(200,'操作成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑
|
|
|
+ */
|
|
|
+ #[Route('GET,POST','edit')]
|
|
|
+ public function edit(mixed $row=null)
|
|
|
+ {
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
+ $ids = $this->request->get('ids');
|
|
|
+ if(!$row || is_array($row)){
|
|
|
+ $row = $this->model->find($ids);
|
|
|
+ }
|
|
|
+ if (!$row) {
|
|
|
+ $this->error(__('没有找到记录'));
|
|
|
+ }
|
|
|
+ $row->customer_id = (string)$row->customer_id;
|
|
|
+ $row->specs = json_decode($row->specs, true);
|
|
|
+ $row->variety = json_decode($row->variety, true);
|
|
|
+ $this->assign('row', $row);
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+ $params = $this->request->post("");
|
|
|
+ if(empty($params['ids']) || empty($params['type_box']) || empty($params['type_list'])) return resp_json(0,'请填写完整信息');
|
|
|
+ $specsList = [];
|
|
|
+ foreach ($params['type_box'] as $item) {
|
|
|
+ if(empty($item['price'])) return resp_json(0, '请填写发货价格');
|
|
|
+ $specsList[] = ['type_id'=> $item['type_id'], 'type_name'=> $item['type_name'],'specs_id'=>$item['value'],'specs_name'=>$item['name'], 'price'=> $item['price']];
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = false;
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+
|
|
|
+ $result = $this->model->where('id', $params['ids'])->save([
|
|
|
+ 'variety' => json_encode($params['type_list'], JSON_UNESCAPED_UNICODE),
|
|
|
+ 'specs' => json_encode($params['type_box'], JSON_UNESCAPED_UNICODE),
|
|
|
+ ]);
|
|
|
+ if($this->callback){
|
|
|
+ $callback=$this->callback;
|
|
|
+ $callback($row);
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+ if (false === $result) {
|
|
|
+ $this->error(__('没有数据被更新'));
|
|
|
+ }
|
|
|
+ return resp_json(200,'操作成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除
|
|
|
+ #[Route("GET,POST","del")]
|
|
|
+ public function del()
|
|
|
+ {
|
|
|
+ //通过定义callback回调函数来执行删除后的操作
|
|
|
+ $this->callback=function ($ids){};
|
|
|
+ return $this->_del();
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新
|
|
|
+ #[Route("GET,POST","multi")]
|
|
|
+ public function multi()
|
|
|
+ {
|
|
|
+ //通过定义callback回调函数来执行更新后的操作
|
|
|
+ $this->callback=function ($ids,$field,$value){};
|
|
|
+ return $this->_multi();
|
|
|
+ }
|
|
|
+
|
|
|
+ #[Route('GET','get_box')]
|
|
|
+ public function get_box()
|
|
|
+ {
|
|
|
+ if($this->request->isAjax()){
|
|
|
+ $type_id = $this->request->get('type_id/d', 0);
|
|
|
+ if(empty($type_id)) return resp_json(0, '参数错误');
|
|
|
+ $list = ProductConfig::where('type_id', $type_id)->order('sort desc')->field('id, title')->select();
|
|
|
+ return json($list, 200);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|