User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 || !$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. 'birthday' => strtotime(input('birthday','')),
  148. 'weight' => input('weight',''),
  149. 'address' => input('address',''),
  150. 'knowus' => input('knowus',''),
  151. 'health' => input('health',''),
  152. 'emergency' => input('emergency',''),
  153. 'emergency_phone' => input('emergency_phone',''),
  154. 'is_first' => input('is_first',''),
  155. 'residential' => input('residential',''),
  156. ];
  157. $ret = $this->auth->register('', $password, $email, $fullmobile, $extend);
  158. if ($ret) {
  159. $data = $this->auth->getUserinfo();
  160. $this->success(__('Sign up successful'), $data);
  161. } else {
  162. $this->error($this->auth->getError());
  163. }
  164. }
  165. //注册配置,怎么知道我们
  166. public function register_config(){
  167. $knowus = [
  168. 'Facebook',
  169. 'Instagram',
  170. 'Family and Friends',
  171. 'Google',
  172. 'Whatsapp',
  173. 'Others',
  174. ];
  175. $residential = [
  176. '北部',
  177. '东北部',
  178. '东部',
  179. '南部',
  180. '西部',
  181. '中部',
  182. ];
  183. $rs['knowus'] = $knowus;
  184. $rs['residential'] = $residential;
  185. $this->success(1,$rs);
  186. }
  187. /**
  188. * 退出登录
  189. * @ApiMethod (POST)
  190. */
  191. public function logout()
  192. {
  193. if (!$this->request->isPost()) {
  194. $this->error(__('Invalid parameters'));
  195. }
  196. $this->auth->logout();
  197. $this->success(__('Logout successful'));
  198. }
  199. /**
  200. * 修改会员个人信息
  201. *
  202. * @ApiMethod (POST)
  203. * @param string $avatar 头像地址
  204. * @param string $username 用户名
  205. * @param string $nickname 昵称
  206. * @param string $bio 个人简介
  207. */
  208. public function profile()
  209. {
  210. $field_array = ['firstname','lastname','height','age','weight','address','avatar','notice_email','notice_whatsapp','whatsapp'];
  211. $data = [];
  212. foreach($field_array as $key => $field){
  213. //前端传不了post,改了
  214. /*if(!request()->has($field,'post')){
  215. continue;
  216. }*/
  217. if(!input('?'.$field)){
  218. continue;
  219. }
  220. $newone = input($field);
  221. if($field == 'avatar'){
  222. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  223. }
  224. $data[$field] = $newone;
  225. }
  226. if(empty($data)){
  227. $this->success();
  228. }
  229. Db::name('user')->where('id',$this->auth->id)->update($data);
  230. $this->success();
  231. }
  232. //用户详细资料
  233. public function userInfo(){
  234. $info = $this->auth->getUserinfo();
  235. $this->success(__('success'),$info);
  236. }
  237. /**
  238. * 修改邮箱
  239. *
  240. * @ApiMethod (POST)
  241. * @param string $email 邮箱
  242. * @param string $captcha 验证码
  243. */
  244. /*public function changeemail()
  245. {
  246. $user = $this->auth->getUser();
  247. $email = input('email');
  248. $captcha = input('captcha');
  249. if (!$email || !$captcha) {
  250. $this->error(__('Invalid parameters'));
  251. }
  252. if (!Validate::is($email, "email")) {
  253. $this->error(__('Email is incorrect'));
  254. }
  255. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  256. $this->error(__('Email already exists'));
  257. }
  258. $result = Ems::check($email, $captcha, 'changeemail');
  259. if (!$result) {
  260. $this->error(__('Captcha is incorrect'));
  261. }
  262. $verification = $user->verification;
  263. $verification->email = 1;
  264. $user->verification = $verification;
  265. $user->email = $email;
  266. $user->save();
  267. Ems::flush($email, 'changeemail');
  268. $this->success();
  269. }*/
  270. /**
  271. * 修改手机号
  272. *
  273. * @ApiMethod (POST)
  274. * @param string $mobile 手机号
  275. * @param string $captcha 验证码
  276. */
  277. /*public function changemobile()
  278. {
  279. $user = $this->auth->getUser();
  280. $mobile = input('mobile');
  281. $captcha = input('captcha');
  282. if (!$mobile || !$captcha) {
  283. $this->error(__('Invalid parameters'));
  284. }
  285. if (!Validate::regex($mobile, "^1\d{10}$")) {
  286. $this->error(__('Mobile is incorrect'));
  287. }
  288. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  289. $this->error(__('Mobile already exists'));
  290. }
  291. $result = Sms::check($mobile, $captcha, 'changemobile');
  292. if (!$result) {
  293. $this->error(__('Captcha is incorrect'));
  294. }
  295. $verification = $user->verification;
  296. $verification->mobile = 1;
  297. $user->verification = $verification;
  298. $user->mobile = $mobile;
  299. $user->save();
  300. Sms::flush($mobile, 'changemobile');
  301. $this->success();
  302. }*/
  303. /**
  304. * 重置密码
  305. *
  306. * @ApiMethod (POST)
  307. * @param string $mobile 手机号
  308. * @param string $newpassword 新密码
  309. * @param string $captcha 验证码
  310. */
  311. public function resetpwd()
  312. {
  313. $email = input("email");
  314. $captcha = input("emailcaptcha");
  315. $newpassword = input("newpassword");
  316. if (!$newpassword || !$captcha) {
  317. $this->error(__('Invalid parameters'));
  318. }
  319. //验证Token
  320. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  321. $this->error(__('Password must be 6 to 30 characters'));
  322. }
  323. if (!Validate::is($email, "email")) {
  324. $this->error(__('Email is incorrect'));
  325. }
  326. $user = \app\common\model\User::getByEmail($email);
  327. if (!$user) {
  328. $this->error(__('User not found'));
  329. }
  330. $ret = Ems::check($email, $captcha, 'resetpwd');
  331. if (!$ret) {
  332. $this->error(__('Captcha is incorrect'));
  333. }
  334. Ems::flush($email, 'resetpwd');
  335. //模拟一次登录
  336. $this->auth->direct($user->id);
  337. $ret = $this->auth->changepwd($newpassword, '', true);
  338. if ($ret) {
  339. $this->success(__('Reset password successful'));
  340. } else {
  341. $this->error($this->auth->getError());
  342. }
  343. }
  344. /**
  345. * 注销账号
  346. *
  347. * @param string $mobile 手机号
  348. * @param string $captcha 验证码
  349. */
  350. public function cancleUser()
  351. {
  352. $captcha = input("emailcaptcha");
  353. if (!$captcha) {
  354. $this->error(__('Invalid parameters'));
  355. }
  356. $email = $this->auth->email;
  357. $ret = Ems::check($email, $captcha, 'resetpwd');
  358. if (!$ret) {
  359. $this->error(__('Captcha is incorrect'));
  360. }
  361. Ems::flush($email, 'resetpwd');
  362. $user = $this->auth->getUser();
  363. $user->status = -1;
  364. $user->save();
  365. $this->auth->logout();
  366. $this->success("账号注销成功");
  367. }
  368. }