User.php 16 KB

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