Shops.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\validate\Shop as ShopValidate;
  4. use app\common\model\ShopList;
  5. use app\common\model\ShopDelivery;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use app\common\service\SpecService;
  9. use think\exception\ValidateException;
  10. class Shops extends Base
  11. {
  12. //protected $noNeedLogin = ['*'];
  13. /**
  14. * @return void 全部平台
  15. */
  16. public function getPlatform(){
  17. $list = site_config('addonsd.platform_list');
  18. $this->success('提交成功', $list);
  19. }
  20. //获取店铺
  21. public function getShop(ShopList $shopList){
  22. $platform_id = $this->request->post('platform_id/d');
  23. if(empty($platform_id)){
  24. $this->error('参数有误');
  25. }
  26. return $this->success('ok', $shopList->where('platform', $platform_id)->column('name', 'id'));
  27. }
  28. //获取规格
  29. public function getSpec(ShopList $shopList){
  30. $shop_id = $this->request->post('shop_id/d');
  31. if( empty($shop_id)){
  32. $this->error('参数有误');
  33. }
  34. $spec = $shopList->where('id', $shop_id)->value('type_spec');
  35. $shopList = $spec?json_decode($spec, true): [];
  36. return $this->success('ok', $shopList);
  37. }
  38. //发货记录
  39. public function delivery(ShopDelivery $shopDelivery){
  40. $data = $this->request->post();
  41. $result = $shopDelivery::where($data)->select();
  42. $this->success();
  43. }
  44. //添加记录
  45. public function create(ShopDelivery $shopDelivery, SpecService $specService)
  46. {
  47. $data = $this->request->post();
  48. $result = false;
  49. Db::startTrans();
  50. try {
  51. validate(ShopValidate::class)->scene('add')->check($data);
  52. //发货数据
  53. $resData = $specService::getDeliveryList($this->userinfo['id'], $data);
  54. //扣除库存
  55. $result = $shopDelivery->saveAll($resData);
  56. Db::commit();
  57. }catch (ValidateException $e) {
  58. Db::rollback();
  59. return $this->error($e->getError());
  60. } catch (\Exception $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. }
  64. if ($result === false) {
  65. $this->error(__('没有新增任何数据'));
  66. }
  67. $this->success();
  68. }
  69. }