AccountLogic.php 3.9 KB

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