xhAdminService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace common\services;
  3. use biz\merchant\services\AdminService;
  4. use Yii;
  5. use linslin\yii2\curl;
  6. use common\components\configDict;
  7. use common\components\stringUtil;
  8. use common\components\util;
  9. use common\components\wxUtil;
  10. use common\models\xhAdmin;
  11. use yii\web\Cookie;
  12. class xhAdminService
  13. {
  14. /**
  15. * 手机号登陆
  16. */
  17. public static function loginByMobile($mobile, $password, $autoLogin = false)
  18. {
  19. $admin = self::getByMobile($mobile);
  20. if (empty($admin)) {
  21. return ['code' => 'A0002', 'msg' => '帐号错误'];
  22. }
  23. $adminId = $admin['id'];
  24. $dbPassword = $admin['password'];
  25. if (md5($password) != $dbPassword) {
  26. return ['code' => 'A0002', 'msg' => '帐号或密码错误'];
  27. }
  28. $adminId = $admin['id'];
  29. $relation = xhAdminToMerchantService::getByAdminId($adminId);
  30. if (empty($relation)) {
  31. return ['code' => 'A0002', 'msg' => '帐号或密码错误!'];
  32. }
  33. $merchantId = $relation['merchantId'];
  34. $merchant = xhMerchantService::getById($merchantId);
  35. $status = $merchant['status'];
  36. $pass = configDict::getConfig('merchantStatus', 'pass');
  37. $unfreeze = configDict::getConfig('merchantStatus', 'unfreeze');
  38. if (in_array($status, [$pass, $unfreeze]) == false) {
  39. $statusNameList = configDict::getConfig('merchantStatusName');
  40. return ['code' => 'A0002', 'msg' => '帐号' . $statusNameList[$status]];
  41. }
  42. //七天免登陆或者10分钟未操作退出
  43. $time = time();
  44. $autoLogin = Yii::$app->request->post('autoLogin', 1);
  45. $expireTime = $autoLogin == 1 ? ($time + 7 * 86400) : (10 * 60 + $time);
  46. $redisExpireTime = 7 * 86400;
  47. $urlSession = Yii::$app->request->get('SESSIONID', '');
  48. if (empty($urlSession)) {
  49. $sessionId = AdminService::generateSessionId($merchantId);
  50. Yii::$app->response->cookies->add(new Cookie([
  51. 'name' => 'SESSIONID',
  52. 'value' => $sessionId,
  53. 'domain' => $_SERVER['SERVER_NAME'],
  54. 'expire' => $expireTime
  55. ]));
  56. //保存到redis
  57. Yii::$app->redis->executeCommand('SET', [$sessionId, $adminId]);
  58. Yii::$app->redis->executeCommand('EXPIRE', [$sessionId, $redisExpireTime]);//过期
  59. }
  60. return ['code' => 'A0001', 'msg' => '登陆成功'];
  61. }
  62. /**
  63. * 微信等第三方登陆
  64. */
  65. public static function loginByThird($admin, $autoLogin = false)
  66. {
  67. $session = Yii::$app->session;
  68. $session['isLogin'] = true;
  69. $session['id'] = $admin['id'];
  70. }
  71. public static function getByMobile($mobile)
  72. {
  73. $admin = xhAdmin::find()->where(['mobile' => $mobile])->asArray()->one();
  74. return $admin;
  75. }
  76. public static function isLogin()
  77. {
  78. $session = Yii::$app->session;
  79. if (isset($session['isLogin']) && !empty($session['isLogin'])) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. public static function getId()
  85. {
  86. $session = Yii::$app->session;
  87. $userId = $session['id'];
  88. return $userId;
  89. }
  90. public static function logout()
  91. {
  92. //清除缓存
  93. $sessionId = Yii::$app->request->cookies->getValue("SESSIONID");
  94. Yii::$app->redis->executeCommand('DEL', [$sessionId]);
  95. //清除cookie
  96. $cookie = Yii::$app->response->cookies;
  97. $cookie->remove('SESSIONID');
  98. $cookie->add(new Cookie([
  99. 'name' => 'SESSIONID',
  100. 'value' => '',
  101. 'expire' => time() - 30 * 24 * 3600,
  102. 'domain' => $_SERVER['SERVER_NAME']
  103. ]));
  104. //清除session
  105. Yii::$app->session->destroy();//销毁session中所有已注册的数据
  106. }
  107. /**
  108. * 通过微信生成用户
  109. */
  110. public static function generateAdmin($baseInfo)
  111. {
  112. $date = date("Y-m-d H:i:s");
  113. $data = [];
  114. $data['mobile'] = isset($baseInfo['mobile']) ? $baseInfo['mobile'] : '';
  115. $nickName = $baseInfo['nickName'];
  116. $nickName = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $nickName);//替换掉微信不支持的图片
  117. $data['adminName'] = $nickName;
  118. $data['avatar'] = '';
  119. $data['password'] = isset($baseInfo['password']) ? md5($baseInfo['password']) : '';
  120. $data['confirmPassword'] = '';
  121. $data['openId'] = $baseInfo['openid'];
  122. $data['unionId'] = isset($baseInfo['unionId']) ? $baseInfo['unionId'] : '';
  123. $data['subscribe'] = isset($baseInfo['subscribe']) ? $baseInfo['subscribe'] : 0;
  124. $data['merchantId'] = isset($baseInfo['merchantId']) ? $baseInfo['merchantId'] : 0;
  125. $data['status'] = 0;
  126. $data['createTime'] = $date;
  127. $admin = self::add($data);
  128. //保存头像
  129. if (!empty($baseInfo['headimgurl'])) {
  130. $imgUrl = $baseInfo['headimgurl'];
  131. $adminId = $admin['id'];
  132. $avatar = self::avatarSave($imgUrl, $adminId);
  133. self::updateById($admin['id'], ['avatar' => $avatar]);
  134. }
  135. return $admin;
  136. }
  137. /**
  138. * 保存头像
  139. * @return string
  140. */
  141. public static function avatarSave($url, $adminId)
  142. {
  143. $directory = configDict::getConfig('imgSavePath', 'wxAdminAvatar');
  144. $path = Yii::getAlias('@webroot') . '/../../images';
  145. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  146. if (!file_exists($path . $pre)) {
  147. mkdir($path . $pre, 0777, true);
  148. }
  149. $randString = stringUtil::buildOrderNo();
  150. $name = $adminId . '_' . $randString . '.jpg';
  151. $fullFileName = $path . $pre . $name;
  152. $fileName = $pre . $name;
  153. $curl = new curl\Curl();
  154. $result = $curl->get($url);
  155. $fp2 = @fopen($fullFileName, 'a');//文件大小
  156. fwrite($fp2, $result);
  157. fclose($fp2);
  158. $dstImg200 = $path . $pre . $adminId . '_' . $randString . '_200.jpg';//缩略宽 200px 中
  159. util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
  160. $dstImg50 = $path . $pre . $adminId . '_' . $randString . '_50.jpg';//缩略成宽50px 小
  161. util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
  162. return $fileName;//默认大小 大
  163. }
  164. /**
  165. * 传入最大的头像,输出三种头像:大 中 小
  166. */
  167. public static function getAvatarList($largeImg)
  168. {
  169. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  170. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  171. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  172. }
  173. public static function getById($id)
  174. {
  175. $admin = xhAdmin::find()->where(['id' => $id])->asArray()->one();
  176. return $admin;
  177. }
  178. public static function getByOpenId($openId)
  179. {
  180. $cacheKey = configDict::getCacheKey('xhAdminOpenId') . $openId;
  181. $admin = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  182. if (!empty($admin)) {
  183. $arr = [];
  184. $i = 0;
  185. $x = 0;
  186. foreach ($admin as $key => $val) {
  187. $i++;
  188. if ($i % 2 == 0) {
  189. $arr[$x]['value'] = $val;
  190. $x++;
  191. } else {
  192. $arr[$x]['key'] = $val;
  193. }
  194. }
  195. $adminArr = [];
  196. foreach ($arr as $key => $val) {
  197. $adminArr[$val['key']] = $val['value'];
  198. }
  199. unset($adminArr['info_already_get_by_sql']);
  200. return $adminArr;
  201. }
  202. $admin = xhAdmin::find()->where(['openId' => $openId])->asArray()->one();
  203. $params = [$cacheKey];
  204. $params[] = 'info_already_get_by_sql';
  205. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  206. if (!empty($admin)) {
  207. foreach ($admin as $key => $val) {
  208. $params[] = $key;
  209. $params[] = $val;
  210. }
  211. }
  212. Yii::$app->redis->executeCommand('HMSET', $params);//将用户信息、用户标签放入缓存
  213. return $admin;
  214. }
  215. public static function refreshById($id)
  216. {
  217. $adminKey = configDict::getCacheKey('admin') . $id;
  218. Yii::$app->redis->executeCommand('DEL', [$adminKey]);
  219. $admin = self::getById($id);
  220. $mobile = $admin['mobile'];
  221. $mobileKey = configDict::getCacheKey('xhAdminMobile') . $mobile;
  222. Yii::$app->redis->executeCommand('DEL', [$mobileKey]);
  223. self::getByMobile($mobile);
  224. $openId = $admin['openId'];
  225. $openKey = configDict::getCacheKey('xhAdminOpenId') . $openId;
  226. Yii::$app->redis->executeCommand('DEL', [$openKey]);
  227. self::getByOpenId($openId);
  228. }
  229. public static function updateById($id, $data)
  230. {
  231. xhAdmin::updateById($id, $data);
  232. self::refreshById($id);
  233. }
  234. public static function deleteById($id)
  235. {
  236. xhAdmin::deleteById($id);
  237. $preKey = configDict::getCacheKey('admin');
  238. $cacheKey = $preKey . $id;
  239. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  240. }
  241. public static function add($data)
  242. {
  243. $admin = xhAdmin::add($data);
  244. $id = $admin['id'];
  245. self::refreshById($id);
  246. return $admin;
  247. }
  248. }