User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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','wechatlogin','bindmobile','resetpwd'];
  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. $from = input('from','app');
  126. $field = $from == 'app' ? 'app_openid' : 'mini_openid';
  127. //微信
  128. $wechat = new Wechat();
  129. $wxuserinfo = $wechat->getAccessToken($code);
  130. if(!$wxuserinfo){
  131. $this->error('openid获取失败');
  132. }
  133. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid']) || !isset($wxuserinfo['unionid'])){
  134. $this->error('openid获取失败');
  135. }
  136. $openid = $wxuserinfo['openid'];
  137. $unionid = $wxuserinfo['unionid'];
  138. //检查用户
  139. $user = Db::name('user')->where('unionid',$unionid)->find();
  140. if ($user) {
  141. if ($user['status'] == -1) {
  142. $this->error('账户已注销');
  143. }
  144. if ($user['status'] != 1) {
  145. $this->error(__('Account is locked'));
  146. }
  147. /*$update = [
  148. $field => $openid,//应该就只有mini支付时用
  149. ];
  150. Db::name('user')->where('id',$user['id'])->update($update);*/
  151. //如果已经有账号则直接登录
  152. $ret = $this->auth->direct($user['id']);
  153. if ($ret) {
  154. $userInfo = $this->auth->getUserinfo_simple();
  155. $userInfo['is_register'] = 0;
  156. $userInfo['code'] = $code;
  157. $this->success(__('Logged in successful'), $userInfo);
  158. } else {
  159. $this->error($this->auth->getError());
  160. }
  161. } else {
  162. //记录code和openid,绑定手机号的时候更新openid
  163. $wechatCodeData = [
  164. 'code' => $code,
  165. 'openid' => $openid,
  166. 'unionid' => $unionid,
  167. 'from' => $from,
  168. 'createtime' => time(),
  169. ];
  170. $wechatCode = Db::name('wechat_code')->where(['unionid'=>$unionid,'from'=>$from])->find();
  171. if (empty($wechatCode)) {
  172. Db::name('wechat_code')->insertGetId($wechatCodeData);
  173. } else {
  174. Db::name('wechat_code')->where(['unionid'=>$unionid,'from'=>$from])->update($wechatCodeData);
  175. }
  176. //直接返回
  177. $userInfo = [];
  178. $userInfo['is_register'] = 1;
  179. $userInfo['code'] = $code;
  180. $this->success('获取信息成功', $userInfo);
  181. }
  182. }
  183. /**
  184. * 微信注册来的,绑定手机号
  185. *
  186. * @ApiMethod (POST)
  187. * @param string $mobile 手机号
  188. * @param string $captcha 验证码
  189. */
  190. public function bindmobile()
  191. {
  192. $mobile = input('mobile');
  193. $captcha = input('captcha');
  194. $code = input('code');
  195. $from = input('from','app');
  196. $field = $from == 'app' ? 'app_openid' : 'mini_openid';
  197. if (!$mobile || !$captcha || !$code) {
  198. $this->error(__('Invalid parameters'));
  199. }
  200. if (!Validate::regex($mobile, "^1\d{10}$")) {
  201. $this->error(__('Mobile is incorrect'));
  202. }
  203. $result = Sms::check($mobile, $captcha, 'wechatregister');
  204. if (!$result) {
  205. $this->error(__('Captcha is incorrect'));
  206. }
  207. $wechatCodeWhere['code'] = $code;
  208. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  209. if (empty($wechatCode)) {
  210. $this->error('请先微信登录');
  211. }
  212. //检查appid绑定的用户
  213. $user = Db::name('user')->where('unionid',$wechatCode['unionid'])->find();
  214. if ($user) {
  215. if ($user['status'] == -1) {
  216. $this->error('账户已注销');
  217. }
  218. if ($user['status'] != 1) {
  219. $this->error(__('Account is locked'));
  220. }
  221. //如果已经有账号则直接登录
  222. $ret = $this->auth->direct($user['id']);
  223. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  224. }
  225. //新的openid用户
  226. $where = [];
  227. $where['mobile'] = $mobile;
  228. $userData = Db::name('user')->where($where)->find();//老用户
  229. if (!empty($userData)) {
  230. if (empty($userData['unionid'])) {
  231. Db::name('user')->where('id',$userData['id'])->update([$field => $wechatCode['openid'],'unionid' => $wechatCode['unionid']]);//老用户更新openid
  232. } else {
  233. if ($userData['unionid'] != $wechatCode['unionid']) {
  234. $this->error('该手机号已被其他用户绑定');
  235. }
  236. }
  237. $ret = $this->auth->direct($userData['id']);
  238. } else {
  239. $extend = [
  240. $field => $wechatCode['openid'],
  241. 'unionid' => $wechatCode['unionid'],
  242. ];
  243. $ret = $this->auth->register('', '','', $mobile, $extend);
  244. }
  245. if (!$ret) {
  246. $this->error($this->auth->getError());
  247. }
  248. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  249. }
  250. /**
  251. * 退出登录
  252. * @ApiMethod (POST)
  253. */
  254. public function logout()
  255. {
  256. if (!$this->request->isPost()) {
  257. $this->error(__('Invalid parameters'));
  258. }
  259. $this->auth->logout();
  260. $this->success(__('Logout successful'));
  261. }
  262. //真注销
  263. public function cancleuser(){
  264. if (!$this->request->isPost()) {
  265. $this->error(__('Invalid parameters'));
  266. }
  267. //退出im
  268. // $tenIm = new Tenim();
  269. // $tenIm->loginoutim($this->auth->id);
  270. $data = [
  271. 'status' => -1,
  272. 'mobile' => 'close_'.$this->auth->mobile,
  273. 'app_openid' => 'close_'.$this->auth->app_openid,
  274. 'mini_openid' => 'close_'.$this->auth->mini_openid,
  275. // 'ios_user_id' => 'close_'.$this->auth->ios_user_id,
  276. ];
  277. Db::name('user')->where('id',$this->auth->id)->update($data);
  278. $this->auth->logout();
  279. $this->success('注销成功');
  280. }
  281. //用户详细资料
  282. public function userinfo(){
  283. $info = $this->auth->getUserinfo();
  284. $this->success(__('success'),$info);
  285. }
  286. /**
  287. * 修改会员个人信息
  288. *
  289. * @ApiMethod (POST)
  290. * @param string $avatar 头像地址
  291. * @param string $username 用户名
  292. * @param string $nickname 昵称
  293. * @param string $bio 个人简介
  294. */
  295. public function profile()
  296. {
  297. $field_array = ['nickname','avatar'];
  298. $data = [];
  299. foreach($field_array as $key => $field){
  300. //前端传不了post,改了
  301. /*if(!request()->has($field,'post')){
  302. continue;
  303. }*/
  304. if(!input('?'.$field)){
  305. continue;
  306. }
  307. $newone = input($field);
  308. if($field == 'avatar'){
  309. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  310. }
  311. $data[$field] = $newone;
  312. }
  313. if(empty($data)){
  314. $this->success();
  315. }
  316. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  317. if($update_rs === false){
  318. $this->error('修改资料失败');
  319. }
  320. $this->success();
  321. }
  322. /**
  323. * 修改手机号
  324. *
  325. * @ApiMethod (POST)
  326. * @param string $mobile 手机号
  327. * @param string $captcha 验证码
  328. */
  329. public function changemobile()
  330. {
  331. $user = $this->auth->getUser();
  332. $mobile = input('mobile');
  333. $captcha = input('captcha');
  334. if (!$mobile || !$captcha) {
  335. $this->error(__('Invalid parameters'));
  336. }
  337. if (!Validate::regex($mobile, "^1\d{10}$")) {
  338. $this->error(__('Mobile is incorrect'));
  339. }
  340. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  341. $this->error(__('Mobile already exists'));
  342. }
  343. $result = Sms::check($mobile, $captcha, 'changemobile');
  344. if (!$result) {
  345. $this->error(__('Captcha is incorrect'));
  346. }
  347. $user->mobile = $mobile;
  348. $user->save();
  349. Sms::flush($mobile, 'changemobile');
  350. $this->success();
  351. }
  352. //修改密码
  353. public function changepwd()
  354. {
  355. $mobile = $this->auth->mobile;
  356. $captcha = input('captcha');
  357. $newpassword = input('newpassword','');
  358. if (!$mobile || !$captcha || !$newpassword) {
  359. $this->error(__('Invalid parameters'));
  360. }
  361. if (!Validate::regex($mobile, "^1\d{10}$")) {
  362. $this->error(__('Mobile is incorrect'));
  363. }
  364. $result = Sms::check($mobile, $captcha, 'changepwd');
  365. if (!$result) {
  366. $this->error(__('Captcha is incorrect'));
  367. }
  368. Sms::flush($mobile, 'changepwd');
  369. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  370. $this->error(__('Password must be 6 to 30 characters'));
  371. }
  372. $ret = $this->auth->changepwd($newpassword, '', true);
  373. if ($ret) {
  374. $this->success(__('Reset password successful'));
  375. } else {
  376. $this->error($this->auth->getError());
  377. }
  378. }
  379. /**
  380. * 重置密码
  381. *
  382. * @ApiMethod (POST)
  383. * @param string $mobile 手机号
  384. * @param string $newpassword 新密码
  385. * @param string $captcha 验证码
  386. */
  387. public function resetpwd()
  388. {
  389. $mobile = input("mobile");
  390. $captcha = input("captcha");
  391. $newpassword = input("newpassword");
  392. if (!$mobile || !$captcha || !$newpassword) {
  393. $this->error(__('Invalid parameters'));
  394. }
  395. if (!Validate::regex($mobile, "^1\d{10}$")) {
  396. $this->error(__('Mobile is incorrect'));
  397. }
  398. $user = \app\common\model\User::getByMobile($mobile);
  399. if (!$user) {
  400. $this->error('不存在的用户');
  401. }else{
  402. if ($user->status != 1) {
  403. $this->error(__('Account is locked'));
  404. }
  405. }
  406. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  407. if (!$ret) {
  408. $this->error(__('Captcha is incorrect'));
  409. }
  410. Sms::flush($mobile, 'resetpwd');
  411. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  412. $this->error(__('Password must be 6 to 30 characters'));
  413. }
  414. //模拟一次登录
  415. $this->auth->direct($user->id);
  416. $ret = $this->auth->changepwd($newpassword, '', true);
  417. if ($ret) {
  418. $this->success(__('Reset password successful'));
  419. } else {
  420. $this->error($this->auth->getError());
  421. }
  422. }
  423. }