User.php 12 KB

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