AccountLogic.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\AccountModel;
  4. use app\model\AccountTokenModel;
  5. use app\model\CommonModel;
  6. use think\Exception;
  7. use think\facade\Config;
  8. use think\facade\Db;
  9. use think\response\Json;
  10. class AccountLogic extends BaseLogic
  11. {
  12. //登录
  13. public static function login(array $data = []): Json
  14. {
  15. Db::startTrans();
  16. try {
  17. $rs = AccountModel::field('id,username,name,password,salt,starttime,expiretime,status')
  18. ->where(['is_del' => CommonModel::$del_normal, 'username' => $data['username']])
  19. ->findOrEmpty();
  20. if ($rs->isEmpty()) throw new Exception('该卡号不存在,请仔细核对');
  21. if (getPassword($data['password'], $rs->salt) != $rs->password) throw new Exception('密码错误');
  22. $date = date('Y-m-d H:i:s');
  23. if (($date < $rs->starttime) || ($date > $rs->expiretime)) throw new Exception('该卡不在有效期内');
  24. $update_data = ['updaterid' => $rs->id, 'updater' => $rs->name, 'updatetime' => $date];
  25. if ($rs->status == AccountModel::$status_not_active) {
  26. //处理激活信息
  27. $update_data['status'] = AccountModel::$status_activated;
  28. $update_data['activetime'] = $date;
  29. }
  30. //根棍账户相关信息
  31. AccountModel::where(['is_del' => CommonModel::$del_normal, 'id' => $rs->id])->save($update_data);
  32. //维护token
  33. $token = base64_encode($rs->username . $rs->salt . (string)time());
  34. $res = AccountTokenModel::field('id')
  35. ->where('accountid', $rs->id)
  36. ->findOrEmpty()
  37. ->isEmpty();
  38. $expire = Config::get('common.expire');
  39. if ($res) AccountTokenModel::create(['token' => $token, 'expiretime' => date('Y-m-d H:i:s', time() + $expire), 'accountid' => $rs->id]);
  40. else AccountTokenModel::where(['accountid' => $rs->id])->update(['token' => $token, 'expiretime' => date('Y-m-d H:i:s', time() + $expire)]);
  41. Db::commit();
  42. return json_show(CommonModel::$success, '登录成功', ['token' => $token]);
  43. } catch (Exception $exception) {
  44. Db::rollback();
  45. return json_show(CommonModel::$error_param, $exception->getMessage());
  46. }
  47. }
  48. //登出
  49. public static function logout(string $token = ''): Json
  50. {
  51. $info = AccountTokenModel::where(['accountid' => self::$aid])->save(['token' => '', 'expiretime' => date('Y-m-d H:i:s')]);
  52. return $info ? json_show(CommonModel::$success, '登出成功') : json_show(CommonModel::$error_param, '登出失败');
  53. }
  54. //详情
  55. public static function info(): Json
  56. {
  57. $info = AccountModel::where(['id' => self::$aid])
  58. ->field('id,username,mobile,name,starttime,expiretime')
  59. ->findOrEmpty()
  60. ->toArray();
  61. return $info ? json_show(CommonModel::$success, '获取账户详情成功', $info) : json_show(CommonModel::$error_param, '账户为空');
  62. }
  63. }