User.php 9.6 KB

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