| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace biz\user\services;
- use biz\base\services\BaseService;
- use biz\merchant\classes\MerchantAssetClass;
- use biz\merchant\services\MerchantAssetService;
- use biz\merchant\services\MerchantExtendService;
- use biz\order\services\OrderService;
- use biz\user\classes\UserClass;
- use common\components\util;
- use common\components\validate;
- use common\services\xhMerchantExtendService;
- use common\services\xhMerchantService;
- use Yii;
- use common\components\weixinUtil;
- class UserService extends BaseService
- {
-
- public static $baseFile = '\biz\user\classes\UserClass';
-
- //获取客户列表 shish 2019.11.30
- public static function getUserList($where)
- {
- $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;
- }
-
- //获取客户基本和资产信息 shish 2019.11.30
- public static function getUserInfo($id)
- {
- return UserClass::getUserInfo($id);
- }
-
- //获取客户基本、资产和订单信息 shish 2019.11.30
- public static function getFullUserInfo($id)
- {
- $userInfo = UserClass::getUserInfo($id);
- $order = OrderService::getUserOrder($id, 10, 'addTime desc');
- $userInfo['orderList'] = $order;
- return $userInfo;
- }
-
- //根据手机号查询客户 shish 2019.12.1
- public static function getByMobile($mobile, $merchantId)
- {
- return UserClass::getByMobile($mobile, $merchantId);
- }
-
- /**
- * 创建以及更新用户
- * @author shish
- * @time 20190507
- */
- public static function replaceUser($userInfo, $source, $merchantId)
- {
- $userInfo = self::switchUserInfo($userInfo, $source);
- $userSource = UserClass::$userSourceId;
- switch ($source) {
- case $userSource['mini']['name']:
- $sourceId = $userSource['mini']['id'];
- $miniOpenId = $userInfo['miniOpenId'];
- $user = UserClass::getByMiniOpenId($miniOpenId, $merchantId);
- break;
- case $userSource['alipay']['name']:
- $sourceId = $userSource['alipay']['id'];
- $alipayId = $userInfo['alipayId'];
- $user = UserClass::getByAlipayId($alipayId, $merchantId);
- break;
- case $userSource['official']['name']:
- $sourceId = $userSource['official']['id'];
- //老客添加没有openId
- $openId = isset($userInfo['openId']) ? $userInfo['openId'] : '';
- $user = [];
- if (!empty($openId)) {
- $user = self::getByOpenId($openId, $merchantId);
- }
- break;
- default:
- throw new \Exception('未知来源的用户');
- }
- if (empty($user)) {
- $userInfo['source'] = $sourceId;
- $userInfo['merchantId'] = $merchantId;
- $user = UserClass::generateUser($userInfo, $merchantId);
- } else {
- //支付宝付款时不需要每次更新用户信息
- if ($source != $userSource['alipay']['name']) {
- $user = UserClass::updateUser($user, $userInfo, $merchantId);
- }
- }
-
- //存在相同unionid则进行合并
- $unionId = isset($userInfo['unionId']) ? $userInfo['unionId'] : '';
- if (!empty($unionId)) {
- $same = UserClass::getSameUnionId($merchantId, $unionId);
- if (count($same) == 2) {
- $delUser = $same[0];
- $retainUser = $same[1];
- $user = UserClass::mergeUser($delUser, $retainUser);
- }
- }
- return $user;
- }
-
- public static function getByOpenId($openId, $merchantId)
- {
- return UserClass::getByOpenId($openId, $merchantId);
- }
-
- public static function getByMiniOpenId($miniOpenId, $merchantId)
- {
- return UserClass::getByMiniOpenId($miniOpenId, $merchantId);
- }
-
- /**
- * 同步微信用户信息
- * @author shish <shish@zhhinc.com>
- * @time 2019.5.11
- */
- public static function syncWeixinUser($merchantId, $nextOpenId = '')
- {
- set_time_limit(0);
- $merchant = xhMerchantService::getById($merchantId);
- //一次拉取调用最多拉取10000个关注者的OpenID
- $result = weixinUtil::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) {
- return ['code' => 'A0001', 'msg' => '完成更新', 'data' => ['total' => $total]];
- }
- $total = count($openIdList);
- $totalPage = $total > 100 ? ceil($total / 100) : 1;
-
- $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
- $source = $userSource['name'];
-
- for ($m = 1; $m <= $totalPage; $m++) {
- $offset = ($m - 1) * 100;
- $subIdList = array_slice($openIdList, $offset, 100);
- $info = weixinUtil::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) {
- $val['isFull'] = 1;
- $val['unionId'] = $val['unionid'];
- $val['headImgUrl'] = $val['headimgurl'];
- $val['openId'] = $val['openid'];
- $val['nickName'] = $val['nickname'];
- $val['merchantId'] = $merchantId;
- self::replaceUser($val, $source, $merchantId);
- }
- }
- return ['code' => 'A0001', 'msg' => '完成更新', 'data' => ['total' => $total]];
- }
-
- /**
- * 更新用户的访问时间
- */
- public static function updateVisitTime($merchantId, $userId)
- {
- $date = date("Ymd");
- if (empty($userId)) {
- return false;
- }
- //每5分钟更新一次访问时间
- $key = 'UserVisitTime_' . $date . '_' . $merchantId . '_' . $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]);
- }
- }
-
- /**
- * 将微信和小程序获取的用户信息转成可以保存的用户信息
- */
- 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:
- throw new \Exception('未知来源的用户');
- }
- }
-
- if (isset($info['nickname'])) {
- $info['userName'] = $info['nickname'];
- }
- if (isset($info['nickName'])) {
- $info['userName'] = $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;
- }
-
- //升级成为会员,并更新用户相关信息 shish 2019.9.7
- public static function upgradeToMember($userId, $merchantId, $data = [])
- {
- self::updateById($userId, $data);
-
- /**会员数+1**/
- MerchantAssetService::addMemberNum($merchantId);
- }
-
- //取消关注 shish 2019.9.8
- public static function unFocus($userId, $merchantId)
- {
- self::updateById($userId, ['subscribe' => 0, 'hasSubscribe' => 1]);
- /**商家粉丝减少**/
- MerchantAssetClass::reduceFans($merchantId);
- }
-
- //批量获取用户信息,图片等信息处理好了 shish 2019.11.28
- public static function getUserByIds($ids)
- {
- return UserClass::getUserByIds($ids);
- }
-
- //验证支付密码是否正确 shish 2019.12.6
- public static function validPayPassword($password, $user)
- {
- if (isset($user['payPassword']) == false || empty($user['payPassword'])) {
- util::fail('您还没有设置支付密码');
- }
- $payPassword = $user['payPassword'];
- if (password_verify($password, $payPassword) == false) {
- util::fail('密码错误');
- }
- }
-
- //验证用户权限
- public static function valid($info, $merchantId)
- {
- return UserClass::valid($info, $merchantId);
- }
-
- //根据消费次数和有没领优惠券来判断是不是新人
- public static function newUser($userInfo)
- {
- return UserClass::newUser($userInfo);
- }
-
- //添加新客户 shish 2020.1.10
- public static function addUser($data)
- {
- //merchantId $source $realName $member $growth $balance $mobile
- //必传参数
- validate::easyCheck(['merchantId', 'mobile'], $data);
-
- $merchantId = $data['merchantId'];
- $source = isset($data['source']) ? $data['source'] : 'official';
- $mobile = $data['mobile'];
- $realName = isset($data['realName']) ? $data['realName'] : $mobile;
- $member = isset($data['member']) ? $data['member'] : -1;
- $balance = isset($data['balance']) ? $data['balance'] : 0;
-
- $originalInfo = [];
- $originalInfo['merchantId'] = $merchantId;
- $originalInfo['mobile'] = $mobile;
- $originalInfo['realName'] = $realName;
-
- $user = UserService::replaceUser($originalInfo, $source, $merchantId);
-
- $extend = MerchantExtendService::getByMerchantId($merchantId);
-
- //等级
- $growth = 0;
- 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, $merchantId, $extend);
- }
-
- }
|