User.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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\common\library\Wechat;
  7. use app\common\service\UserService;
  8. use fast\Random;
  9. use think\Exception;
  10. use think\Log;
  11. use think\Validate;
  12. use miniprogram\wxBizDataCrypt;
  13. use onlogin\onlogin;
  14. use think\Db;
  15. /**
  16. * 会员接口
  17. */
  18. class User extends Api
  19. {
  20. protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin','bindmobile'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 手机验证码登录
  35. *
  36. * @param string $mobile 手机号
  37. * @param string $captcha 验证码
  38. */
  39. public function mobilelogin()
  40. {
  41. $mobile = input('mobile');
  42. $captcha = input('captcha');
  43. if (!$mobile || !$captcha) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. if (!Validate::regex($mobile, "^1\d{10}$")) {
  47. $this->error(__('Mobile is incorrect'));
  48. }
  49. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  50. $this->error(__('Captcha is incorrect'));
  51. }
  52. $user = \app\common\model\User::getByMobile($mobile);
  53. if ($user) {
  54. if ($user->status == -1) {
  55. $this->error('账户已注销');
  56. }
  57. if ($user->status != 1) {
  58. $this->error(__('Account is locked'));
  59. }
  60. //如果已经有账号则直接登录
  61. $is_register = 0;
  62. $ret = $this->auth->direct($user->id);
  63. } else {
  64. $is_register = 1;
  65. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, []);
  66. }
  67. if ($ret) {
  68. Sms::flush($mobile, 'mobilelogin');
  69. // $data = ['is_register' => $is_register, 'userinfo' => $this->auth->getUserinfo()];
  70. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  71. } else {
  72. $this->error($this->auth->getError());
  73. }
  74. }
  75. //苹果登录+注册
  76. public function applelogin(){
  77. $iosUserId = input('ios_user_id','');
  78. if(!$iosUserId){
  79. $this->error(__('Invalid parameters'));
  80. }
  81. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  82. if ($user) {
  83. if ($user['status'] == -1) {
  84. $this->error('账户已经注销');
  85. }
  86. if ($user['status'] != 1) {
  87. $this->error(__('Account is locked'));
  88. }
  89. //如果已经有账号则直接登录
  90. $ret = $this->auth->direct($user['id']);
  91. } else {
  92. $ret = $this->auth->ios_register($iosUserId);
  93. }
  94. if ($ret) {
  95. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  96. } else {
  97. $this->error($this->auth->getError());
  98. }
  99. }
  100. /*
  101. * 修改用户的坐标
  102. * */
  103. public function change_longlat(){
  104. $longitude = input('longitude',0);
  105. $latitude = input('latitude',0);
  106. if(empty($longitude) || empty($latitude)){
  107. $this->error();
  108. }
  109. $data = [
  110. 'longitude' => $longitude,
  111. 'latitude' => $latitude,
  112. ];
  113. Db::name('user')->where('id',$this->auth->id)->update($data);
  114. $this->success();
  115. }
  116. /**
  117. * 绑定用户
  118. */
  119. public function bindUser()
  120. {
  121. $invite_no = input('invite_no'); // 邀请码
  122. if (!$invite_no) {
  123. $this->error("请输入邀请码!");
  124. }
  125. $user_id = $this->auth->id;
  126. // 查询邀请码用户信息
  127. $inviteUserInfo = \app\common\model\User::where(["invite_no" => $invite_no])->find();
  128. if (!$inviteUserInfo) {
  129. $this->error("查询不到该邀请码用户信息!");
  130. }
  131. //判断邀请码用户不能是自己的下级
  132. if ($inviteUserInfo['pre_userid'] == $user_id) {
  133. throw new Exception('对方已被您邀请过');
  134. }
  135. if ($inviteUserInfo->id == $user_id) $this->error("不能邀请自己哦!");
  136. if ($inviteUserInfo->is_auth != 2) $this->error("该邀请码用户尚未完成实名认证");
  137. $res = \app\common\model\User::update(["pre_userid" => $inviteUserInfo->id], ["id" => $user_id]);
  138. if ($res) {
  139. $this->success("恭喜,绑定成功!");
  140. } else {
  141. $this->success("网络繁忙,请稍后重试!");
  142. }
  143. }
  144. /**
  145. * 退出登录
  146. */
  147. public function logout()
  148. {
  149. $this->auth->logout();
  150. $this->success(__('Logout successful'));
  151. }
  152. /**
  153. * 修改会员个人信息
  154. *
  155. * @ApiMethod (POST)
  156. * @param string $avatar 头像地址
  157. * @param string $username 用户名
  158. * @param string $nickname 昵称
  159. * @param string $bio 个人简介
  160. */
  161. public function profile()
  162. {
  163. $field_array = ['avatar','nickname','desc','age_id','gender','job_id','province_id','city_id','has_info'];
  164. $data = [];
  165. foreach($field_array as $key => $field){
  166. //前端传不了post,改了
  167. if(!request()->has($field,'post')){
  168. continue;
  169. }
  170. /*if(!input('?'.$field)){
  171. continue;
  172. }*/
  173. $newone = input($field);
  174. if($field == 'avatar'){
  175. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  176. }
  177. $data[$field] = $newone;
  178. }
  179. if(isset($data['invite_no']) && !empty($data['invite_no'])){
  180. $intro_user = Db::name('user')->where('invite_no',$data['invite_no'])->value('id');
  181. if(!$intro_user){
  182. $this->error('不存在的邀请人');
  183. }
  184. if(!empty($this->auth->pre_userid)){
  185. $this->error('您已经填写过邀请人');
  186. }
  187. unset($data['invite_no']);//别人的邀请码,不能改了自己的
  188. $data['pre_userid'] = $intro_user;
  189. //邀请用户注册,给邀请人奖励
  190. if(isset($data['pre_userid']) && !empty($data['pre_userid'])){
  191. /*$intro_gold = config('site.intro_newuser_gift_goldnum') ?: 0;
  192. if($intro_gold > 0){
  193. $wallet_rs = model('wallet')->lockChangeAccountRemain($data['intro_uid'],'gold',$intro_gold,63,'邀请'.$this->auth->username);
  194. if($wallet_rs['status'] === false){
  195. Db::rollback();
  196. $this->setError($wallet_rs['msg']);
  197. return false;
  198. }
  199. }*/
  200. }
  201. }
  202. if(empty($data)){
  203. $this->error('没有任何改变');
  204. }
  205. /*if (!empty($data['nickname']) && !empty($data['avatar']) && !empty($data['age_id']) && $data['has_info']==0) {
  206. $data['has_info'] = 1;
  207. }*/
  208. Db::startTrans();
  209. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  210. if($update_rs === false){
  211. Db::rollback();
  212. $this->error('修改资料失败');
  213. }
  214. Db::commit();
  215. $this->success();
  216. }
  217. /**
  218. * 修改手机号
  219. *
  220. * @param string $mobile 手机号
  221. * @param string $captcha 验证码
  222. */
  223. public function changemobile()
  224. {
  225. $user = $this->auth->getUser();
  226. $mobile = input('mobile');
  227. $captcha = input('captcha');
  228. if (!$mobile || !$captcha) {
  229. $this->error(__('Invalid parameters'));
  230. }
  231. if (!Validate::regex($mobile, "^1\d{10}$")) {
  232. $this->error(__('Mobile is incorrect'));
  233. }
  234. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  235. $this->error(__('Mobile already exists'));
  236. }
  237. $result = Sms::check($mobile, $captcha, 'changeMobile');
  238. if (!$result) {
  239. $this->error(__('Captcha is incorrect'));
  240. }
  241. $verification = $user->verification;
  242. $verification->mobile = 1;
  243. $user->verification = $verification;
  244. $user->mobile = $mobile;
  245. $user->save();
  246. Sms::flush($mobile, 'changeMobile');
  247. $this->success("手机号更换成功!");
  248. }
  249. /**
  250. * 重置密码
  251. *
  252. * @param string $mobile 手机号
  253. * @param string $newpassword 新密码
  254. * @param string $captcha 验证码
  255. */
  256. public function resetpwd()
  257. {
  258. $type = input("type");
  259. $mobile = input("mobile");
  260. $email = input("email");
  261. $newpassword = input("newpassword");
  262. $captcha = input("captcha");
  263. if (!$newpassword || !$captcha) {
  264. $this->error(__('Invalid parameters'));
  265. }
  266. if ($type == 'mobile') {
  267. if (!Validate::regex($mobile, "^1\d{10}$")) {
  268. $this->error(__('Mobile is incorrect'));
  269. }
  270. $user = \app\common\model\User::getByMobile($mobile);
  271. if (!$user) {
  272. $this->error(__('User not found'));
  273. }
  274. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  275. if (!$ret) {
  276. $this->error(__('Captcha is incorrect'));
  277. }
  278. Sms::flush($mobile, 'resetpwd');
  279. } else {
  280. if (!Validate::is($email, "email")) {
  281. $this->error(__('Email is incorrect'));
  282. }
  283. $user = \app\common\model\User::getByEmail($email);
  284. if (!$user) {
  285. $this->error(__('User not found'));
  286. }
  287. $ret = Ems::check($email, $captcha, 'resetpwd');
  288. if (!$ret) {
  289. $this->error(__('Captcha is incorrect'));
  290. }
  291. Ems::flush($email, 'resetpwd');
  292. }
  293. //模拟一次登录
  294. $this->auth->direct($user->id);
  295. $ret = $this->auth->changepwd($newpassword, '', true);
  296. if ($ret) {
  297. $this->success(__('Reset password successful'));
  298. } else {
  299. $this->error($this->auth->getError());
  300. }
  301. }
  302. /**
  303. * 设置密码
  304. * @param string $newpassword 新密码
  305. * @param string $newpassword 新密码
  306. */
  307. public function setpwd()
  308. {
  309. $params = $this->request->param();
  310. $validate = new \app\api\validate\User();
  311. $result = $validate->scene('setPwd')->check($params);
  312. if (!$result) {
  313. $this->error($validate->getError());
  314. }
  315. $ret = $this->auth->changepwd($params['password'], '', true);
  316. if ($ret) {
  317. $this->success(__('Set password successful'));
  318. } else {
  319. $this->error($this->auth->getError());
  320. }
  321. }
  322. /**
  323. * 修改密码
  324. *
  325. * @param string $mobile 手机号
  326. * @param string $newpassword 新密码
  327. * @param string $captcha 验证码
  328. */
  329. public function changepwd()
  330. {
  331. $params = $this->request->param();
  332. $validate = new \app\api\validate\User();
  333. $result = $validate->scene('changePwd')->check($params);
  334. if (!$result) {
  335. $this->error($validate->getError());
  336. }
  337. $mobile = input("mobile");
  338. $newpassword = input("password");
  339. $captcha = input("captcha");
  340. $user = \app\common\model\User::getByMobile($mobile);
  341. if (!$user) {
  342. $this->error(__('User not found'));
  343. }
  344. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  345. if (!$ret) {
  346. $this->error(__('Captcha is incorrect'));
  347. }
  348. Sms::flush($mobile, 'resetpwd');
  349. $ret = $this->auth->changepwd($newpassword, '', true);
  350. if ($ret) {
  351. $this->success(__('Change password successful'));
  352. } else {
  353. $this->error($this->auth->getError());
  354. }
  355. }
  356. /**
  357. * 运营商一键登录
  358. */
  359. public function onLogin()
  360. {
  361. $accessToken = $this->request->param('accessToken');// 运营商预取号获取到的token
  362. $token = $this->request->param('tokenT');// 易盾返回的token
  363. if (!$accessToken || !$token) {
  364. $this->error("参数获取失败!");
  365. }
  366. $params = array(
  367. // 运营商预取号获取到的token
  368. "accessToken" => $accessToken,
  369. // 易盾返回的token
  370. "token" => $token
  371. );
  372. // 获取密钥配置
  373. $configInfo = config("onLogin");
  374. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  375. $ret = $onlogin->check($params);
  376. // $ret = [];
  377. // $ret["code"] = 200;
  378. // $ret["msg"] = "ok";
  379. // $ret["data"] = [
  380. // "phone" => "17574504021",
  381. // "resultCode" => 0
  382. // ];
  383. if ($ret["code"] == 200) {
  384. $data = $ret["data"];
  385. $phone = $data["phone"];
  386. if (empty($phone)) {
  387. // 取号失败,建议进行二次验证,例如短信验证码
  388. $this->error("取号登录失败,请用验证码方式登录!");
  389. } else {
  390. // 取号成功, 执行登录等流程
  391. // 用户登录逻辑 === 开始
  392. $userModel = new \app\common\model\User();
  393. $auth = \app\common\library\Auth::instance();
  394. $userInfo = $userModel->where(["mobile" => $phone])->find();
  395. // 用户信息不存在时使用
  396. $extend = ["mobile" => $phone];
  397. // 判断用户是否已经存在
  398. if ($userInfo) { // 登录
  399. $user = \app\common\model\User::get($userInfo["id"]);
  400. if (!$user) {
  401. $this->error("网络错误!请稍后重试");
  402. }
  403. if ($user->status != 'normal') {
  404. $this->error(__('Account is locked'));
  405. }
  406. $user->save(["logintime" => time()]);
  407. $res = $auth->direct($user->id);
  408. $is_register = 0;
  409. } else { // 注册
  410. // 先随机一个用户名,随后再变更为u+数字id
  411. $username = Random::alnum(20);
  412. $password = Random::alnum(6);
  413. Db::startTrans();
  414. try {
  415. // 默认注册一个会员
  416. $result = $auth->register($username, $password, "", $extend);
  417. if (!$result) {
  418. return false;
  419. }
  420. $user = $auth->getUser();
  421. $fields = ['username' => 'u' . $user->id];
  422. // 更新会员资料
  423. $user = \app\common\model\User::get($user->id);
  424. $user->save($fields);
  425. Db::commit();
  426. } catch (PDOException $e) {
  427. Db::rollback();
  428. $auth->logout();
  429. return false;
  430. }
  431. // 写入登录Cookies和Token
  432. $res = $auth->direct($user->id);
  433. $is_register = 1;
  434. }
  435. $userInfo["userinfo"] = $auth->getUserinfo();
  436. $userInfo["is_register"] = $is_register;
  437. if ($res) {
  438. $this->success("登录成功!", $userInfo);
  439. } else {
  440. $this->error("登录失败!");
  441. }
  442. // 用户登录逻辑 === 结束
  443. }
  444. } else {
  445. $this->error("登录失败,请用验证码方式登录!");
  446. }
  447. }
  448. /**
  449. * 注销账号
  450. *
  451. * @param string $mobile 手机号
  452. * @param string $captcha 验证码
  453. */
  454. public function cancleUser()
  455. {
  456. $user = $this->auth->getUser();
  457. $user->status = -1;
  458. //unset($user->power);
  459. $user->save();
  460. $this->auth->logout();
  461. $this->success("账号注销成功!");
  462. }
  463. /**
  464. * 获取昵称
  465. * @return string
  466. */
  467. public function getNickName()
  468. {
  469. $result['nickname'] = get_rand_nick_name();
  470. $this->success('获取成功',$result);
  471. }
  472. //微信登录,预先假注册
  473. public function wechatlogin(){
  474. $code = input('code','');
  475. if(!$code){
  476. $this->error(__('Invalid parameters'));
  477. }
  478. //微信
  479. $wechat = new Wechat();
  480. $wxuserinfo = $wechat->getAccessToken($code);
  481. if(!$wxuserinfo){
  482. $this->error('openid获取失败');
  483. }
  484. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  485. $this->error('openid获取失败');
  486. }
  487. $openid = $wxuserinfo['openid'];
  488. $user = Db::name('user')->where('openid',$openid)->find();
  489. if ($user) {
  490. if ($user['status'] == -1) {
  491. $this->error('账户已注销');
  492. }
  493. if ($user['status'] != 1) {
  494. $this->error(__('Account is locked'));
  495. }
  496. //如果已经有账号则直接登录
  497. $ret = $this->auth->direct($user['id']);
  498. $is_register = 0;
  499. $userInfo = $this->auth->getUserinfo();
  500. } else {
  501. //记录code和openid,绑定手机号的时候更新openid
  502. $wechatCodeData = [
  503. 'code' => $code,
  504. 'openid' => $openid,
  505. 'createtime' => time(),
  506. ];
  507. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  508. if (empty($wechatCode)) {
  509. Db::name('wechat_code')->insertGetId($wechatCodeData);
  510. } else {
  511. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  512. }
  513. $ret = true;
  514. $is_register = 1;
  515. $userInfo = [];
  516. $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
  517. $this->success('获取信息成功', $data, 2);
  518. }
  519. if ($ret) {
  520. $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
  521. $this->success(__('Logged in successful'), $data);
  522. } else {
  523. $this->error($this->auth->getError());
  524. }
  525. }
  526. //获取openid
  527. public function getopenid() {
  528. //code
  529. $code = $this->request->post('code', '', 'trim');// code值
  530. if (!$code) {
  531. $this->error(__('Invalid parameters'));
  532. }
  533. $config = config('wxMiniProgram');
  534. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  535. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  536. $openidInfo = json_decode($openidInfo,true);
  537. if(!isset($openidInfo['openid'])) {
  538. $this->error('用户openid获取失败', $openidInfo);
  539. }
  540. $user = Db::name('user')->where('mini_openid',$openidInfo['openid'])->find();
  541. //$openidInfo['mobile'] = isset($user['mobile']) ? $user['mobile'] : '';
  542. $this->success('获取成功', $openidInfo);
  543. }
  544. /**
  545. * 微信注册来的,绑定手机号
  546. *
  547. * @ApiMethod (POST)
  548. * @param string $mobile 手机号
  549. * @param string $captcha 验证码
  550. */
  551. public function bindmobile()
  552. {
  553. try {
  554. $code = $this->request->param('code');
  555. $mobile = $this->request->param('mobile');
  556. $captcha = $this->request->param('captcha');
  557. if (!$mobile || !$captcha || !$code) {
  558. throw new Exception(__('Invalid parameters'));
  559. }
  560. if (!Validate::regex($mobile, "^1\d{10}$")) {
  561. throw new Exception(__('Mobile is incorrect'));
  562. }
  563. $result = Sms::check($mobile, $captcha, 'changemobile');
  564. if (!$result) {
  565. throw new Exception(__('Captcha is incorrect'));
  566. }
  567. $wechatCodeWhere['code'] = $code;
  568. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  569. if (empty($wechatCode)) {
  570. throw new Exception('请先微信登录');
  571. }
  572. //检查appid绑定的用户
  573. $user = Db::name('user')->where('openid',$wechatCode['openid'])->find();
  574. if ($user) {
  575. if ($user['status'] == -1) {
  576. $this->error('账户已注销');
  577. }
  578. if ($user['status'] != 1) {
  579. $this->error(__('Account is locked'));
  580. }
  581. //如果已经有账号则直接登录
  582. $ret = $this->auth->direct($user['id']);
  583. $this->success('success',$this->userInfo('return'));
  584. }
  585. $where['mobile'] = $mobile;
  586. $userData = model('User')->where($where)->find();//老用户
  587. if (!empty($userData)) {
  588. if (empty($userData['openid'])) {
  589. model('User')->update(['openid' => $wechatCode['openid']],$where);//老用户更新openid
  590. } else {
  591. if ($userData['openid'] != $wechatCode['openid']) {
  592. throw new Exception('该手机号已被其他用户绑定');
  593. }
  594. }
  595. $ret = $this->auth->direct($userData['id']);
  596. } else {
  597. $extend = [
  598. 'openid' => $wechatCode['openid'],
  599. ];
  600. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
  601. }
  602. if (!$ret) {
  603. throw new Exception($this->auth->getError());
  604. }
  605. Sms::flush($mobile, 'changemobile');
  606. $this->success('success',$this->userInfo('return'));
  607. } catch (Exception $e) {
  608. $this->error($e->getMessage());
  609. }
  610. }
  611. /**
  612. * 手机注册来的,绑定微信
  613. *
  614. * @ApiMethod (POST)
  615. * @param string $wechat_openid
  616. */
  617. public function bindopenid()
  618. {
  619. Db::startTrans();
  620. try {
  621. $code = $this->request->param('code','');
  622. if(!$code){
  623. throw new Exception(__('Invalid parameters'));
  624. }
  625. //微信
  626. $wechat = new Wechat();
  627. $openid = $wechat->getOpenid($code);
  628. if(!$openid){
  629. throw new Exception('openid获取失败');
  630. }
  631. $user = model('User')->find($this->auth->id);
  632. if(!empty($user['openid']) && $openid != $user['openid']){
  633. throw new Exception('已经绑定了微信号');
  634. }
  635. $otherUserWhere['openid'] = $openid;
  636. $otherUserWhere['id'] = ['neq',$this->auth->id];
  637. $otherUser = model('User')->where($otherUserWhere)->find();
  638. if (!empty($otherUser)) {
  639. throw new Exception('该微信已被其他用户绑定过');
  640. }
  641. $user->openid = $openid;
  642. $userRes = $user->save();
  643. if (!$userRes) {
  644. throw new Exception('绑定微信失败');
  645. }
  646. Db::commit();
  647. $this->success('success',$this->userInfo('return'));
  648. } catch (Exception $e) {
  649. Db::rollback();
  650. $this->error($e->getMessage());
  651. }
  652. }
  653. //用户详细资料
  654. public function userInfo($type = 1){
  655. $info = $this->auth->getUserinfo();
  656. if($type == 'return'){
  657. return $info;
  658. }
  659. $this->success(__('success'),$info);
  660. }
  661. /**
  662. * 微信解绑
  663. * @return void
  664. */
  665. public function clearopenid()
  666. {
  667. try {
  668. $userId = $this->auth->id;
  669. $where['id'] = $userId;
  670. $data['openid'] = '';
  671. $user = model('User')->update($data,$where);
  672. $this->success('解绑成功');
  673. } catch (Exception $e) {
  674. $this->error($e->getMessage());
  675. }
  676. }
  677. }