User.php 10.0 KB

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