User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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','register_config'];
  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 has been cancelled'));
  85. }
  86. if ($user->status != 1) {
  87. $this->error(__('Account is locked'));
  88. }
  89. //如果已经有账号则直接登录
  90. $ret = $this->auth->direct($user->id);
  91. } else {
  92. //$ret = $this->auth->register('', '', '', $mobile, []);
  93. $this->error(__('Account is incorrect'));
  94. }
  95. if ($ret) {
  96. Sms::flush($mobile, 'mobilelogin');
  97. $data = $this->auth->getUserinfo();
  98. $this->success(__('Logged in successful'), $data);
  99. } else {
  100. $this->error($this->auth->getError());
  101. }
  102. }
  103. /**
  104. * 注册会员
  105. *
  106. * @ApiMethod (POST)
  107. * @param string $username 用户名
  108. * @param string $password 密码
  109. * @param string $email 邮箱
  110. * @param string $mobile 手机号
  111. * @param string $code 验证码
  112. */
  113. public function register()
  114. {
  115. $firstname = input('firstname');
  116. $lastname = input('lastname');
  117. $countrycode = input('countrycode',65,'intval');
  118. $mobile = input('mobile');
  119. $captcha = input('captcha');
  120. $email = input('email');
  121. $emailcaptcha = input('emailcaptcha');
  122. $password = input('password');
  123. if (!$firstname || !$lastname || !$mobile || !$captcha || !$email || !$emailcaptcha || !$password) {
  124. $this->error(__('Invalid parameters'));
  125. }
  126. if ($email && !Validate::is($email, "email")) {
  127. $this->error(__('Email is incorrect'));
  128. }
  129. /*if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  130. $this->error(__('Mobile is incorrect'));
  131. }*/
  132. $fullmobile = $countrycode.$mobile;
  133. $ret = Sms::check($fullmobile, $captcha, 'register');
  134. if (!$ret) {
  135. $this->error(__('Mobile Captcha is incorrect'));
  136. }
  137. $ret = Ems::check($email, $emailcaptcha, 'register');
  138. if (!$ret) {
  139. $this->error(__('Email Captcha is incorrect'));
  140. }
  141. $extend = [
  142. 'firstname' => $firstname,
  143. 'lastname' => $lastname,
  144. 'simplemobile' => $mobile,
  145. 'height' => input('height',''),
  146. 'age' => input('age',''),
  147. 'weight' => input('weight',''),
  148. 'address' => input('address',''),
  149. 'knowus' => input('knowus',''),
  150. 'health' => input('health',''),
  151. 'emergency' => input('emergency',''),
  152. 'is_first' => input('is_first',''),
  153. ];
  154. $ret = $this->auth->register('', $password, $email, $fullmobile, $extend);
  155. if ($ret) {
  156. $data = $this->auth->getUserinfo();
  157. $this->success(__('Sign up successful'), $data);
  158. } else {
  159. $this->error($this->auth->getError());
  160. }
  161. }
  162. //注册配置,怎么知道我们
  163. public function register_config(){
  164. $rs = [
  165. 'Facebook',
  166. 'Instagram',
  167. 'Family and Friends',
  168. 'Google',
  169. 'Whatsapp',
  170. 'Others',
  171. ];
  172. $this->success(1,$rs);
  173. }
  174. /**
  175. * 退出登录
  176. * @ApiMethod (POST)
  177. */
  178. public function logout()
  179. {
  180. if (!$this->request->isPost()) {
  181. $this->error(__('Invalid parameters'));
  182. }
  183. $this->auth->logout();
  184. $this->success(__('Logout successful'));
  185. }
  186. /**
  187. * 修改会员个人信息
  188. *
  189. * @ApiMethod (POST)
  190. * @param string $avatar 头像地址
  191. * @param string $username 用户名
  192. * @param string $nickname 昵称
  193. * @param string $bio 个人简介
  194. */
  195. public function profile()
  196. {
  197. $field_array = ['firstname','lastname','height','age','weight','address','avatar','notice_email','notice_whatsapp','whatsapp'];
  198. $data = [];
  199. foreach($field_array as $key => $field){
  200. //前端传不了post,改了
  201. /*if(!request()->has($field,'post')){
  202. continue;
  203. }*/
  204. if(!input('?'.$field)){
  205. continue;
  206. }
  207. $newone = input($field);
  208. if($field == 'avatar'){
  209. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  210. }
  211. $data[$field] = $newone;
  212. }
  213. if(empty($data)){
  214. $this->success();
  215. }
  216. Db::name('user')->where('id',$this->auth->id)->update($data);
  217. $this->success();
  218. }
  219. //用户详细资料
  220. public function userInfo(){
  221. $info = $this->auth->getUserinfo();
  222. $this->success(__('success'),$info);
  223. }
  224. /**
  225. * 修改邮箱
  226. *
  227. * @ApiMethod (POST)
  228. * @param string $email 邮箱
  229. * @param string $captcha 验证码
  230. */
  231. /*public function changeemail()
  232. {
  233. $user = $this->auth->getUser();
  234. $email = input('email');
  235. $captcha = input('captcha');
  236. if (!$email || !$captcha) {
  237. $this->error(__('Invalid parameters'));
  238. }
  239. if (!Validate::is($email, "email")) {
  240. $this->error(__('Email is incorrect'));
  241. }
  242. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  243. $this->error(__('Email already exists'));
  244. }
  245. $result = Ems::check($email, $captcha, 'changeemail');
  246. if (!$result) {
  247. $this->error(__('Captcha is incorrect'));
  248. }
  249. $verification = $user->verification;
  250. $verification->email = 1;
  251. $user->verification = $verification;
  252. $user->email = $email;
  253. $user->save();
  254. Ems::flush($email, 'changeemail');
  255. $this->success();
  256. }*/
  257. /**
  258. * 修改手机号
  259. *
  260. * @ApiMethod (POST)
  261. * @param string $mobile 手机号
  262. * @param string $captcha 验证码
  263. */
  264. /*public function changemobile()
  265. {
  266. $user = $this->auth->getUser();
  267. $mobile = input('mobile');
  268. $captcha = input('captcha');
  269. if (!$mobile || !$captcha) {
  270. $this->error(__('Invalid parameters'));
  271. }
  272. if (!Validate::regex($mobile, "^1\d{10}$")) {
  273. $this->error(__('Mobile is incorrect'));
  274. }
  275. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  276. $this->error(__('Mobile already exists'));
  277. }
  278. $result = Sms::check($mobile, $captcha, 'changemobile');
  279. if (!$result) {
  280. $this->error(__('Captcha is incorrect'));
  281. }
  282. $verification = $user->verification;
  283. $verification->mobile = 1;
  284. $user->verification = $verification;
  285. $user->mobile = $mobile;
  286. $user->save();
  287. Sms::flush($mobile, 'changemobile');
  288. $this->success();
  289. }*/
  290. /**
  291. * 重置密码
  292. *
  293. * @ApiMethod (POST)
  294. * @param string $mobile 手机号
  295. * @param string $newpassword 新密码
  296. * @param string $captcha 验证码
  297. */
  298. public function resetpwd()
  299. {
  300. $email = input("email");
  301. $captcha = input("emailcaptcha");
  302. $newpassword = input("newpassword");
  303. if (!$newpassword || !$captcha) {
  304. $this->error(__('Invalid parameters'));
  305. }
  306. //验证Token
  307. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  308. $this->error(__('Password must be 6 to 30 characters'));
  309. }
  310. if (!Validate::is($email, "email")) {
  311. $this->error(__('Email is incorrect'));
  312. }
  313. $user = \app\common\model\User::getByEmail($email);
  314. if (!$user) {
  315. $this->error(__('User not found'));
  316. }
  317. $ret = Ems::check($email, $captcha, 'resetpwd');
  318. if (!$ret) {
  319. $this->error(__('Captcha is incorrect'));
  320. }
  321. Ems::flush($email, 'resetpwd');
  322. //模拟一次登录
  323. $this->auth->direct($user->id);
  324. $ret = $this->auth->changepwd($newpassword, '', true);
  325. if ($ret) {
  326. $this->success(__('Reset password successful'));
  327. } else {
  328. $this->error($this->auth->getError());
  329. }
  330. }
  331. /**
  332. * 注销账号
  333. *
  334. * @param string $mobile 手机号
  335. * @param string $captcha 验证码
  336. */
  337. public function cancleUser()
  338. {
  339. $captcha = input("emailcaptcha");
  340. if (!$captcha) {
  341. $this->error(__('Invalid parameters'));
  342. }
  343. $email = $this->auth->email;
  344. $ret = Ems::check($email, $captcha, 'resetpwd');
  345. if (!$ret) {
  346. $this->error(__('Captcha is incorrect'));
  347. }
  348. Ems::flush($email, 'resetpwd');
  349. $user = $this->auth->getUser();
  350. $user->status = -1;
  351. $user->save();
  352. $this->auth->logout();
  353. $this->success("账号注销成功");
  354. }
  355. }