'朋友圈', 1 => '门店', 0 => '公众号', 3 => '淘宝', ]; //用户来源分类 public static $userSource = [ 0 => ['name' => 'official', 'title' => '公众号'], 1 => ['name' => 'alipay', 'title' => '支付宝'], 2 => ['name' => 'mini', 'title' => '小程序'], 3 => ['name' => 'friend', 'title' => '朋友圈'], 4 => ['name' => 'meituan', 'title' => '美团'], 5 => ['name' => 'system', 'title' => '系统'], ]; //用户来源分类ID public static $userSourceId = [ 'official' => ['id' => 0, 'name' => 'official'], 'alipay' => ['id' => 1, 'name' => 'alipay'], 'mini' => ['id' => 2, 'name' => 'mini'], 'friend' => ['id' => 3, 'name' => 'friend'], 'meituan' => ['id' => 4, 'name' => 'meituan'], 'system' => ['id' => 5, 'name' => 'system'], ]; //组合客户基本和资产信息 public static function groupUserBaseInfo($list) { //取客户资产 $ids = array_column($list, 'id'); $ids = array_unique($ids); $asset = UserAssetClass::getByUserIds($ids, 'userId'); $sjIdList = array_column($list, 'sjId'); $sjIdList = array_filter(array_unique($sjIdList)); $merchantList = MerchantService::getByIds($sjIdList, null, 'id'); foreach ($list as $key => $val) { $userId = $val['id']; //访问时间 $visitTime = date("m-d H:i", $val['visitTime']); if (date("m-d") == date("m-d", $val['visitTime'])) { $visitTime = '今天 ' . date("H:i", $val['visitTime']); } $list[$key]['visitTimeFormat'] = $visitTime; $list[$key]['userAsset'] = isset($asset[$userId]) ? $asset[$userId] : []; //客户发来新消息数 $list[$key]['unReadNum'] = ChatClass::getUnreadNum($userId); $list[$key] = business::formatUserAvatar($list[$key]); $sjId = $val['sjId']; $list[$key]['name'] = isset($merchantList[$sjId]['name']) ? $merchantList[$sjId]['name'] : ''; } return $list; } //获取客户基本和资产信息 ssh 2019.11.30 public static function getUserInfo($id) { $list = self::getByIds([$id]); $data = self::groupUserBaseInfo($list); return current($data); } //根据手机号查询客户 ssh 2019.12.1 public static function getByMobile($mobile, $sjId) { return self::getByCondition(['sjId' => $sjId, 'mobile' => $mobile]); } public static function replaceUser($getUserInfo) { $mobile = $getUserInfo['mobile'] ?? ''; if (stringUtil::isMobile($mobile) == false) { util::fail('请填写正确手机号哦,' . $mobile); } $user = self::getByCondition(['mobile' => $mobile], true); if (!empty($user)) { return $user; } $date = date("Y-m-d H:i:s"); $time = time(); //替换掉微信名称不支持的图片 $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : ''; $userName = empty($userName) ? $mobile : $userName; $getUserInfo['name'] = $userName; $getUserInfo['sex'] = isset($getUserInfo['sex']) ? $getUserInfo['sex'] : 0;//未知 $getUserInfo['identity'] = 0; $subscribe = isset($getUserInfo['subscribe']) ? $getUserInfo['subscribe'] : 0; $getUserInfo['subscribe'] = $subscribe; $getUserInfo['status'] = 0; //默认没有完整用户信息 $isFull = isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl']) ? 1 : 0; $getUserInfo['isFull'] = $isFull; $getUserInfo['createTime'] = $date; $getUserInfo['avatar'] = ''; $getUserInfo['password'] = ''; $getUserInfo['payPassword'] = ''; $getUserInfo['mobile'] = isset($getUserInfo['mobile']) ? $getUserInfo['mobile'] : ''; $getUserInfo['addTime'] = $date; $getUserInfo['visitTime'] = $time; $getUserInfo['source'] = isset($getUserInfo['source']) ? $getUserInfo['source'] : 0; $getUserInfo['hasSubscribe'] = 0; //生成用户基础信息 $user = self::add($getUserInfo, true); $userId = $user->id ?? 0; $assetData = ['userId' => $userId, 'createTime' => $date]; UserAssetClass::add($assetData); //保存头像 if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) { if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) { $imgUrl = $getUserInfo['headImgUrl']; $currentImg = ImageService::generateAvatar(0, $imgUrl); if (!empty($currentImg)) { self::updateById($userId, ['avatar' => $currentImg]); } } } return $user; } public static function updateUser($originalUser, $getUserInfo, $sjId) { $data = []; $userId = $originalUser['id']; //替换掉微信不支持的图片 $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : ''; if (!empty($userName)) { $data['name'] = $userName; } if (isset($getUserInfo['openId']) && !empty($getUserInfo['openId'])) { $openId = $getUserInfo['openId']; $data['openId'] = $openId; } if (isset($getUserInfo['miniOpenId']) && !empty($getUserInfo['miniOpenId'])) { $data['miniOpenId'] = $getUserInfo['miniOpenId']; } if (isset($getUserInfo['ghsMiniOpenId']) && !empty($getUserInfo['ghsMiniOpenId'])) { $data['ghsMiniOpenId'] = $getUserInfo['ghsMiniOpenId']; } if (isset($getUserInfo['unionId']) && !empty($getUserInfo['unionId'])) { $data['unionId'] = $getUserInfo['unionId']; } if (isset($getUserInfo['sex'])) { $data['sex'] = $getUserInfo['sex']; } if (isset($getUserInfo['subscribe'])) { $data['subscribe'] = $getUserInfo['subscribe']; } if (isset($getUserInfo['subscribeTime'])) { $data['subscribeTime'] = $getUserInfo['subscribeTime']; } if (isset($getUserInfo['alipayId']) && !empty($getUserInfo['alipayId'])) { $data['alipayId'] = $getUserInfo['alipayId']; } if (isset($getUserInfo['isFull'])) { $data['isFull'] = $getUserInfo['isFull']; } if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) { $data['isFull'] = 1; } //更新头像 if (isset($originalUser['avatar']) == false || empty($originalUser['avatar'])) { if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) { $imgUrl = $getUserInfo['headImgUrl']; $name = ImageService::generateAvatar(0, $imgUrl); if (!empty($name)) { $data['avatar'] = $name; } } } self::updateById($userId, $data); $user = self::getById($userId); return $user; } public static function getByMiniOpenId($miniOpenId) { return self::getByCondition(['miniOpenId' => $miniOpenId]); } public static function getByOpenId($openId) { return self::getByCondition(['openId' => $openId]); } public static function getByUnionId($unionId) { return self::getByCondition(['unionId' => $unionId]); } //合并用户 public static function mergeUser($delUser, $retainUser) { $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $delUserId = $delUser['id']; $retainUserId = $retainUser['id']; $userName = $retainUser['userName']; $sjId = $retainUser['sjId']; //合并记录 $return = UserMergeClass::add([ 'delUserId' => $delUserId, 'sjId' => $sjId, 'retainUserId' => $retainUserId, 'addTime' => time(), 'createTime' => date("Y-m-d H:i:s") ]); $targetId = $return['id']; $capitalType = dict::getDict('capitalType', 'xhMergeUser', 'id'); $now = time(); $date = date("Y-m-d H:i:s"); //数据迁移 $updateData = []; $updateKey = [ 'mobile', 'password', 'payPassword', 'subscribe', 'hasSubscribe', 'subscribeTime', 'alipayId', 'openId', 'miniOpenId', 'unionId', ]; foreach ($updateKey as $key) { if (isset($delUser[$key]) && !empty($delUser[$key])) { $updateData[$key] = $delUser[$key]; } } if (!empty($updateData)) { $updateData['visitTime'] = time(); self::updateById($retainUserId, $updateData); } //资产累加 $addData = []; $delUserAsset = UserAssetClass::getByUserId($delUserId); $retainUserAsset = UserAssetClass::getByUserId($retainUserId); if (!empty($delUserAsset)) { //累计消费 $delTotalExpend = isset($delUserAsset['totalExpend']) ? $delUserAsset['totalExpend'] : 0; if (!empty($delTotalExpend)) { $addData['totalExpend'] = $retainUserAsset['totalExpend'] + $delTotalExpend; //变化记录要增加 } //累计收入 $delTotalIncome = isset($delUserAsset['totalIncome']) ? $delUserAsset['totalIncome'] : 0; if (!empty($delTotalIncome)) { $addData['totalIncome'] = $retainUserAsset['totalIncome'] + $delTotalIncome; //变化记录要增加 } //累计充值 $delTotalRecharge = isset($delUserAsset['totalRecharge']) ? $delUserAsset['totalRecharge'] : 0; if (!empty($delTotalRecharge)) { $addData['totalRecharge'] = $retainUserAsset['totalRecharge'] + $delTotalRecharge; //变化记录要增加 } //累计购买数量 $delTotalBuyNum = isset($delUserAsset['totalBuyNum']) ? $delUserAsset['totalBuyNum'] : 0; if (!empty($delTotalBuyNum)) { $addData['totalBuyNum'] = $retainUserAsset['totalBuyNum'] + $delTotalBuyNum; //变化记录要增加 } //余额 $delBalance = isset($delUserAsset['balance']) ? $delUserAsset['balance'] : 0; if (!empty($delBalance)) { $addData['balance'] = $retainUserAsset['balance'] + $delBalance; //变化记录要增加 $userCapitalData = [ 'relateId' => $targetId, 'balance' => $addData['balance'], 'totalIncome' => $retainUserAsset['totalIncome'] + $delBalance, 'totalExpend' => $retainUserAsset['totalExpend'], 'amount' => $delBalance, 'io' => 1, 'payWay' => 0, 'event' => '帐号合并', 'sjId' => $sjId, 'userId' => $retainUserId, 'alipayId' => '', 'userName' => $userName, 'operateId' => 0, 'createTime' => $date, 'addTime' => $now, 'capitalType' => $capitalType, ]; xhUserCapitalService::add($userCapitalData);//用户资金流水增加 } //成长值 $delGrowth = isset($delUserAsset['growth']) ? $delUserAsset['growth'] : 0; if (!empty($delGrowth)) { $addData['growth'] = $retainUserAsset['growth'] + $delGrowth; //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加 $growthData = [ 'userId' => $retainUserId, 'userName' => $userName, 'sjId' => $sjId, 'relateId' => $targetId, 'integral' => $addData['growth'], 'num' => $delGrowth, 'io' => 1, 'payWay' => 0, 'alipayId' => '', 'event' => '帐号合并', 'operatorId' => 0, 'createTime' => $date, 'capitalType' => $capitalType, 'addTime' => $now, ]; UserGrowthClass::add($growthData); //成长值增加升级 $preLevel = $retainUserAsset['member']; $preGrowth = $retainUserAsset['growth']; $merchantExtend = xhMerchantExtendService::getBySjId($sjId); UserIntegralService::increaseToUpgrade($preGrowth, $addData['growth'], $preLevel, $retainUserId, $sjId, $merchantExtend); } if (!empty($addData)) { UserAssetClass::updateByUserId($retainUserId, $addData); } } //删除用户 $clearData = ['openId' => '', 'miniOpenId' => '', 'unionId' => '', 'alipayId' => '', 'sjId' => 0, 'mobile' => '']; self::updateById($delUserId, $clearData); UserAssetClass::updateByUserId($delUserId, ['sjId' => 0]); //如果有支付宝信息则清除支付宝用户 UserAlipayClass::updateByUserId($delUserId, ['alipayId' => '', 'alipayAccount' => '', 'sjId' => 0]); //合并帐号减少客户数 ssh 2020.5.13 MerchantAssetClass::mergeToReduceUser($sjId); $transaction->commit(); return self::getById($retainUserId); } catch (Exception $e) { $transaction->rollBack(); Yii::info("合并userid $delUserId $retainUserId 时事务回滚,原因:" . $e->getMessage()); return []; } } //找出相同unionId的用户 public static function getSameUnionId($unionId) { return self::getAllByCondition(['unionId' => $unionId], 'addTime desc', '*', 'id'); } public static function valid($info, $sjId) { if (empty($info)) { util::fail('找不到客户'); } if ($info['sjId'] != $sjId) { util::fail('这个客户您无法操作'); } } //根据消费次数和有没领优惠券来判断是不是新人 public static function newUser($userInfo) { $asset = isset($userInfo['userAsset']) ? $userInfo['userAsset'] : []; $totalBuyNum = isset($asset['totalBuyNum']) ? $asset['totalBuyNum'] : 0; if ($totalBuyNum > 0) { return false; } $getCouponNum = isset($asset['getCouponNum']) ? $asset['getCouponNum'] : 0; if ($getCouponNum) { return false; } return true; } public static function getUserByIds($ids) { $info = self::getByIds($ids, null, 'id'); if (empty($info)) { return []; } $data = self::groupUserBaseInfo($info); return $data; } //将微信和小程序获取的用户信息转成可以保存的用户信息 public static function switchUserInfo($info, $source) { $openId = ''; if (isset($info['openid']) && !empty($info['openid'])) { $openId = $info['openid']; unset($info['openid']); } if (isset($info['openId']) && !empty($info['openId'])) { $openId = $info['openId']; unset($info['openId']); } if (!empty($openId)) { $userSource = self::$userSourceId; switch ($source) { case $userSource['mini']['name']: $info['miniOpenId'] = $openId; break; case $userSource['official']['name']: $info['openId'] = $openId; break; default: } } if (isset($info['miniOpenId'])) { $info['miniOpenId'] = $info['miniOpenId']; } if (isset($info['nickName'])) { $info['userName'] = $info['nickName']; $info['nickName'] = $info['nickName']; } if (isset($info['unionid'])) { $info['unionId'] = $info['unionid']; } if (isset($info['unionId'])) { $info['unionId'] = $info['unionId']; } if (isset($info['headimgurl'])) { $info['headImgUrl'] = $info['headimgurl']; } if (isset($info['avatarUrl'])) { $info['headImgUrl'] = $info['avatarUrl']; } if (isset($info['subscribe_time'])) { $info['subscribeTime'] = $info['subscribe_time']; } if (isset($info['subscribe'])) { $info['subscribe'] = $info['subscribe']; } return $info; } }