'已上架推广', 2 => '已下架推广', 4 => '已删除', 5 => '未达到准入标准', 10 => '待生效', ]; if($status === null){ return $arr; } return isset($arr[$status]) ? $arr[$status] : ''; } public static function leagueItemTypeName($status = null) { $arr = [ 1 => '普通推广商品', 2 => '定向推广商品', 3 => '专属推广商品', ]; if($status === null){ return $arr; } return isset($arr[$status]) ? $arr[$status] : ''; } public function init() { parent::init(); if (empty($this->store_id)) { $this->store_id = get_store_id(); } } public static function afterOrderSave($order, $insert = 0, $changedAttributes = []) { try{ $store_id = $order->store_id; if(!$order->user_id){ return; } $orderExt = VideoShopOrderExt::find()->where(['store_id' => $store_id, 'order_id' => $order->id])->one(); if(!$orderExt){ return; } $promoter_ids = explode(',', $orderExt->promoter_id); if($promoter_ids){ $promoter_id = $promoter_ids[0]; if($order->is_sale){ $doTask = PublicRankingTask::doTask($store_id, $order->user_id, $promoter_id, $order); debug_log([__METHOD__, __LINE__, $store_id, $order->id, $insert, $promoter_id, $doTask ? $doTask->id : $doTask, $changedAttributes], __CLASS__ . '.log'); } } } catch (\Exception $ex) { \Yii::error($ex); debug_log([__METHOD__, __LINE__, $order->id, $insert, $changedAttributes, $ex->getMessage()], __CLASS__ . '.log'); return [ 'code' => 1, 'msg' => $ex->getMessage(), ]; } } public static function afterVideoShopOrderExtSave($order, $insert = 0, $changedAttributes = []) { try{ if (!$insert && isset($changedAttributes['commission_status']) && $order->commission_status == 2) { //复购 $price2Fugou = self::price2Fugou($order); debug_log([__METHOD__, __LINE__, '复购', $order->id, $price2Fugou], __CLASS__ . '.log'); } } catch (\Exception $ex) { \Yii::error($ex); debug_log([__METHOD__, __LINE__, $order->id, $ex->getMessage()], __CLASS__ . '.log'); return [ 'code' => 1, 'msg' => $ex->getMessage(), ]; } } public static function url_callback($store_id = 0, $mini_id = 0) { $url_callback = ''; if(empty($mini_id)){ return '需要先设置小店信息'; } if (\Yii::$app instanceof \yii\web\Application){ $url_callback = \Yii::$app->request->hostInfo . '/index.php/wechat/video-shop-order/callback/' . $mini_id; } return $url_callback; } public static function url_callback_decode($code = '') { $codeArr = explode('_', $code); if($codeArr[0] !== 'PublicRanking'){ return false; } return [ 'store_id' => $codeArr[1], ]; } public static $confs = []; public static function conf($store_id = 0, $refresh = 0) { if (isset(self::$confs[$store_id]) && !$refresh) { return self::$confs[$store_id]; } $confDef = [ 'is_open' => 0, //规则 'PublicRankingRow' => 0, //3人公排(33公排)或2人公排(22公排) ○若已有公排点位,不可修改此项 'task_count' => 10, //任务单数 'finder_name' => '', //视频号名字 'finder_id' => '', //视频号id 'promoter_id' => '', //达人带货id 'info_ids' => [], //定向推广id数组,用于自动绑定新达人 //小店配置 'mini_id' => '', 'app_id' => '', 'app_secret' => '', 'token' => '', 'encodingAesKey' => '', //复购 'price2Fugou' => '', //强制复购金额:佣金达到强制复购金额后,需重新完成任务,保完成后才能获得对应层级奖金 //躺赚 'auto_price_child_cnt' => '', //躺赚直推人数 'auto_price_wait_days' => '', //躺赚直推等待天数 ]; $conf = Option::get(OptionSetting::PUBLIC_RANKING_SETTING, $store_id, 'store')['value']; if ($conf) { $conf = array_merge($confDef, json_decode($conf, true)); } else { $conf = $confDef; } self::$confs[$store_id] = $conf; return $conf; } public static function confSave($store_id = 0, $config = []) { $oldConf = self::conf($store_id, 1); $conf = array_merge($oldConf, $config); $saveStoreMini = self::saveStoreMini($store_id, $conf); if($saveStoreMini['code']){ return $saveStoreMini; } $conf['mini_id'] = $saveStoreMini['data']['id']; $set = Option::set(OptionSetting::PUBLIC_RANKING_SETTING, json_encode($conf, JSON_UNESCAPED_UNICODE), $store_id, 'store'); self::conf($store_id, 1); return [ 'code' => 0, 'msg' => 'ok', '$save' => $set, ]; } public static function isopen($store_id = 0) { $conf = self::conf($store_id); $isopen = $conf['is_open']; return empty($isopen) ? 0 : 1; } //添加小店 public static function saveStoreMini($store_id, $conf) { $form = new VideoShopForm(); $form->params = [ 'app_id' => $conf['app_id'], 'app_secret' => $conf['app_secret'], 'token' => $conf['token'], 'encodingAesKey' => $conf['encodingAesKey'], ]; $form->store_id = $store_id; $res = $form->addShop(); return $res; } //添加小店 public static function getStoreMini($store_id, &$conf) { if(empty($conf['app_id'])){ return null; } $form = new WechatThirdForm(); $form->appid = $conf['app_id']; $form->store_id = $store_id; $form->fuwu_type = 1; $res = $form->getMiniList(); $mini = $res['data'][0]; if($res['data'][0]){ $conf['app_secret'] = $mini['app_secret']; $conf['token'] = $mini['token']; $conf['encodingAesKey'] = $mini['encodingAesKey']; } return $mini; } //躺赚 public static function isAutoPriceSuccess($store_id, $user_id) { $publicRankingUser = PublicRankingUser::findOne(['user_id' => $user_id]); $conf = self::conf($store_id); $conf_auto_price_child_cnt = $conf['auto_price_child_cnt']; $conf_auto_price_wait_days = $conf['auto_price_wait_days']; if($conf_auto_price_child_cnt <= 0){ return true; } if($conf_auto_price_wait_days > 0 && ($publicRankingUser['created_at'] > (time() - 86400 * $conf_auto_price_wait_days))){ return true; } $childCnt = PublicRankingUser::find()->where(['user_id' => User::find()->select('id')->where(['old_parent_id' => $user_id])])->count(); if($childCnt >= $conf_auto_price_child_cnt){ return true; } return false; } //复购 public static function price2Fugou(VideoShopOrderExt $order) { $store_id = $order->store_id; $promoter_id = $order->promoter_id; debug_log([__METHOD__, __LINE__, '检测复购', $store_id, $promoter_id], __CLASS__ . '.log'); $conf = self::conf($store_id); $conf_price2Fugou = $conf['price2Fugou']; if($conf_price2Fugou <= 0){ debug_log([__METHOD__, __LINE__, '强制复购金额未配置', $store_id, $promoter_id], __CLASS__ . '.log'); return null; } $prom = Promoter::findOne(['store_id' => $store_id, 'promoter_id' => $promoter_id]); if(!$prom){ debug_log([__METHOD__, __LINE__, '达人未绑定', $store_id, $promoter_id], __CLASS__ . '.log'); return null; } $promPriceTotal = VideoShopOrderExt::promoterPriceTotal($store_id, $promoter_id, 0); if($promPriceTotal >= $conf_price2Fugou){ $t = \Yii::$app->db->beginTransaction(); try { $updateAll = VideoShopOrderExt::updateAll(['has_fugou' => 1], ['store_id' => $store_id, 'promoter_id' => $promoter_id, 'has_fugou' => 0, 'commission_status' => 2]); $prom->is_fugou = 1; if(!$prom->save()){ throw new \Exception(array_shift($prom->getFirstErrors())); } $genTaskFugou = PublicRankingTask::genTaskFugou($store_id, $prom->user_id); $t->commit(); debug_log([__METHOD__, __LINE__, '检测复购', $order->id, $updateAll, $genTaskFugou, $prom->id], __CLASS__ . '.log'); return true; } catch (\Exception $e) { $t->rollBack(); \Yii::error($e); debug_log([__METHOD__, __LINE__, '检测复购', $store_id, $order->id, $e->getMessage()], __CLASS__ . '.log'); } } return false; } public static function _promoterList($store_id, $pageSize = 10) { try { $conf = self::conf($store_id); if(!$conf['mini_id']){ throw new \Exception('请先配置参数'); } $url = 'channels/ec/league/promoter/list/get'; $data = [ "page_size" => (int)$pageSize, ]; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { // foreach((array)$res['finder_ids'] as &$item){ // $res['finder_list'][] = self::_promoterGet($store_id, '', $item); // } foreach((array)$res['promoter_ids'] as &$item){ $res['promoter_list'][] = self::_promoterGet($store_id, $item); } return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); debug_log([__METHOD__, __LINE__, $store_id, $data, $res, $e->getMessage()], __CLASS__ . '.log'); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function promoterList($store_id, $params = []) { $conf = self::conf($store_id); $query = Promoter::find()->where(['store_id' => $store_id, 'is_delete' => 0]); $params['promoter_id'] && $query->andWhere(['promoter_id' => $params['promoter_id']]); $params['finder_id'] && $query->andWhere(['finder_id' => $params['finder_id']]); if($params['user_name']){ $query->andWhere(['user_id' => User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'u.binding=su.mobile')->select('u.id')->where(['like', 'su.name', $params['user_name']])]); } if($params['mobile']){ $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'binding', $params['mobile']])]); } if($params['time_begin']){ $query->andWhere(['>=', 'created_at', strtotime($params['time_begin'])]); } if($params['time_end']){ $query->andWhere(['<=', 'created_at', strtotime($params['time_end'])]); } if(isset($params['status']) && $params['status'] > -1){ $query->andWhere(['status' => $params['status']]); } $query->orderBy('id DESC'); $data = pagination_make($query); foreach($data['list'] as &$item){ $item['isPromoterIdSameStoreConf'] = $item['promoter_id'] == $conf['promoter_id']; $item['user'] = User::findOne($item['user_id']); $item['saas_user'] = $item['user'] ? SaasUser::findOne(['mobile' => $item['user']['binding']]) : null; $pInfo = self::_promoterGet($store_id, $item['promoter_id']); $item['promoterInfo'] = $pInfo; if($pInfo['data']){ $item['status'] = $pInfo['data']['status']; } $item['status_name'] = Promoter::statusName($item['status']); $item['priceTotal'] = VideoShopOrderExt::promoterPriceTotal($store_id, $item['promoter_id']); } return [ 'code' => 0, 'msg' => 'ok', 'data' => $data, 'status_list' => Promoter::statusName(), ]; } public static function _promoterGet($store_id, $promoter_id = '', $finder_id = '') { try { $finder_id = $promoter_id ? '' : $finder_id; $conf = self::conf($store_id); $url = 'channels/ec/league/promoter/get'; $data = []; $promoter_id && $data['promoter_id'] = (string)$promoter_id; $finder_id && $data['finder_id'] = (string)$finder_id; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res['promoter'], ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function syncPromoter($store_id, $promoter_id = '') { try { $count = 0; $query = Promoter::find()->where(['store_id' => $store_id]); if($promoter_id){ $query->andWhere(['promoter_id' => $promoter_id]); } $pList = $query->all(); foreach($pList as $p){ $pInfo = self::_promoterGet($store_id, $p->promoter_id); if(!$pInfo['code']){ $status = $pInfo['data']['status']; $ucount = Promoter::updateAll(['status' => $status], ['store_id' => $store_id, 'promoter_id' => $p->promoter_id]); $count += $ucount; if($status == 2 && $ucount){ self::leagueItemBind($store_id, [$p->promoter_id]); } }else{ debug_log([__FUNCTION__, __LINE__, $store_id, $p->promoter_id, $pInfo], __CLASS__); } } return [ 'code' => 0, 'msg' => '同步成功,修改数量:' . $count, 'data' => $count, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _promoterAdd($store_id, $promoter_id = '', $finder_id = '') { try { $finder_id = $promoter_id ? '' : $finder_id; $conf = self::conf($store_id); $url = 'channels/ec/league/promoter/add'; $data = []; $promoter_id && $data['promoter_id'] = (string)$promoter_id; $finder_id && $data['finder_id'] = (string)$finder_id; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _promoterUpd($store_id, $type, $promoter_id = '', $finder_id = '') { try { $finder_id = $promoter_id ? '' : $finder_id; $conf = self::conf($store_id); $url = 'channels/ec/league/promoter/upd'; $data = [ 'type' => $type, ]; $promoter_id && $data['promoter_id'] = (string)$promoter_id; $finder_id && $data['finder_id'] = (string)$finder_id; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _promoterDelete($store_id, $promoter_id = '', $finder_id = '') { try { $finder_id = $promoter_id ? '' : $finder_id; $conf = self::conf($store_id); $url = 'channels/ec/league/promoter/delete'; $data = []; $promoter_id && $data['promoter_id'] = (string)$promoter_id; $finder_id && $data['finder_id'] = (string)$finder_id; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, 'p' => $data, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function promoterAdd($store_id, $user_id, $promoter_id = '', $finder_id = '', $finder_name = '') { $apiProm = self::_promoterGet($store_id, $promoter_id); if($apiProm['code']){ return $apiProm; } $promoter = $apiProm['data']; $apiFind = self::_promoterGet($store_id, $finder_id); if($apiFind['code']){ return $apiFind; } if(!in_array($promoter['status'], [1,2])){ $api = self::_promoterAdd($store_id, $promoter_id, $finder_id); if($api['code']){ return $api; } } $add = Promoter::add($store_id, $user_id, $promoter_id, $finder_id, $finder_name); self::syncPromoter($store_id, $promoter_id); return $add; } public static function promoterReadd($store_id, $promoter_id = '') { return self::_promoterAdd($store_id, $promoter_id); } public static function promoterUpd($store_id, $type, $promoter_id = '', $finder_id = '') { $api = self::_promoterUpd($store_id, $type, $promoter_id, $finder_id); if($api['code']){ return $api; } self::syncPromoter($store_id, $promoter_id); return $api; } public static function promoterDelete($store_id, $promoter_id = '', $finder_id = '') { $api = self::_promoterDelete($store_id, $promoter_id, $finder_id); if($api['code']){ return $api; } self::syncPromoter($store_id, $promoter_id); return $api; } public static function _leagueItemBatchadd($store_id, $type, $list, $promoter_ids = [], $is_forerver = 1, $begin_time = 10, $end_time = 100) { try { $conf = self::conf($store_id); $url = 'channels/ec/league/item/batchadd'; $data = [ 'type' => (int)$type, 'list' => $list, 'promoter_ids' => $promoter_ids, 'is_forerver' => (bool)$is_forerver, ]; if(!$is_forerver){ $data['begin_time'] = (int)$begin_time; $data['end_time'] = (int)$end_time; } $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _leagueItemUpd($store_id, $type, $operate_type, $product_id = null, $info_id = null, $ratio = null, $exclusive_info = null) { try { $conf = self::conf($store_id); $url = 'channels/ec/league/item/upd'; $data = [ 'type' => (int)$type, 'operate_type' => (int)$operate_type, ]; $info_id && $data['info_id'] = (string)$info_id; $product_id && $data['product_id'] = (string)$product_id; $ratio && $data['ratio'] = (int)$ratio; $exclusive_info && $data['exclusive_info'] = $exclusive_info; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, 'p' => $data, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _leagueItemDelete($store_id, $type, $product_id = null, $info_id = null) { try { $conf = self::conf($store_id); $url = 'channels/ec/league/item/delete'; $data = [ 'type' => (int)$type, ]; $info_id && $data['info_id'] = (string)$info_id; $product_id && $data['product_id'] = (string)$product_id; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _leagueItemListGet($store_id, $type, $promoter_id = '', $product_id = null, $page_size = 10, $page_index = 1, $need_total_num = 1) { try { if($type == 1 && $product_id){ return [ 'code' => 0, 'data' => [ 'items' => [['product_id' => $product_id]], ], ]; } $conf = self::conf($store_id); $url = 'channels/ec/league/item/list/get'; $data = [ 'type' => (int)$type, 'page_size' => (int)$page_size, ]; $promoter_id && $data['promoter_id'] = (string)$promoter_id; $product_id && $data['product_id'] = (string)$product_id; $page_index && $data['page_index'] = (int)$page_index; $need_total_num && $data['need_total_num'] = (bool)$need_total_num; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, 'p' => $data, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function _leagueItemGet($store_id, $type, $product_id = null, $info_id = null, $page_size = 10, $page_index = 1, $need_total_num = 1, $need_relation = 1) { try { $conf = self::conf($store_id); $url = 'channels/ec/league/item/get'; $data = [ 'type' => (int)$type, 'page_size' => (int)$page_size, ]; $info_id && $data['info_id'] = (string)$info_id; $product_id && $data['product_id'] = (string)$product_id; $page_index && $data['page_index'] = (int)$page_index; $need_total_num && $data['need_total_num'] = (bool)$need_total_num; $need_relation && $data['need_relation'] = (bool)$need_relation; $miniProgram = WechatMini::getWechatConfig($store_id, $conf['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res['item'], ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public static function shopProductSelectList($store_id) { $list = VideoShopGoodsExt::find()->alias('ge')->leftJoin(['g' => Goods::tableName()], 'g.id=ge.goods_id')->where(['ge.store_id' => $store_id])->select('g.name, ge.product_id')->asArray()->all(); return $list; } public static function leagueItemList($store_id, $type, $promoter_id = '', $product_id = null, $page_size = 10, $page_index = 1, $need_total_num = 1) { $api = self::_leagueItemListGet($store_id, $type, $promoter_id, $product_id, $page_size, $page_index, $need_total_num); if($api['code']){ return $api; } $conf = self::conf($store_id); foreach($api['data']['items'] as &$item){ $item['autoBind'] = in_array($item['info_id'], $conf['info_ids']); $goods_info = Goods::find()->where(['id' => VideoShopGoodsExt::find()->select('goods_id')->where(['store_id' => $store_id, 'product_id' => $item['product_id']])])->one(); $item['goods_info'] = $goods_info; $info = self::_leagueItemGet($store_id, $type, $item['product_id'], $item['info_id'])['data']; if($info['exclusive_info']){ foreach($info['exclusive_info']['promoter_info_list'] as &$pil){ $promoter_id = $pil['promoter_id']; $prom = Promoter::findOne(['store_id' => $store_id, 'promoter_id' => $promoter_id]); $user = $prom['user_id'] ? User::findOne($prom['user_id']) : null; $saasUser = $user ? SaasUser::findOne(['mobile' => $user['binding']]) : null; $pil['prom'] = $prom; $pil['user'] = $user; $pil['saasUser'] = $saasUser; } } $info['status_name'] = self::leagueItemStatusName($info['status']); $info['type_name'] = self::leagueItemTypeName($info['type']); $item['info'] = $info; } return [ 'code' => 0, 'msg' => 'ok', 'data' => $api['data'], ]; } public static function leagueItemAutoBind($store_id, $info_id) { $conf = self::conf($store_id); if(!in_array($info_id, $conf['info_ids'])){ $conf['info_ids'][] = $info_id; }else{ $conf['info_ids'] = array_diff($conf['info_ids'], [$info_id]); } $conf['info_ids'] = array_unique($conf['info_ids']); return self::confSave($store_id, ['info_ids' => $conf['info_ids']]); } public static function leagueItemBind($store_id, $promoter_ids = []) { $conf = self::conf($store_id); $type = 2; foreach((array)$conf['info_ids'] as $info_id){ $info = self::_leagueItemGet($store_id, $type, '', $info_id)['data']; if(!$info){ return null; } $exclusive_info = ['add_promoter_ids' => $promoter_ids]; if($info['exclusive_info']['promoter_info_list'][0]['is_forerver']){ $exclusive_info['is_forerver'] = true; }else{ $exclusive_info['begin_time'] = $info['exclusive_info']['promoter_info_list'][0]['begin_time']; $exclusive_info['end_time'] = $info['exclusive_info']['promoter_info_list'][0]['end_time']; } $bind = self::leagueItemUpd($store_id, $type, 1, '', $info_id, $info['ratio'], $exclusive_info); debug_log([$store_id, $promoter_ids, $info_id, $bind], __CLASS__); } return $bind; } public static function leagueItemAdd($store_id, $type, $list, $promoter_ids, $is_forerver, $begin_time, $end_time) { $api = self::_leagueItemBatchadd($store_id, $type, $list, $promoter_ids, $is_forerver, $begin_time, $end_time); return $api; } public static function leagueItemUpd($store_id, $type, $operate_type, $product_id, $info_id, $ratio, $exclusive_info) { $api = self::_leagueItemUpd($store_id, $type, $operate_type, $product_id, $info_id, $ratio, $exclusive_info); return $api; } public static function leagueItemDelete($store_id, $type, $product_id, $info_id) { $api = self::_leagueItemDelete($store_id, $type, $product_id, $info_id); return $api; } public static function publicRankingUserList($store_id, $params = []) { $query = PublicRankingUser::find()->where(['store_id' => $store_id, 'is_delete' => 0]); if(!empty($params['promoter_id'])){ $query->andWhere(['user_id' => Promoter::find()->select('user_id')->where(['like', 'promoter_id', $params['promoter_id']])]); } if($params['user_name']){ $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'nickname', $params['user_name']])]); } if($params['mobile']){ $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'binding', $params['mobile']])]); } if($params['time_begin']){ $query->andWhere(['>=', 'created_at', strtotime($params['time_begin'])]); } if($params['time_end']){ $query->andWhere(['<=', 'created_at', strtotime($params['time_end'])]); } $query->orderBy('sort'); $data = pagination_make($query); foreach($data['list'] as &$item){ $item['promoter'] = Promoter::findOne(['user_id' => $item['user_id']]); $item['user'] = User::findOne($item['user_id']); $item['saas_user'] = $item['user'] ? SaasUser::findOne(['mobile' => $item['user']['binding']]) : null; $buyOrderCnt = (int)VideoShopOrderExt::find()->where(['store_id' => $store_id, 'order_id' => Order::find()->select('id')->where(['user_id' => $item['user_id']])])->andWhere(['!=', 'promoter_id', ''])->count(); $item['buy_order_cnt'] = $buyOrderCnt; $childOrderCnt = $item['promoter'] ? (int)VideoShopOrderExt::find()->where(['store_id' => $store_id, 'promoter_id' => $item['promoter']['promoter_id']])->count() : 0; $item['child_order_cnt'] = $childOrderCnt; $item['task'] = PublicRankingTask::findAll(['user_id' => $item['user_id']]); } return [ 'code' => 0, 'msg' => 'ok', 'data' => $data, ]; } public static function publicRankingUserIndex($store_id, $user_id) { try{ if(!self::isopen($store_id)){ throw new \Exception('商城功能未开启'); } $user = User::findOne($user_id); if(!$user['wechat_union_id']){ return [ 'code' => 1, 'msg' => '缺少unionid,请退出账号重新登陆', ]; } $conf = self::conf($store_id); $prom = Promoter::findOne(['store_id' => $store_id, 'user_id' => $user_id]); $promPriceTotal = !$prom ? 0 : VideoShopOrderExt::promoterPriceTotal($store_id, $prom['promoter_id']); $taskIsOver = PublicRankingTask::taskIsOver($store_id, $user_id); $task1 = PublicRankingTask::findOne(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 1]); $taskFinshCount = PublicRankingTask::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 1])->count(); // $user = User::findOne($user_id); $publicRankingUser = PublicRankingUser::findOne(['user_id' => $user_id]); if($publicRankingUser && $publicRankingUser->sort > 1){ $psort = PublicRankingUser::getParentSorts($publicRankingUser->sort, $conf['PublicRankingRow'], 1); $publicRankingUserParent = PublicRankingUser::findOne(['store_id' => $store_id, 'sort' => $psort]); $publicRankingUserParentUser = $publicRankingUserParent->user_id ? User::findOne($publicRankingUserParent->user_id) : null; $publicRankingUserParentSaasUser = $publicRankingUserParentUser ? SaasUser::findOne(['mobile' => $publicRankingUserParentUser->binding]) : null; } $parentUser = $user->old_parent_id ? User::findOne($user->old_parent_id) : null; $parentSaasUser = $parentUser ? SaasUser::findOne(['mobile' => $parentUser->binding]) : null; $isAutoPriceSuccess = self::isAutoPriceSuccess($store_id, $user_id); $isPriceSuccess = !$prom->is_fugou && $isAutoPriceSuccess; $childCnt = PublicRankingUser::find()->where(['user_id' => User::find()->select('id')->where(['old_parent_id' => $user_id])])->count(); return [ 'code' => 0, 'data' => [ 'conf' => $conf, 'publicRankingUserParentUser' => $publicRankingUserParentUser, 'publicRankingUserParentSaasUser' => $publicRankingUserParentSaasUser, 'parentUser' => $parentUser, 'parentSaasUser' => $parentSaasUser, 'publicRankingUser' => $publicRankingUser, 'user' => $user, 'saas_user' => get_saas_user(), 'prom' => $prom, 'taskIsOver' => $taskIsOver, 'canToTask' => $publicRankingUser && !$taskIsOver, 'canBind' => $publicRankingUser && (!$prom || (!in_array($prom['status'], [0,1,2]))) && $task1, 'promPriceTotal' => $promPriceTotal, 'taskFinshCount' => $taskFinshCount, 'isAutoPriceSuccess' => $isAutoPriceSuccess, 'isPriceSuccess' => $isPriceSuccess, 'childCnt' => $childCnt, ], ]; } catch (\Exception $e) { \Yii::error($e); debug_log([__METHOD__, __LINE__, $e->getMessage()], __CLASS__ . '.log'); return [ 'code' => 1, 'msg' => '操作失败,' . $e->getMessage() ]; } } public static function publicRankingUserIndexTask($store_id, $user_id, $task_id = 0) { if($task_id){ $task = [ 'code' => 0, 'data' => PublicRankingTask::findAll(['id' => $task_id]), ]; }else{ $task = PublicRankingTask::getTask($store_id, $user_id); if($task['code']){ return $task; } } $conf = self::conf($store_id); $openTask = 0; foreach($task['data'] as &$item){ $item = $item->toArray(); $prom = [ 'finder_name' => $conf['finder_name'], //视频号名字 'finder_id' => $conf['finder_id'], //视频号id 'promoter_id' => $conf['promoter_id'], //达人带货id ]; if($item['promoter_id']){ $prom = Promoter::findOne($item['promoter_id']); } $item['prom'] = $prom; $item['openTask'] = 0; if(!$openTask && $item['status'] == 0){ $openTask = 1; $item['openTask'] = 1; } } return $task; } public static function publicRankingUserIndexPromAdd($store_id, $user_id, $promoter_id, $finder_id, $finder_name) { $task = self::promoterAdd($store_id, $user_id, $promoter_id, $finder_id, $finder_name); return $task; } }