Users.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 app\api\service;
  10. use app\common\models\users\Users as UsersModel;
  11. use mall\library\tools\jwt\Token;
  12. use mall\utils\Check;
  13. use think\facade\Db;
  14. use think\facade\Request;
  15. use mall\basic\Users as UsersUtils;
  16. use app\common\models\users\UsersGroup as UsersGroupModel;
  17. use app\common\models\Setting as SettingModel;
  18. use app\common\models\users\UsersSms as UsersSmsModel;
  19. class Users extends Service {
  20. /**
  21. * 会员登录
  22. * @param $data
  23. * @return array
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public static function login($data){
  29. $username = $data["username"]??"";
  30. $password = $data["password"]??"";
  31. $condition = [];
  32. if(Check::mobile($username)){
  33. $condition["mobile"] = $username;
  34. }else if(Check::email($username)){
  35. $condition["email"] = $username;
  36. }else{
  37. $condition["username"] = $username;
  38. }
  39. $users = UsersModel::where($condition)->find();
  40. if(empty($users)){
  41. throw new \Exception("用户不存在!",0);
  42. }
  43. if($users["status"] == 3){
  44. throw new \Exception("您的用户已被管理员禁止登录!",0);
  45. }
  46. if($users["password"] != md5($password)){
  47. throw new \Exception("您填写的密码不正确!",0);
  48. }
  49. if(in_array($users["status"],[1,2])){
  50. $status = UsersModel::statusInfo($users["status"]);
  51. throw new \Exception("您当前用户状态:" . $status . ",如有疑问请联系管理员。",0);
  52. }
  53. $data = [ "last_ip"=>Request::ip(), "last_login"=>time() ];
  54. UsersModel::where("id",$users["id"])->update($data);
  55. $token = Token::get("id",$users["id"]);
  56. $info = UsersUtils::info($users["id"]);
  57. return [
  58. "id" => $users["id"],
  59. "token" => $token,
  60. "username" => $info["username"],
  61. "nickname" => $info["nickname"],
  62. "group_name" => $info["group_name"],
  63. "shop_count" => $info["shop_count"],
  64. "coupon_count" => $info["coupon_count"],
  65. "mobile" => $info["mobile"],
  66. "sex" => $info["sex"],
  67. "point" => $info["point"],
  68. "amount" => $info["amount"],
  69. "last_ip" => $info["last_ip"],
  70. "last_login" => $info["last_login"]
  71. ];
  72. }
  73. /**
  74. * 注册
  75. * @param $data
  76. * @return array
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public static function register($data){
  82. $username = strip_tags($data["username"]??"");
  83. $password = strip_tags($data["password"]??"");
  84. $group_id = UsersGroupModel::order('minexp','ASC')->value("id");
  85. $data = [
  86. "group_id"=>$group_id,
  87. "username"=>$username,
  88. "mobile"=>$username,
  89. "password"=>md5($password),
  90. "status"=>0,
  91. "create_ip"=>Request::ip(),
  92. "last_ip"=>Request::ip(),
  93. "create_time"=>time(),
  94. "last_login"=>time()
  95. ];
  96. $user_id = UsersModel::create($data)->id;
  97. $token = Token::get("id",$user_id);
  98. Db::name("users_sms")->where("mobile",$username)->delete();
  99. $info = UsersUtils::info($user_id);
  100. return [
  101. "id" => $user_id,
  102. "token" => $token,
  103. "username" => $info["username"],
  104. "nickname" => $info["nickname"],
  105. "group_name" => $info["group_name"],
  106. "shop_count" => $info["shop_count"],
  107. "coupon_count" => $info["coupon_count"],
  108. "mobile" => $info["mobile"],
  109. "sex" => $info["sex"],
  110. "point" => $info["point"],
  111. "amount" => $info["amount"],
  112. "last_ip" => $info["last_ip"],
  113. "last_login" => $info["last_login"]
  114. ];
  115. }
  116. /**
  117. * 修改密码
  118. * @param $data
  119. * @return array
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public static function forget($data){
  125. $username = strip_tags($data["username"]??"");
  126. $password = strip_tags($data["password"]??"");
  127. if(!$users = Db::name("users")->where("mobile",$username)->find()){
  128. throw new \Exception("您填写的手机号码不存在!",0);
  129. }
  130. Db::name("users")->where("mobile",$username)->save([ "password"=>md5($password), "last_ip"=>Request::ip(), "last_login"=>time() ]);
  131. $user_id = $users["id"];
  132. $token = Token::get("id",$users["id"]);
  133. Db::name("users_sms")->where("mobile",$username)->delete();
  134. $info = UsersUtils::info($user_id);
  135. return [
  136. "id" => $info["id"],
  137. "token" => $token,
  138. "username" => $info["username"],
  139. "nickname" => $info["nickname"],
  140. "group_name" => $info["group_name"],
  141. "shop_count" => $info["shop_count"],
  142. "coupon_count" => $info["coupon_count"],
  143. "mobile" => $info["mobile"],
  144. "sex" => $info["sex"],
  145. "point" => $info["point"],
  146. "amount" => $info["amount"],
  147. "last_ip" => $info["last_ip"],
  148. "last_login" => $info["last_login"]
  149. ];
  150. }
  151. public static function autologin(){
  152. $token = Token::check();
  153. $result = Token::parse($token,"id");
  154. if(!is_array($result)){
  155. throw new \Exception("您还未登录,请先登录",401);
  156. }
  157. $info = UsersUtils::info($result["value"]);
  158. return [
  159. "id" => $info["id"],
  160. "token" => $token,
  161. "username" => $info["username"],
  162. "nickname" => $info["nickname"],
  163. "group_name" => $info["group_name"],
  164. "shop_count" => $info["shop_count"],
  165. "coupon_count" => $info["coupon_count"],
  166. "mobile" => $info["mobile"],
  167. "sex" => $info["sex"],
  168. "point" => $info["point"],
  169. "amount" => $info["amount"],
  170. "last_ip" => $info["last_ip"],
  171. "last_login" => $info["last_login"]
  172. ];
  173. }
  174. /**
  175. * 发送短信
  176. * @param $data
  177. * @return string
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public static function sendSMS($data){
  183. $config = SettingModel::getArrayData("sms");
  184. $sms = UsersSmsModel::where("mobile",$data["username"])->order("id","DESC")->find();
  185. if(!empty($sms) && (strtotime($sms["create_time"]) + (60 * $config["duration_time"])) > time()){
  186. throw new \Exception("您的验证码已发送,请注意查收",1);
  187. }
  188. sendSMS(["mobile"=>$data["username"]],$data["type"]);
  189. return "发送成功,验证码".$config["duration_time"]."分钟内有效";
  190. }
  191. }