AccountLogic.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 (get_password($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. //获取微信网页授权URL
  45. // $url = '';
  46. // if ($data['callback_url'] != '') $url = WechatLogic::getOauthRedirect($data['callback_url'], $token);
  47. Db::commit();
  48. // return json_show($url ? CommonModel::$error_jump : CommonModel::$success, '登录成功', ['token' => $token, 'url' => $url]);
  49. return json_show(CommonModel::$success, '登录成功', ['token' => $token]);
  50. } catch (Exception $exception) {
  51. Db::rollback();
  52. return json_show(CommonModel::$error_param, $exception->getMessage());
  53. }
  54. }
  55. //登出
  56. public static function logout(): Json
  57. {
  58. $info = AccountTokenModel::where(['accountid' => self::$aid])->save(['token' => '', 'expiretime' => date('Y-m-d H:i:s')]);
  59. return $info ? json_show(CommonModel::$success, '登出成功') : json_show(CommonModel::$error_param, '登出失败');
  60. }
  61. //详情
  62. public static function info(): Json
  63. {
  64. $info = AccountModel::where(['id' => self::$aid])
  65. ->field('id,username,mobile,name,starttime,expiretime')
  66. ->findOrEmpty()
  67. ->toArray();
  68. return $info ? json_show(CommonModel::$success, '获取账户详情成功', $info) : json_show(CommonModel::$error_token, '账户为空');
  69. }
  70. //更改密码
  71. public static function updatePassword(array $data = []): Json
  72. {
  73. $rs = AccountModel::field('id,password,salt')
  74. ->where(['is_del' => CommonModel::$del_normal, 'id' => self::$aid])
  75. ->findOrEmpty()
  76. ->getData();//password,salt这两个字段在模型里定义了隐藏,所以要在这里使用getData方法获取原始数据
  77. if (empty($rs)) return json_show(CommonModel::$error_token, '该账户不存在');
  78. if (get_password($data['old_password'], $rs['salt']) != $rs['password']) return json_show(CommonModel::$error_param, '密码错误');
  79. $salt = randomkeys(6);
  80. $password = get_password($data['new_password'], $salt);
  81. $da = [
  82. 'pwd' => $data['new_password'],
  83. 'salt' => $salt,
  84. 'password' => $password,
  85. 'updaterid' => self::$aid,
  86. 'updater' => self::$aname,
  87. 'updatetime' => date('Y-m-d H:i:s'),
  88. ];
  89. $rs = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  90. ->save($da);
  91. return $rs ? json_show(CommonModel::$error_token, '修改密码成功') : json_show(CommonModel::$error_param, '修改密码失败');
  92. }
  93. //通过微信端code绑定账户
  94. // public static function bindAccountByCode(): Json
  95. // {
  96. //
  97. // $openId = WechatLogic::getOauthAccessToken();
  98. //
  99. // $rs = AccountModel::where('id', self::$aid)
  100. // ->save(['wx_openId' => $openId, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => date('Y-m-d H:i:s')]);
  101. //
  102. // return $rs ? json_show(CommonModel::$success, '绑定成功') : json_show(CommonModel::$error_param, '绑定失败');
  103. //
  104. // }
  105. }