User.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 think\Validate;
  7. use think\Db;
  8. /**
  9. * 会员接口
  10. */
  11. class User extends Api
  12. {
  13. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  14. protected $noNeedRight = '*';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 会员中心
  21. */
  22. public function index()
  23. {
  24. }
  25. /**
  26. * 会员登录
  27. *
  28. * @ApiMethod (POST)
  29. * @param string $account 账号
  30. * @param string $password 密码
  31. */
  32. public function login()
  33. {
  34. $account = $this->request->post('account');
  35. $password = $this->request->post('password');
  36. if (!$account || !$password) {
  37. $this->error(__('Invalid parameters'));
  38. }
  39. $ret = $this->auth->login($account, $password);
  40. if ($ret) {
  41. $data = $this->auth->getUserinfo_smiple();
  42. $this->success(__('Logged in successful'), $data);
  43. } else {
  44. $this->error($this->auth->getError());
  45. }
  46. }
  47. /**
  48. * 手机验证码登录
  49. *
  50. * @ApiMethod (POST)
  51. * @param string $mobile 手机号
  52. * @param string $captcha 验证码
  53. */
  54. public function mobilelogin()
  55. {
  56. $mobile = $this->request->post('mobile');
  57. $captcha = $this->request->post('captcha');
  58. if (!$mobile || !$captcha) {
  59. $this->error(__('Invalid parameters'));
  60. }
  61. if (!Validate::regex($mobile, "^1\d{10}$")) {
  62. $this->error(__('Mobile is incorrect'));
  63. }
  64. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  65. $this->error(__('Captcha is incorrect'));
  66. }
  67. $user = \app\common\model\User::getByMobile($mobile);
  68. if ($user) {
  69. if ($user->status != 1) {
  70. $this->error(__('Account is locked'));
  71. }
  72. //如果已经有账号则直接登录
  73. $ret = $this->auth->direct($user->id);
  74. } else {
  75. $this->error('不存在的用户');
  76. }
  77. if ($ret) {
  78. Sms::flush($mobile, 'mobilelogin');
  79. $data = $this->auth->getUserinfo_smiple();
  80. $this->success(__('Logged in successful'), $data);
  81. } else {
  82. $this->error($this->auth->getError());
  83. }
  84. }
  85. /**
  86. * 注册会员
  87. *
  88. * @ApiMethod (POST)
  89. * @param string $username 用户名
  90. * @param string $password 密码
  91. * @param string $email 邮箱
  92. * @param string $mobile 手机号
  93. * @param string $code 验证码
  94. */
  95. public function register()
  96. {
  97. $mobile = $this->request->post('mobile');
  98. $captcha = $this->request->post('captcha');
  99. $password = $this->request->post('password');
  100. if (!$mobile || !$captcha || !$password) {
  101. $this->error(__('Invalid parameters'));
  102. }
  103. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  104. $this->error(__('Mobile is incorrect'));
  105. }
  106. $ret = Sms::check($mobile, $captcha, 'register');
  107. if (!$ret) {
  108. $this->error(__('Captcha is incorrect'));
  109. }
  110. $ret = $this->auth->register('', $password, '', $mobile, []);
  111. if ($ret) {
  112. $data = $this->auth->getUserinfo_smiple();
  113. $this->success(__('Sign up successful'), $data);
  114. } else {
  115. $this->error($this->auth->getError());
  116. }
  117. }
  118. /**
  119. * 退出登录
  120. * @ApiMethod (POST)
  121. */
  122. public function logout()
  123. {
  124. if (!$this->request->isPost()) {
  125. $this->error(__('Invalid parameters'));
  126. }
  127. $this->auth->logout();
  128. $this->success(__('Logout successful'));
  129. }
  130. //真注销
  131. public function cancleuser(){
  132. if (!$this->request->isPost()) {
  133. $this->error(__('Invalid parameters'));
  134. }
  135. //退出im
  136. // $tenIm = new Tenim();
  137. // $tenIm->loginoutim($this->auth->id);
  138. $data = [
  139. 'status' => -1,
  140. 'mobile' => 'close_'.$this->auth->mobile,
  141. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  142. // 'ios_user_id' => 'close_'.$this->auth->ios_user_id,
  143. ];
  144. Db::name('user')->where('id',$this->auth->id)->update($data);
  145. $this->auth->logout();
  146. $this->success('注销成功');
  147. }
  148. //用户详细资料
  149. public function userinfo(){
  150. $info = $this->auth->getUserinfo();
  151. $this->success(__('success'),$info);
  152. }
  153. /**
  154. * 修改会员个人信息
  155. *
  156. * @ApiMethod (POST)
  157. * @param string $avatar 头像地址
  158. * @param string $username 用户名
  159. * @param string $nickname 昵称
  160. * @param string $bio 个人简介
  161. */
  162. public function profile()
  163. {
  164. $field_array = ['nickname','avatar'];
  165. $data = [];
  166. foreach($field_array as $key => $field){
  167. //前端传不了post,改了
  168. /*if(!request()->has($field,'post')){
  169. continue;
  170. }*/
  171. if(!input('?'.$field)){
  172. continue;
  173. }
  174. $newone = input($field);
  175. if($field == 'avatar'){
  176. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  177. }
  178. $data[$field] = $newone;
  179. }
  180. if(empty($data)){
  181. $this->success();
  182. }
  183. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  184. if($update_rs === false){
  185. $this->error('修改资料失败');
  186. }
  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. $user->mobile = $mobile;
  215. $user->save();
  216. Sms::flush($mobile, 'changemobile');
  217. $this->success();
  218. }
  219. //修改密码
  220. public function changepwd()
  221. {
  222. $mobile = $this->auth->mobile;
  223. $captcha = $this->request->post('captcha');
  224. $newpassword = input('newpassword','');
  225. if (!$mobile || !$captcha || !$newpassword) {
  226. $this->error(__('Invalid parameters'));
  227. }
  228. if (!Validate::regex($mobile, "^1\d{10}$")) {
  229. $this->error(__('Mobile is incorrect'));
  230. }
  231. $result = Sms::check($mobile, $captcha, 'changepwd');
  232. if (!$result) {
  233. $this->error(__('Captcha is incorrect'));
  234. }
  235. Sms::flush($mobile, 'changepwd');
  236. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  237. $this->error(__('Password must be 6 to 30 characters'));
  238. }
  239. $ret = $this->auth->changepwd($newpassword, '', true);
  240. if ($ret) {
  241. $this->success(__('Reset password successful'));
  242. } else {
  243. $this->error($this->auth->getError());
  244. }
  245. }
  246. /**
  247. * 重置密码
  248. *
  249. * @ApiMethod (POST)
  250. * @param string $mobile 手机号
  251. * @param string $newpassword 新密码
  252. * @param string $captcha 验证码
  253. */
  254. public function resetpwd()
  255. {
  256. $mobile = $this->request->post("mobile");
  257. $captcha = $this->request->post("captcha");
  258. $newpassword = $this->request->post("newpassword");
  259. if (!$mobile || !$captcha || !$newpassword) {
  260. $this->error(__('Invalid parameters'));
  261. }
  262. if (!Validate::regex($mobile, "^1\d{10}$")) {
  263. $this->error(__('Mobile is incorrect'));
  264. }
  265. $user = \app\common\model\User::getByMobile($mobile);
  266. if (!$user) {
  267. $this->error('不存在的用户');
  268. }else{
  269. if ($user->status != 1) {
  270. $this->error(__('Account is locked'));
  271. }
  272. }
  273. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  274. if (!$ret) {
  275. $this->error(__('Captcha is incorrect'));
  276. }
  277. Sms::flush($mobile, 'resetpwd');
  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. //模拟一次登录
  282. $this->auth->direct($user->id);
  283. $ret = $this->auth->changepwd($newpassword, '', true);
  284. if ($ret) {
  285. $this->success(__('Reset password successful'));
  286. } else {
  287. $this->error($this->auth->getError());
  288. }
  289. }
  290. }