User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. use app\common\library\Wechat;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Api
  13. {
  14. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. /**
  21. * 会员中心
  22. */
  23. public function index()
  24. {
  25. }
  26. /**
  27. * 会员登录
  28. *
  29. * @ApiMethod (POST)
  30. * @param string $account 账号
  31. * @param string $password 密码
  32. */
  33. public function login()
  34. {
  35. $account = input('account');
  36. $password = input('password');
  37. if (!$account || !$password) {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $ret = $this->auth->login($account, $password);
  41. if ($ret) {
  42. $data = $this->auth->getUserinfo_smiple();
  43. $this->success(__('Logged in successful'), $data);
  44. } else {
  45. $this->error($this->auth->getError());
  46. }
  47. }
  48. /**
  49. * 手机验证码登录
  50. *
  51. * @ApiMethod (POST)
  52. * @param string $mobile 手机号
  53. * @param string $captcha 验证码
  54. */
  55. public function mobilelogin()
  56. {
  57. $mobile = input('mobile');
  58. $captcha = input('captcha');
  59. if (!$mobile || !$captcha) {
  60. $this->error(__('Invalid parameters'));
  61. }
  62. if (!Validate::regex($mobile, "^1\d{10}$")) {
  63. $this->error(__('Mobile is incorrect'));
  64. }
  65. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  66. $this->error(__('Captcha is incorrect'));
  67. }
  68. $user = \app\common\model\User::getByMobile($mobile);
  69. if ($user) {
  70. if ($user->status != 1) {
  71. $this->error(__('Account is locked'));
  72. }
  73. //如果已经有账号则直接登录
  74. $ret = $this->auth->direct($user->id);
  75. } else {
  76. $this->error('不存在的用户');
  77. }
  78. if ($ret) {
  79. Sms::flush($mobile, 'mobilelogin');
  80. $data = $this->auth->getUserinfo_smiple();
  81. $this->success(__('Logged in successful'), $data);
  82. } else {
  83. $this->error($this->auth->getError());
  84. }
  85. }
  86. /**
  87. * 注册会员
  88. *
  89. * @ApiMethod (POST)
  90. * @param string $username 用户名
  91. * @param string $password 密码
  92. * @param string $email 邮箱
  93. * @param string $mobile 手机号
  94. * @param string $code 验证码
  95. */
  96. public function register()
  97. {
  98. $mobile = input('mobile');
  99. $captcha = input('captcha');
  100. $password = input('password');
  101. if (!$mobile || !$captcha || !$password) {
  102. $this->error(__('Invalid parameters'));
  103. }
  104. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  105. $this->error(__('Mobile is incorrect'));
  106. }
  107. $ret = Sms::check($mobile, $captcha, 'register');
  108. if (!$ret) {
  109. $this->error(__('Captcha is incorrect'));
  110. }
  111. $ret = $this->auth->register('', $password, '', $mobile, []);
  112. if ($ret) {
  113. $data = $this->auth->getUserinfo_smiple();
  114. $this->success(__('Sign up successful'), $data);
  115. } else {
  116. $this->error($this->auth->getError());
  117. }
  118. }
  119. //微信登录,预先假注册
  120. public function wechatlogin(){
  121. $code = input('code','');
  122. if(!$code){
  123. $this->error(__('Invalid parameters'));
  124. }
  125. //微信
  126. $wechat = new Wechat();
  127. $wxuserinfo = $wechat->getAccessToken($code);
  128. if(!$wxuserinfo){
  129. $this->error('openid获取失败');
  130. }
  131. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  132. $this->error('openid获取失败');
  133. }
  134. $openid = $wxuserinfo['openid'];
  135. //检查用户
  136. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  137. if ($user) {
  138. if ($user['status'] == -1) {
  139. $this->error('账户已注销');
  140. }
  141. if ($user['status'] != 1) {
  142. $this->error(__('Account is locked'));
  143. }
  144. //如果已经有账号则直接登录
  145. $ret = $this->auth->direct($user['id']);
  146. if ($ret) {
  147. $userInfo = $this->auth->getUserinfo_simple();
  148. $userInfo['is_register'] = 0;
  149. $userInfo['code'] = $code;
  150. $this->success(__('Logged in successful'), $userInfo);
  151. } else {
  152. $this->error($this->auth->getError());
  153. }
  154. } else {
  155. //记录code和openid,绑定手机号的时候更新openid
  156. $wechatCodeData = [
  157. 'code' => $code,
  158. 'openid' => $openid,
  159. 'createtime' => time(),
  160. ];
  161. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  162. if (empty($wechatCode)) {
  163. Db::name('wechat_code')->insertGetId($wechatCodeData);
  164. } else {
  165. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  166. }
  167. //直接返回
  168. $userInfo = [];
  169. $userInfo['is_register'] = 1;
  170. $userInfo['code'] = $code;
  171. $this->success('获取信息成功', $userInfo);
  172. }
  173. }
  174. /**
  175. * 微信注册来的,绑定手机号
  176. *
  177. * @ApiMethod (POST)
  178. * @param string $mobile 手机号
  179. * @param string $captcha 验证码
  180. */
  181. public function bindmobile()
  182. {
  183. $mobile = input('mobile');
  184. $captcha = input('captcha');
  185. $code = input('code');
  186. if (!$mobile || !$captcha || !$code) {
  187. $this->error(__('Invalid parameters'));
  188. }
  189. if (!Validate::regex($mobile, "^1\d{10}$")) {
  190. $this->error(__('Mobile is incorrect'));
  191. }
  192. $result = Sms::check($mobile, $captcha, 'wechatregister');
  193. if (!$result) {
  194. $this->error(__('Captcha is incorrect'));
  195. }
  196. $wechatCodeWhere['code'] = $code;
  197. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  198. if (empty($wechatCode)) {
  199. $this->error('请先微信登录');
  200. }
  201. //检查appid绑定的用户
  202. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  203. if ($user) {
  204. if ($user['status'] == -1) {
  205. $this->error('账户已注销');
  206. }
  207. if ($user['status'] != 1) {
  208. $this->error(__('Account is locked'));
  209. }
  210. //如果已经有账号则直接登录
  211. $ret = $this->auth->direct($user['id']);
  212. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  213. }
  214. //新的openid用户
  215. $where = [];
  216. $where['mobile'] = $mobile;
  217. $userData = Db::name('user')->where($where)->find();//老用户
  218. if (!empty($userData)) {
  219. if (empty($userData['wechat_openid'])) {
  220. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  221. } else {
  222. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  223. $this->error('该手机号已被其他用户绑定');
  224. }
  225. }
  226. $ret = $this->auth->direct($userData['id']);
  227. } else {
  228. $extend = [
  229. 'wechat_openid' => $wechatCode['openid'],
  230. ];
  231. $ret = $this->auth->register('', '','', $mobile, $extend);
  232. }
  233. if (!$ret) {
  234. $this->error($this->auth->getError());
  235. }
  236. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  237. }
  238. /**
  239. * 退出登录
  240. * @ApiMethod (POST)
  241. */
  242. public function logout()
  243. {
  244. if (!$this->request->isPost()) {
  245. $this->error(__('Invalid parameters'));
  246. }
  247. $this->auth->logout();
  248. $this->success(__('Logout successful'));
  249. }
  250. //真注销
  251. public function cancleuser(){
  252. if (!$this->request->isPost()) {
  253. $this->error(__('Invalid parameters'));
  254. }
  255. //退出im
  256. // $tenIm = new Tenim();
  257. // $tenIm->loginoutim($this->auth->id);
  258. $data = [
  259. 'status' => -1,
  260. 'mobile' => 'close_'.$this->auth->mobile,
  261. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  262. // 'ios_user_id' => 'close_'.$this->auth->ios_user_id,
  263. ];
  264. Db::name('user')->where('id',$this->auth->id)->update($data);
  265. $this->auth->logout();
  266. $this->success('注销成功');
  267. }
  268. //用户详细资料
  269. public function userinfo(){
  270. $info = $this->auth->getUserinfo();
  271. $this->success(__('success'),$info);
  272. }
  273. /**
  274. * 修改会员个人信息
  275. *
  276. * @ApiMethod (POST)
  277. * @param string $avatar 头像地址
  278. * @param string $username 用户名
  279. * @param string $nickname 昵称
  280. * @param string $bio 个人简介
  281. */
  282. public function profile()
  283. {
  284. $field_array = ['nickname','avatar'];
  285. $data = [];
  286. foreach($field_array as $key => $field){
  287. //前端传不了post,改了
  288. /*if(!request()->has($field,'post')){
  289. continue;
  290. }*/
  291. if(!input('?'.$field)){
  292. continue;
  293. }
  294. $newone = input($field);
  295. if($field == 'avatar'){
  296. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  297. }
  298. $data[$field] = $newone;
  299. }
  300. if(empty($data)){
  301. $this->success();
  302. }
  303. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  304. if($update_rs === false){
  305. $this->error('修改资料失败');
  306. }
  307. $this->success();
  308. }
  309. /**
  310. * 修改手机号
  311. *
  312. * @ApiMethod (POST)
  313. * @param string $mobile 手机号
  314. * @param string $captcha 验证码
  315. */
  316. public function changemobile()
  317. {
  318. $user = $this->auth->getUser();
  319. $mobile = input('mobile');
  320. $captcha = input('captcha');
  321. if (!$mobile || !$captcha) {
  322. $this->error(__('Invalid parameters'));
  323. }
  324. if (!Validate::regex($mobile, "^1\d{10}$")) {
  325. $this->error(__('Mobile is incorrect'));
  326. }
  327. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  328. $this->error(__('Mobile already exists'));
  329. }
  330. $result = Sms::check($mobile, $captcha, 'changemobile');
  331. if (!$result) {
  332. $this->error(__('Captcha is incorrect'));
  333. }
  334. $user->mobile = $mobile;
  335. $user->save();
  336. Sms::flush($mobile, 'changemobile');
  337. $this->success();
  338. }
  339. //修改密码
  340. public function changepwd()
  341. {
  342. $mobile = $this->auth->mobile;
  343. $captcha = input('captcha');
  344. $newpassword = input('newpassword','');
  345. if (!$mobile || !$captcha || !$newpassword) {
  346. $this->error(__('Invalid parameters'));
  347. }
  348. if (!Validate::regex($mobile, "^1\d{10}$")) {
  349. $this->error(__('Mobile is incorrect'));
  350. }
  351. $result = Sms::check($mobile, $captcha, 'changepwd');
  352. if (!$result) {
  353. $this->error(__('Captcha is incorrect'));
  354. }
  355. Sms::flush($mobile, 'changepwd');
  356. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  357. $this->error(__('Password must be 6 to 30 characters'));
  358. }
  359. $ret = $this->auth->changepwd($newpassword, '', true);
  360. if ($ret) {
  361. $this->success(__('Reset password successful'));
  362. } else {
  363. $this->error($this->auth->getError());
  364. }
  365. }
  366. /**
  367. * 重置密码
  368. *
  369. * @ApiMethod (POST)
  370. * @param string $mobile 手机号
  371. * @param string $newpassword 新密码
  372. * @param string $captcha 验证码
  373. */
  374. public function resetpwd()
  375. {
  376. $mobile = input("mobile");
  377. $captcha = input("captcha");
  378. $newpassword = input("newpassword");
  379. if (!$mobile || !$captcha || !$newpassword) {
  380. $this->error(__('Invalid parameters'));
  381. }
  382. if (!Validate::regex($mobile, "^1\d{10}$")) {
  383. $this->error(__('Mobile is incorrect'));
  384. }
  385. $user = \app\common\model\User::getByMobile($mobile);
  386. if (!$user) {
  387. $this->error('不存在的用户');
  388. }else{
  389. if ($user->status != 1) {
  390. $this->error(__('Account is locked'));
  391. }
  392. }
  393. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  394. if (!$ret) {
  395. $this->error(__('Captcha is incorrect'));
  396. }
  397. Sms::flush($mobile, 'resetpwd');
  398. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  399. $this->error(__('Password must be 6 to 30 characters'));
  400. }
  401. //模拟一次登录
  402. $this->auth->direct($user->id);
  403. $ret = $this->auth->changepwd($newpassword, '', true);
  404. if ($ret) {
  405. $this->success(__('Reset password successful'));
  406. } else {
  407. $this->error($this->auth->getError());
  408. }
  409. }
  410. }