xhAdminService.php 9.1 KB

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