AccountLogic.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\GroupModel;
  7. use app\model\ThemeModel;
  8. use app\model\VideoModel;
  9. use think\Exception;
  10. use think\facade\Config;
  11. use think\facade\Db;
  12. use think\response\Json;
  13. class AccountLogic extends BaseLogic
  14. {
  15. //登录
  16. public static function login(array $data = []): Json
  17. {
  18. Db::startTrans();
  19. try {
  20. $rs = AccountModel::field('id,username,name,password,salt,starttime,expiretime,status')
  21. ->where(['is_del' => CommonModel::$del_normal, 'username' => $data['username']])
  22. ->findOrEmpty();
  23. if ($rs->isEmpty()) throw new Exception('该卡号不存在,请仔细核对');
  24. if (getPassword($data['password'], $rs->salt) != $rs->password) throw new Exception('密码错误');
  25. $date = date('Y-m-d H:i:s');
  26. if (($date < $rs->starttime) || ($date > $rs->expiretime)) throw new Exception('该卡不在有效期内');
  27. $update_data = ['updaterid' => $rs->id, 'updater' => $rs->name, 'updatetime' => $date];
  28. if ($rs->status == AccountModel::$status_not_active) {
  29. //处理激活信息
  30. $update_data['status'] = AccountModel::$status_activated;
  31. $update_data['activetime'] = $date;
  32. }
  33. //根棍账户相关信息
  34. AccountModel::where(['is_del' => CommonModel::$del_normal, 'id' => $rs->id])->save($update_data);
  35. //维护token
  36. $token = base64_encode($rs->username . $rs->salt . (string)time());
  37. $res = AccountTokenModel::field('id')
  38. ->where('accountid', $rs->id)
  39. ->findOrEmpty()
  40. ->isEmpty();
  41. $expire = Config::get('common.expire');
  42. if ($res) AccountTokenModel::create(['token' => $token, 'expiretime' => date('Y-m-d H:i:s', time() + $expire), 'accountid' => $rs->id]);
  43. else AccountTokenModel::where(['accountid' => $rs->id])->update(['token' => $token, 'expiretime' => date('Y-m-d H:i:s', time() + $expire)]);
  44. Db::commit();
  45. return json_show(CommonModel::$success, '登录成功', ['token' => $token]);
  46. } catch (Exception $exception) {
  47. Db::rollback();
  48. return json_show(CommonModel::$error_param, $exception->getMessage());
  49. }
  50. }
  51. //登出
  52. public static function logout(): Json
  53. {
  54. $info = AccountTokenModel::where(['accountid' => self::$aid])->save(['token' => '', 'expiretime' => date('Y-m-d H:i:s')]);
  55. return $info ? json_show(CommonModel::$success, '登出成功') : json_show(CommonModel::$error_param, '登出失败');
  56. }
  57. //详情
  58. public static function info(): Json
  59. {
  60. $info = AccountModel::where(['id' => self::$aid])
  61. ->field('id,username,mobile,name,starttime,expiretime')
  62. ->findOrEmpty()
  63. ->toArray();
  64. return $info ? json_show(CommonModel::$success, '获取账户详情成功', $info) : json_show(CommonModel::$error_param, '账户为空');
  65. }
  66. //视频列表
  67. public static function getVideoList(array $data = []): Json
  68. {
  69. $rs = AccountModel::field('id,video_ids')
  70. ->where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  71. ->findOrEmpty()
  72. ->toArray();
  73. if (empty($rs)) return json_show(CommonModel::$error_param, '该账户不存在');
  74. $db = VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  75. ->whereIn('id', $rs['video_ids']);
  76. $count = $db->count('id');
  77. $list = $db
  78. ->field('id,video_sn,video_name,video_url,video_img')
  79. ->page($data['page'], $data['size'])
  80. ->order(['weight' => 'desc', 'id' => 'desc'])
  81. ->select()
  82. ->toArray();
  83. return json_show(CommonModel::$success, '获取视频列表成功', ['count' => $count, 'list' => $list]);
  84. }
  85. //手机主题
  86. public static function theme(): Json
  87. {
  88. $group_id = GroupModel::where(['is_del' => CommonModel::$del_normal, 'company_id' => self::$company_id, 'card_id' => self::$card_id])
  89. ->value('id', 0);
  90. if (!$group_id) return json_show(CommonModel::$error_param, '该账户所对应的分组不存在');
  91. $rs = ThemeModel::field('id,code')
  92. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $group_id, 'status' => CommonModel::$status_normal])
  93. ->append(['modular'])
  94. ->withAttr('modular', function ($val, $data) {
  95. return Db::name('theme_modular')
  96. ->field('id,title,type')
  97. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id'], 'status' => CommonModel::$status_normal])
  98. ->order(['addtime' => 'desc', 'id' => 'desc'])
  99. ->append(['data'])
  100. ->withAttr('data', function ($v, $d) {
  101. return Db::name('theme_modular_data')
  102. ->field('id,img,jump_type,jump_param,good_name,good_id,style_type')
  103. ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
  104. ->order(['addtime' => 'desc', 'id' => 'desc'])
  105. ->select()
  106. ->toArray();
  107. })
  108. ->select()
  109. ->toArray();
  110. })
  111. ->findOrEmpty()
  112. ->toArray();
  113. return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题成功', $rs);
  114. }
  115. }