LoginServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\dao\user\UserDao;
  14. use app\services\BaseServices;
  15. use app\services\message\sms\SmsRecordServices;
  16. use app\services\message\sms\SmsSendServices;
  17. use app\services\wechat\WechatUserServices;
  18. use crmeb\services\CacheService;
  19. use think\exception\ValidateException;
  20. use think\facade\Config;
  21. /**
  22. *
  23. * Class LoginServices
  24. * @package app\services\user
  25. */
  26. class LoginServices extends BaseServices
  27. {
  28. /**
  29. * LoginServices constructor.
  30. * @param LoginDao $dao
  31. */
  32. public function __construct(UserDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * H5账号登陆
  38. * @param Request $request
  39. * @return mixed
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. */
  44. public function login($account, $password, $spread)
  45. {
  46. $user = $this->dao->getOne(['account|phone' => $account]);
  47. if ($user) {
  48. if ($user->pwd !== md5((string)$password))
  49. throw new ValidateException('账号或密码错误');
  50. if ($user->pwd === md5('123456'))
  51. throw new ValidateException('请修改您的初始密码,再尝试登录!');
  52. } else {
  53. throw new ValidateException('账号或密码错误');
  54. }
  55. if (!$user['status'])
  56. throw new ValidateException('已被禁止,请联系管理员');
  57. //更新用户信息
  58. $this->updateUserInfo(['code' => $spread], $user);
  59. $token = $this->createToken((int)$user['uid'], 'api');
  60. if ($token) {
  61. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  62. } else
  63. throw new ValidateException('登录失败');
  64. }
  65. /**
  66. * 更新用户信息
  67. * @param $user
  68. * @param $uid
  69. * @return bool
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function updateUserInfo($user, $userInfo)
  75. {
  76. $data = [];
  77. $data['nickname'] = !isset($user['nickname']) || !$user['nickname'] ? $userInfo->nickname : $user['nickname'];
  78. $data['avatar'] = !isset($user['headimgurl']) || !$user['headimgurl'] ? $userInfo->avatar : $user['headimgurl'];
  79. $data['phone'] = !isset($user['phone']) || !$user['phone'] ? $userInfo->phone : $user['phone'];
  80. $data['last_time'] = time();
  81. $data['last_ip'] = app()->request->ip();
  82. //永久绑定
  83. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  84. if ($userInfo->spread_uid && $store_brokergae_binding_status == 1) {
  85. $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
  86. } else {
  87. //绑定分销关系 = 所有用户
  88. if (sys_config('brokerage_bindind', 1) == 1) {
  89. //分销绑定类型为时间段且过期 ||临时
  90. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  91. if (!$userInfo['spread_uid'] || $store_brokergae_binding_status == 3 || ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
  92. $spreadUid = isset($user['code']) && $user['code'] && $user['code'] != $userInfo->uid ? $user['code'] : 0;
  93. if ($spreadUid && $userInfo->uid == $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
  94. $spreadUid = 0;
  95. }
  96. if ($spreadUid) {
  97. $spreadUid = (int)$spreadUid;
  98. $data['spread_uid'] = $spreadUid;
  99. $data['spread_time'] = $userInfo->last_time;
  100. $this->dao->incField($spreadUid, 'spread_count', 1);
  101. //绑定用户后置事件
  102. event('user.register', [$spreadUid, $userInfo['user_type'], $userInfo['nickname'], $userInfo['uid'], 0]);
  103. //推送消息
  104. event('notice.notice', [['spreadUid' => $spreadUid, 'user_type' => $userInfo['user_type'], 'nickname' => $userInfo['nickname']], 'bind_spread_uid']);
  105. }
  106. }
  107. }
  108. }
  109. if (!$this->dao->update($userInfo['uid'], $data, 'uid')) {
  110. throw new ValidateException('修改信息失败');
  111. }
  112. return true;
  113. }
  114. public function verify(SmsSendServices $services, $phone, $type, $time, $ip)
  115. {
  116. if ($this->dao->getOne(['account' => $phone]) && $type == 'register') {
  117. throw new ValidateException('手机号已注册');
  118. }
  119. $default = Config::get('sms.default', 'yunxin');
  120. $defaultMaxPhoneCount = Config::get('sms.maxPhoneCount', 10);
  121. $defaultMaxIpCount = Config::get('sms.maxIpCount', 50);
  122. $maxPhoneCount = Config::get('sms.stores.' . $default . '.maxPhoneCount', $defaultMaxPhoneCount);
  123. $maxIpCount = Config::get('sms.stores.' . $default . '.maxIpCount', $defaultMaxIpCount);
  124. /** @var SmsRecordServices $smsRecord */
  125. $smsRecord = app()->make(SmsRecordServices::class);
  126. if ($smsRecord->count(['phone' => $phone, 'add_ip' => $ip, 'time' => 'today']) >= $maxPhoneCount) {
  127. throw new ValidateException('您今日发送得短信次数已经达到上限');
  128. }
  129. if ($smsRecord->count(['add_ip' => $ip, 'time' => 'today']) >= $maxIpCount) {
  130. throw new ValidateException('此IP今日发送次数已经达到上限');
  131. }
  132. $code = rand(100000, 999999);
  133. $data['code'] = $code;
  134. $data['time'] = $time;
  135. $res = $services->send(true, $phone, $data, 'VERIFICATION_CODE_TIME');
  136. if ($res !== true)
  137. throw new ValidateException('短信平台验证码发送失败' . $res);
  138. return $code;
  139. }
  140. /**
  141. * H5用户注册
  142. * @param $account
  143. * @param $password
  144. * @param $spread
  145. * @return User|\think\Model
  146. */
  147. public function register($account, $password, $spread, $user_type = 'h5')
  148. {
  149. if ($this->dao->getOne(['account|phone' => $account])) {
  150. throw new ValidateException('用户已存在,请去修改密码');
  151. }
  152. $phone = $account;
  153. $data['account'] = $account;
  154. $data['pwd'] = md5((string)$password);
  155. $data['phone'] = $phone;
  156. if ($spread) {
  157. $data['spread_uid'] = $spread;
  158. $data['spread_time'] = time();
  159. }
  160. $data['real_name'] = '';
  161. $data['birthday'] = 0;
  162. $data['card_id'] = '';
  163. $data['mark'] = '';
  164. $data['addres'] = '';
  165. $data['user_type'] = $user_type;
  166. $data['add_time'] = time();
  167. $data['add_ip'] = app('request')->ip();
  168. $data['last_time'] = time();
  169. $data['last_ip'] = app('request')->ip();
  170. $data['nickname'] = substr(md5($account . time()), 0, 12);
  171. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  172. $data['city'] = '';
  173. $data['language'] = '';
  174. $data['province'] = '';
  175. $data['country'] = '';
  176. $data['status'] = 1;
  177. if (!$re = $this->dao->save($data)) {
  178. throw new ValidateException('注册失败');
  179. } else {
  180. //用户生成后置事件
  181. event('user.register', [$spread, $user_type, $data['nickname'], $re->uid, 1]);
  182. //推送消息
  183. event('notice.notice', [['spreadUid' => $spread, 'user_type' => $user_type, 'nickname' => $data['nickname']], 'bind_spread_uid']);
  184. return $re;
  185. }
  186. }
  187. /**
  188. * 重置密码
  189. * @param $account
  190. * @param $password
  191. */
  192. public function reset($account, $password)
  193. {
  194. $user = $this->dao->getOne(['account|phone' => $account]);
  195. if (!$user) {
  196. throw new ValidateException('用户不存在');
  197. }
  198. if (!$this->dao->update($user['uid'], ['pwd' => md5((string)$password)], 'uid')) {
  199. throw new ValidateException('修改密码失败');
  200. }
  201. return true;
  202. }
  203. /**
  204. * 手机号登录
  205. * @param $phone
  206. * @param $spread
  207. * @return array
  208. * @throws \think\db\exception\DataNotFoundException
  209. * @throws \think\db\exception\DbException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. */
  212. public function mobile($phone, $spread, $user_type = 'h5')
  213. {
  214. //数据库查询
  215. $user = $this->dao->getOne(['phone' => $phone]);
  216. if (!$user) {
  217. $user = $this->register($phone, '123456', $spread, $user_type);
  218. if (!$user) {
  219. throw new ValidateException('用户登录失败,无法生成新用户,请稍后再试!');
  220. }
  221. }
  222. if (!$user->status)
  223. throw new ValidateException('已被禁止,请联系管理员');
  224. // 设置推广关系
  225. $this->updateUserInfo(['code' => $spread], $user);
  226. $token = $this->createToken((int)$user['uid'], 'api');
  227. if ($token) {
  228. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  229. } else {
  230. throw new ValidateException('登录失败');
  231. }
  232. }
  233. /**
  234. * 切换登录
  235. * @param $user
  236. * @param $from
  237. */
  238. public function switchAccount($user, $from)
  239. {
  240. if ($from === 'h5') {
  241. $where = [['phone', '=', $user['phone']], ['user_type', '<>', 'h5']];
  242. $login_type = 'wechat';
  243. } else {
  244. //数据库查询
  245. $where = [['account|phone', '=', $user['phone']], ['user_type', '=', 'h5']];
  246. $login_type = 'h5';
  247. }
  248. $switch_user = $this->dao->getOne($where);
  249. if (!$switch_user) {
  250. return app('json')->fail('用户不存在,无法切换');
  251. }
  252. if (!$switch_user->status) {
  253. return app('json')->fail('已被禁止,请联系管理员');
  254. }
  255. $edit_data = ['login_type' => $login_type];
  256. if (!$this->dao->update($switch_user['uid'], $edit_data, 'uid')) {
  257. throw new ValidateException('修改新用户登录类型出错');
  258. }
  259. $token = $this->createToken((int)$switch_user['uid'], 'api');
  260. if ($token) {
  261. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  262. } else {
  263. throw new ValidateException('切换失败');
  264. }
  265. }
  266. /**
  267. * 绑定手机号(静默还没写入用户信息)
  268. * @param $user
  269. * @param $phone
  270. * @param $step
  271. * @return mixed
  272. */
  273. public function bindind_phone($phone, $key = '')
  274. {
  275. if (!$key) {
  276. throw new ValidateException('请刷新页面或者重新授权');
  277. }
  278. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
  279. if (!$createData) {
  280. throw new ValidateException('请刷新页面或者重新授权');
  281. }
  282. $wechatInfo['phone'] = $phone;
  283. /** @var WechatUserServices $wechatUser */
  284. $wechatUser = app()->make(WechatUserServices::class);
  285. //更新用户信息
  286. $user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  287. $token = $this->createToken((int)$user['uid'], $userType);
  288. if ($token) {
  289. return [
  290. 'token' => $token['token'],
  291. 'userInfo' => $user,
  292. 'expires_time' => $token['params']['exp'],
  293. ];
  294. } else
  295. return app('json')->fail('获取用户访问token失败!');
  296. }
  297. /**
  298. * 用户绑定手机号
  299. * @param $user
  300. * @param $phone
  301. * @param $step
  302. * @return mixed
  303. */
  304. public function userBindindPhone(int $uid, $phone, $step)
  305. {
  306. $userInfo = $this->dao->get($uid);
  307. if (!$userInfo) {
  308. throw new ValidateException('用户不存在');
  309. }
  310. if ($this->dao->getOne([['phone', '=', $phone], ['user_type', '<>', 'h5']])) {
  311. throw new ValidateException('此手机已经绑定,无法多次绑定!');
  312. }
  313. if ($userInfo->phone) {
  314. throw new ValidateException('您的账号已经绑定过手机号码!');
  315. }
  316. $data = [];
  317. if ($this->dao->getOne(['account' => $phone, 'phone' => $phone, 'user_type' => 'h5'])) {
  318. if (!$step) return ['msg' => 'H5已有账号是否绑定此账号上', 'data' => ['is_bind' => 1]];
  319. } else {
  320. $data['account'] = $phone;
  321. }
  322. $data['phone'] = $phone;
  323. if ($this->dao->update($userInfo['uid'], $data, 'uid') || $userInfo->phone == $phone)
  324. return ['msg' => '绑定成功', 'data' => []];
  325. else
  326. throw new ValidateException('绑定失败');
  327. }
  328. /**
  329. * 用户绑定手机号
  330. * @param $user
  331. * @param $phone
  332. * @param $step
  333. * @return mixed
  334. */
  335. public function updateBindindPhone(int $uid, $phone)
  336. {
  337. $userInfo = $this->dao->get($uid);
  338. if (!$userInfo) {
  339. throw new ValidateException('用户不存在');
  340. }
  341. if ($userInfo->phone == $phone) {
  342. throw new ValidateException('新手机号和原手机号相同,无需修改');
  343. }
  344. if ($this->dao->getOne([['phone', '=', $phone]])) {
  345. throw new ValidateException('此手机已经注册');
  346. }
  347. $data = [];
  348. $data['phone'] = $phone;
  349. if ($this->dao->update($userInfo['uid'], $data, 'uid'))
  350. return ['msg' => '修改成功', 'data' => []];
  351. else
  352. throw new ValidateException('修改失败');
  353. }
  354. }