|
@@ -0,0 +1,274 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace app\controller\admin;
|
|
|
|
|
+
|
|
|
|
|
+use app\extra\basic\Base;
|
|
|
|
|
+use app\extra\dyMini\PlanLive;
|
|
|
|
|
+use app\middleware\AuthMiddleware;
|
|
|
|
|
+use app\model\saas\SaasLivePlan;
|
|
|
|
|
+use app\model\saas\SaasLivePlanGoods;
|
|
|
|
|
+use app\model\saas\SaasLivePlanStar;
|
|
|
|
|
+use app\service\saas\GoodsService;
|
|
|
|
|
+use app\service\saas\PlanService;
|
|
|
|
|
+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/plan"),Middleware(AuthMiddleware::class)]
|
|
|
|
|
+class Plan extends Base
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+ #[Inject]
|
|
|
|
|
+ protected PlanService $service;
|
|
|
|
|
+
|
|
|
|
|
+ #[Inject]
|
|
|
|
|
+ protected GoodsService $goodsService;
|
|
|
|
|
+
|
|
|
|
|
+ #[Inject]
|
|
|
|
|
+ protected SaasLivePlan $model;
|
|
|
|
|
+
|
|
|
|
|
+ #[GetMapping('list')]
|
|
|
|
|
+ public function getPlanList(Request $request): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $param = $request->all();
|
|
|
|
|
+ $data = $this->service->setModel()->getList($param,['poi' => function($query){
|
|
|
|
|
+ $query->field("poi_id,poi_name,poi_address,nick_name");
|
|
|
|
|
+ }]);
|
|
|
|
|
+ return successTrans(100010,pageFormat($data),200);
|
|
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
|
|
+ return error($throwable->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发布佣金计划
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return Response
|
|
|
|
|
+ */
|
|
|
|
|
+ #[PostMapping('save')]
|
|
|
|
|
+ public function addPlanData(Request $request): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $param = $this->_valid([
|
|
|
|
|
+ "plan_name.require" => trans("empty.require"),
|
|
|
|
|
+ "merchant_phone.require" => trans("empty.require"),
|
|
|
|
|
+ "merchant_phone.mobile" => trans("error.mobile"),
|
|
|
|
|
+ "status.require" => trans("empty.require"),
|
|
|
|
|
+ "goods.require" => trans("empty.require"),
|
|
|
|
|
+ "star.require" => trans("empty.require"),
|
|
|
|
|
+ "plan_id.default" => 0,
|
|
|
|
|
+ ],$request->method());
|
|
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
|
|
+ $goodsData = is_string($param['goods']) ? json_decode($param['goods'],true) : $param['goods'];
|
|
|
|
|
+ $starData = is_string($param['star']) ? json_decode($param['star'],true) : $param['star'];
|
|
|
|
|
+ $plan = $this->model->where("plan_id",$param['plan_id'])->findOrEmpty();
|
|
|
|
|
+ $liveDyStar = $liveDyGoods = [];
|
|
|
|
|
+ foreach ($goodsData as $k => $v) {
|
|
|
|
|
+ $liveDyGoods[$k] = [
|
|
|
|
|
+ "commission_rate" => round($v['rate'] * 100),
|
|
|
|
|
+ "product_id" => $v['goods_id'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ foreach ($starData as $k => $v) {
|
|
|
|
|
+ $liveDyStar[$k] = $v['unique_id'];
|
|
|
|
|
+ }
|
|
|
|
|
+ $resp = (new PlanLive)->config([
|
|
|
|
|
+ "appid" => sConf("wechat.mini_appid"),
|
|
|
|
|
+ "secret" => sConf("wechat.mini_secret"),
|
|
|
|
|
+ ])->token()->planData($param['plan_id'],['name' => $param['plan_name'],'mobile' => $param['merchant_phone'],'goods' => $liveDyGoods,'star' => $liveDyStar]);
|
|
|
|
|
+ if (isset($resp['msg'])) return error($resp['msg']);
|
|
|
|
|
+ if (!isset($resp['plan_id'])) return error("发布直播计划失败");
|
|
|
|
|
+ if ($plan->isEmpty()) { // 新增
|
|
|
|
|
+ $state = $plan->insertGetId([
|
|
|
|
|
+ "plan_id" => $resp['plan_id'],
|
|
|
|
|
+ "poi_id" => $request->user['store_id'],
|
|
|
|
|
+ "plan_name" => $param['plan_name'],
|
|
|
|
|
+ "merchant_phone" => $param['merchant_phone'],
|
|
|
|
|
+ "status" => $param['status'],
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } else { // 编辑
|
|
|
|
|
+ (new SaasLivePlanGoods)->where("plan_id",$resp['plan_id'])->delete();
|
|
|
|
|
+ (new SaasLivePlanStar)->where("plan_id",$resp['plan_id'])->delete();
|
|
|
|
|
+ $state = $plan->save([
|
|
|
|
|
+ "plan_name" => $param['plan_name'],
|
|
|
|
|
+ "merchant_phone" => $param['merchant_phone'],
|
|
|
|
|
+ "status" => $param['status'],
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ $goodsAll = $starAll = [];
|
|
|
|
|
+ foreach ($liveDyGoods as $k => $v) {
|
|
|
|
|
+ $goodsAll[$k] = [
|
|
|
|
|
+ "plan_id" => $resp['plan_id'],
|
|
|
|
|
+ "goods_id" => $v['product_id'],
|
|
|
|
|
+ "commission_rate" => $v['commission_rate'] / 100,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ (new SaasLivePlanGoods)->insertAll($goodsAll);
|
|
|
|
|
+ foreach ($liveDyStar as $k => $v) {
|
|
|
|
|
+ $starAll[$k] = [
|
|
|
|
|
+ "plan_id" => $resp['plan_id'],
|
|
|
|
|
+ "unique_id" => $v
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ (new SaasLivePlanStar)->insertAll($starAll);
|
|
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
|
|
+ return successTrans("success.data");
|
|
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
|
|
+ return error($throwable->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[GetMapping('detail')]
|
|
|
|
|
+ public function detailPlan(Request $request): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $param = $this->_valid([
|
|
|
|
|
+ "id.require" => trans("empty.require"),
|
|
|
|
|
+ ],$request->method());
|
|
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
|
|
+ $productType = $this->goodsService->productType();
|
|
|
|
|
+ $plan = $this->model->where("id",$param['id'])->with(['goods' => function ($query) use($productType){
|
|
|
|
|
+ $query->with(['goods' => function($subQue) use($productType){
|
|
|
|
|
+ $subQue->field("goods_id,product_name,image_list,product_id,price,line_price,limit_use_rule,use_num_per_consume,status,product_type")->append(['types'])->withAttr(['types' => function($query,$resp) use($productType){
|
|
|
|
|
+ $productTypeArr = [];
|
|
|
|
|
+ foreach ($productType as $val) {
|
|
|
|
|
+ $productTypeArr[$val['key']] = $val['name'];
|
|
|
|
|
+ }
|
|
|
|
|
+ return $productTypeArr[$resp['product_type']]??'';
|
|
|
|
|
+ }]);
|
|
|
|
|
+ }]);
|
|
|
|
|
+ },'star' => function ($query) {
|
|
|
|
|
+ $query->with(['star' => function($subQue){
|
|
|
|
|
+ $subQue->field("unique_id,avatar_url,show_nickname,fans_count,fans_tag_list,local_fans_count,talent_item_level_display,talent_live_level_display,content_level_display,credit_score_display,city_name,rank_display,status");
|
|
|
|
|
+ }]);
|
|
|
|
|
+ }])->findOrEmpty();
|
|
|
|
|
+ if ($plan->isEmpty()) return errorTrans("empty.data");
|
|
|
|
|
+ return successTrans("success.data",$plan->toArray());
|
|
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
|
|
+ return error($throwable->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ #[PostMapping('goods')]
|
|
|
|
|
+ public function setGoodsRate(Request $request): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $param = $this->_valid([
|
|
|
|
|
+ "id.require" => trans("empty.require"),
|
|
|
|
|
+ "data.require" => trans("empty.require"),
|
|
|
|
|
+ "type.require" => trans("empty.require"),
|
|
|
|
|
+ ],$request->method());
|
|
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
|
|
+ $plan = $this->model->where("id",$param['id'])->findOrEmpty();
|
|
|
|
|
+ if ($plan->isEmpty()) return errorTrans("empty.data");
|
|
|
|
|
+ $planData = is_string($param['data']) ? json_decode($param['data'],true) : $param['data'];
|
|
|
|
|
+ $editData = [];
|
|
|
|
|
+ $editPlanData = [
|
|
|
|
|
+ 'name' => $plan['plan_name'],
|
|
|
|
|
+ 'mobile' => $plan['merchant_phone'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ if ($param['type'] == 'goods') {
|
|
|
|
|
+ foreach ($planData as $k => $v) {
|
|
|
|
|
+ $commission_rate = $v['commission_rate'] * 100;
|
|
|
|
|
+ echo $commission_rate."\n";
|
|
|
|
|
+ $editData[$k] = [
|
|
|
|
|
+ "commission_rate" => round($commission_rate),
|
|
|
|
|
+ "product_id" => $v['goods_id'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ $editPlanData['goods'] = $editData;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($param['type'] == 'star') {
|
|
|
|
|
+ foreach ($planData as $k => $v) {
|
|
|
|
|
+ $editData[$k] = $v['unique_id'];
|
|
|
|
|
+ }
|
|
|
|
|
+ $editPlanData['star'] = $editData;
|
|
|
|
|
+ }
|
|
|
|
|
+ $resp = (new PlanLive)->config([
|
|
|
|
|
+ "appid" => sConf("wechat.mini_appid"),
|
|
|
|
|
+ "secret" => sConf("wechat.mini_secret"),
|
|
|
|
|
+ ])->token()->planData($plan['plan_id'],$editPlanData);
|
|
|
|
|
+ if (isset($resp['msg'])) return error($resp['msg']);
|
|
|
|
|
+ $goodsAll = $starAll = [];
|
|
|
|
|
+ $state = false;
|
|
|
|
|
+ if ($param['type'] == 'goods') {
|
|
|
|
|
+ (new SaasLivePlanGoods)->where("plan_id",$plan['plan_id'])->delete();
|
|
|
|
|
+ foreach ($planData as $k => $v) {
|
|
|
|
|
+ $goodsAll[$k] = [
|
|
|
|
|
+ "plan_id" => $resp['plan_id'],
|
|
|
|
|
+ "goods_id" => $v['goods_id'],
|
|
|
|
|
+ "commission_rate" => $v['commission_rate'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ $state = (new SaasLivePlanGoods)->insertAll($goodsAll);
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($param['type'] == 'star') {
|
|
|
|
|
+ (new SaasLivePlanStar)->where("plan_id",$plan['plan_id'])->delete();
|
|
|
|
|
+ foreach ($planData as $k => $v) {
|
|
|
|
|
+ $starAll[$k] = [
|
|
|
|
|
+ "plan_id" => $resp['plan_id'],
|
|
|
|
|
+ "unique_id" => $v
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ $state = (new SaasLivePlanStar)->insertAll($starAll);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
|
|
+ return successTrans("success.data");
|
|
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
|
|
+ return error($throwable->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ #[PostMapping('close')]
|
|
|
|
|
+ public function closePlan(Request $request): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $param = $this->_valid([
|
|
|
|
|
+ "id.require" => trans("empty.require"),
|
|
|
|
|
+ ],$request->method());
|
|
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
|
|
+ $plan = $this->model->where("id",$param['id'])->findOrEmpty();
|
|
|
|
|
+ if ($plan->isEmpty()) return errorTrans("empty.data");
|
|
|
|
|
+ $resp = (new PlanLive)->config([
|
|
|
|
|
+ "appid" => sConf("wechat.mini_appid"),
|
|
|
|
|
+ "secret" => sConf("wechat.mini_secret"),
|
|
|
|
|
+ ])->token()->planClose($plan['plan_id']);
|
|
|
|
|
+ if (isset($resp['msg'])) return error($resp['msg']);
|
|
|
|
|
+ $plan->status = 2;
|
|
|
|
|
+ $state = $plan->save();
|
|
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
|
|
+ return successTrans("success.data");
|
|
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
|
|
+ return error($throwable->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ #[PostMapping('del')]
|
|
|
|
|
+ public function delPlan(Request $request): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $param = $this->_valid([
|
|
|
|
|
+ "id.require" => trans("empty.require"),
|
|
|
|
|
+ ],$request->method());
|
|
|
|
|
+ if (!is_array($param)) return error($param);
|
|
|
|
|
+ $plan = $this->model->where("id",$param['id'])->findOrEmpty();
|
|
|
|
|
+ if ($plan->isEmpty()) return errorTrans("empty.data");
|
|
|
|
|
+ if ($plan['status'] <> 2) return error("该计划暂不支持删除");
|
|
|
|
|
+ $state = $plan->delete();
|
|
|
|
|
+ if (!$state) return errorTrans("error.data");
|
|
|
|
|
+ return successTrans("success.data");
|
|
|
|
|
+ } catch (\Throwable $throwable) {
|
|
|
|
|
+ return error($throwable->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|