123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\adminapi\controller\v1\setting;
- use app\models\system\ShippingTemplatesFree;
- use app\models\system\ShippingTemplatesRegion;
- use app\models\system\SystemCity;
- use app\models\system\ShippingTemplates as STModel;
- use app\adminapi\controller\AuthController;
- use crmeb\services\UtilService;
- class ShippingTemplates extends AuthController
- {
-
- public function temp_list()
- {
- $where = UtilService::getMore([
- [['page','d'], 1],
- [['limit','d'], 20],
- [['name','s'], '']
- ]);
- $where['mer_id'] = $this->merId ?: '';
- return $this->success(STModel::getList($where));
- }
-
- public function edit($id = 0)
- {
- $templates = STModel::get($id);
- if (!$templates) {
- return $this->fail('修改的模板不存在');
- }
- $data['appointList'] = ShippingTemplatesFree::getFreeList($id);
- $data['templateList'] = ShippingTemplatesRegion::getRegionList($id);
- if (!isset($data['templateList'][0]['region'])) {
- $data['templateList'][0]['region'] = ['city_id' => 0, 'name' => '默认全国'];
- }
- $data['formData'] = [
- 'name' => $templates->name,
- 'type' => $templates->getData('type'),
- 'appoint_check' => $templates->getData('appoint'),
- 'sort' => $templates->getData('sort'),
- ];
- return $this->success($data);
- }
-
- public function save($id = 0)
- {
- $data = UtilService::postMore([
- [['region_info','a'], []],
- [['appoint_info','a'], []],
- [['sort','d'], 0],
- [['type','d'], 0],
- [['name','s'], ''],
- [['appoint','d'], 0],
- ]);
- $this->validate($data, \app\adminapi\validates\setting\ShippingTemplatesValidate::class, 'save');
- $temp['name'] = $data['name'];
- $temp['type'] = $data['type'];
- $temp['appoint'] = $data['appoint'];
- $temp['sort'] = $data['sort'];
- $temp['add_time'] = time();
- $temp['mer_id'] = $this->merId ?: '';
- STModel::beginTrans();
- $res = true;
- try {
- if ($id) {
- $res = STModel::where('id', $id)->update($temp);
- } else {
- $id = STModel::insertGetId($temp);
- }
-
- $res = $res && ShippingTemplatesRegion::saveRegion($data['region_info'], $data['type'], $id);
- if (!$res) {
- STModel::rollbackTrans();
- return $this->fail(ShippingTemplatesRegion::getErrorInfo());
- }
-
- if ($data['appoint']) {
- $res = $res && ShippingTemplatesFree::saveFree($data['appoint_info'], $data['type'], $id);
- }
- if ($res) {
- STModel::commitTrans();
- return $this->success('保存成功');
- } else {
- STModel::rollbackTrans();
- return $this->fail(ShippingTemplatesFree::getErrorInfo('保存失败'));
- }
- } catch (\Throwable $e) {
- STModel::rollbackTrans();
- return $this->fail($e->getMessage());
- }
- }
-
- public function delete()
- {
- $data = UtilService::getMore([
- [['id','d'], 0],
- ]);
- if ($data['id'] == 1) {
- return $this->fail('默认模板不能删除');
- } else {
- STModel::del($data['id']);
- ShippingTemplatesRegion::where('temp_id', $data['id'])->delete();
- ShippingTemplatesFree::where('temp_id', $data['id'])->delete();
- return $this->success('删除成功');
- }
- }
-
- public function city_list()
- {
- $list = SystemCity::with('children')->where('parent_id', 0)->order('id asc')->select();
- return $this->success($list->toArray());
- }
- }
|