Users.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use mall\utils\CString;
  11. use think\facade\Db;
  12. use think\facade\Request;
  13. use mall\utils\Check;
  14. use think\facade\Session;
  15. use think\facade\Cookie;
  16. use mall\utils\Hash;
  17. class Users {
  18. private static $data = [];
  19. public static function init(){
  20. if(Cookie::has("user") && ($data = Hash::decrypt(Cookie::get("user"))) != false){
  21. Session::set("user_id",$data["value"]);
  22. }
  23. if(!Session::has("user_id")){
  24. return false;
  25. }
  26. if(Db::name("users")->where("id",Session::get("user_id"))->count() <= 0){
  27. Session::delete("user_id");
  28. Cookie::delete("user");
  29. return false;
  30. }
  31. $userInfo = self::info(Session::get("user_id"));
  32. $userInfo["register_time"] = date("Y-m-d H:i:s",$userInfo["create_time"]);
  33. if(Session::has("users_last_login")){
  34. $userInfo["last_login_time"] = date("Y-m-d H:i:s",$userInfo["last_login"]);
  35. $userInfo["users_last_time"] = Session::has("users_last_time") ? Session::get("users_last_time") : date("Y-m-d H:i:s",$userInfo["last_login"]);
  36. }else{
  37. $last_login = time();
  38. Db::name("users")->where(["id"=>$userInfo["id"]])->update([
  39. "last_login"=>$last_login
  40. ]);
  41. $userInfo["users_last_time"] = date("Y-m-d H:i:s",$userInfo["last_login"]);
  42. Session::set("users_last_time",$userInfo["users_last_time"]);
  43. $userInfo["last_login_time"] = date("Y-m-d H:i:s",$last_login);
  44. Session::set("users_last_login",$last_login);
  45. }
  46. if($userInfo['exp'] > 0){
  47. $r = Db::name("users_group")->where("minexp","<=",$userInfo["exp"])->where("maxexp",">=",$userInfo["exp"])->find();
  48. if($userInfo["group_id"] != $r['id']){
  49. Db::name("users")->where(["id"=>$userInfo["id"]])->update(["group_id"=>$r['id']]);
  50. }
  51. }
  52. return G(["users"=>$userInfo]);
  53. }
  54. public static function isEmpty($field){
  55. return empty(self::$data[$field]);
  56. }
  57. public static function get($field=null){
  58. if(is_null($field)){
  59. return null;
  60. }
  61. return isset(self::$data[$field]) ? self::$data[$field] : null;
  62. }
  63. public static function set($data){
  64. self::$data = $data;
  65. }
  66. public static function info($user_id){
  67. if(($row=Db::name("users")->where("id",$user_id)->find()) == false){
  68. throw new \Exception("查找会员信息失败",0);
  69. }
  70. $row["group_name"] = Db::name("users_group")->where(["id"=>$row["group_id"]])->value("name");
  71. $row["shop_count"] = (int)Db::name("cart")->where("user_id",$user_id)->count();
  72. $row["coupon_count"] = (int)Db::name("users_bonus")
  73. ->alias("u")
  74. ->field("b.*")
  75. ->join("promotion_bonus b","u.bonus_id=b.id","LEFT")
  76. ->where('u.status=0 and b.end_time > ' . time())
  77. ->where("u.user_id",$user_id)->count();
  78. self::set($row);
  79. return $row;
  80. }
  81. public static function getComments($id,$type=0,$size=5,$page=1){
  82. $count = Db::name("users_comment")->alias("uc")
  83. ->join("order o","uc.order_no=o.order_no","LEFT")
  84. ->join("users u","uc.user_id=u.id","LEFT")
  85. ->where("o.type",$type)
  86. ->where("uc.goods_id",$id)
  87. ->where("uc.status",1)->count();
  88. $total = ceil($count / $size);
  89. if($total == $page -1){
  90. throw new \Exception("没有数据了哦!",-1);
  91. }
  92. $result = Db::name("users_comment")->alias("uc")
  93. ->field("uc.contents,uc.reply_content,u.avatar,uc.comment_time,u.username,u.nickname,u.mobile")
  94. ->join("order o","uc.order_no=o.order_no","LEFT")
  95. ->join("users u","uc.user_id=u.id","LEFT")
  96. ->where("o.type",$type)
  97. ->where("uc.goods_id",$id)
  98. ->where("uc.status",1)
  99. ->order("uc.id","DESC")->paginate($size);
  100. $rs = array_map(function ($data){
  101. $array = [];
  102. $username = !empty($data["nickname"]) ? $data["nickname"] : $data["username"];
  103. $array['time'] = date("Y-m-d",$data['comment_time']);
  104. $array['avatar'] = self::avatar($data['avatar']);
  105. $array['content'] = strip_tags($data['contents']);
  106. $array['reply_content'] = strip_tags($data['reply_content']);
  107. if(!empty($username)){
  108. $array['username'] = CString::msubstr($username,3,false) . "***";
  109. }else{
  110. $array['username'] = preg_replace('/(1[3-9]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$data['mobile']);
  111. }
  112. return $array;
  113. },$result->items());
  114. return [
  115. "count"=>$count,
  116. "total"=>$total,
  117. "data"=>$rs
  118. ];
  119. }
  120. public static function avatar($image="",$root=true){
  121. if(Check::url($image)){
  122. return $image;
  123. }
  124. if(Request::domain() == "http://" || Request::domain() == "https://"){
  125. $domain = $root ? trim(env("web.app_web_url"),"/") : "";
  126. }else{
  127. $domain = $root ? trim(Request::domain(),"/") : "";
  128. }
  129. $setting = Setting::get("upload");
  130. if(isset($setting["type"]) && $setting["type"] == 1){
  131. if(empty($image)){
  132. return $domain . "/static/images/avatar.png";
  133. }
  134. if(!empty($setting["domain"])){
  135. return trim($setting["domain"],"/") . "/" . trim($image,"/");
  136. }
  137. $protocol = !empty($setting["protocol"]) ? $setting["protocol"] : "auto";
  138. $url = "";
  139. if($protocol == "http" || $protocol == "https"){
  140. $url .= $protocol . "://";
  141. }else{
  142. $url .= "//";
  143. }
  144. $url .= $setting["bucket"] . "." . $setting["endpoint"] . ".aliyuncs.com/" . trim($image,"/");
  145. return $url;
  146. }
  147. $default = $domain . "/static/images/avatar.png";
  148. $file = trim($image,"/");
  149. if(empty($image) || !file_exists($file)){
  150. return $default;
  151. }
  152. return $domain . '/' . $file;
  153. }
  154. public static function delete($id=""){
  155. if(($row = Db::name("users")->where(['id'=>$id])->find()) == false){
  156. return true;
  157. }
  158. try {
  159. Db::name("users")->where(['id'=>$id])->delete();
  160. }catch (\Exception $e){
  161. throw new \Exception($e->getMessage());
  162. }
  163. return true;
  164. }
  165. public static function statusInfo($status){
  166. $arr = ["0"=>"正常","1"=>"审核","2"=>"锁定","3"=>"删除"];
  167. return isset($arr[$status]) ? $arr[$status] : "未知";
  168. }
  169. }