LoginServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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(['staff_id' => $userInfo['uid'], 'spread_uid' => $userInfo['uid']], ['spread_uid' => $spreadInfo['agent_id'], 'staff_id' => 0]);
  102. $this->dao->update(['staff_id' => $userInfo['uid'], 'not_spread_uid' => $userInfo['uid']], ['staff_id' => 0]);
  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('UserRegisterListener', [$spreadUid, $userInfo['user_type'], $userInfo['nickname'], $userInfo['uid'], 0]);
  116. //推送消息
  117. event('NoticeListener', [['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('UserRegisterListener', [$spreadUid, $userInfo['user_type'], $userInfo['nickname'], $userInfo['uid'], 0]);
  140. //推送消息
  141. event('NoticeListener', [['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)
  153. {
  154. if ($this->dao->getOne(['account' => $phone, 'is_del' => 0]) && $type == 'register') {
  155. throw new ApiException(410028);
  156. }
  157. $code = rand(100000, 999999);
  158. $data['code'] = $code;
  159. $data['time'] = $time;
  160. $res = $services->send(true, $phone, $data, 'verify_code');
  161. if ($res !== true)
  162. throw new ApiException(410031);
  163. return $code;
  164. }
  165. /**
  166. * H5用户注册
  167. * @param $account
  168. * @param $password
  169. * @param $spread
  170. * @param string $user_type
  171. * @return mixed
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public function register($account, $password, $spread, $user_type = 'h5')
  177. {
  178. if ($this->dao->getOne(['account|phone' => $account, 'is_del' => 0])) {
  179. throw new ApiException(410028);
  180. }
  181. /** @var UserServices $userServices */
  182. $userServices = app()->make(UserServices::class);
  183. $phone = $account;
  184. $data['account'] = $account;
  185. $data['pwd'] = md5((string)$password);
  186. $data['phone'] = $phone;
  187. if ($spread) {
  188. $data['spread_uid'] = $spread;
  189. $data['spread_time'] = time();
  190. $spreadInfo = $userServices->get($spread);
  191. $data['division_id'] = $spreadInfo['division_id'];
  192. $data['agent_id'] = $spreadInfo['agent_id'];
  193. $data['staff_id'] = $spreadInfo['staff_id'];
  194. }
  195. $data['real_name'] = '';
  196. $data['birthday'] = 0;
  197. $data['card_id'] = '';
  198. $data['mark'] = '';
  199. $data['addres'] = '';
  200. $data['user_type'] = $user_type;
  201. $data['add_time'] = time();
  202. $data['add_ip'] = app('request')->ip();
  203. $data['last_time'] = time();
  204. $data['last_ip'] = app('request')->ip();
  205. $data['nickname'] = substr_replace($account, '****', 3, 4);
  206. $data['avatar'] = sys_config('h5_avatar');
  207. $data['city'] = '';
  208. $data['language'] = '';
  209. $data['province'] = '';
  210. $data['country'] = '';
  211. $data['status'] = 1;
  212. if (!$re = $this->dao->save($data)) {
  213. throw new ApiException(410014);
  214. } else {
  215. $userServices->rewardNewUser((int)$re->uid);
  216. //用户生成后置事件
  217. event('UserRegisterListener', [$spread, $user_type, $data['nickname'], $re->uid, 1]);
  218. //推送消息
  219. event('NoticeListener', [['spreadUid' => $spread, 'user_type' => $user_type, 'nickname' => $data['nickname']], 'bind_spread_uid']);
  220. return $re;
  221. }
  222. }
  223. /**
  224. * 重置密码
  225. * @param $account
  226. * @param $password
  227. * @return bool
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function reset($account, $password)
  233. {
  234. $user = $this->dao->getOne(['account|phone' => $account, 'is_del' => 0], 'uid');
  235. if (!$user) {
  236. throw new ApiException(410032);
  237. }
  238. if (!$this->dao->update($user['uid'], ['pwd' => md5((string)$password)], 'uid')) {
  239. throw new ApiException(410033);
  240. }
  241. return true;
  242. }
  243. /**
  244. * 手机号登录
  245. * @param $phone
  246. * @param $spread
  247. * @param string $user_type
  248. * @return array
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\DbException
  251. * @throws \think\db\exception\ModelNotFoundException
  252. */
  253. public function mobile($phone, $spread, string $user_type = 'h5')
  254. {
  255. //数据库查询
  256. $user = $this->dao->getOne(['account|phone' => $phone, 'is_del' => 0]);
  257. if (!$user) {
  258. $user = $this->register($phone, '123456', $spread, $user_type);
  259. if (!$user) {
  260. throw new ApiException(410034);
  261. }
  262. }
  263. if (!$user->status)
  264. throw new ApiException(410027);
  265. // 设置推广关系
  266. $this->updateUserInfo(['code' => $spread], $user);
  267. $token = $this->createToken((int)$user['uid'], 'api');
  268. if ($token) {
  269. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  270. } else {
  271. throw new ApiException(410019);
  272. }
  273. }
  274. /**
  275. * 切换登录
  276. * @param $user
  277. * @param $from
  278. * @return array
  279. * @throws \think\db\exception\DataNotFoundException
  280. * @throws \think\db\exception\DbException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. */
  283. public function switchAccount($user, $from)
  284. {
  285. if ($from === 'h5') {
  286. $where = [['phone', '=', $user['phone']], ['user_type', '<>', 'h5'], ['is_del', '=', 0]];
  287. $login_type = 'wechat';
  288. } else {
  289. //数据库查询
  290. $where = [['account|phone', '=', $user['phone']], ['user_type', '=', 'h5'], ['is_del', '=', 0]];
  291. $login_type = 'h5';
  292. }
  293. $switch_user = $this->dao->getOne($where);
  294. if (!$switch_user) {
  295. return app('json')->fail(410035);
  296. }
  297. if (!$switch_user->status) {
  298. return app('json')->fail(410027);
  299. }
  300. $edit_data = ['login_type' => $login_type];
  301. if (!$this->dao->update($switch_user['uid'], $edit_data, 'uid')) {
  302. throw new ApiException(410036);
  303. }
  304. $token = $this->createToken((int)$switch_user['uid'], 'api');
  305. if ($token) {
  306. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  307. } else {
  308. throw new ApiException(410019);
  309. }
  310. }
  311. /**
  312. * 绑定手机号(静默还没写入用户信息)
  313. * @param $phone
  314. * @param string $key
  315. * @return array
  316. * @throws \Psr\SimpleCache\InvalidArgumentException
  317. * @throws \think\db\exception\DataNotFoundException
  318. * @throws \think\db\exception\ModelNotFoundException
  319. */
  320. public function bindind_phone($phone, string $key = '')
  321. {
  322. if (!$key) {
  323. throw new ApiException(410037);
  324. }
  325. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $createData = CacheService::get($key);
  326. if (!$createData) {
  327. throw new ApiException(410037);
  328. }
  329. $wechatInfo['phone'] = $phone;
  330. /** @var WechatUserServices $wechatUser */
  331. $wechatUser = app()->make(WechatUserServices::class);
  332. //更新用户信息
  333. $user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  334. $token = $this->createToken((int)$user['uid'], 'api');
  335. if ($token) {
  336. return [
  337. 'token' => $token['token'],
  338. 'userInfo' => $user,
  339. 'expires_time' => $token['params']['exp'],
  340. ];
  341. } else
  342. return app('json')->fail(410019);
  343. }
  344. /**
  345. * 用户绑定手机号
  346. * @param int $uid
  347. * @param $phone
  348. * @param $step
  349. * @return array
  350. * @throws \think\db\exception\DataNotFoundException
  351. * @throws \think\db\exception\DbException
  352. * @throws \think\db\exception\ModelNotFoundException
  353. */
  354. public function userBindindPhone(int $uid, $phone, $step)
  355. {
  356. $userInfo = $this->dao->get($uid);
  357. if (!$userInfo) {
  358. throw new ApiException(410113);
  359. }
  360. if ($this->dao->getOne([['phone', '=', $phone], ['user_type', '<>', 'h5'], ['is_del', '=', 0]])) {
  361. throw new ApiException(410039);
  362. }
  363. if ($userInfo->phone) {
  364. throw new ApiException(410040);
  365. }
  366. $data = [];
  367. if ($this->dao->getOne(['account' => $phone, 'phone' => $phone, 'user_type' => 'h5', 'is_del' => 0])) {
  368. if (!$step) return ['msg' => 410041, 'data' => ['is_bind' => 1]];
  369. } else {
  370. $data['account'] = $phone;
  371. }
  372. $data['phone'] = $phone;
  373. if ($this->dao->update($userInfo['uid'], $data, 'uid') || $userInfo->phone == $phone)
  374. return ['msg' => 410016, 'data' => []];
  375. else
  376. throw new ApiException(410017);
  377. }
  378. /**
  379. * 用户绑定手机号
  380. * @param int $uid
  381. * @param $phone
  382. * @return array
  383. * @throws \think\db\exception\DataNotFoundException
  384. * @throws \think\db\exception\DbException
  385. * @throws \think\db\exception\ModelNotFoundException
  386. */
  387. public function updateBindindPhone(int $uid, $phone)
  388. {
  389. $userInfo = $this->dao->get(['uid' => $uid, 'is_del' => 0]);
  390. if (!$userInfo) {
  391. throw new ApiException(410113);
  392. }
  393. if ($userInfo->phone == $phone) {
  394. throw new ApiException(410042);
  395. }
  396. if ($this->dao->getOne([['phone', '=', $phone], ['is_del', '=', 0]])) {
  397. throw new ApiException(410043);
  398. }
  399. $data = [];
  400. $data['phone'] = $phone;
  401. $data['account'] = $phone;
  402. if ($this->dao->update($userInfo['uid'], $data, 'uid'))
  403. return ['msg' => 100001, 'data' => []];
  404. else
  405. throw new ApiException(100007);
  406. }
  407. }