| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace common\services;
- use bizHd\merchant\services\AdminService;
- use Yii;
- use linslin\yii2\curl;
- use common\components\dict;
- use common\components\stringUtil;
- use common\components\util;
- use common\components\wxUtil;
- use common\models\xhAdmin;
- use yii\web\Cookie;
- class xhAdminService
- {
- /**
- * 手机号登陆
- */
- public static function loginByMobile($mobile, $password, $autoLogin = false)
- {
- $admin = self::getByMobile($mobile);
- if (empty($admin)) {
- return ['code' => 'A0002', 'msg' => '帐号错误'];
- }
- $adminId = $admin['id'];
- $dbPassword = $admin['password'];
- if (md5($password) != $dbPassword) {
- return ['code' => 'A0002', 'msg' => '帐号或密码错误'];
- }
- $adminId = $admin['id'];
- $relation = xhAdminToMerchantService::getByAdminId($adminId);
- if (empty($relation)) {
- return ['code' => 'A0002', 'msg' => '帐号或密码错误!'];
- }
- $sjId = $relation['sjId'];
- $merchant = xhMerchantService::getById($sjId);
- $status = $merchant['status'];
- $pass = dict::getDict('merchantStatus', 'pass');
- $unfreeze = dict::getDict('merchantStatus', 'unfreeze');
- if (in_array($status, [$pass, $unfreeze]) == false) {
- return ['code' => 'A0002', 'msg' => '帐号有问题'];
- }
- //七天免登陆或者10分钟未操作退出
- $time = time();
- $autoLogin = Yii::$app->request->post('autoLogin', 1);
- $expireTime = $autoLogin == 1 ? ($time + 7 * 86400) : (10 * 60 + $time);
- $redisExpireTime = 7 * 86400;
- $urlSession = Yii::$app->request->get('SESSIONID', '');
- if (empty($urlSession)) {
- $sessionId = AdminService::generateSessionId($sjId);
- Yii::$app->response->cookies->add(new Cookie([
- 'name' => 'SESSIONID',
- 'value' => $sessionId,
- 'domain' => $_SERVER['SERVER_NAME'],
- 'expire' => $expireTime
- ]));
- //保存到redis
- Yii::$app->redis->executeCommand('SET', [$sessionId, $adminId]);
- Yii::$app->redis->executeCommand('EXPIRE', [$sessionId, $redisExpireTime]);//过期
- }
- return ['code' => 'A0001', 'msg' => '登陆成功'];
- }
- /**
- * 微信等第三方登陆
- */
- public static function loginByThird($admin, $autoLogin = false)
- {
- $session = Yii::$app->session;
- $session['isLogin'] = true;
- $session['id'] = $admin['id'];
- }
- public static function getByMobile($mobile)
- {
- $admin = xhAdmin::find()->where(['mobile' => $mobile])->asArray()->one();
- return $admin;
- }
- public static function isLogin()
- {
- $session = Yii::$app->session;
- if (isset($session['isLogin']) && !empty($session['isLogin'])) {
- return true;
- }
- return false;
- }
- public static function getId()
- {
- $session = Yii::$app->session;
- $userId = $session['id'];
- return $userId;
- }
- public static function logout()
- {
- //清除缓存
- $sessionId = Yii::$app->request->cookies->getValue("SESSIONID");
- Yii::$app->redis->executeCommand('DEL', [$sessionId]);
- //清除cookie
- $cookie = Yii::$app->response->cookies;
- $cookie->remove('SESSIONID');
- $cookie->add(new Cookie([
- 'name' => 'SESSIONID',
- 'value' => '',
- 'expire' => time() - 30 * 24 * 3600,
- 'domain' => $_SERVER['SERVER_NAME']
- ]));
- //清除session
- Yii::$app->session->destroy();//销毁session中所有已注册的数据
- }
- /**
- * 通过微信生成用户
- */
- public static function generateAdmin($baseInfo)
- {
- $date = date("Y-m-d H:i:s");
- $data = [];
- $data['mobile'] = isset($baseInfo['mobile']) ? $baseInfo['mobile'] : '';
- $name = $baseInfo['nickName'];
- $name = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $name);//替换掉微信不支持的图片
- $data['name'] = $name;
- $data['avatar'] = '';
- $data['password'] = isset($baseInfo['password']) ? md5($baseInfo['password']) : '';
- $data['payPassword'] = '';
- $data['openId'] = $baseInfo['openid'];
- $data['unionId'] = isset($baseInfo['unionId']) ? $baseInfo['unionId'] : '';
- $data['subscribe'] = isset($baseInfo['subscribe']) ? $baseInfo['subscribe'] : 0;
- $data['sjId'] = isset($baseInfo['sjId']) ? $baseInfo['sjId'] : 0;
- $data['status'] = 0;
- $data['createTime'] = $date;
- $admin = self::add($data);
- //保存头像
- if (!empty($baseInfo['headimgurl'])) {
- $imgUrl = $baseInfo['headimgurl'];
- $adminId = $admin['id'];
- $avatar = self::avatarSave($imgUrl, $adminId);
- self::updateById($admin['id'], ['avatar' => $avatar]);
- }
- return $admin;
- }
- /**
- * 保存头像
- * @return string
- */
- public static function avatarSave($url, $adminId)
- {
- $directory = dict::getDict('imgSavePath', 'wxAdminAvatar');
- $path = Yii::getAlias('@webroot') . '/../../images';
- $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($path . $pre)) {
- mkdir($path . $pre, 0777, true);
- }
- $randString = stringUtil::buildOrderNo();
- $name = $adminId . '_' . $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 . $adminId . '_' . $randString . '_200.jpg';//缩略宽 200px 中
- util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
- $dstImg50 = $path . $pre . $adminId . '_' . $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 getById($id)
- {
- $admin = xhAdmin::find()->where(['id' => $id])->asArray()->one();
- return $admin;
- }
- public static function getByOpenId($openId)
- {
- $cacheKey = dict::getCacheKey('xhAdminOpenId') . $openId;
- $admin = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if (!empty($admin)) {
- $arr = [];
- $i = 0;
- $x = 0;
- foreach ($admin as $key => $val) {
- $i++;
- if ($i % 2 == 0) {
- $arr[$x]['value'] = $val;
- $x++;
- } else {
- $arr[$x]['key'] = $val;
- }
- }
- $adminArr = [];
- foreach ($arr as $key => $val) {
- $adminArr[$val['key']] = $val['value'];
- }
- unset($adminArr['info_already_get_by_sql']);
- return $adminArr;
- }
- $admin = xhAdmin::find()->where(['openId' => $openId])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if (!empty($admin)) {
- foreach ($admin as $key => $val) {
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET', $params);//将用户信息、用户标签放入缓存
- return $admin;
- }
- public static function refreshById($id)
- {
- $adminKey = dict::getCacheKey('admin') . $id;
- Yii::$app->redis->executeCommand('DEL', [$adminKey]);
- $admin = self::getById($id);
- $mobile = $admin['mobile'];
- $mobileKey = dict::getCacheKey('xhAdminMobile') . $mobile;
- Yii::$app->redis->executeCommand('DEL', [$mobileKey]);
- self::getByMobile($mobile);
- $openId = $admin['openId'];
- $openKey = dict::getCacheKey('xhAdminOpenId') . $openId;
- Yii::$app->redis->executeCommand('DEL', [$openKey]);
- self::getByOpenId($openId);
- }
- public static function updateById($id, $data)
- {
- xhAdmin::updateById($id, $data);
- self::refreshById($id);
- }
- public static function deleteById($id)
- {
- xhAdmin::deleteById($id);
- $preKey = dict::getCacheKey('admin');
- $cacheKey = $preKey . $id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- }
- public static function add($data)
- {
- $admin = xhAdmin::add($data);
- $id = $admin['id'];
- self::refreshById($id);
- return $admin;
- }
- }
|