| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace common\services;
- use bizHd\user\services\UserService;
- use common\components\util;
- use Yii;
- use linslin\yii2\curl;
- use common\components\stringUtil;
- use common\components\dict;
- use common\components\wxUtil;
- use common\models\xhUser;
- class xhUserService
- {
-
- public static function getByUnionId($unionId)
- {
- return xhUser::getByCondition(['unionId' => $unionId]);
- }
-
- public static function logout()
- {
- Yii::$app->session->destroy();//销毁session中所有已注册的数据
- }
-
- /**
- * 支付宝ID创建或获取用户
- */
- public static function generateUserByAlipay($alipayId, $sjId)
- {
- $getUserInfo = ['alipayId' => $alipayId, 'source' => 1];
- $user = self::replaceUserByWeixin($getUserInfo, $sjId);
- return $user;
- }
- public static function replaceUserByMini($openId, $unionid, $sjId)
- {
- $preUser = xhUser::getByCondition(['sjId' => $sjId, 'unionId' => $unionid]);
- $getUserInfo = ['miniOpenId' => $openId, 'unionid' => $unionid, 'source' => 2];
- return self::replaceUserByWeixin($getUserInfo, $sjId, $preUser);
- }
-
- /**
- * 微信仅有openid创建用户
- */
- public static function replaceUserByOpenID($openId, $sjId, $preUser)
- {
- $getUserInfo = ['openid' => $openId];
- return self::replaceUserByWeixin($getUserInfo, $sjId, $preUser);
- }
-
- /**
- * 通过微信生成更新用户信息
- */
- public static function replaceUserByWeixin($getUserInfo, $sjId, $preUser = [])
- {
- //替换掉微信不支持的图片
- $nickName = isset($getUserInfo['nickName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['nickName']) : '';
- $openId = isset($getUserInfo['openid']) ? $getUserInfo['openid'] : '';
- $date = date("Y-m-d H:i:s");
- $data = [];
- $data['userName'] = $nickName;
- $data['sex'] = isset($getUserInfo['sex']) ? $getUserInfo['sex'] : 0;//未知
- $data['openId'] = $openId;
- $data['miniOpenId'] = isset($getUserInfo['miniOpenId']) ? $getUserInfo['miniOpenId'] : '';
- $data['unionId'] = isset($getUserInfo['unionid']) ? $getUserInfo['unionid'] : '';
- $data['identity'] = 0;
- $subscribe = isset($getUserInfo['subscribe']) ? $getUserInfo['subscribe'] : 0;
- $data['subscribe'] = $subscribe;
- $data['subscribeTime'] = isset($getUserInfo['subscribe_time']) ? $getUserInfo['subscribe_time'] : 0;
- $data['sjId'] = $sjId;
- $data['status'] = 0;
- $data['alipayId'] = isset($getUserInfo['alipayId']) ? $getUserInfo['alipayId'] : '';
- //默认完整的用户信息
- $data['isFull'] = isset($getUserInfo['isFull']) ? $getUserInfo['isFull'] : 0;
-
- if (!empty($preUser)) {
- $userId = $preUser['id'];
- self::updateById($userId, $data);
- $user = self::getById($userId);
- } else {
- $data['createTime'] = $date;
- $data['avatar'] = '';
- $data['password'] = '';
- $data['payPassword'] = '';
- $data['mobile'] = '';
- $data['source'] = isset($getUserInfo['source']) ? $getUserInfo['source'] : 0;
- $data['hasSubscribe'] = 0;
- $user = self::add($data);//生成用户基础信息
- $userId = $user['id'];
- if (empty($nickName)) {
- self::updateById($userId, ['userName' => $userId]);
- }
- $random = Yii::$app->getSecurity()->generateRandomString();//yii2 里生成32位唯一随机数
- $uniqueCode = $userId . $random;
- $assetData = ['userId' => $userId, 'sjId' => $sjId, 'createTime' => $date, 'uniqueCode' => $uniqueCode];
- xhUserAssetService::add($userId, $assetData);
- }
-
- if (isset($getUserInfo['unionid']) && !empty($getUserInfo['unionid'])) {
- $unionId = $getUserInfo['unionid'];
- $now = time();
- $unionData = ['unionId' => $unionId, 'createTime' => $now];
- xhUnionUserService::add($unionData);
- }
- //保存头像
- if (isset($getUserInfo['headimgurl']) && !empty($getUserInfo['headimgurl'])) {
- if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) {
- $imgUrl = $getUserInfo['headimgurl'];
- $name = self::avatarSave($userId, $imgUrl);
- self::updateById($userId, ['avatar' => $name]);
- }
- }
- //商家粉丝增加1个
- if ($subscribe == 1) {
- $mAsset = xhMerchantAssetService::getBySjId($sjId);
- $totalFans = $mAsset['totalFans'] + 1;
- $asset = xhMerchantAssetService::updateBySjId($sjId, ['totalFans' => $totalFans]);
- xhUserByMonthService::replace($sjId, 1, $asset);//月粉丝量 +1
- xhUserByDayService::replace($sjId, 1, $asset);//每天增加粉丝 +1
- }
- return $user;
- }
-
- /**
- * 保存头像
- * @return string
- */
- public static function avatarSave($userId, $url)
- {
- $directory = dict::getDict('imgSavePath', 'wxUserAvatar');
- $path = Yii::getAlias('@webroot') . '/../../../resource/images';
- $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($path . $pre)) {
- mkdir($path . $pre, 0777, true);
- }
- $randString = stringUtil::buildOrderNo();
- $name = $userId . '_' . $randString . '.jpg';
- $fullFileName = $path . $pre . $name;
- $fileName = $pre . $name;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $fp2 = @fopen($fullFileName, 'a');//文件大小
- fwrite($fp2, $result);
- fclose($fp2);
-
- $dstImg200 = $path . $pre . $userId . '_' . $randString . '_200.jpg';//缩略宽 200px 中
- util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
-
- $dstImg50 = $path . $pre . $userId . '_' . $randString . '_50.jpg';//缩略成宽50px 小
- util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
- return $fileName;//默认大小 大
- }
-
- /**
- * 传入最大的头像,输出三种头像:大 中 小
- */
- public static function getAvatarList($largeImg)
- {
- $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
- $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
- return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
- }
-
- public static function add($data)
- {
- $user = xhUser::add($data);
- $userId = $user['id'];
- self::refreshById($userId);
- return $user;
- }
-
- /**
- * 是否关注了公众号
- */
- public static function isSubscribe($userId)
- {
- $user = self::getById($userId);
- return empty($user['subscribe']) ? false : true;
- }
-
- /**
- * 检查手机号在平台下没有被使用
- */
- public static function mobileNotUsed($mobile, $sjId)
- {
- $user = xhUser::getByCondition(['sjId' => $sjId, 'mobile' => $mobile]);//每个公众号下有唯一的手机号
- if (!empty($user)) {
- return false;
- }
- return true;
- }
-
- //获取用户信息
- public static function getById($id, $sjId = 0)
- {
- return xhUser::find()->where(['id' => $id])->asArray()->one();
- }
-
- /**
- * 获取用户信息
- */
- public static function getByOpenId($openId)
- {
- $user = xhUser::find()->where(['openId' => $openId])->asArray()->one();
- return $user;
- }
-
- public static function refreshById($userId)
- {
- $preKey = dict::getCacheKey('xhUser');
- $key = $preKey . $userId;
- Yii::$app->redis->executeCommand('DEL', [$key]);
- $user = self::getById($userId);
- $openId = $user['openId'];
- $preKey = dict::getCacheKey('xhUserOpenId');
- $key = $preKey . $openId;
- Yii::$app->redis->executeCommand('DEL', [$key]);
- self::getByOpenId($openId);
- }
-
- public static function updateById($id, $data)
- {
- xhUser::updateById($id, $data);
- }
-
- /**
- * 更新用户的标签
- */
- public static function updateTag($userId)
- {
- $ids = xhUserTagService::refreshUserTag($userId);
- $string = implode(',', $ids);
- self::updateById($userId, ['tag' => $string]);
- }
-
- /**
- * 通过微信获取用户是否关注公众号
- */
- public static function checkUserFocus($user, $merchant)
- {
- $openId = $user['openId'];
- $info = wxUtil::batchGetUserInfo($merchant, [$openId]);
- $wxUserInfo = isset($info['user_info_list'][0]) ? $info['user_info_list'][0] : [];
- return isset($wxUserInfo['subscribe']) && !empty($wxUserInfo['subscribe']) ? true : false;
- }
-
- /**
- * 根据手机号搜索
- */
- public static function searchByMobile($mobile, $sjId)
- {
- $query = xhUser::find();
- $query->where(['sjId' => $sjId]);
- $query->andWhere(['like', 'mobile', $mobile]);
- $respond = $query->orderBy('id desc')->limit(5)->asArray()->all();
- return $respond;
- }
-
- public static function getAlipay($sjId, $alipayId)
- {
- return xhUser::find()->where(['sjId' => $sjId, 'alipayId' => $alipayId])->asArray()->one();
- }
-
- }
|