| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace bizMall\user\services;
- use bizMall\base\services\BaseService;
- use bizMall\coupon\classes\CouponAssetClass;
- use bizMall\merchant\classes\MerchantAssetClass;
- use bizMall\merchant\services\MerchantAssetService;
- use bizMall\merchant\services\MerchantExtendService;
- use bizMall\order\services\OrderService;
- use bizMall\user\classes\UserAssetClass;
- use bizMall\user\classes\UserClass;
- use common\components\util;
- use common\components\validate;
- use common\services\ImageService;
- use common\services\xhMerchantExtendService;
- use common\services\xhMerchantService;
- use Yii;
- use common\components\wxUtil;
- class UserService extends BaseService
- {
-
- public static $baseFile = '\bizMall\user\classes\UserClass';
-
- //获取客户列表 ssh 2019.11.30
- public static function getUserList($where, $delStatus = 0)
- {
- //默认取没有删除的用户
- $where['delStatus'] = $delStatus;
- $data = UserClass::getList('*', $where, 'visitTime DESC');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- $data['list'] = UserClass::groupUserBaseInfo($list);
- return $data;
- }
-
- //获取客户基本和资产信息 ssh 2019.11.30
- public static function getUserInfo($id)
- {
- return UserClass::getUserInfo($id);
- }
-
- //获取客户基本、资产和订单信息 ssh 2019.11.30
- public static function getFullUserInfo($id)
- {
- $userInfo = UserClass::getUserInfo($id);
- $order = OrderService::getUserOrder($id, 10, 'addTime desc');
- $userInfo['orderList'] = $order;
- return $userInfo;
- }
-
- //根据手机号查询客户 ssh 2019.12.1
- public static function getByMobile($mobile, $sjId)
- {
- return UserClass::getByMobile($mobile, $sjId);
- }
- public static function getByOpenId($openId)
- {
- return UserClass::getByOpenId($openId);
- }
-
- public static function getByMiniOpenId($miniOpenId)
- {
- return UserClass::getByMiniOpenId($miniOpenId);
- }
-
- public static function getByUnionId($id)
- {
- return UserClass::getByUnionId($id);
- }
-
- //同步微信用户 ssh 2020.2.8
- public static function syncWxAllUser($sjId)
- {
- $return = UserClass::syncWxUser($sjId);
- while (isset($return['count']) && $return['count'] > 0 && isset($return['nextOpenId']) && !empty($return['nextOpenId'])) {
- $nextOpenId = $return['nextOpenId'];
- $return = UserClass::syncWxUser($sjId, $nextOpenId);
- }
- return ['total' => $return['total']];
- }
-
- //更新微信头像 ssh 2020.1.12
- public static function syncAvatar($sjId, $nextOpenId = '')
- {
- set_time_limit(0);
- $merchant = xhMerchantService::getById($sjId);
- //一次拉取调用最多拉取10000个关注者的OpenID
- $result = wxUtil::getSubscribeUserList($merchant, $nextOpenId);
- $total = isset($result['total']) ? $result['total'] : 0;
- $next_openid = isset($result['next_openid']) ? $result['next_openid'] : '';
- $count = isset($result['count']) ? $result['count'] : 0;
- $openIdList = isset($result['data']['openid']) ? $result['data']['openid'] : [];
- if ($count == 0) {
- util::stop("完成更新\n");
- }
- $total = count($openIdList);
- $totalPage = $total > 100 ? ceil($total / 100) : 1;
- for ($m = 1; $m <= $totalPage; $m++) {
- $offset = ($m - 1) * 100;
- $subIdList = array_slice($openIdList, $offset, 100);
- $info = wxUtil::batchGetUserInfo($merchant, $subIdList);
- if (empty($info) || isset($info['user_info_list']) == false || empty($info['user_info_list'])) {
- continue;
- }
- $user_info_list = $info['user_info_list'];
- foreach ($user_info_list as $val) {
- $headImgUrl = $val['headimgurl'];
- $openId = $val['openid'];
- $img = ImageService::generateAvatar(0, $headImgUrl);
- if (!empty($img)) {
- UserClass::updateByCondition(['openId' => $openId], ['avatar' => $img]);
- }
- }
- }
- echo "更新成功\n";
- }
-
- /**
- * 更新用户的访问时间
- */
- public static function updateVisitTime($sjId, $userId)
- {
- $date = date("Ymd");
- if (empty($userId)) {
- return false;
- }
- //每5分钟更新一次访问时间
- $key = 'UserVisitTime_' . $date . '_' . $sjId . '_' . $userId;
- $respond = Yii::$app->redis->executeCommand('GET', [$key]);
- //每过5分钟更新访问时间
- if (empty($respond)) {
- Yii::$app->redis->executeCommand('SET', [$key, $date]);
- Yii::$app->redis->executeCommand('EXPIRE', [$key, 300]);
- $time = time();
- self::updateById($userId, ['visitTime' => $time]);
- }
- }
-
- //注册成为会员,并更新用户相关信息 ssh 2019.9.7
- public static function becomeMember($user, $merchant, $data)
- {
- //合并店长后台添加的客户
- $sjId = $merchant['id'];
- $mobile = isset($data['mobile']) ? $data['mobile'] : '';
- if (!empty($mobile)) {
- $hasUser = UserClass::getByCondition(['sjId' => $sjId, 'mobile' => $mobile]);
- $userSource = UserClass::$userSourceId;
- $systemId = $userSource['system']['id'];
- Yii::info($hasUser['sourceType'] . ' -- ' . $systemId);
- if (!empty($hasUser) && $hasUser['sourceType'] == $systemId) {
- UserClass::mergeUser($hasUser, $user);
- }
- }
-
- //合并之后再更新信息
- $userId = $user['id'];
- UserClass::updateById($userId, $data);
- //注意成为会员和关注公众号时要去检查是否符合发优惠券条件
- $userAsset = UserAssetClass::getByUserId($userId);
- $user['mobile'] = $mobile;
- CouponAssetClass::giveCoupon($user, $merchant, $userAsset);
- }
-
- //取消关注 ssh 2019.9.8
- public static function unFocus($userId, $sjId)
- {
- self::updateById($userId, ['subscribe' => 0, 'hasSubscribe' => 1]);
- }
-
- //关注公众号的后续 ssh 2020.5.5
- public static function focus($user, $merchant)
- {
- $userId = $user['id'];
- UserClass::updateById($userId, ['subscribe' => 1]);
- $userAsset = UserAssetClass::getByUserId($userId);
- CouponAssetClass::giveCoupon($user, $merchant, $userAsset);
- }
-
- //批量获取用户信息,图片等信息处理好了 ssh 2019.11.28
- public static function getUserByIds($ids)
- {
- return UserClass::getUserByIds($ids);
- }
-
- //验证支付密码是否正确 ssh 2019.12.6
- public static function validPayPassword($password, $user)
- {
- if (isset($user['hasPayPwd']) == false || $user['hasPayPwd'] == 0) {
- util::fail('您还没有设置支付密码');
- }
- $payPassword = $user['payPassword'];
- if (password_verify($password, $payPassword) == false) {
- util::fail('密码错误');
- }
- }
-
- //验证用户权限
- public static function valid($info, $sjId)
- {
- return UserClass::valid($info, $sjId);
- }
-
- //根据消费次数和有没领优惠券来判断是不是新人
- public static function newUser($userInfo)
- {
- return UserClass::newUser($userInfo);
- }
-
- //添加新客户 ssh 2020.1.10
- public static function addUser($data)
- {
- //sjId $source $realName $member $growth $balance $mobile
- //必传参数
- validate::easyCheck(['sjId', 'mobile'], $data);
-
- $sjId = $data['sjId'];
- $source = 'system';
- $mobile = $data['mobile'];
- $realName = isset($data['realName']) ? $data['realName'] : $mobile;
- $member = isset($data['member']) ? $data['member'] : -1;
- $balance = isset($data['balance']) ? $data['balance'] : 0;
- $growth = isset($data['growth']) ? $data['growth'] : 0;
-
- $originalInfo = [];
- $originalInfo['sjId'] = $sjId;
- $originalInfo['mobile'] = $mobile;
- $originalInfo['realName'] = $realName;
- $originalInfo['userName'] = $realName;
-
- $user = UserClass::replaceUser($originalInfo, $source, $sjId);
-
- $extend = MerchantExtendService::getBySjId($sjId);
-
- //等级
- if ($member > 0) {
- $gradeList = xhMerchantExtendService::getGradeList($extend, 'desc');
- $growth = isset($gradeList[$member]['growth']) ? $gradeList[$member]['growth'] : 0;
- }
-
- //余额成长值更新
- $userId = $user['id'];
- $data = [];
- $data['growth'] = $growth;
- $data['balance'] = $balance > 0 ? $balance : 0;
- UserAssetService::updateByUserId($userId, $data);
-
- //会员升级
- UserIntegralService::increaseToUpgrade(0, $growth, 0, $userId, $sjId, $extend);
- return $user;
- }
-
- //合并用户 ssh 2020.5.12
- public static function mergeUser($delUser, $retainUser)
- {
- return UserClass::mergeUser($delUser, $retainUser);
- }
-
- }
|