LoginServices.php 17 KB

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