LoginServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. if (isset($user['is_staff']) && !$userInfo['is_agent'] && !$userInfo['is_division']) {
  84. $spreadUid = isset($user['code']) ? $user['code'] : 0;
  85. $spreadInfo = $this->dao->get($spreadUid);
  86. if ($userInfo['uid'] != $spreadUid) {
  87. $data['spread_uid'] = $spreadUid;
  88. $data['spread_time'] = $userInfo->last_time;
  89. }
  90. $data['agent_id'] = $spreadInfo->agent_id;
  91. $data['division_id'] = $spreadInfo->division_id;
  92. $data['staff_id'] = $userInfo['uid'];
  93. $data['is_staff'] = $user['is_staff'] ?? 0;
  94. $data['division_type'] = 3;
  95. $data['division_change_time'] = time();
  96. $data['division_end_time'] = $spreadInfo->division_end_time;
  97. //如果店员切换代理商,则店员在之前代理商下推广的用户,他们的直接上级从当前店员变为之前代理商
  98. if ($userInfo->agent_id != 0 && $userInfo->agent_id != $spreadInfo->agent_id) {
  99. $this->dao->update($userInfo['uid'], ['spread_uid' => $userInfo->agent_id], 'spread_uid');
  100. }
  101. }
  102. //永久绑定
  103. $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
  104. if ($userInfo->spread_uid && $store_brokergae_binding_status == 1 && !isset($user['is_staff'])) {
  105. $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
  106. } else {
  107. $spreadUid = isset($user['code']) && $user['code'] && $user['code'] != $userInfo->uid ? $user['code'] : 0;
  108. if ($spreadUid && $userInfo->uid == $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
  109. $spreadUid = 0;
  110. }
  111. //绑定分销关系 = 所有用户
  112. if (sys_config('brokerage_bindind', 1) == 1) {
  113. //分销绑定类型为时间段且过期 ||临时
  114. $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
  115. if (!$userInfo['spread_uid'] || $store_brokergae_binding_status == 3 || ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
  116. if ($spreadUid) {
  117. $spreadInfo = $this->dao->get($spreadUid);
  118. $spreadUid = (int)$spreadUid;
  119. $data['spread_uid'] = $spreadUid;
  120. $data['spread_time'] = time();
  121. $data['agent_id'] = $spreadInfo->agent_id;
  122. $data['division_id'] = $spreadInfo->division_id;
  123. $data['staff_id'] = $spreadInfo->staff_id;
  124. }
  125. }
  126. }
  127. if ($spreadUid) {
  128. //绑定用户后置事件
  129. event('user.register', [$spreadUid, $userInfo['user_type'], $userInfo['nickname'], $userInfo['uid'], 0]);
  130. //推送消息
  131. event('notice.notice', [['spreadUid' => $spreadUid, 'user_type' => $userInfo['user_type'], 'nickname' => $userInfo['nickname']], 'bind_spread_uid']);
  132. }
  133. }
  134. if (!$this->dao->update($userInfo['uid'], $data, 'uid')) {
  135. throw new ValidateException('修改信息失败');
  136. }
  137. return true;
  138. }
  139. public function verify(SmsSendServices $services, $phone, $type, $time, $ip)
  140. {
  141. if ($this->dao->getOne(['account' => $phone]) && $type == 'register') {
  142. throw new ValidateException('手机号已注册');
  143. }
  144. $default = Config::get('sms.default', 'yunxin');
  145. $defaultMaxPhoneCount = Config::get('sms.maxPhoneCount', 10);
  146. $defaultMaxIpCount = Config::get('sms.maxIpCount', 50);
  147. $maxPhoneCount = Config::get('sms.stores.' . $default . '.maxPhoneCount', $defaultMaxPhoneCount);
  148. $maxIpCount = Config::get('sms.stores.' . $default . '.maxIpCount', $defaultMaxIpCount);
  149. /** @var SmsRecordServices $smsRecord */
  150. $smsRecord = app()->make(SmsRecordServices::class);
  151. if ($smsRecord->count(['phone' => $phone, 'add_ip' => $ip, 'time' => 'today']) >= $maxPhoneCount) {
  152. throw new ValidateException('您今日发送得短信次数已经达到上限');
  153. }
  154. if ($smsRecord->count(['add_ip' => $ip, 'time' => 'today']) >= $maxIpCount) {
  155. throw new ValidateException('此IP今日发送次数已经达到上限');
  156. }
  157. $code = rand(100000, 999999);
  158. $data['code'] = $code;
  159. $data['time'] = $time;
  160. $res = $services->send(true, $phone, $data, 'VERIFICATION_CODE_TIME');
  161. if ($res !== true)
  162. throw new ValidateException('短信平台验证码发送失败' . $res);
  163. return $code;
  164. }
  165. /**
  166. * H5用户注册
  167. * @param $account
  168. * @param $password
  169. * @param $spread
  170. * @return User|\think\Model
  171. */
  172. public function register($account, $password, $spread, $user_type = 'h5')
  173. {
  174. if ($this->dao->getOne(['account|phone' => $account])) {
  175. throw new ValidateException('用户已存在,请去修改密码');
  176. }
  177. $phone = $account;
  178. $data['account'] = $account;
  179. $data['pwd'] = md5((string)$password);
  180. $data['phone'] = $phone;
  181. if ($spread) {
  182. $data['spread_uid'] = $spread;
  183. $data['spread_time'] = time();
  184. }
  185. $data['real_name'] = '';
  186. $data['birthday'] = 0;
  187. $data['card_id'] = '';
  188. $data['mark'] = '';
  189. $data['addres'] = '';
  190. $data['user_type'] = $user_type;
  191. $data['add_time'] = time();
  192. $data['add_ip'] = app('request')->ip();
  193. $data['last_time'] = time();
  194. $data['last_ip'] = app('request')->ip();
  195. $data['nickname'] = substr(md5($account . time()), 0, 12);
  196. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  197. $data['city'] = '';
  198. $data['language'] = '';
  199. $data['province'] = '';
  200. $data['country'] = '';
  201. $data['status'] = 1;
  202. if (!$re = $this->dao->save($data)) {
  203. throw new ValidateException('注册失败');
  204. } else {
  205. //用户生成后置事件
  206. event('user.register', [$spread, $user_type, $data['nickname'], $re->uid, 1]);
  207. //推送消息
  208. event('notice.notice', [['spreadUid' => $spread, 'user_type' => $user_type, 'nickname' => $data['nickname']], 'bind_spread_uid']);
  209. return $re;
  210. }
  211. }
  212. /**
  213. * 重置密码
  214. * @param $account
  215. * @param $password
  216. */
  217. public function reset($account, $password)
  218. {
  219. $user = $this->dao->getOne(['account|phone' => $account]);
  220. if (!$user) {
  221. throw new ValidateException('用户不存在');
  222. }
  223. if (!$this->dao->update($user['uid'], ['pwd' => md5((string)$password)], 'uid')) {
  224. throw new ValidateException('修改密码失败');
  225. }
  226. return true;
  227. }
  228. /**
  229. * 手机号登录
  230. * @param $phone
  231. * @param $spread
  232. * @return array
  233. * @throws \think\db\exception\DataNotFoundException
  234. * @throws \think\db\exception\DbException
  235. * @throws \think\db\exception\ModelNotFoundException
  236. */
  237. public function mobile($phone, $spread, $user_type = 'h5')
  238. {
  239. //数据库查询
  240. $user = $this->dao->getOne(['phone' => $phone]);
  241. if (!$user) {
  242. $user = $this->register($phone, '123456', $spread, $user_type);
  243. if (!$user) {
  244. throw new ValidateException('用户登录失败,无法生成新用户,请稍后再试!');
  245. }
  246. }
  247. if (!$user->status)
  248. throw new ValidateException('已被禁止,请联系管理员');
  249. // 设置推广关系
  250. $this->updateUserInfo(['code' => $spread], $user);
  251. $token = $this->createToken((int)$user['uid'], 'api');
  252. if ($token) {
  253. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  254. } else {
  255. throw new ValidateException('登录失败');
  256. }
  257. }
  258. /**
  259. * 切换登录
  260. * @param $user
  261. * @param $from
  262. */
  263. public function switchAccount($user, $from)
  264. {
  265. if ($from === 'h5') {
  266. $where = [['phone', '=', $user['phone']], ['user_type', '<>', 'h5']];
  267. $login_type = 'wechat';
  268. } else {
  269. //数据库查询
  270. $where = [['account|phone', '=', $user['phone']], ['user_type', '=', 'h5']];
  271. $login_type = 'h5';
  272. }
  273. $switch_user = $this->dao->getOne($where);
  274. if (!$switch_user) {
  275. return app('json')->fail('用户不存在,无法切换');
  276. }
  277. if (!$switch_user->status) {
  278. return app('json')->fail('已被禁止,请联系管理员');
  279. }
  280. $edit_data = ['login_type' => $login_type];
  281. if (!$this->dao->update($switch_user['uid'], $edit_data, 'uid')) {
  282. throw new ValidateException('修改新用户登录类型出错');
  283. }
  284. $token = $this->createToken((int)$switch_user['uid'], 'api');
  285. if ($token) {
  286. return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
  287. } else {
  288. throw new ValidateException('切换失败');
  289. }
  290. }
  291. /**
  292. * 绑定手机号(静默还没写入用户信息)
  293. * @param $user
  294. * @param $phone
  295. * @param $step
  296. * @return mixed
  297. */
  298. public function bindind_phone($phone, $key = '')
  299. {
  300. if (!$key) {
  301. throw new ValidateException('请刷新页面或者重新授权');
  302. }
  303. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
  304. if (!$createData) {
  305. throw new ValidateException('请刷新页面或者重新授权');
  306. }
  307. $wechatInfo['phone'] = $phone;
  308. /** @var WechatUserServices $wechatUser */
  309. $wechatUser = app()->make(WechatUserServices::class);
  310. //更新用户信息
  311. $user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  312. $token = $this->createToken((int)$user['uid'], $userType);
  313. if ($token) {
  314. return [
  315. 'token' => $token['token'],
  316. 'userInfo' => $user,
  317. 'expires_time' => $token['params']['exp'],
  318. ];
  319. } else
  320. return app('json')->fail('获取用户访问token失败!');
  321. }
  322. /**
  323. * 用户绑定手机号
  324. * @param $user
  325. * @param $phone
  326. * @param $step
  327. * @return mixed
  328. */
  329. public function userBindindPhone(int $uid, $phone, $step)
  330. {
  331. $userInfo = $this->dao->get($uid);
  332. if (!$userInfo) {
  333. throw new ValidateException('用户不存在');
  334. }
  335. if ($this->dao->getOne([['phone', '=', $phone], ['user_type', '<>', 'h5']])) {
  336. throw new ValidateException('此手机已经绑定,无法多次绑定!');
  337. }
  338. if ($userInfo->phone) {
  339. throw new ValidateException('您的账号已经绑定过手机号码!');
  340. }
  341. $data = [];
  342. if ($this->dao->getOne(['account' => $phone, 'phone' => $phone, 'user_type' => 'h5'])) {
  343. if (!$step) return ['msg' => 'H5已有账号是否绑定此账号上', 'data' => ['is_bind' => 1]];
  344. } else {
  345. $data['account'] = $phone;
  346. }
  347. $data['phone'] = $phone;
  348. if ($this->dao->update($userInfo['uid'], $data, 'uid') || $userInfo->phone == $phone)
  349. return ['msg' => '绑定成功', 'data' => []];
  350. else
  351. throw new ValidateException('绑定失败');
  352. }
  353. /**
  354. * 用户绑定手机号
  355. * @param $user
  356. * @param $phone
  357. * @param $step
  358. * @return mixed
  359. */
  360. public function updateBindindPhone(int $uid, $phone)
  361. {
  362. $userInfo = $this->dao->get($uid);
  363. if (!$userInfo) {
  364. throw new ValidateException('用户不存在');
  365. }
  366. if ($userInfo->phone == $phone) {
  367. throw new ValidateException('新手机号和原手机号相同,无需修改');
  368. }
  369. if ($this->dao->getOne([['phone', '=', $phone]])) {
  370. throw new ValidateException('此手机已经注册');
  371. }
  372. $data = [];
  373. $data['phone'] = $phone;
  374. if ($this->dao->update($userInfo['uid'], $data, 'uid'))
  375. return ['msg' => '修改成功', 'data' => []];
  376. else
  377. throw new ValidateException('修改失败');
  378. }
  379. }