CommonLogic.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\AdminModel;
  4. use app\model\AdminTokenModel;
  5. use app\model\CommonModel;
  6. use think\facade\Db;
  7. use think\response\Json;
  8. class CommonLogic extends BaseLogic
  9. {
  10. //登录
  11. public static function Logic(array $data = []): Json
  12. {
  13. $res = AdminModel::field(true)
  14. ->where(["is_del" => CommonModel::$del_normal, "username" => $data['username']])
  15. ->findOrEmpty();
  16. if ($res->isEmpty()) return json_show(CommonModel::$error_token, '该账户不存在');
  17. if ($res->status == CommonModel::$status_disable) return json_show(CommonModel::$error_token, '该账户已禁用');
  18. if (get_password($data['password'], $res->salt) != $res->password) return json_show(CommonModel::$error_token, '账户密码错误');
  19. $token = make_token($res->id, $data['username'], $res->salt);
  20. $userinfo = [
  21. "username" => $res->username,
  22. "nickname" => $res->nickname,
  23. "mobile" => $res->mobile,
  24. "role_id" => $res->role_id,
  25. 'token' => $token
  26. ];
  27. // write_log("账户{$account['username']}登录系统","","login","",0);
  28. return json_show(0, "登录成功", $userinfo);
  29. }
  30. //登出
  31. public static function logout(string $token = ''): Json
  32. {
  33. $info = AdminTokenModel::where(['token' => $token])->save(['token' => '', 'expiretime' => date('Y-m-d H:i:s')]);
  34. return $info ? json_show(CommonModel::$success, '登出成功') : json_show(CommonModel::$error_param, '登出失败');
  35. }
  36. //上传图片
  37. public static function upload($files): Json
  38. {
  39. $list = upload_img($files);
  40. return (is_array($list) && !empty($list)) ? json_show(CommonModel::$success, '上传成功', $list) : json_show(CommonModel::$error_default, '上传失败');
  41. }
  42. //获取全部商品单位
  43. public static function unitAll(string $keyword = ''): Json
  44. {
  45. $where = [['is_del', '=', CommonModel::$del_normal]];
  46. if ($keyword != '') $where[] = ['unit', 'like', $keyword . '%'];
  47. $list = Db::name('unit')
  48. ->field('id unit_id,unit unit_title')
  49. ->where($where)
  50. ->order('id', 'desc')
  51. ->select()
  52. ->toArray();
  53. return json_show(CommonModel::$success, '获取全部商品单位成功', $list);
  54. }
  55. //获取省市区地址
  56. public static function getAddr(string $parent_code = ''): Json
  57. {
  58. $rs = Db::name('area')
  59. ->field('id,name,code,level')
  60. ->where('parent_code', $parent_code)
  61. ->select()
  62. ->toArray();
  63. return json_show(CommonModel::$success, '获取省市区地址成功', $rs);
  64. }
  65. }