LoginServices.php 17 KB

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