User.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getUserOpenid','wxMiniProgramLogin'];
  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 login()
  40. {
  41. $account = $this->request->post('account');
  42. $password = $this->request->post('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = ['userinfo' => $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. $mobile = $this->request->post('mobile');
  64. $captcha = $this->request->post('captcha');
  65. $introcode = $this->request->post('introcode','');
  66. if (!$mobile || !$captcha) {
  67. $this->error(__('Invalid parameters'));
  68. }
  69. if (!Validate::regex($mobile, "^1\d{10}$")) {
  70. $this->error(__('Mobile is incorrect'));
  71. }
  72. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  73. $this->error(__('Captcha is incorrect'));
  74. }
  75. //
  76. $intro_uid = 0;
  77. if(!empty($introcode)){
  78. $intro_uid = Db::name('user')->where('introcode',$introcode)->value('id');
  79. if(empty($intro_uid)){
  80. $this->error('请填写正确的邀请码或者不填');
  81. }
  82. }
  83. $user = \app\common\model\User::getByMobile($mobile);
  84. if ($user) {
  85. if ($user->status != 1) {
  86. $this->error('账号异常,请联系客服处理');
  87. }
  88. //如果已经有账号则直接登录
  89. $ret = $this->auth->direct($user->id);
  90. } else {
  91. // 用户信息不存在时使用
  92. $extend = [
  93. 'intro_uid' => $intro_uid,
  94. ];
  95. $ret = $this->auth->mobile_register($mobile, Random::alnum(), '', $mobile, $extend);
  96. }
  97. if ($ret) {
  98. Sms::flush($mobile, 'mobilelogin');
  99. $data = ['userinfo' => $this->auth->getUserinfo()];
  100. $this->success(__('Logged in successful'), $data);
  101. } else {
  102. $this->error($this->auth->getError());
  103. }
  104. }
  105. public function getUserinfo(){
  106. $info = $this->auth->getUserinfo();
  107. $this->success(1,$info);
  108. }
  109. /**
  110. * 注册会员
  111. *
  112. * @ApiMethod (POST)
  113. * @param string $username 用户名
  114. * @param string $password 密码
  115. * @param string $email 邮箱
  116. * @param string $mobile 手机号
  117. * @param string $code 验证码
  118. */
  119. public function register()
  120. {
  121. $username = $this->request->post('username');
  122. $password = $this->request->post('password');
  123. // $mobile = $this->request->post('mobile');
  124. $introcode = $this->request->post('introcode','');
  125. if (!$username || !$password) {
  126. $this->error(__('Invalid parameters'));
  127. }
  128. /* if ($email && !Validate::is($email, "email")) {
  129. $this->error(__('Email is incorrect'));
  130. }
  131. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  132. $this->error(__('Mobile is incorrect'));
  133. }
  134. $ret = Sms::check($mobile, $code, 'register');
  135. if (!$ret) {
  136. $this->error(__('Captcha is incorrect'));
  137. }*/
  138. //
  139. $intro_uid = 0;
  140. if(!empty($introcode)){
  141. $intro_uid = Db::name('user')->where('introcode',$introcode)->value('id');
  142. if(empty($intro_uid)){
  143. $this->error('请填写正确的邀请码或者不填');
  144. }
  145. }
  146. // 用户信息不存在时使用
  147. $extend = [
  148. 'intro_uid' => $intro_uid,
  149. ];
  150. $ret = $this->auth->register($username, $password, '', '', $extend);
  151. if ($ret) {
  152. $data = ['userinfo' => $this->auth->getUserinfo()];
  153. $this->success(__('Sign up successful'), $data);
  154. } else {
  155. $this->error($this->auth->getError());
  156. }
  157. }
  158. /**
  159. * 退出登录
  160. * @ApiMethod (POST)
  161. */
  162. public function logout()
  163. {
  164. if (!$this->request->isPost()) {
  165. $this->error(__('Invalid parameters'));
  166. }
  167. $this->auth->logout();
  168. $this->success(__('Logout successful'));
  169. }
  170. /**
  171. * 修改会员个人信息
  172. *
  173. * @ApiMethod (POST)
  174. * @param string $avatar 头像地址
  175. * @param string $username 用户名
  176. * @param string $nickname 昵称
  177. * @param string $bio 个人简介
  178. */
  179. public function profile()
  180. {
  181. $user = $this->auth->getUser();
  182. $nickname = $this->request->post('nickname');
  183. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  184. $user->nickname = $nickname;
  185. $user->avatar = $avatar;
  186. $user->save();
  187. $this->success();
  188. }
  189. /**
  190. * 修改手机号
  191. *
  192. * @ApiMethod (POST)
  193. * @param string $mobile 手机号
  194. * @param string $captcha 验证码
  195. */
  196. public function changemobile()
  197. {
  198. $user = $this->auth->getUser();
  199. $mobile = $this->request->post('mobile');
  200. $captcha = $this->request->post('captcha');
  201. if (!$mobile || !$captcha) {
  202. $this->error(__('Invalid parameters'));
  203. }
  204. if (!Validate::regex($mobile, "^1\d{10}$")) {
  205. $this->error(__('Mobile is incorrect'));
  206. }
  207. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  208. $this->error(__('Mobile already exists'));
  209. }
  210. $result = Sms::check($mobile, $captcha, 'changemobile');
  211. if (!$result) {
  212. $this->error(__('Captcha is incorrect'));
  213. }
  214. $verification = $user->verification;
  215. $verification->mobile = 1;
  216. $user->verification = $verification;
  217. $user->mobile = $mobile;
  218. $user->save();
  219. Sms::flush($mobile, 'changemobile');
  220. $this->success();
  221. }
  222. /**
  223. * 重置密码
  224. *
  225. * @ApiMethod (POST)
  226. * @param string $mobile 手机号
  227. * @param string $newpassword 新密码
  228. * @param string $captcha 验证码
  229. */
  230. public function resetpwd()
  231. {
  232. $type = $this->request->post("type");
  233. $mobile = $this->request->post("mobile");
  234. $email = $this->request->post("email");
  235. $newpassword = $this->request->post("newpassword");
  236. $captcha = $this->request->post("captcha");
  237. if (!$newpassword || !$captcha) {
  238. $this->error(__('Invalid parameters'));
  239. }
  240. //验证Token
  241. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  242. $this->error(__('Password must be 6 to 30 characters'));
  243. }
  244. if ($type == 'mobile') {
  245. if (!Validate::regex($mobile, "^1\d{10}$")) {
  246. $this->error(__('Mobile is incorrect'));
  247. }
  248. $user = \app\common\model\User::getByMobile($mobile);
  249. if (!$user) {
  250. $this->error(__('User not found'));
  251. }
  252. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  253. if (!$ret) {
  254. $this->error(__('Captcha is incorrect'));
  255. }
  256. Sms::flush($mobile, 'resetpwd');
  257. } else {
  258. if (!Validate::is($email, "email")) {
  259. $this->error(__('Email is incorrect'));
  260. }
  261. $user = \app\common\model\User::getByEmail($email);
  262. if (!$user) {
  263. $this->error(__('User not found'));
  264. }
  265. $ret = Ems::check($email, $captcha, 'resetpwd');
  266. if (!$ret) {
  267. $this->error(__('Captcha is incorrect'));
  268. }
  269. Ems::flush($email, 'resetpwd');
  270. }
  271. //模拟一次登录
  272. $this->auth->direct($user->id);
  273. $ret = $this->auth->changepwd($newpassword, '', true);
  274. if ($ret) {
  275. $this->success(__('Reset password successful'));
  276. } else {
  277. $this->error($this->auth->getError());
  278. }
  279. }
  280. /**
  281. * 修改密码
  282. *
  283. * @ApiMethod (POST)
  284. * @param string $newpassword 新密码
  285. * @param string $oldpassword 旧密码
  286. */
  287. public function changepwd(){
  288. $newpassword = input('newpassword');
  289. $oldpassword = input('oldpassword','');
  290. if (!$newpassword) {
  291. $this->error(__('Invalid parameters'));
  292. }
  293. if($this->auth->password && empty($oldpassword)){
  294. $this->error('原密码必填');
  295. }
  296. if(empty($this->auth->password)){
  297. $ret = $this->auth->changepwd($newpassword, '', true);
  298. }else{
  299. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  300. }
  301. if ($ret) {
  302. $this->success(__('Reset password successful'));
  303. } else {
  304. $this->error($this->auth->getError());
  305. }
  306. }
  307. }