| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\publicRanking;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\User;
- use app\models\SaasUser;
- use app\utils\Notice\NoticeSend;
- use app\modules\client\models\v1\ShareQrcodeForm;
- use app\utils\Baidu\BaiduAiTools;
- use app\utils\DeepSeek\DeepSeekAiTools;
- use app\models\AccountLog;
- use app\models\GivingGiftsOrder;
- use app\models\GivingGiftsGoods;
- use app\models\Order;
- use app\modules\admin\models\VideoShopForm;
- use app\modules\admin\models\WechatThirdForm;
- use app\utils\Wechat\WechatMini;
- use EasyWeChat\Kernel\BaseClient;
- use app\models\Promoter;
- use app\models\Goods;
- use app\models\VideoShopGoodsExt;
- use app\models\PublicRankingUser;
- use app\models\VideoShopOrderExt;
- use app\models\PublicRankingTask;
- class PublicRankingForm extends Model {
- public $store_id;
- public $id;
- public $ids;
- public $goods_name;
- public $supplier_name;
- public $goods_id;
- public $cat_id;
- public $supplier_id;
- public $warehouse_id;
- public $warehouse_zone_id;
- public $is_delete;
- public $warning_num;
- public $eiId;
- public $eipId;
- public $nickname;
- public $phone;
- public $order_no;
- public $type;
- public $purchase_in_price;
-
- public static function leagueItemStatusName($status = null) {
- $arr = [
- 1 => '已上架推广',
- 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;
- }
- }
|