LoginServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\yihaotong\SmsRecordServices;
  16. use app\services\message\notice\SmsService;
  17. use app\services\wechat\WechatUserServices;
  18. use crmeb\exceptions\ApiException;
  19. use crmeb\services\CacheService;
  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 UserDao $dao
  31. */
  32. public function __construct(UserDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * H5账号登陆
  38. * @param $account
  39. * @param $password
  40. * @param $spread
  41. * @return array
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function login($account, $password, $spread)
  47. {
  48. $user = $this->dao->getOne(['account|phone' => $account, 'is_del' => 0]);
  49. if ($user) {
  50. if ($user->pwd !== md5((string)$password))
  51. throw new ApiException(410025);
  52. if ($user->pwd === md5('123456'))
  53. throw new ApiException(410026);
  54. } else {
  55. throw new ApiException(410025);
  56. }
  57. if (!$user['status'])
  58. throw new ApiException(410027);
  59. //更新用户信息
  60. $this->updateUserInfo(['code' => $spread], $user);
  61. $token = $this->createToken((int)$user['uid'], 'api');
  62. if ($token) {
  63. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  64. } else
  65. throw new ApiException(410019);
  66. }
  67. /**
  68. * 更新用户信息
  69. * @param $user
  70. * @param $userInfo
  71. * @param false $is_new
  72. * @return bool
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function updateUserInfo($user, $userInfo, $is_new = false)
  78. {
  79. $data = [];
  80. $data['nickname'] = !isset($user['nickname']) || !$user['nickname'] ? $userInfo->nickname : $user['nickname'];
  81. $data['avatar'] = !isset($user['headimgurl']) || !$user['headimgurl'] ? $userInfo->avatar : $user['headimgurl'];
  82. $data['phone'] = !isset($user['phone']) || !$user['phone'] ? $userInfo->phone : $user['phone'];
  83. $data['last_time'] = time();
  84. $data['last_ip'] = app()->request->ip();
  85. $spreadUid = $user['code'] ?? 0;
  86. //如果扫了员工邀请码,上级,代理商,区域代理都会改动。
  87. if (isset($user['is_staff']) && !$userInfo['is_agent'] && !$userInfo['is_division']) {
  88. $spreadInfo = $this->dao->get($spreadUid);
  89. if ($userInfo['uid'] != $spreadUid) {
  90. $data['spread_uid'] = $spreadUid;
  91. $data['spread_time'] = $userInfo->last_time;
  92. }
  93. $data['agent_id'] = $spreadInfo->agent_id;
  94. $data['division_id'] = $spreadInfo->division_id;
  95. $data['staff_id'] = $userInfo['uid'];
  96. $data['is_staff'] = $user['is_staff'] ?? 0;
  97. $data['division_type'] = 3;
  98. $data['division_change_time'] = time();
  99. $data['division_end_time'] = $spreadInfo->division_end_time;
  100. //如果店员切换代理商,则店员在之前代理商下推广的用户,他们的直接上级从当前店员变为之前代理商
  101. if ($userInfo->agent_id != 0 && $userInfo->agent_id != $spreadInfo->agent_id) {
  102. $this->dao->update($userInfo['uid'], ['spread_uid' => $userInfo->agent_id], 'spread_uid');
  103. }
  104. }
  105. if ($is_new) {
  106. if ($spreadUid) {
  107. $spreadInfo = $this->dao->get($spreadUid);
  108. $spreadUid = (int)$spreadUid;
  109. $data['spread_uid'] = $spreadUid;
  110. $data['spread_time'] = time();
  111. $data['agent_id'] = $spreadInfo->agent_id;
  112. $data['division_id'] = $spreadInfo->division_id;
  113. $data['staff_id'] = $spreadInfo->staff_id;
  114. //绑定用户后置事件
  115. event('user.register', [$spreadUid, $userInfo['user_type'], $userInfo['nickname'], $userInfo['uid'], 0]);
  116. //推送消息
  117. event('notice.notice', [['spreadUid' => $spreadUid, 'user_type' => $userInfo['user_type'], 'nickname' => $userInfo['nickname']], 'bind_spread_uid']);
  118. }
  119. } else {
  120. //永久绑定
  121. $store_brokerage_binding_status = sys_config('store_brokerage_binding_status', 1);
  122. if ($userInfo->spread_uid && $store_brokerage_binding_status == 1 && !isset($user['is_staff'])) {
  123. $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
  124. } else {
  125. //绑定分销关系 = 所有用户
  126. if (sys_config('brokerage_bindind', 1) == 1) {
  127. //分销绑定类型为时间段且过期 ||临时
  128. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  129. if (!$userInfo['spread_uid'] || $store_brokerage_binding_status == 3 || ($store_brokerage_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
  130. if ($spreadUid && $user['code'] != $userInfo->uid && $userInfo->uid != $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
  131. $spreadInfo = $this->dao->get($spreadUid);
  132. $spreadUid = (int)$spreadUid;
  133. $data['spread_uid'] = $spreadUid;
  134. $data['spread_time'] = time();
  135. $data['agent_id'] = $spreadInfo->agent_id;
  136. $data['division_id'] = $spreadInfo->division_id;
  137. $data['staff_id'] = $spreadInfo->staff_id;
  138. //绑定用户后置事件
  139. event('user.register', [$spreadUid, $userInfo['user_type'], $userInfo['nickname'], $userInfo['uid'], 0]);
  140. //推送消息
  141. event('notice.notice', [['spreadUid' => $spreadUid, 'user_type' => $userInfo['user_type'], 'nickname' => $userInfo['nickname']], 'bind_spread_uid']);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. if (!$this->dao->update($userInfo['uid'], $data, 'uid')) {
  148. throw new ApiException(100007);
  149. }
  150. return true;
  151. }
  152. public function verify(SmsService $services, $phone, $type, $time, $ip)
  153. {
  154. if ($this->dao->getOne(['account' => $phone, 'is_del' => 0]) && $type == 'register') {
  155. throw new ApiException(410028);
  156. }
  157. $default = Config::get('sms.default', 'yihaotong');
  158. $defaultMaxPhoneCount = Config::get('sms.maxPhoneCount', 10);
  159. $defaultMaxIpCount = Config::get('sms.maxIpCount', 50);
  160. $maxPhoneCount = Config::get('sms.stores.' . $default . '.maxPhoneCount', $defaultMaxPhoneCount);
  161. $maxIpCount = Config::get('sms.stores.' . $default . '.maxIpCount', $defaultMaxIpCount);
  162. /** @var SmsRecordServices $smsRecord */
  163. $smsRecord = app()->make(SmsRecordServices::class);
  164. if ($smsRecord->count(['phone' => $phone, 'add_ip' => $ip, 'time' => 'today']) >= $maxPhoneCount) {
  165. throw new ApiException(410029);
  166. }
  167. if ($smsRecord->count(['add_ip' => $ip, 'time' => 'today']) >= $maxIpCount) {
  168. throw new ApiException(410030);
  169. }
  170. $code = rand(100000, 999999);
  171. $data['code'] = $code;
  172. $data['time'] = $time;
  173. $res = $services->send(true, $phone, $data, 'verify_code');
  174. if ($res !== true)
  175. throw new ApiException(410031);
  176. return $code;
  177. }
  178. /**
  179. * H5用户注册
  180. * @param $account
  181. * @param $password
  182. * @param $spread
  183. * @param string $user_type
  184. * @return mixed
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function register($account, $password, $spread, $user_type = 'h5')
  190. {
  191. if ($this->dao->getOne(['account|phone' => $account, 'is_del' => 0])) {
  192. throw new ApiException(410028);
  193. }
  194. /** @var UserServices $userServices */
  195. $userServices = app()->make(UserServices::class);
  196. $phone = $account;
  197. $data['account'] = $account;
  198. $data['pwd'] = md5((string)$password);
  199. $data['phone'] = $phone;
  200. if ($spread) {
  201. $data['spread_uid'] = $spread;
  202. $data['spread_time'] = time();
  203. $spreadInfo = $userServices->get($spread);
  204. $data['division_id'] = $spreadInfo['division_id'];
  205. $data['agent_id'] = $spreadInfo['agent_id'];
  206. $data['staff_id'] = $spreadInfo['staff_id'];
  207. }
  208. $data['real_name'] = '';
  209. $data['birthday'] = 0;
  210. $data['card_id'] = '';
  211. $data['mark'] = '';
  212. $data['addres'] = '';
  213. $data['user_type'] = $user_type;
  214. $data['add_time'] = time();
  215. $data['add_ip'] = app('request')->ip();
  216. $data['last_time'] = time();
  217. $data['last_ip'] = app('request')->ip();
  218. $data['nickname'] = substr_replace($account, '****', 3, 4);
  219. $data['avatar'] = sys_config('h5_avatar');
  220. $data['city'] = '';
  221. $data['language'] = '';
  222. $data['province'] = '';
  223. $data['country'] = '';
  224. $data['status'] = 1;
  225. if (!$re = $this->dao->save($data)) {
  226. throw new ApiException(410014);
  227. } else {
  228. $userServices->rewardNewUser((int)$re->uid);
  229. //用户生成后置事件
  230. event('user.register', [$spread, $user_type, $data['nickname'], $re->uid, 1]);
  231. //推送消息
  232. event('notice.notice', [['spreadUid' => $spread, 'user_type' => $user_type, 'nickname' => $data['nickname']], 'bind_spread_uid']);
  233. return $re;
  234. }
  235. }
  236. /**
  237. * 重置密码
  238. * @param $account
  239. * @param $password
  240. * @return bool
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function reset($account, $password)
  246. {
  247. $user = $this->dao->getOne(['account|phone' => $account, 'is_del' => 0]);
  248. if (!$user) {
  249. throw new ApiException(410032);
  250. }
  251. if (!$this->dao->update($user['uid'], ['pwd' => md5((string)$password)], 'uid')) {
  252. throw new ApiException(410033);
  253. }
  254. return true;
  255. }
  256. /**
  257. * 手机号登录
  258. * @param $phone
  259. * @param $spread
  260. * @param string $user_type
  261. * @return array
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. public function mobile($phone, $spread, string $user_type = 'h5')
  267. {
  268. //数据库查询
  269. $user = $this->dao->getOne(['account|phone' => $phone, 'is_del' => 0]);
  270. if (!$user) {
  271. $user = $this->register($phone, '123456', $spread, $user_type);
  272. if (!$user) {
  273. throw new ApiException(410034);
  274. }
  275. }
  276. if (!$user->status)
  277. throw new ApiException(410027);
  278. // 设置推广关系
  279. $this->updateUserInfo(['code' => $spread], $user);
  280. $token = $this->createToken((int)$user['uid'], 'api');
  281. if ($token) {
  282. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  283. } else {
  284. throw new ApiException(410019);
  285. }
  286. }
  287. /**
  288. * 切换登录
  289. * @param $user
  290. * @param $from
  291. * @return array
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function switchAccount($user, $from)
  297. {
  298. if ($from === 'h5') {
  299. $where = [['phone', '=', $user['phone']], ['user_type', '<>', 'h5'], ['is_del', '=', 0]];
  300. $login_type = 'wechat';
  301. } else {
  302. //数据库查询
  303. $where = [['account|phone', '=', $user['phone']], ['user_type', '=', 'h5'], ['is_del', '=', 0]];
  304. $login_type = 'h5';
  305. }
  306. $switch_user = $this->dao->getOne($where);
  307. if (!$switch_user) {
  308. return app('json')->fail(410035);
  309. }
  310. if (!$switch_user->status) {
  311. return app('json')->fail(410027);
  312. }
  313. $edit_data = ['login_type' => $login_type];
  314. if (!$this->dao->update($switch_user['uid'], $edit_data, 'uid')) {
  315. throw new ApiException(410036);
  316. }
  317. $token = $this->createToken((int)$switch_user['uid'], 'api');
  318. if ($token) {
  319. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  320. } else {
  321. throw new ApiException(410019);
  322. }
  323. }
  324. /**
  325. * 绑定手机号(静默还没写入用户信息)
  326. * @param $phone
  327. * @param string $key
  328. * @return array
  329. * @throws \Psr\SimpleCache\InvalidArgumentException
  330. * @throws \think\db\exception\DataNotFoundException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public function bindind_phone($phone, string $key = '')
  334. {
  335. if (!$key) {
  336. throw new ApiException(410037);
  337. }
  338. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
  339. if (!$createData) {
  340. throw new ApiException(410037);
  341. }
  342. $wechatInfo['phone'] = $phone;
  343. /** @var WechatUserServices $wechatUser */
  344. $wechatUser = app()->make(WechatUserServices::class);
  345. //更新用户信息
  346. $user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  347. $token = $this->createToken((int)$user['uid'], $userType);
  348. if ($token) {
  349. return [
  350. 'token' => $token['token'],
  351. 'userInfo' => $user,
  352. 'expires_time' => $token['params']['exp'],
  353. ];
  354. } else
  355. return app('json')->fail(410019);
  356. }
  357. /**
  358. * 用户绑定手机号
  359. * @param int $uid
  360. * @param $phone
  361. * @param $step
  362. * @return array
  363. * @throws \think\db\exception\DataNotFoundException
  364. * @throws \think\db\exception\DbException
  365. * @throws \think\db\exception\ModelNotFoundException
  366. */
  367. public function userBindindPhone(int $uid, $phone, $step)
  368. {
  369. $userInfo = $this->dao->get($uid);
  370. if (!$userInfo) {
  371. throw new ApiException(410113);
  372. }
  373. if ($this->dao->getOne([['phone', '=', $phone], ['user_type', '<>', 'h5'], ['is_del', '=', 0]])) {
  374. throw new ApiException(410039);
  375. }
  376. if ($userInfo->phone) {
  377. throw new ApiException(410040);
  378. }
  379. $data = [];
  380. if ($this->dao->getOne(['account' => $phone, 'phone' => $phone, 'user_type' => 'h5', 'is_del' => 0])) {
  381. if (!$step) return ['msg' => 410041, 'data' => ['is_bind' => 1]];
  382. } else {
  383. $data['account'] = $phone;
  384. }
  385. $data['phone'] = $phone;
  386. if ($this->dao->update($userInfo['uid'], $data, 'uid') || $userInfo->phone == $phone)
  387. return ['msg' => 410016, 'data' => []];
  388. else
  389. throw new ApiException(410017);
  390. }
  391. /**
  392. * 用户绑定手机号
  393. * @param int $uid
  394. * @param $phone
  395. * @return array
  396. * @throws \think\db\exception\DataNotFoundException
  397. * @throws \think\db\exception\DbException
  398. * @throws \think\db\exception\ModelNotFoundException
  399. */
  400. public function updateBindindPhone(int $uid, $phone)
  401. {
  402. $userInfo = $this->dao->get(['uid' => $uid, 'is_del' => 0]);
  403. if (!$userInfo) {
  404. throw new ApiException(410113);
  405. }
  406. if ($userInfo->phone == $phone) {
  407. throw new ApiException(410042);
  408. }
  409. if ($this->dao->getOne([['phone', '=', $phone], ['is_del', '=', 0]])) {
  410. throw new ApiException(410043);
  411. }
  412. $data = [];
  413. $data['phone'] = $phone;
  414. $data['account'] = $phone;
  415. if ($this->dao->update($userInfo['uid'], $data, 'uid'))
  416. return ['msg' => 100001, 'data' => []];
  417. else
  418. throw new ApiException(100007);
  419. }
  420. }