| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528 |
- <?php
- namespace backend\modules\mall\controllers;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\models\RedpackWallet;
- use addons\Store\common\models\StoreCouponUserRelate;
- use addons\Store\common\services\StoreAssociatedUserService;
- use addons\UserSync\common\models\UserSync;
- use common\enums\AddonsEnum;
- use common\enums\DownloadEnum;
- use common\enums\LevelUpgradeLogEnum;
- use common\enums\MemberLabelEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\UserWalletEnum;
- use common\events\census\UserUpgradeAfterEvent;
- use common\events\user\UserCancellationEvent;
- use common\events\user\UserModifyPayPwdEvent;
- use common\events\user\UserModifyPwdEvent;
- use common\events\user\UserUpdateEvent;
- use common\helpers\ArrayHelper;
- use common\helpers\csv\CsvExportHelper;
- use common\helpers\RegularHelper;
- use common\logic\memberLabel\MemberLabelLogic;
- use common\models\census\CensusMallSingleMonth;
- use common\models\common\Addons;
- use common\models\common\ChangeMobileLog;
- use common\models\elasticsearch\user\LoginLog;
- use common\models\mall\MallAddons;
- use common\models\user\MemberLabel;
- use common\models\user\UserActionRecord;
- use common\models\user\UserDigital;
- use common\models\user\UserImportFailLog;
- use backend\controllers\MallController;
- use common\components\CsvExport;
- use common\enums\StatusEnum;
- use common\models\user\Tag;
- use common\models\user\User;
- use common\models\user\UserExtra;
- use common\models\user\UserImportLog;
- use common\models\user\UserInfo;
- use common\models\user\UserLabel;
- use common\models\user\UserLabelRelationship;
- use common\models\user\UserLevel;
- use common\models\user\UserLevelExpired;
- use common\models\user\UserLevelUpgradeLog;
- use common\models\user\UserMemberLabel;
- use common\models\user\UserPartitionRelationship;
- use common\models\user\UserRelationshipLog;
- use common\models\user\UserTagRelate;
- use common\models\user\UserTeamStatistics;
- use common\models\user\UserWallet;
- use common\services\mall\user\UserBehaviorLogsService;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- use common\models\statistics\UserStatisticsTotal;
- use common\models\common\CapitalLog;
- use common\enums\EventNameEnum;
- use common\events\user\UserRegisterEvent;
- use common\logic\statistics\StatisticsHandle;
- use common\models\common\MallSetting;
- use common\models\user\UserFreezeCommission;
- /**
- * 会员控制器
- *
- * Class IdentityController
- * @package backend\modules\mall\controllers
- */
- class UserController extends MallController
- {
- /**
- * 会员首页
- * @return mixed|string|void
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $type_arr = DownloadEnum::getTypeMap();
- $post['mall_id'] = $this->mall_id;
- $post['type'] = DownloadEnum::TYPE_MEMBER;
- $post['status'] = DownloadEnum::STATUS_DOWNING;
- $post['name'] = $type_arr[DownloadEnum::TYPE_MEMBER] . '列表' . date('YmdHis');
- $filename = '会员列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_MEMBER, User::class, 'exportHandle', $post);
- if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- if (Yii::$app->request->isGet) {
- $search = \Yii::$app->request->get('search');
- $isChanjetAddons = \Yii::$app->request->get('is_chanjet_addons');
- !empty($search) && $params = Json::decode($search, true);
- $sort = \Yii::$app->request->get('sort', 'user_id desc');
- // 查询参数
- $where = [];
- $where[] = ['=', 'u.mall_id', $this->mall_id];
- $where[] = ['in', 'u.status', [StatusEnum::ENABLED,StatusEnum::DISABLED]];
- $week_time = strtotime('-7 day');// 七天前
- $month_time = strtotime('-30 day');// 一个月前
- // 初始化数据
- $data = [
- 'channel' => [],
- 'tag_list' => [],
- 'member_label_List' => [],
- 'currency_list' => [],
- 'mall_data' => [
- 'user_total' => 0,
- 'new_user_total' => 0,
- 'active_user_total' => 0,
- 'loss_user_total' => 0,
- 'no_pay_user_total' => 0,
- 'o2o_user_total' => 0,
- 'updated_at' => '该商城暂未更新',
- ],
- 'user_list' => [],
- 'is_freeze_commission' => UserFreezeCommission::getSwitchStatus($this->mall_id),
- ];
- if (!empty($params['user_type'])) {
- switch ($params['user_type']) {
- case 1:// 新增会员
- $where[] = ['>=', 'u.created_at', $week_time];
- break;
- case 2:// 活跃会员
- $where[] = ['>=', 'u.last_login_at', $week_time];
- break;
- case 3:// 近7天有购买行为,但没有成功付款的客户
- // $where[] = ['>=', 'ua.last_no_pay_at', $week_time];
- break;
- case 4:// 流失会员
- $where[] = ['<', 'u.last_login_at', $month_time];
- break;
- case 5:// 线下会员
- $where[] = ['=', 'u.source', User::SOURCE_SHARE_O2O];
- break;
- }
- }
- if (\Yii::$app->request->get('page') == 1) {
- // 判断用户插件是否开启
- // 获取标签列表
- $tag_list = UserLabel::getUserLabel([['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED]], [], true, 'id tag_id,name');
- if ($tag_list) $data['tag_list'] = $tag_list;
- $data['channel'] = array_merge(['all' => '全部'], User::TYPE_NAME_LIST);
- $memberLabelList = MemberLabel::lists(['where'=>[
- ['mall_id'=>$this->mall_id],
- ['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]
- ]
- ],'order'=>'cate_id asc','select'=>'id,name,cate_id']);
- if (!empty($memberLabelList)){
- $cateMap = MemberLabelEnum::getMap();
- foreach ($memberLabelList as &$val){
- if(isset($cateMap[$val['cate_id']]))$val['name'] = $cateMap[$val['cate_id']].' : '. $val['name'];
- }
- $data['member_label_List'] = $memberLabelList;
- }
- }
- // 用户条件筛选搜索
- // 根据用户id精准查找
- if (!empty($params['user_id'])) $where[] = ['=', 'u.id', $params['user_id']];
- // 根据用户团队查找
- if (isset($params['channel']) && $params['channel'] != 'all') $where[] = ['u.platform' => $params['channel']];
- // 根据用户昵称或手机号码查找
- if (!empty($params['nickname_or_mobile'])) $where[] = [
- 'OR',
- ['like', 'u.nickname', $params['nickname_or_mobile']],
- ['like', 'u.mobile', $params['nickname_or_mobile']],
- ];
- //根据智能标签进行搜索
- if (!empty($params['lableValue'])){
- $user_label_where = ['mall_id' =>$this->mall_id,'status' => StatusEnum::ENABLED,'label_id' => $params['lableValue']];
- $user_ids = UserLabelRelationship::find()->select("user_id")->where($user_label_where)->asArray()->column();
- $where[] = ['u.id'=>$user_ids ];
- }
- // 查找上级推荐人手机号
- if (isset($params['p_mobile']) && $params['p_mobile'] !== '') {
- $where[] = [
- 'in',
- 'u.parent_id',
- User::find()
- ->where([
- 'mall_id' => $this->mall_id,
- 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]
- ])
- ->andWhere(['like', 'mobile', $params['p_mobile']])
- ->select('id')
- ->column()
- ];
- }
- // 查找上级推荐人
- if (!empty($params['parent_id'])) $where[] = ['=', 'u.parent_id', $params['parent_id']];
- // 查找注册来源团队
- if (!empty($params['source']) && $params['source'] != 'all') $where[] = ['=', 'u.platform', $params['source']];
- //添加等级查询
- $levels = UserLevel::getListIndexByLevelColumn($this->mall_id, [StatusEnum::ENABLED, StatusEnum::DISABLED]);
- $default_level_name = Yii::$app->services->setting->mall->get($this->mall_id,'user.default_level_name','basic',true);
- if (empty($default_level_name)) $default_level_name = '普通会员';
- $levels[0] = $default_level_name;
- $data['levels'] = $levels;
- if (isset($params['level']) && $params['level'] !== '') $where[] = ['=', 'u.level', $params['level']];
- // if (!empty($params['level'])) $where[] = ['=', 'u.level', $params['level']];
- // 查找会员标签
- // if (!empty($params['tag_id'])) {
- //
- // }
- // 查找注册时间
- if (!empty($params['register_start']) && !empty($params['register_end'])) $where[] = ['between', 'u.created_at', strtotime($params['register_start']), strtotime($params['register_end'])];
- // 最后更新时间
- if (!empty($params['update_start']) && !empty($params['update_end'])) $where[] = ['between', 'u.updated_at', strtotime($params['update_start']), strtotime($params['update_end'])];
- // 查找上次消费时间
- // if (!empty($params['pay_start']) && !empty($params['pay_end'])) $where[] = ['between', 'ua.last_pay_at', strtotime($params['pay_start']), strtotime($params['pay_end'])];
- // 查找余额范围
- if (!empty($params['balance_start']) && !empty($params['balance_end'])) $where[] = ['between', 'uw.balance', $params['balance_start'], $params['balance_end']];
- if (!empty($params['balance_start']) && empty($params['balance_end']) ) $where[] = ['>', 'uw.balance', $params['balance_start']];
- if (!empty($params['balance_end']) && empty($params['balance_start'])) $where[] = ['<', 'uw.balance', $params['balance_end']];
- // 查找积分范围
- if (!empty($params['score_start']) && !empty($params['score_end'])) $where[] = ['between', 'uw.score', $params['score_start'], $params['score_end']];
- if (!empty($params['score_start']) && empty($params['score_end'])) $where[] = ['>', 'uw.score', $params['score_start']];
- if (empty($params['score_start']) && !empty($params['score_end'])) $where[] = ['<', 'uw.score', $params['score_end']];
- // 查找佣金范围
- if (!empty($params['commission_start']) && !empty($params['commission_end'])) $where[] = ['between', 'uw.commission', $params['commission_start'], $params['commission_end']];
- if (!empty($params['commission_start']) && empty($params['commission_end'])) $where[] = ['>', 'uw.commission', $params['commission_start']];
- if (empty($params['commission_start']) && !empty($params['commission_end'])) $where[] = ['<', 'uw.commission', $params['commission_end']];
- // 查找累计消费金额范围
- // if (!empty($params['price_total_start']) && !empty($params['price_total_end'])) $where[] = ['between', 'us.pay_order_price_total', $params['price_total_start'], $params['price_total_end']];
- // 查找购买次数范围
- // if (!empty($params['order_total_start']) && !empty($params['order_total_end'])) $where[] = ['between', 'us.pay_order_total', $params['order_total_start'], $params['order_total_end']];
- //根据会员标签进行搜索
- if (!empty($params['memberLabelIds'])){
- $cateIdArr = [];
- foreach ($params['memberLabelIds'] as $value){
- $temp_arr = explode('_', $value);
- $cateIdArr[$temp_arr[0]][] = $temp_arr[1];
- }
- foreach ($cateIdArr as $cate_id => $memberLabelIds) {
- $where[] = ['uml.'.'cate_id' . $cate_id=>$memberLabelIds];
- }
- }
- // 黑名单
- if (!empty($params['black'])) {
- $where[] = ['=', 'u.status', $params['black'] == 2 ? 1 : 0];
- }
- // 联表查询
- $joins = [
- ['LEFT JOIN', UserWallet::tableName() . 'uw', 'uw.user_id = u.id'],
- ];
- if (!empty($params['memberLabelIds'])){
- $joins[] = [
- 'LEFT JOIN', UserMemberLabel::tableName() . 'uml', 'uml.user_id = u.id'
- ];
- }
- $select = 'u.id,u.id as user_id,nickname,avatar_url,mobile,level,u.status,platform,u.is_inviter,
- balance,commission as income,u.source,score,u.created_at,u.parent_id';
- if (Addons::isInstall(AddonsEnum::ADDONS_NAME_CHANJET)) {
- $select .= ',frozen_commission,total_commission';
- }
- $order = $sort;
- $with = ['userLabel', 'userFreezeCommission', 'userLevelExpired'];
- // 畅捷通
- if ($isChanjetAddons) {
- $with[] = 'addonsChanjetRequest';
- $select .= ',u.updated_at';
- $exportList = User::chanjetFieldsList();
- }
- $params = array('alias' => 'u', 'select' => $select, 'where' => $where, 'with' => $with, 'join' => $joins, 'order' => $order, 'limit' => 10);
- $user_data = User::listPage($params, false, true);
- if (empty($user_data)) return $this->success('操作成功', $data);
- $default_parent = ['id' => 0, 'nickname' => '平台', 'avatar_url' => ''];
- // 获取用户user_id
- $user_ids = array_column($user_data['list'], 'user_id');
- // 获取用户推荐人
- $parent_ids = array_column($user_data['list'], 'parent_id');
- $parent_list = User::getUserList([['in', 'id', $parent_ids]], 'id,nickname,avatar_url', [], 1);
- $parent = [];
- foreach ($parent_list as $v) {
- $parent[$v['id']] = $v;
- }
- $user_action_arr = UserActionRecord::find()->where(['user_id'=>$user_ids,'mall_id'=>$this->mall_id])->indexBy('user_id')->all();
- // 获取用户标签
- // $user_tag_list = UserTagRelate::getUserTagList($this->mall_id, $user_ids);
- $user_tag_list = UserLabel::getUserLabel([['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED]], [], true, 'id,name');
- $user_tag_arr = [];
- foreach ($user_tag_list as $v) {
- $user_tag_arr[$v['id']][] = ['tag_id' => $v['id'], 'name' => $v['name']];
- }
- $item['tag_name'] = empty($user_tag) ? [] : $user_tag;
- $user_level_arr = UserLevel::getUserLevelArr($this->mall_id);
- foreach ($user_data['list'] as &$item) {
- $item['last_pay_at'] =!empty($user_action_arr[$item['id']]['last_pay_at'])? date('Y-m-d H:i:s',$user_action_arr[$item['id']]['last_pay_at']):'暂无消费';
- $item['nickname'] = empty($item['nickname']) ? '--' : $item['nickname'];
- $item['level_name'] = empty($user_level_arr[$item['level']]) ? MallSetting::getDefaultLevelName($this->mall_id) : $user_level_arr[$item['level']];// 会员等级名称
- $item['source'] = empty(User::$sources[$item['source']]) ? '' : User::$sources[$item['source']];// 来源
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);// 注册时间
- // $item['average_price'] = $item['pay_order_total'] == 0 ? 0 : bcdiv($item['pay_order_price_total'], $item['pay_order_total'], 2);// 平均客单价
- $item['average_price'] = 0;// 平均客单价
- $item['parent'] = (!empty($direct_id_arr[$item['user_id']]) && $parent[$direct_id_arr[$item['user_id']]]) ? $parent[$direct_id_arr[$item['user_id']]] : $default_parent;
- $item['parent'] = !empty($parent[$item['parent_id']])? $parent[$item['parent_id']] : $default_parent;
- $item['platform'] = empty($item['platform']) ? '其他' : (User::TYPE_NAME_LIST)[$item['platform']];
- $item['tag_name'] = empty($user_tag_arr[$item['user_id']]) ? [] : $user_tag_arr[$item['user_id']];
- if(empty($item['userFreezeCommission'])){
- $item['userFreezeCommission'] = ['user_id' => $item['id'], 'freeze_scale' => 0, 'freeze_cap' => 0, 'freeze_amount' => 0,'not_freeze_amount' => 0];
- }else{
- $item['userFreezeCommission']['not_freeze_amount'] = bcsub($item['income'], $item['userFreezeCommission']['freeze_amount'],2);
- $item['userFreezeCommission']['not_freeze_amount'] = max($item['userFreezeCommission']['not_freeze_amount'], 0);
- }
- $userLevelExpired = array_column($item['userLevelExpired'], 'expired_at', 'level');
- $item['expired_at_text'] = !empty($userLevelExpired[$item['level']]) ? date('Y-m-d H:i:s', $userLevelExpired[$item['level']]) : '--';
- }
- $data['user_list'] = $user_data;
- $data['export_list'] = $exportList ?? User::fieldsList();
- // 获取已安装的插件
- $data['install_addons'] = MallAddons::getValidAddons($this->mall_id);
- return $this->success('操作成功', $data);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 会员详情
- * @return mixed|string|void
- */
- public function actionUserDetail()
- {
- if (\Yii::$app->request->isAjax) {
- $user_id = Yii::$app->request->get('id', '');
- if (empty($user_id)) return $this->error('会员id不能为空');
- $is_show_user_relationship_log = 0;
- if(!isset(Yii::$app->params['is_show_user_relationship_log'])||Yii::$app->params['is_show_user_relationship_log']==1) {
- $is_show_user_relationship_log = 1;
- }
- return $this->successStr('操作成功', [
- 'detail' => User::getUserDetail($this->mall_id, $user_id),
- 'default_level_name' => MallSetting::getDefaultLevelName($this->mall_id),
- 'is_show_user_relationship_log'=>$is_show_user_relationship_log,
- ]);
- }
- return $this->render($this->action->id);
- }
- /**
- * 关系设置
- * @return mixed|string|void
- */
- public function actionRelationEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $relation_setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'user_relation_setting.relation_setting');
- $data = [];
- if (!empty($relation_setting)) $data = json_decode($relation_setting, true);
- return $this->success('ok', $data);
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['get_power_way', 'buy_num_selected', 'buy_num', 'buy_price_selected',
- 'buy_goods_selected', 'buy_goods_way', 'buy_compute_way', 'become_child_way', 'status',
- 'goods_ids', 'cat_ids', 'cat_list', 'goods_list', 'notice', 'protocol','buy_price'
- ], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $data['user_relation_setting'] = ['relation_setting' => json_encode($params)];
- \Yii::$app->services->setting->mall->set($this->mall_id, $data);
- if ($res === false) return $this->error('设置失败');
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render($this->action->id);
- }
- /**
- * 后台添加会员
- * @return mixed|string|void
- */
- public function actionAdd()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $list = UserLevel::getUserLevel([['mall_id' => $this->mall_id]], [], 1);
- return $this->success('ok', $list);
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $mall_id= $this->mall_id;
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['nickname', 'mobile', 'level',], 'required'],
- [['parent_id','username'], 'safe'],
- [['mobile'], 'match', 'pattern' => RegularHelper::mobile(), 'message' => '请输入正确的手机号码'],
- ['mobile', function ($attribute) use ($mall_id) {
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['mobile' => trim($this->$attribute)];
- if (User::getUser($where)) $this->addError($attribute, '手机号码已存在');
- }],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $password = substr($params['mobile'],-6,6);
- $params['mall_id'] = $this->mall_id;
- if(empty($params['username'])){
- $params['username'] = $params['mobile'];
- }
- $params['source'] = User::SOURCE_ADD_PLATFORM;
- $params['platform'] = User::TYPE_MANUAL;
- $params['password'] = Yii::$app->getSecurity()->generatePasswordHash($password);
- $model = User::setData($params);
- if (empty($model)) throw new Exception(User::$static_error);
- !empty($model->id) && $this->memberExpired(['mall_id' => $mall_id, 'user_id' => $model->id, 'after_level' => $params['level']]);
- // 记录升级日志(如果创建的不是默认等级,则认为手动升级)
- if ($model && $model->level > 0) {
- $upgrade_log_obj = new UserLevelUpgradeLog();
- $upgrade_log_obj->admin_id = $this->admin->id;
- $upgrade_log_obj->mall_id = $this->mall_id;
- $upgrade_log_obj->user_id = $model->id;
- $upgrade_log_obj->before_level = 0;
- $upgrade_log_obj->after_level = $model->level;
- $upgrade_log_obj->upgrade_type = LevelUpgradeLogEnum::MANUALLY_UPGRADE;
- $upgrade_log_obj->save();
- }
- //添加用户数量统计
- $event = new UserRegisterEvent($this->mall_id);
- $data = ['user_id' => $model->id, 'platform' => User::TYPE_MANUAL];
- Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render($this->action->id);
- }
- /**
- * 修改密码
- * @return mixed|void
- * @throws \yii\base\Exception
- */
- public function actionUpdatePassword()
- {
- $params = Yii::$app->request->post();
- $res = User::updateAll(['password' => Yii::$app->getSecurity()->generatePasswordHash($params['password'])], ['id' => $params['user_id']]);
- if ($res === false) return $this->error(User::getStaticError());
- // 修改密码事件
- $event = new UserModifyPwdEvent($this->mall_id);
- Yii::$app->services->events->dispatch($event, [
- 'operator_user_id' => $this->admin->id,
- 'new_pwd' => trim($params['password']),
- 'is_admin' => 1,
- 'user_id' => $params['user_id'],
- 'mall_id' => $this->mall_id
- ], $event->listeners);
- return $this->success('操作成功');
- }
- /**
- * 黑白名单
- * @return mixed|void
- */
- public function actionAddBlacklist()
- {
- $params = Yii::$app->request->post();
- // $res = User::updateAll(['status' => $params['status']], ['in', 'id', explode(',', $params['user_id'])]);
- // if ($res === false) return $this->error(User::getStaticError());
- // return $this->success('操作成功');
- try {
- $params['mall_id'] = $this->mall_id;
- User::addBlacklist($params);
- if(empty($params['status'])){
- $event = new UserCancellationEvent($this->mall_id);
- \Yii::$app->services->events->dispatch($event, [
- 'mall_id' => $this->mall_id,
- 'user_id' => $params['user_id'],
- 'is_admin' => 1,
- 'operator_user_id' => $this->admin->id
- ], $event->listeners);
- }
- return $this->success('操作成功');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 积分充值
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionScore()
- {
- $admin = Yii::$app->getUser()->identity;
- $params = Yii::$app->request->post();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['in', 'id', explode(',', $params['user_id'])];
- $user_list = User::getUserList($where);
- $score_name = \Yii::$app->services->setting->mall->get($this->mall_id, UserWalletService::WALLET_TYPE_SCORE.'.score_name');
- $score_name =$score_name?? '积分';
- $remark = $params['remark'] ? ',备注信息['. $params['remark'] .']' : '';
- $pre = $params['num'] > 0 ? '充值' : '扣减';
- foreach ($user_list as $user) {
- $desc = '管理员'.$admin->username.' 后台' . $pre .$params['num'].$score_name.$remark;
- if($params['type']==3){
- $params['num'] = -$user['userWallet']['score'];
- $desc = '管理员'.$admin->username.' 后台清空'.$params['num'].$score_name.$remark;
- }
- $insert_wallet [] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $user->id,
- 'is_frozen' => false,
- 'wallet_type' =>UserWalletService::WALLET_TYPE_SCORE,
- 'money' => $params['num'],
- 'desc' => $desc,
- 'source_table' => 'admin',
- 'source_table_id' => $admin->id,
- 'from_type' => $params['num'] > 0 ? UserWalletEnum::RECHARGE : UserWalletEnum::DEDUCT,
- 'capital_type' => $params['num'] > 0 ? UserWalletEnum::RECHARGE_PLATFORM : UserWalletEnum::BACKEND_DEDUCT,
- ];
- }
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if ($res === false) return $this->error(UserWalletService::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * 后台余额充值
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionBalance()
- {
- $admin = Yii::$app->getUser()->identity;
- $params = Yii::$app->request->post();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['in', 'id', explode(',', $params['user_id'])];
- $user_list = User::getUserList($where);
- $balance_name = \Yii::$app->services->setting->mall->get($this->mall_id, UserWalletService::WALLET_TYPE_BALANCE.'.balance_name');
- $balance_name = $balance_name?? '余额';
- $remark = $params['remark'] ? ',备注信息['. $params['remark'] .']' : '';
- $pre = $params['price'] > 0 ? '充值' : '扣减';
- foreach ($user_list as $user) {
- $desc = '管理员'.$admin->username.' 后台' . $pre .$params['price'].$balance_name.$remark;
- if($params['type']==3){
- $params['price'] = -$user['userWallet']['balance'];
- $desc = '管理员'.$admin->username.' 后台清空'.$params['num'].$balance_name.$remark;
- }
- $insert_wallet [] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $user->id,
- 'is_frozen' => false,
- 'wallet_type' =>UserWalletService::WALLET_TYPE_BALANCE,
- 'money' => $params['price'],
- 'desc' =>$desc,
- 'source_table' => 'admin',
- 'source_table_id' => $admin->id,
- 'from_type' => $params['price'] > 0 ? UserWalletEnum::RECHARGE : UserWalletEnum::DEDUCT,
- 'capital_type' => $params['price'] > 0 ? UserWalletEnum::RECHARGE_PLATFORM : UserWalletEnum::BACKEND_DEDUCT,
- ];
- }
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if ($res === false) return $this->error(UserWallet::getStaticError());
- if ($params['price'] > 0) {
- $user_ids = explode(',', $params['user_id']);
- foreach ($user_ids as $user_id) {
- Yii::$app->services->statistics->user->set($this->mall_id, $user_id, ['recharge_money' => $params['price']]);
- UserStatisticsTotal::setUserStatisticsTotal($this->mall_id, $user_id, ['recharge_money' => $params['price']]);
- }
- $data = ['total_recharge_money' => $params['price'] * count($user_ids)];
- CensusMallSingleMonth::setSingleMonthCache($this->mall_id, 'total_recharge_money', $data, 'total_recharge_money');
- }
- return $this->success('操作成功');
- }
- /**
- * 后台佣金充值
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionCommission()
- {
- $admin = Yii::$app->getUser()->identity;
- $params = Yii::$app->request->post();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['in', 'id', explode(',', $params['user_id'])];
- $user_list = User::getUserList($where);
- $commission_name = '佣金';
- $remark = $params['remark'] ? ',备注信息['. $params['remark'] .']' : '';
- $pre = $params['price'] > 0 ? '充值' : '扣减';
- foreach ($user_list as $user) {
- $desc = '管理员'.$admin->username.' 后台'. $pre .$params['price'].$commission_name.$remark;
- if($params['type']==3){
- $params['price'] = -$user['userWallet']['commission'];
- $desc = '管理员'.$admin->username.' 后台清空'.$params['num'].$commission_name.$remark;
- }
- $insert_wallet [] = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $user->id,
- 'is_frozen' => false,
- 'wallet_type' =>UserWalletService::WALLET_TYPE_COMMISSION,
- 'money' => $params['price'],
- 'desc' =>$desc,
- 'source_table' => 'admin',
- 'source_table_id' => $admin->id,
- 'from_type' => $params['price'] > 0 ? UserWalletEnum::RECHARGE : UserWalletEnum::DEDUCT,
- 'capital_type' => $params['price'] > 0 ? UserWalletEnum::RECHARGE_PLATFORM : UserWalletEnum::BACKEND_DEDUCT,
- ];
- }
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if ($res === false) return $this->error(UserWallet::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * 红包充值
- * @Author: ltm
- * @DateTime: 2022/3/23 0023 17:38
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public function actionRedpack(){
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- // 验证
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'money','type'], 'required'],
- [['money'], 'number'],
- [['type','desc'], 'string'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if($params['type'] == RedpackEnum::WALLET_REDPACK_FROZEN) {
- $is_frozen = true;
- }else{
- $is_frozen = false;
- }
- //接收红包者
- $wallet = RedpackWallet::setData([
- 'mall_id' => $this->mall_id,
- 'user_id' => $params['user_id'],
- 'is_frozen' => $is_frozen,
- 'is_deduct' => true,
- 'money' => $params['money'],
- 'from_type' => RedpackEnum::FROM_RECHARGE,
- 'desc' => $params['desc'] ?? "手动充值",
- ]);
- if ($wallet == false) throw new Exception(RedpackWallet::getStaticError());
- return $this->success('操作成功');
- }
- return $this->success('操作成功');
- }
- /**
- * 批量加标签
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionAddTagUser()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'tag_ids'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id_arr = json_decode($params['user_id'], true);
- $tag_id_arr = json_decode($params['tag_ids'], true);
- foreach ($user_id_arr as $user_id) {
- $res = UserTagRelate::setData($user_id, $tag_id_arr);
- if ($res === false) return $this->error(User::getStaticError());
- }
- return $this->success('操作成功');
- }
- /**
- * 单个用户加标签-已废除
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionEditUserTag()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'tag_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $tag_id_arr = json_decode($params['tag_id'], true);
- $res = UserTagRelate::setData($params['user_id'], $tag_id_arr);
- if ($res === false) return $this->error(UserTagRelate::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * 编辑会员标签
- * user_label_arr[user_id] = [1,2,3,4,5]
- * @Author: lun
- * @DateTime: 2022/3/14 0014 10:52
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionAddUserLabel()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_label_arr', 'is_batch'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $res = UserLabelRelationship::batchSetData($this->mall_id, $params['user_label_arr'], $params['is_batch']);
- if ($res === false) return $this->error(UserTagRelate::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * 编辑备注
- * @return mixed|void
- */
- public function actionEditRemark()
- {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'remark'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $res = UserExtra::setData($params);
- if ($res === false) return $this->error(UserExtra::getStaticError());
- return $this->success('操作成功');
- }
- public function actionEditUsername()
- {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'username'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $res = User::updateAll(['username'=>$params['username']],['id'=>$params['user_id'],'mall_id'=>$this->mall_id]);
- if ($res === false) return $this->error(User::getStaticError());
- // 触发用户信息修改事件
- $event = new UserUpdateEvent($this->mall_id);
- Yii::$app->services->events->dispatch($event, ['user_id'=>$params['user_id'], 'mall_id' => $this->mall_id], $event->listeners);
- return $this->success('操作成功');
- }
- /**
- * 编辑会员资料
- * @return \yii\web\Response
- */
- public function actionUserExtraEdit()
- {
- $params = \Yii::$app->request->post();
- $res = UserExtra::setData($params);
- if ($res === false) return $this->error(UserExtra::getStaticError());
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'user_id'=>$params['user_id'],
- 'mall_id'=>$this->mall_id,
- 'cate_ids'=>[MemberLabelEnum::USER_AGE]
- ], MemberLabelLogic::class, 'executeSyncCateId11');
- return $this->success('操作成功');
- }
- /**
- * 修改推广资格
- * @return mixed|void
- */
- public function actionChangeInviter()
- {
- $postData = \Yii::$app->request->post();
- $res = User::updateAll(['is_inviter' => $postData['is_inviter']], ['id' => $postData['user_id']]);
- if ($res) return $this->success('修改成功');
- return $this->error('修改失败');
- }
- /**
- * 获取可以绑定的推荐人
- * @return \yii\web\Response
- */
- public function actionGetCanBindInviter()
- {
- if (\Yii::$app->request->isAjax) {
- $params = \Yii::$app->request->get();
- $params['mall_id'] = $this->mall_id;
- $list = User::getCanBindInviter($params);
- return $this->success('操作成功', ['list' => $list]);
- }
- }
- /**
- * 修改推荐人
- * @return mixed|void
- */
- public function actionUpdateParent()
- {
- if (\Yii::$app->request->isAjax) {
- $params = \Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- // $res = User::updateParent($params);
- //会员同步插件
- if(MallAddons::isInstall($this->mall_id,'UserSync')){
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, 'UserSync', 'basic');
- if(!empty($configs['is_enable'])){
- $insert = [
- 'mall_id'=>$this->mall_id,
- 'user_id'=>$params['user_id'],
- 'type'=>2,
- ];
- UserSync::setData($insert);
- }
- }
- try {
- $params['is_admin'] = 1;
- $params['ip'] = ArrayHelper::get_client_ip();
- $params['operator_user_id'] = $this->admin->id;
- $res = User::updateParentByLock($params);
- if (\common\models\mall\MallAddons::isInstall($this->mall_id, \common\enums\AddonsEnum::ADDONS_NAME_STORE)) {
- //安装了o2o,获取关联门店数据
- $store_associated_user_service = new StoreAssociatedUserService();
- $store_associated_user_service->getStoreAssociated([
- 'user_id' => $params['user_id'],
- 'mall_id' => $this->mall_id,
- ]);
- }
- if ($res) return $this->success('修改成功');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->error(user::getStaticError());
- }
- }
- /**
- * 修改推荐人记录
- * @return mixed|void
- */
- public function actionUserChangeParentLog()
- {
- $params = \Yii::$app->request->get();
- $where[] = ['user_id' => $params['user_id']];
- $with = ['beforeParent', 'afterParent'];
- $params['where'] = $where;
- $params['with'] = $with;
- $params['order'] = 'id desc';
- $list = UserRelationshipLog::listPage($params);
- if (!empty($list['list'])) {
- foreach ($list['list'] as $k => &$item) {
- if ($k == 0) {
- $list['parent_id'] = $item['after_id'];
- $list['parent_nickname'] = $item['afterParent']['nickname'];
- }
- $item['before_name'] = $item['beforeParent']['nickname'];
- $item['after_name'] = $item['afterParent']['nickname'];
- $item['updated_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- }
- return $this->success('操作成功', $list);
- }
- /**
- * 批量导入会员
- * @return mixed|string|void
- */
- public function actionImportUsers()
- {
- set_time_limit(0);
- if (\Yii::$app->request->isAjax) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['current_num'], 'required'],
- [['file'], 'file', 'extensions' => ['csv']],
- [['file_path'], 'string'],
- [['sync'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- if(!empty($params['sync'])){
- $res = User::importUsersSync($params);
- }else{
- $res = User::importUsers($params);
- }
- if($res==false){
- return $this->error(user::getStaticError());
- }
- if (empty($res['fail_counts'])) return $this->success('操作成功', $res);
- if (!empty($res['fail_counts'])) return $this->error('导入失败'.$res['fail_counts'].'条');
- return $this->error(user::getStaticError());
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 会员批量导入模板下载
- */
- public function actionUserBatchSendModel()
- {
- $csv = new CsvExport();
- $file_name = '会员批量导入模板-' . date('YmdHis', time());
- $head_list = array_column(User::$head_list, 'field_name');
- return $csv->export([], $head_list, $file_name);
- }
- /**
- * 导入日志记录
- * @return mixed|void
- */
- public function actionGetUserImportLog()
- {
- $params = \Yii::$app->request->get();
- $params['where'] = [
- ['mall_id'=>$this->mall_id]
- ];
- $params['order'] = 'id desc';
- $data = UserImportLog::listPage($params, false, true);
- if (!empty($data['list'])) {
- foreach ($data['list'] as &$v) {
- $v['url'] = '';
- if ($v['fail_counts']) $v['url'] = \Yii::$app->urlManager->createUrl(['mall/user/export-fail', 'user_import_log_id' => $v['id']]);
- }
- }
- return $this->success('操作成功', $data);
- }
- /**
- * 导出失败日志
- */
- public function actionExportFail()
- {
- UserImportFailLog::exportFailLog(\Yii::$app->request->get());
- }
- /**
- * 登录日志
- * @return mixed|string|void
- */
- public function actionLoginLog(){
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = \Yii::$app->request->get();
- $params['where'] = [['mall_id'=>$this->mall_id]];
- $params['order'] = 'created_at desc';
- $list = LoginLog::getLoginLogList($params);
- return $this->success('ok', $list);
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render($this->action->id);
- }
- public function actionEditUserLevel(){
- $params = \Yii::$app->request->post();
- $params['id'] = $params['user_id'];
- $params['mall_id'] = $this->mall_id;
- //查询修改前用户信息
- $user = User::getUser([['id' => $params['user_id'],'mall_id' => $params['mall_id']]],'*',[],1);
- $res = User::change($params);
- if($res){
- if ($params['level'] != $user['level']) {
- // 记录升级日志
- $upgrade_log_obj = new UserLevelUpgradeLog();
- $upgrade_log_obj->admin_id = $this->admin->id;
- $upgrade_log_obj->mall_id = $this->mall_id;
- $upgrade_log_obj->user_id = $params['user_id'];
- $upgrade_log_obj->before_level = $user['level'];
- $upgrade_log_obj->after_level = $params['level'];
- $upgrade_log_obj->upgrade_type = $params['level'] > $user['level'] ? LevelUpgradeLogEnum::MANUALLY_UPGRADE : LevelUpgradeLogEnum::DOWNGRADE_MANUALLY;
- $upgrade_log_obj->save();
- }
- //触发事件
- $event = new UserUpgradeAfterEvent($this->mall_id);
- $data = ['after_level' => $params['level'], 'before_level' => $user['level'],'mall_id'=>$this->mall_id,
- 'user_id' => $params['user_id'], 'is_admin' => 1, 'operator_user_id' => $this->admin->id];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'user_id'=>$params['user_id'],
- 'mall_id'=>$this->mall_id,
- 'cate_ids'=>[MemberLabelEnum::USER_LEVEL]
- ], MemberLabelLogic::class, 'executeSyncCateId2Or3');
- }
- if ($res) return $this->success('修改成功');
- return $this->error(user::getStaticError());
- }
- //批量修改用户等级
- public function actionBatchEditUserLevel(){
- $params = \Yii::$app->request->post();
- $params['id'] = explode(',', $params['user_id']);
- $params['mall_id'] = $this->mall_id;
- // print_r($params);exit;
- $list = User::lists(['where'=>[['in','id',$params['id']]]],false);
- $num = 0;
- $event = new UserUpgradeAfterEvent($this->mall_id);
- foreach($list as $user){
- $data = [
- 'id' => $user['id'],
- 'level' => $params['level'],
- 'mall_id' => $params['mall_id'],
- ];
- //修改用户等级数据
- $res = User::change($data);
- //触发异步事件
- if($res){
- // //触发事件
- $event = new UserUpgradeAfterEvent($this->mall_id);
- $data = ['after_level' => $params['level'], 'before_level' => $user['level'],'mall_id'=>$this->mall_id,
- 'user_id' => $user['id'], 'is_admin' => 1, 'operator_user_id' => $this->admin->id];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'user_id' => $user['id'],
- 'mall_id' => $this->mall_id,
- 'cate_ids' => [MemberLabelEnum::USER_LEVEL]
- ], MemberLabelLogic::class, 'executeSyncCateId2Or3');
- $num ++;
- }else{
- return $this->error(user::getStaticError());
- }
- }
- if ($num > 0) return $this->success('批量修改成功'.$num.'条用户数据');
- return $this->error('批量修改失败');
- }
- /**
- * 查询会员的优惠卷
- *
- * @return mixed
- */
- public function actionGetUserCoupon()
- {
- $params = Yii::$app->request->get();
- if ($params['coupon_list_type'] == 2) { // 判断是否为门店
- if (!MallAddons::isInstall($this->mall_id, 'Store')) {
- return $this->success('操作成功', []);
- }
- $userRelate = StoreCouponUserRelate::tableName();
- $params['where'][] = ['acu.from_type' => 1];
- } else {
- $userRelate = AddonsCouponUserRelate::tableName();
- }
- $params['join'] = [['left join', $userRelate . ' as acu', 'ac.id = acu.coupon_id']];
- if (isset($params['type'])) {
- if ($params['type'] == 2) {
- // 未使用
- $params['where'][] = ['ac.status' => StatusEnum::ENABLED];
- $params['where'][] = ['acu.is_use' => 0];
- $params['where'][] = ['>','acu.end_time', time()];
- $params['where'][] = ['=','acu.coupon_status', 2];
- }
- if ($params['type'] == 1) {
- // 不可用
- $params['where'][] = ['=', 'acu.is_use', 1];
- }
- if ($params['type'] == 3) {
- // 已过期
- $params['where'][] = ['=', 'acu.coupon_status', AddonsCouponEnum::STATUS_EXPIRED];
- }
- if ($params['type'] == 4) {
- // 已失效
- $params['where'][] = ['=', 'acu.coupon_status', AddonsCouponEnum::STATUS_FAILURE];
- }
- }
- $params['where'][] = ['acu.user_id' => $params['user_id']];
- $params['where'][] = ['acu.status' => StatusEnum::ENABLED];
- if (!empty($params['keyword'])) $params['where'][] = ['like', 'name', $params['keyword']];
- $params['order'] = 'sort asc,id desc';
- $params['alias'] = 'ac';
- $params['select'] = 'ac.id as coupon_id,ac.name,acu.created_at as get_time,acu.end_time';
- $params['order'] = 'acu.created_at desc';
- //dd($params);
- $list = AddonsCoupon::listPage($params);
- $data['list'] = $list;
- return $this->success('操作成功', $data);
- }
- /**
- * 团队数据
- *
- * @param int $user_id
- * @param int $status 1:直推客户-下级 2:间推客户-下级的下级 3:我的团队-全部下级 4:推荐人-全部上级
- * @param string $keyword 关键字
- * @param int $search_type 1:会员ID 2:会员昵称 3:手机号码
- * @param int $page
- * @return mixed|array|void
- */
- public function actionMyCustomer() {
- if (!Yii::$app->request->isAjax) {
- $user_id = Yii::$app->request->get('user_id');
- if (!$user_id) throw new \Exception('用户不存在');
- $where[] = ['=', 'u.id', $user_id];
- $join = [['LEFT JOIN', User::tableName() . ' as p', 'u.parent_id = p.id']];
- $select = ['u.id', 'u.nickname', 'u.mobile', 'u.parent_id','u.avatar_url', 'u.created_at', 'p.nickname p_nickname'];
- $params = [
- 'where' => $where,
- 'alias' => 'u',
- 'select' => $select,
- 'join' => $join
- ];
- $model = User::find();
- User::paramPack($params, $model);
- $user_info = $model->asArray()->one();
- if (empty($user_info)) throw new \Exception('用户不存在');
- // 团队用户数据统计
- $statistics = UserTeamStatistics::find()->select('user_team_total,user_direct_total,user_indirect_total')->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- $user_info['team_num'] = $statistics['user_team_total'] ?: 0;
- $user_info['first_num'] = $statistics['user_direct_total'] ?: 0;
- $user_info['second_num'] = $statistics['user_indirect_total'] ?: 0;
- //累计消费
- $user_info['pay_money'] = UserStatisticsTotal::find()->where(['user_id' => $user_id])->sum('pay_money');
- CapitalLog::$capitalType = 'commission_frozen';
- //累计佣金
- $user_info['commission'] = CapitalLog::find()->where(['user_id' => $user_id, 'change_type' => CapitalLog::CHANGE_TYPE_ADD])->sum('change_num');
- //未结算
- $user_info['not_income'] = CapitalLog::find()->where(['user_id' => $user_id, 'change_type' => CapitalLog::CHANGE_TYPE_ADD, 'status' => CapitalLog::STATUS_WAIT_SEND])->sum('change_num');
- return $this->render('my-customer', ['user_info' => json_encode($user_info)]);
- }
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['user_id'], 'required'],
- [['user_id', 'status', 'page'], 'integer'],
- [['keyword', 'search_type'],'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id = $get['user_id'];
- $status = $get['status'] ?? 1;
- $keyword = $get['keyword'] ?? '';
- $search_type = $get['search_type'] ?? 1;
- $page = $get['page'] ?? 1;
- switch ($status) {
- case '1':
- $user_relations = UserPartitionRelationship::getRelationship($user_id, $page, $keyword, $search_type, 1, 1);
- break;
- case '2':
- $user_relations = UserPartitionRelationship::getRelationship($user_id, $page, $keyword, $search_type, 1, 2);
- break;
- case '3':
- $user_relations = UserPartitionRelationship::getRelationship($user_id, $page, $keyword, $search_type, 1);
- break;
- case '4':
- $user_relations = UserPartitionRelationship::getRelationship($user_id, $page, $keyword, $search_type, 2,1);
- break;
- }
- if (empty($user_relations)) return $this->success('操作成功', ['list' => [], 'page_count' => 0]);
- $user_list = $user_relations['list'];
- $user_ids = array_keys($user_list);
- $where[] = ['in', 'u.id', $user_ids];
- $select = ['u.id', 'u.nickname', 'u.mobile', 'u.avatar_url', 'ust.user_id', 'ust.pay_money', 'ust.pay_num'];
- $join = [['LEFT JOIN', UserStatisticsTotal::tableName() . ' as ust', 'u.id=ust.user_id']];
- //需要查询父级
- if (in_array($status, [2,3])) {
- array_push($select, 'p.id p_id', 'p.avatar_url p_avatar_url', 'p.nickname p_nickname');
- array_push($join, ['LEFT JOIN', USER::tableName() . ' as p', 'p.id=u.parent_id']);
- }
- $params = [
- 'alias' => 'u',
- 'where' => $where,
- 'select' => $select,
- 'join' => $join
- ];
- $model = User::find();
- User::paramPack($params, $model);
- $list = $model->asArray()->all();
- foreach ($list as &$val) {
- $val['pay_money'] = $val['pay_money'] ?? 0;
- $val['pay_num'] = $val['pay_num'] ?? 0;
- $val['bind_time'] = isset($user_list[$val['id']]) ? $user_list[$val['id']] : '暂无';
- }
- $data = ['list' => $list, 'page_count' => ceil($user_relations['count'] / 10)];
- return $this->success('操作成功', $data);
- }
- /**
- * 我的收益
- *
- * @param int user_id
- */
- public function actionMyCommission() {
- $user_id = Yii::$app->request->get('user_id');
- if (!$user_id) return [];
- $params = Yii::$app->request->get();
- CapitalLog::$capitalType = 'commission';
- $where[] = ['user_id'=>$user_id];
- $where[] = ['mall_id'=>$this->mall_id];
- if (Yii::$app->request->get('id')) $where[] = ['>','id'=>Yii::$app->request->get('id')];
- if (Yii::$app->request->get('id')) $where[] = ['=','capital_type'=>Yii::$app->request->get('capital_type')];
- $capital_type = UserWalletEnum::getCapitalTypeMapByCommission();
- $params['where'] = $where;
- $params['select'] = 'change_type,change_num,current_num,created_at,from_type,capital_type,id,desc';
- $params['order'] = 'id desc';
- $params['limit'] = 10;
- $list = CapitalLog::listPage($params);
- $list['capital_type'] = $capital_type;
- return $this->success('操作成功', $list);
- }
- /**
- * @notes:用户树状关系图
- * @return mixed|string|null
- * @author: lun
- * @Time: 2022/12/13 17:20
- */
- public function actionRelationTree()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- try {
- // 提交内容
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['type','tree_type'], 'required'],
- [['keyword',], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $field = $params['type'] == 1 ? 'id' : 'mobile';
- $select = 'id,mobile,nickname,level,parent_id,created_at';
- $user = User::find()->select($select)->where(['mall_id' => $this->mall_id, $field => $params['keyword']])->asArray()->one();
- if(empty($user)) return $this->error("查询的会员不存在");
- // 获取5层用户
- if ($params['tree_type'] == 'under') {// 查询下级
- $data = UserPartitionRelationship::getChildIdMake($user['id'], 10, 200, 1);
- $list = $data['list'];
- $child_uids = array_column($list, 'user_id');
- if (count($list) <= 1) return $this->error('此用户暂无下级数据');
- } else {// 查询上级
- $parent_ids = UserPartitionRelationship::getParentIdArr($user['id']);
- array_push($parent_ids, $user['id']);
- $child_uids = $list = $parent_ids;
- if (count($list) <= 1) return $this->error('此用户暂无上级数据');
- }
- $where[] = ['in', 'id', $child_uids];
- $index = "id";
- $param = compact('where','select', 'index');
- $child_arr = User::lists($param);
- // 等级
- $level_arr = UserLevel::getUserLevelArr($this->mall_id);
- $new_list = [];
- foreach($list as $key => $item) {
- if ($params['tree_type'] == 'under') {
- $user_id = $item['user_id'];
- $pid = $child_arr[$user_id]['parent_id'];
- } else {
- $user_id = $item;
- $pid = $list[$key - 1];
- }
- $new_list[] = [
- 'id' => $user_id,
- 'user_id' => $user_id,
- 'name' => $user_id,
- 'level_name' => $level_arr[$child_arr[$user_id]['level']] ?? MallSetting::getDefaultLevelName($this->mall_id),
- 'pid' => $user_id == $user['id'] && $params['tree_type'] == 'under' ? 0 : $pid,
- 'nickname' => $child_arr[$user_id]['nickname'],
- 'mobile' => $child_arr[$user_id]['mobile'],
- 'created_at' => date('Y-m-d H:i:s', $child_arr[$user_id]['created_at']),
- ];
- }
- // 组合成树形结构返回
- $tree_arr = UserPartitionRelationship::listToTree($new_list, 0);
- return $this->success('获取成功', $tree_arr);
- } catch (\Exception $e) {
- return $this->error('获取失败:' . $e->getMessage());
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- //修改手机号码
- public function actionUpdateMobile(){
- try {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile', 'user_id'], 'required'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['operator'] = $this->admin->username??'';
- $params['operator_user_id'] = $this->admin->id;
- User::updateMobile($params);
- return $this->success('操作成功');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionGetChangeMobileLog(){
- $params = \Yii::$app->request->get();
- $where[] = ['user_id' => $params['user_id']];
- $where[] = ['type' => 1];
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $params['select'] = 'user_id,operator,old_mobile,new_mobile,created_at';
- $list = ChangeMobileLog::listPage($params);
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s',$item['created_at']);
- if($item['user_id']==$item['operator']){
- $item['operator'] = '会员自己';
- }
- }
- }
- return $this->success('操作成功', $list);
- }
- public function actionHandleFreezeCommission()
- {
- try {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['freeze_cap', 'freeze_scale', 'user_id'], 'required'],
- [['freeze_amount'], 'number'],
- [['freeze_scale'], 'number', 'min' => 0, 'max' => 100],
- [['freeze_cap'], 'number', 'min' => 0],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- if(UserFreezeCommission::setData($params) === false){
- throw new \Exception(UserFreezeCommission::getStaticError());
- }
- return $this->success('操作成功');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionHandleClearFreezeCommission()
- {
- try {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['id'], 'number'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- if(!empty(UserFreezeCommission::getSwitchStatus($this->mall_id))){
- if(UserFreezeCommission::clearUserData($this->mall_id, $params['id']) === false){
- throw new \Exception(UserFreezeCommission::getStaticError());
- }
- }
- return $this->success('操作成功');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 清除用户授权,传user_id则代表清单用户,不传则删除某平台
- *
- * @return mixed|null
- */
- public function actionClearAuth()
- {
- $userId = Yii::$app->request->post('user_id', 0);
- $platform = Yii::$app->request->post('platform', '');
- if (!$userId && !$platform) {
- return $this->error('参数错误');
- }
- if ($platform && !in_array($platform, ['wechat', 'mp-wx'])) {
- return $this->error('参数错误');
- }
- UserInfo::clearAuth($this->mall_id, $userId, $platform);
- return $this->success();
- }
- /**
- * 重置用户支付密码
- * @params user_id 用户id
- * @params password 支付密码
- * @return mixed|null
- * @throws \yii\base\Exception
- */
- public function actionResetPaymentPassword()
- {
- $userId = Yii::$app->request->post('user_id', 0);
- $password = Yii::$app->request->post('password', '');
- if (!$userId && !$password) {
- return $this->error('参数错误');
- }
- $res = User::updateAll(['transaction_password' => Yii::$app->getSecurity()->generatePasswordHash($password)], ['id' => $userId]);
- if ($res === false) return $this->error(User::getStaticError());
- $event = new UserModifyPayPwdEvent($this->mall_id);
- Yii::$app->services->events->dispatch($event, [
- 'operator_user_id' => $this->admin->id,
- 'new_pwd' => trim($password),
- 'is_admin' => 1,
- 'user_id' => $userId,
- 'mall_id' => $this->mall_id
- ], $event->listeners);
- return $this->success('操作成功');
- }
- /**
- * 会员过期
- *
- * @param $data
- *
- * @return true|void
- */
- public function memberExpired($data){
- $user_level = UserLevel::findOne(['mall_id'=>$data['mall_id'],'status'=>StatusEnum::ENABLED,'level'=>$data['after_level']]);
- if(!$user_level) return true;
- if(isset($user_level['is_open_expired']) && $user_level['is_open_expired']){
- $expire_at = strtotime("+".$user_level['expired_day']." day");
- $user_level_expired = UserLevelExpired::findOne(['user_id'=> $data['user_id'],'level'=>$data['after_level'],'status'=>StatusEnum::ENABLED]);
- if(!$user_level_expired){
- $user_level_expired = new UserLevelExpired();
- $user_level_expired->mall_id = $data['mall_id'];
- $user_level_expired->user_id = $data['user_id'];
- $user_level_expired->level = $data['after_level'];
- }
- $user_level_expired->expired_at = $expire_at;
- $user_level_expired->save();
- }
- }
- //后台数字币充值
- public function actionAddDigital()
- {
- try {
- $admin = Yii::$app->getUser()->identity;
- $params = Yii::$app->request->post();
- $user = User::getOne([['mall_id' => $this->mall_id, 'id' => $params['user_id'], 'status' => StatusEnum::ENABLED]], true);
- if (!$user) throw new \Exception('用户不存在');
- $desc = '管理员' . $admin->username .' 后台充值' . $params['price'] . '数字币';
- $res = UserDigital::handleUserDigital($user['mall_id'], $user['id'], $params['price'], false, [
- 'from_type' => UserWalletEnum::RECHARGE,
- 'capital_type' => UserWalletEnum::RECHARGE_PLATFORM,
- 'desc' => $desc
- ]);
- if (!$res) {
- throw new \Exception(UserDigital::getStaticError());
- }
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|