SaleLogic.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\workerapi\logic;
  15. use app\common\enum\LoginEnum;
  16. use app\common\enum\notice\NoticeEnum;
  17. use app\common\model\sale\Sale;
  18. use app\common\logic\BaseLogic;
  19. use app\common\service\sms\SmsDriver;
  20. use think\facade\Config;
  21. use think\facade\Db;
  22. /**
  23. * Sale逻辑
  24. * Class SaleLogic
  25. * @package app\workerapi\logic
  26. */
  27. class SaleLogic extends BaseLogic
  28. {
  29. public static function login($params)
  30. {
  31. try {
  32. // 账号/手机号 密码登录
  33. $where = ['mobile' => $params['mobile']];
  34. $user = Sale::where($where)->findOrEmpty();
  35. if ($user->isEmpty()) {
  36. throw new \Exception('无此用户');
  37. }
  38. // 账号密码登录
  39. if (LoginEnum::ACCOUNT_PASSWORD == $params['scene']) {
  40. if (!isset($params['password'])) {
  41. throw new \Exception('请输入密码');
  42. }
  43. $passwordSalt = Config::get('project.unique_identification');
  44. if ($user['password'] !== create_password($params['password'], $passwordSalt)) {
  45. throw new \Exception('密码错误');
  46. }
  47. }
  48. // 手机验证码登录
  49. if (LoginEnum::MOBILE_CAPTCHA == $params['scene']) {
  50. if (!isset($params['code'])) {
  51. throw new \Exception('请输入手机验证码');
  52. }
  53. $smsDriver = new SmsDriver();
  54. $result = $smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::LOGIN_CAPTCHA);
  55. if (!$result) {
  56. throw new \Exception('验证码错误');
  57. }
  58. }
  59. //获取token
  60. $token = create_token($params['mobile']);
  61. $user->token = $token;
  62. $user->save();
  63. return [
  64. 'id' => $user['id'],
  65. 'mobile' => $user['mobile'],
  66. 'sale_token' => $token,
  67. ];
  68. } catch (\Exception $e) {
  69. self::setError($e->getMessage());
  70. return false;
  71. }
  72. }
  73. public static function getSaleIdByToken($sale_token)
  74. {
  75. $user = Sale::where('token',$sale_token)->findOrEmpty();
  76. if ($user->isEmpty()) {
  77. return -1;
  78. }
  79. return $user['id'];
  80. }
  81. }