| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?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\api\service\SpecService;
- use app\common\model\CustomerSpecBack 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('typeList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
- $this->relationField=['customer'];
- }
- //查看
- #[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,'请填写完整信息');
- //判断是否有发货价格
- foreach ($type_box as $key => $item) {
- if(!(isset($item['price']) && $item['price'] > 0)){
- unset($type_box[$key]);
- }
- }
- if(empty($type_box)) return resp_json(0, '请填写发货价格');
- 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(__('没有找到记录'));
- }
-
- $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,'请填写完整信息');
- //判断是否有发货价格
- if(SpecService::getIsZeroSpecsPrice($params['type_box']) == false) return resp_json(0, '请填写发货价格');
-
- $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);
- }
- }
- }
|