| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace app\controller\merchant;
- use app\extra\basic\Base;
- use app\extra\dyLife\data\BaseData;
- use app\extra\tools\CodeExtend;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasGoods;
- use app\model\saas\SaasGoodsSku;
- use app\model\saas\SaasStore;
- use app\service\saas\GoodsService;
- use DI\Attribute\Inject;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use LinFly\Annotation\Attributes\Route\Middleware;
- use LinFly\Annotation\Attributes\Route\PostMapping;
- use support\Request;
- use support\Response;
- #[Controller("/api/merchant/goods"),Middleware(AuthMiddleware::class)]
- class Goods extends Base
- {
- #[Inject]
- protected GoodsService $service;
- #[Inject]
- protected SaasGoods $model;
- #[Inject]
- protected SaasGoodsSku $skuModel;
- #[Inject]
- protected SaasStore $store;
- #[GetMapping('list')]
- public function getDataList(Request $request): Response
- {
- try {
- $param = $request->all();
- $param['poi_id'] = $request->user['store_id'];
- $data = $this->service->setModel()->getList($request->all());
- return successTrans(100010,pageFormat($data),200);
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- /**
- * 商品详情
- * @param Request $request
- * @return Response
- */
- #[GetMapping('detail')]
- public function getGoodDetail(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "id.require" => trans("empty.require"),
- "product_id.require" => trans("empty.require"),
- ],$request->method());
- if (!is_array($param)) return error($param);
- $detail = $this->model->where("id",$param["id"])->with(['skuSpecs'])->findOrEmpty();
- if ($detail->isEmpty()) return errorTrans("empty.data");
- if ($detail['poi_id'] <> $request->user['store_id']) return errorTrans("error.param");
- $detail['sold_start_times'] = [(string)$detail['sold_start_time'],(string)$detail['sold_end_time']];
- $detail['use_times'] = [(string)$detail['use_time_start'],(string)$detail['use_time_end']];
- return successTrans('success.data',$detail->toArray());
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[PostMapping("template")]
- public function getTemplate(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "product_type.require" => trans("empty.require"),
- "category.require" => trans("empty.require"),
- ],$request->method());
- if (!is_array($param)) return error($param);
- $data = (new BaseData)->config([
- "appid" => sConf("wechat.appid"),
- "secret" => sConf("wechat.secret"),
- "account" => sConf("wechat.shop_id"),
- ])->token()->getStoreCategoryTemplate($param['category'][2],$param['product_type']);
- return success("ok",$data);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping('type')]
- public function getProductType(Request $request): Response
- {
- try {
- $poiId = $request->user['store_id'];
- $productType = $this->store->where("poi_id", $poiId)->value("product_type");
- if (empty($productType)) return error("未配置");
- $typeData = [];
- foreach ($this->service->productType() as $key => $value) {
- if (in_array($value['key'],json_decode($productType,true))) {
- $typeData[] = $value;
- }
- }
- return success('ok', $typeData);
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- /**
- * 新增商品
- * @param Request $request
- * @return Response
- */
- #[PostMapping("save")]
- public function saveGoods(Request $request): Response
- {
- try {
- $param = $request->post();
- if (empty($param['auto_renew'])) $param['auto_renew'] = 0;
- $param['image_list'] = empty($param['image_list']) ? [] : json_encode($param['image_list']);
- $param['detail_image_list'] = empty($param['detail_image_list']) ? [] : json_encode($param['detail_image_list']);
- $param['environment_image_list'] = empty($param['environment_image_list']) ? [] : json_encode($param['environment_image_list']);
- if (is_array($param['specs'])) $param['specs'] = json_encode($param['specs']);
- if (is_array($param['category'])) {
- $param['category_id'] = $param['category'][2]??0;
- $param['category'] = json_encode($param['category']);
- }
- if (!isset($param['id'])) {
- $param['product_id'] = strtoupper(CodeExtend::random(18,3));
- }
- $param['price'] = $param['price']*100;
- $param['poi_id'] = $request->user['store_id'];
- $skuData = [];
- if (is_array($param['skuSpecs'])) {
- foreach ($param['skuSpecs'] as $key => $value) {
- $skuData[$key] = $value;
- $skuData[$key]['product_id'] = $param['product_id'];
- $skuData[$key]['specs'] = json_encode($value['specs']);
- }
- }
- unset($param['skuSpecs']);
- $param['line_price'] = $param['line_price'] * 100;
- if (!empty($skuData)) {
- [$state,$msg] = $this->skuData($skuData);
- if (!$state) return error($msg);
- }
- $state = $this->model->setAutoData($param);
- if (!$state) return errorTrans("error.data");
- return successTrans("success.data");
- } catch (\Throwable $throwable) {
- echo $throwable->getLine()."\n";
- return error($throwable->getMessage());
- }
- }
- /**
- * SKU编辑
- * @param array $skuData
- * @return array
- */
- protected function skuData(array $skuData = []): array
- {
- try {
- if (empty($skuData)) return [0,trans('empty.require')];
- foreach ($skuData as $vo) {
- $sku = $this->skuModel->where("sku_id",$vo['sku_id'])->findOrEmpty();
- if ($sku->isEmpty()) {
- $sku->insertGetId($vo);
- } else {
- $sku->update($vo);
- }
- }
- return [1,'success'];
- } catch (\Throwable $throwable) {
- return [0,$throwable->getMessage()];
- }
- }
- }
|