User.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use think\Validate;
  7. use think\Db;
  8. /**
  9. * 会员接口
  10. */
  11. class User extends Api
  12. {
  13. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  14. protected $noNeedRight = '*';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 会员中心
  21. */
  22. public function index()
  23. {
  24. }
  25. /**
  26. * 会员登录
  27. *
  28. * @ApiMethod (POST)
  29. * @param string $account 账号
  30. * @param string $password 密码
  31. */
  32. public function login()
  33. {
  34. $account = $this->request->post('account');
  35. $password = $this->request->post('password');
  36. if (!$account || !$password) {
  37. $this->error(__('Invalid parameters'));
  38. }
  39. $ret = $this->auth->login($account, $password);
  40. if ($ret) {
  41. $data = $this->auth->getUserinfo_smiple();
  42. $this->success(__('Logged in successful'), $data);
  43. } else {
  44. $this->error($this->auth->getError());
  45. }
  46. }
  47. /**
  48. * 手机验证码登录
  49. *
  50. * @ApiMethod (POST)
  51. * @param string $mobile 手机号
  52. * @param string $captcha 验证码
  53. */
  54. public function mobilelogin()
  55. {
  56. $mobile = $this->request->post('mobile');
  57. $captcha = $this->request->post('captcha');
  58. if (!$mobile || !$captcha) {
  59. $this->error(__('Invalid parameters'));
  60. }
  61. if (!Validate::regex($mobile, "^1\d{10}$")) {
  62. $this->error(__('Mobile is incorrect'));
  63. }
  64. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  65. $this->error(__('Captcha is incorrect'));
  66. }
  67. $user = \app\common\model\User::getByMobile($mobile);
  68. if ($user) {
  69. if ($user->status != 1) {
  70. $this->error(__('Account is locked'));
  71. }
  72. //如果已经有账号则直接登录
  73. $ret = $this->auth->direct($user->id);
  74. } else {
  75. $this->error('不存在的用户');
  76. }
  77. if ($ret) {
  78. Sms::flush($mobile, 'mobilelogin');
  79. $data = $this->auth->getUserinfo_smiple();
  80. $this->success(__('Logged in successful'), $data);
  81. } else {
  82. $this->error($this->auth->getError());
  83. }
  84. }
  85. /**
  86. * 注册会员
  87. *
  88. * @ApiMethod (POST)
  89. * @param string $username 用户名
  90. * @param string $password 密码
  91. * @param string $email 邮箱
  92. * @param string $mobile 手机号
  93. * @param string $code 验证码
  94. */
  95. public function register()
  96. {
  97. $username = $this->request->post('username');
  98. $password = $this->request->post('password');
  99. $email = $this->request->post('email');
  100. $mobile = $this->request->post('mobile');
  101. $code = $this->request->post('code');
  102. if (!$username || !$password) {
  103. $this->error(__('Invalid parameters'));
  104. }
  105. if ($email && !Validate::is($email, "email")) {
  106. $this->error(__('Email is incorrect'));
  107. }
  108. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  109. $this->error(__('Mobile is incorrect'));
  110. }
  111. $ret = Sms::check($mobile, $code, 'register');
  112. if (!$ret) {
  113. $this->error(__('Captcha is incorrect'));
  114. }
  115. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  116. if ($ret) {
  117. $data = $this->auth->getUserinfo_smiple();
  118. $this->success(__('Sign up successful'), $data);
  119. } else {
  120. $this->error($this->auth->getError());
  121. }
  122. }
  123. /**
  124. * 退出登录
  125. * @ApiMethod (POST)
  126. */
  127. public function logout()
  128. {
  129. if (!$this->request->isPost()) {
  130. $this->error(__('Invalid parameters'));
  131. }
  132. $this->auth->logout();
  133. $this->success(__('Logout successful'));
  134. }
  135. //真注销
  136. public function cancleuser(){
  137. if (!$this->request->isPost()) {
  138. $this->error(__('Invalid parameters'));
  139. }
  140. //退出im
  141. // $tenIm = new Tenim();
  142. // $tenIm->loginoutim($this->auth->id);
  143. $data = [
  144. 'status' => -1,
  145. 'mobile' => 'close_'.$this->auth->mobile,
  146. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  147. // 'ios_user_id' => 'close_'.$this->auth->ios_user_id,
  148. ];
  149. Db::name('user')->where('id',$this->auth->id)->update($data);
  150. $this->auth->logout();
  151. $this->success('注销成功');
  152. }
  153. //用户详细资料
  154. public function userinfo(){
  155. $info = $this->auth->getUserinfo();
  156. $this->success(__('success'),$info);
  157. }
  158. /**
  159. * 修改会员个人信息
  160. *
  161. * @ApiMethod (POST)
  162. * @param string $avatar 头像地址
  163. * @param string $username 用户名
  164. * @param string $nickname 昵称
  165. * @param string $bio 个人简介
  166. */
  167. public function profile()
  168. {
  169. $field_array = ['nickname','avatar'];
  170. $data = [];
  171. foreach($field_array as $key => $field){
  172. //前端传不了post,改了
  173. /*if(!request()->has($field,'post')){
  174. continue;
  175. }*/
  176. if(!input('?'.$field)){
  177. continue;
  178. }
  179. $newone = input($field);
  180. if($field == 'avatar'){
  181. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  182. }
  183. $data[$field] = $newone;
  184. }
  185. if(empty($data)){
  186. $this->success();
  187. }
  188. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  189. if($update_rs === false){
  190. $this->error('修改资料失败');
  191. }
  192. $this->success();
  193. }
  194. /**
  195. * 修改手机号
  196. *
  197. * @ApiMethod (POST)
  198. * @param string $mobile 手机号
  199. * @param string $captcha 验证码
  200. */
  201. public function changemobile()
  202. {
  203. $user = $this->auth->getUser();
  204. $mobile = $this->request->post('mobile');
  205. $captcha = $this->request->post('captcha');
  206. if (!$mobile || !$captcha) {
  207. $this->error(__('Invalid parameters'));
  208. }
  209. if (!Validate::regex($mobile, "^1\d{10}$")) {
  210. $this->error(__('Mobile is incorrect'));
  211. }
  212. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  213. $this->error(__('Mobile already exists'));
  214. }
  215. $result = Sms::check($mobile, $captcha, 'changemobile');
  216. if (!$result) {
  217. $this->error(__('Captcha is incorrect'));
  218. }
  219. $user->mobile = $mobile;
  220. $user->save();
  221. Sms::flush($mobile, 'changemobile');
  222. $this->success();
  223. }
  224. //修改密码
  225. public function changepwd()
  226. {
  227. $mobile = $this->auth->mobile;
  228. $captcha = $this->request->post('captcha');
  229. $newpassword = input('newpassword','');
  230. if (!$mobile || !$captcha || !$newpassword) {
  231. $this->error(__('Invalid parameters'));
  232. }
  233. if (!Validate::regex($mobile, "^1\d{10}$")) {
  234. $this->error(__('Mobile is incorrect'));
  235. }
  236. $result = Sms::check($mobile, $captcha, 'changepwd');
  237. if (!$result) {
  238. $this->error(__('Captcha is incorrect'));
  239. }
  240. Sms::flush($mobile, 'changepwd');
  241. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  242. $this->error(__('Password must be 6 to 30 characters'));
  243. }
  244. $ret = $this->auth->changepwd($newpassword, '', true);
  245. if ($ret) {
  246. $this->success(__('Reset password successful'));
  247. } else {
  248. $this->error($this->auth->getError());
  249. }
  250. }
  251. /**
  252. * 重置密码
  253. *
  254. * @ApiMethod (POST)
  255. * @param string $mobile 手机号
  256. * @param string $newpassword 新密码
  257. * @param string $captcha 验证码
  258. */
  259. public function resetpwd()
  260. {
  261. $mobile = $this->request->post("mobile");
  262. $captcha = $this->request->post("captcha");
  263. $newpassword = $this->request->post("newpassword");
  264. if (!$mobile || !$captcha || !$newpassword) {
  265. $this->error(__('Invalid parameters'));
  266. }
  267. if (!Validate::regex($mobile, "^1\d{10}$")) {
  268. $this->error(__('Mobile is incorrect'));
  269. }
  270. $user = \app\common\model\User::getByMobile($mobile);
  271. if (!$user) {
  272. $this->error('不存在的用户');
  273. }else{
  274. if ($user->status != 1) {
  275. $this->error(__('Account is locked'));
  276. }
  277. }
  278. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  279. if (!$ret) {
  280. $this->error(__('Captcha is incorrect'));
  281. }
  282. Sms::flush($mobile, 'resetpwd');
  283. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  284. $this->error(__('Password must be 6 to 30 characters'));
  285. }
  286. //模拟一次登录
  287. $this->auth->direct($user->id);
  288. $ret = $this->auth->changepwd($newpassword, '', true);
  289. if ($ret) {
  290. $this->success(__('Reset password successful'));
  291. } else {
  292. $this->error($this->auth->getError());
  293. }
  294. }
  295. }