User.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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 $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = $this->request->request('account');
  42. $password = $this->request->request('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = ['userinfo' => $this->auth->getUserinfo()];
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @param string $mobile 手机号
  58. * @param string $captcha 验证码
  59. */
  60. public function mobilelogin()
  61. {
  62. $mobile = $this->request->request('mobile');
  63. $captcha = $this->request->request('captcha');
  64. if (!$mobile || !$captcha) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::regex($mobile, "^1\d{10}$")) {
  68. $this->error(__('Mobile is incorrect'));
  69. }
  70. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  71. $this->error(__('Captcha is incorrect'));
  72. }
  73. $user = \app\common\model\User::getByMobile($mobile);
  74. if ($user) {
  75. if ($user->status == -1) {
  76. $this->error('账户已注销');
  77. }
  78. if ($user->status != 1) {
  79. $this->error(__('Account is locked'));
  80. }
  81. //如果已经有账号则直接登录
  82. $is_register = 0;
  83. $ret = $this->auth->direct($user->id);
  84. } else {
  85. $is_register = 1;
  86. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, []);
  87. }
  88. if ($ret) {
  89. Sms::flush($mobile, 'mobilelogin');
  90. $data = ['is_register' => $is_register, 'userinfo' => $this->auth->getUserinfo()];
  91. $this->success(__('Logged in successful'), $data);
  92. } else {
  93. $this->error($this->auth->getError());
  94. }
  95. }
  96. //苹果登录+注册
  97. public function applelogin(){
  98. $iosUserId = input('ios_user_id','');
  99. if(!$iosUserId){
  100. $this->error(__('Invalid parameters'));
  101. }
  102. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  103. if ($user) {
  104. if ($user['status'] == -1) {
  105. $this->error('账户已经注销');
  106. }
  107. if ($user['status'] != 1) {
  108. $this->error(__('Account is locked'));
  109. }
  110. //如果已经有账号则直接登录
  111. $ret = $this->auth->direct($user['id']);
  112. } else {
  113. $ret = $this->auth->ios_register($iosUserId);
  114. }
  115. if ($ret) {
  116. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  117. } else {
  118. $this->error($this->auth->getError());
  119. }
  120. }
  121. /*
  122. * 修改用户的坐标
  123. * */
  124. public function change_longlat(){
  125. $longitude = input('longitude',0);
  126. $latitude = input('latitude',0);
  127. if(empty($longitude) || empty($latitude)){
  128. $this->error();
  129. }
  130. $data = [
  131. 'longitude' => $longitude,
  132. 'latitude' => $latitude,
  133. ];
  134. Db::name('user')->where('id',$this->auth->id)->update($data);
  135. $this->success();
  136. }
  137. /**
  138. * 绑定用户
  139. */
  140. public function bindUser()
  141. {
  142. $invite_no = input('invite_no'); // 邀请码
  143. if (!$invite_no) {
  144. $this->error("请输入邀请码!");
  145. }
  146. $user_id = $this->auth->id;
  147. // 查询邀请码用户信息
  148. $inviteUserInfo = \app\common\model\User::where(["invite_no" => $invite_no])->find();
  149. if (!$inviteUserInfo) {
  150. $this->error("查询不到该邀请码用户信息!");
  151. }
  152. //判断邀请码用户不能是自己的下级
  153. if ($inviteUserInfo['pre_userid'] == $user_id) {
  154. throw new Exception('对方已被您邀请过');
  155. }
  156. if ($inviteUserInfo->id == $user_id) $this->error("不能邀请自己哦!");
  157. if ($inviteUserInfo->is_auth != 2) $this->error("该邀请码用户尚未完成实名认证");
  158. $res = \app\common\model\User::update(["pre_userid" => $inviteUserInfo->id], ["id" => $user_id]);
  159. if ($res) {
  160. $this->success("恭喜,绑定成功!");
  161. } else {
  162. $this->success("网络繁忙,请稍后重试!");
  163. }
  164. }
  165. /**
  166. * 注册会员
  167. *
  168. * @param string $username 用户名
  169. * @param string $password 密码
  170. * @param string $email 邮箱
  171. * @param string $mobile 手机号
  172. * @param string $code 验证码
  173. */
  174. public function register()
  175. {
  176. $username = $this->request->request('username');
  177. $password = $this->request->request('password');
  178. $mobile = $this->request->request('mobile');
  179. $code = $this->request->request('code');
  180. if (!$username || !$password) {
  181. $this->error(__('Invalid parameters'));
  182. }
  183. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  184. $this->error(__('Mobile is incorrect'));
  185. }
  186. // $ret = Sms::check($mobile, $code, 'register');
  187. // if (!$ret) {
  188. // $this->error(__('Captcha is incorrect'));
  189. // }
  190. $ret = $this->auth->register($username, $password, $mobile, []);
  191. if ($ret) {
  192. $data = ['userinfo' => $this->auth->getUserinfo()];
  193. $this->success(__('Sign up successful'), $data);
  194. } else {
  195. $this->error($this->auth->getError());
  196. }
  197. }
  198. /**
  199. * 退出登录
  200. */
  201. public function logout()
  202. {
  203. $this->auth->logout();
  204. $this->success(__('Logout successful'));
  205. }
  206. /**
  207. * 修改会员个人信息
  208. *
  209. * @ApiMethod (POST)
  210. * @param string $avatar 头像地址
  211. * @param string $username 用户名
  212. * @param string $nickname 昵称
  213. * @param string $bio 个人简介
  214. */
  215. public function profile()
  216. {
  217. $field_array = ['avatar','nickname','desc','age_id','gender','job_id','province_id','city_id','has_info'];
  218. $data = [];
  219. foreach($field_array as $key => $field){
  220. //前端传不了post,改了
  221. if(!request()->has($field,'post')){
  222. continue;
  223. }
  224. /*if(!input('?'.$field)){
  225. continue;
  226. }*/
  227. $newone = input($field);
  228. if($field == 'avatar'){
  229. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  230. }
  231. $data[$field] = $newone;
  232. }
  233. if(isset($data['invite_no']) && !empty($data['invite_no'])){
  234. $intro_user = Db::name('user')->where('invite_no',$data['invite_no'])->value('id');
  235. if(!$intro_user){
  236. $this->error('不存在的邀请人');
  237. }
  238. if(!empty($this->auth->pre_userid)){
  239. $this->error('您已经填写过邀请人');
  240. }
  241. unset($data['invite_no']);//别人的邀请码,不能改了自己的
  242. $data['pre_userid'] = $intro_user;
  243. //邀请用户注册,给邀请人奖励
  244. if(isset($data['pre_userid']) && !empty($data['pre_userid'])){
  245. /*$intro_gold = config('site.intro_newuser_gift_goldnum') ?: 0;
  246. if($intro_gold > 0){
  247. $wallet_rs = model('wallet')->lockChangeAccountRemain($data['intro_uid'],'gold',$intro_gold,63,'邀请'.$this->auth->username);
  248. if($wallet_rs['status'] === false){
  249. Db::rollback();
  250. $this->setError($wallet_rs['msg']);
  251. return false;
  252. }
  253. }*/
  254. }
  255. }
  256. if(empty($data)){
  257. $this->error('没有任何改变');
  258. }
  259. /*if (!empty($data['nickname']) && !empty($data['avatar']) && !empty($data['age_id']) && $data['has_info']==0) {
  260. $data['has_info'] = 1;
  261. }*/
  262. Db::startTrans();
  263. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  264. if($update_rs === false){
  265. Db::rollback();
  266. $this->error('修改资料失败');
  267. }
  268. Db::commit();
  269. $this->success();
  270. }
  271. /**
  272. * 修改手机号
  273. *
  274. * @param string $mobile 手机号
  275. * @param string $captcha 验证码
  276. */
  277. public function changemobile()
  278. {
  279. $user = $this->auth->getUser();
  280. $mobile = $this->request->request('mobile');
  281. $captcha = $this->request->request('captcha');
  282. if (!$mobile || !$captcha) {
  283. $this->error(__('Invalid parameters'));
  284. }
  285. if (!Validate::regex($mobile, "^1\d{10}$")) {
  286. $this->error(__('Mobile is incorrect'));
  287. }
  288. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  289. $this->error(__('Mobile already exists'));
  290. }
  291. $result = Sms::check($mobile, $captcha, 'changeMobile');
  292. if (!$result) {
  293. $this->error(__('Captcha is incorrect'));
  294. }
  295. $verification = $user->verification;
  296. $verification->mobile = 1;
  297. $user->verification = $verification;
  298. $user->mobile = $mobile;
  299. $user->save();
  300. Sms::flush($mobile, 'changeMobile');
  301. $this->success("手机号更换成功!");
  302. }
  303. /**
  304. * 第三方登录
  305. *
  306. * @param string $platform 平台名称
  307. * @param string $code Code码
  308. */
  309. public function third()
  310. {
  311. $url = url('user/index');
  312. $platform = $this->request->request("platform");
  313. $code = $this->request->request("code");
  314. $config = get_addon_config('third');
  315. if (!$config || !isset($config[$platform])) {
  316. $this->error(__('Invalid parameters'));
  317. }
  318. $app = new \addons\third\library\Application($config);
  319. //通过code换access_token和绑定会员
  320. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  321. if ($result) {
  322. $loginret = \addons\third\library\Service::connect($platform, $result);
  323. if ($loginret) {
  324. $data = [
  325. 'userinfo' => $this->auth->getUserinfo(),
  326. 'thirdinfo' => $result
  327. ];
  328. $this->success(__('Logged in successful'), $data);
  329. }
  330. }
  331. $this->error(__('Operation failed'), $url);
  332. }
  333. /**
  334. * 重置密码
  335. *
  336. * @param string $mobile 手机号
  337. * @param string $newpassword 新密码
  338. * @param string $captcha 验证码
  339. */
  340. public function resetpwd()
  341. {
  342. $type = $this->request->request("type");
  343. $mobile = $this->request->request("mobile");
  344. $email = $this->request->request("email");
  345. $newpassword = $this->request->request("newpassword");
  346. $captcha = $this->request->request("captcha");
  347. if (!$newpassword || !$captcha) {
  348. $this->error(__('Invalid parameters'));
  349. }
  350. if ($type == 'mobile') {
  351. if (!Validate::regex($mobile, "^1\d{10}$")) {
  352. $this->error(__('Mobile is incorrect'));
  353. }
  354. $user = \app\common\model\User::getByMobile($mobile);
  355. if (!$user) {
  356. $this->error(__('User not found'));
  357. }
  358. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  359. if (!$ret) {
  360. $this->error(__('Captcha is incorrect'));
  361. }
  362. Sms::flush($mobile, 'resetpwd');
  363. } else {
  364. if (!Validate::is($email, "email")) {
  365. $this->error(__('Email is incorrect'));
  366. }
  367. $user = \app\common\model\User::getByEmail($email);
  368. if (!$user) {
  369. $this->error(__('User not found'));
  370. }
  371. $ret = Ems::check($email, $captcha, 'resetpwd');
  372. if (!$ret) {
  373. $this->error(__('Captcha is incorrect'));
  374. }
  375. Ems::flush($email, 'resetpwd');
  376. }
  377. //模拟一次登录
  378. $this->auth->direct($user->id);
  379. $ret = $this->auth->changepwd($newpassword, '', true);
  380. if ($ret) {
  381. $this->success(__('Reset password successful'));
  382. } else {
  383. $this->error($this->auth->getError());
  384. }
  385. }
  386. /**
  387. * 设置密码
  388. * @param string $newpassword 新密码
  389. * @param string $newpassword 新密码
  390. */
  391. public function setpwd()
  392. {
  393. $params = $this->request->param();
  394. $validate = new \app\api\validate\User();
  395. $result = $validate->scene('setPwd')->check($params);
  396. if (!$result) {
  397. $this->error($validate->getError());
  398. }
  399. $ret = $this->auth->changepwd($params['password'], '', true);
  400. if ($ret) {
  401. $this->success(__('Set password successful'));
  402. } else {
  403. $this->error($this->auth->getError());
  404. }
  405. }
  406. /**
  407. * 修改密码
  408. *
  409. * @param string $mobile 手机号
  410. * @param string $newpassword 新密码
  411. * @param string $captcha 验证码
  412. */
  413. public function changepwd()
  414. {
  415. $params = $this->request->param();
  416. $validate = new \app\api\validate\User();
  417. $result = $validate->scene('changePwd')->check($params);
  418. if (!$result) {
  419. $this->error($validate->getError());
  420. }
  421. $mobile = $this->request->request("mobile");
  422. $newpassword = $this->request->request("password");
  423. $captcha = $this->request->request("captcha");
  424. $user = \app\common\model\User::getByMobile($mobile);
  425. if (!$user) {
  426. $this->error(__('User not found'));
  427. }
  428. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  429. if (!$ret) {
  430. $this->error(__('Captcha is incorrect'));
  431. }
  432. Sms::flush($mobile, 'resetpwd');
  433. $ret = $this->auth->changepwd($newpassword, '', true);
  434. if ($ret) {
  435. $this->success(__('Change password successful'));
  436. } else {
  437. $this->error($this->auth->getError());
  438. }
  439. }
  440. /**
  441. * 获取用户openid
  442. */
  443. public function getUserOpenid()
  444. {
  445. $code = $this->request->param('code');// code值
  446. if (!$code) {
  447. $this->error(__('Invalid parameters'));
  448. }
  449. $config = config("wxMiniProgram");
  450. $getopenid = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config["appid"] . "&secret=" . $config["secret"] . "&js_code=" . $code . "&grant_type=authorization_code";
  451. $openidInfo = $this->getJson($getopenid);
  452. if (!isset($openidInfo["openid"])) {
  453. $this->error("用户openid获取失败", $openidInfo);
  454. }
  455. // 获取的结果存入数据库
  456. $sessionkeyModel = new \app\common\model\UserSessionkey();
  457. if ($sessionkeyModel->where(["openid" => $openidInfo["openid"]])->find()) {
  458. $update = [];
  459. $update["sessionkey"] = $openidInfo["session_key"];
  460. $res = $sessionkeyModel->update($update, ["openid" => $openidInfo["openid"]]);
  461. } else {
  462. $insert = [];
  463. $insert["sessionkey"] = $openidInfo["session_key"];
  464. $insert["openid"] = $openidInfo["openid"];
  465. $insert["createtime"] = time();
  466. $res = $sessionkeyModel->insert($insert);
  467. }
  468. if ($res) {
  469. $this->success("获取成功!", $openidInfo);
  470. } else {
  471. $this->error("获取失败!");
  472. }
  473. }
  474. /**
  475. * 微信小程序登录
  476. */
  477. public function wxMiniProgramLogin()
  478. {
  479. $openid = $this->request->param('openid');// openid值
  480. $encryptedData = $this->request->param('encryptedData');// 加密数据
  481. $iv = $this->request->param('iv');// 加密算法
  482. $signature = $this->request->param('signature');// 签名验证
  483. $rawData = $this->request->param('rawData');// 签名验证
  484. $logintype = $this->request->param('loginType', 1);// 登录方式:1=手机号,2=微信授权openid
  485. if (!$openid || !$encryptedData || !$iv) {
  486. $this->error(__('Invalid parameters'));
  487. }
  488. $encryptedData = urldecode($encryptedData);
  489. $config = config("wxMiniProgram");
  490. // 获取openid和sessionkey
  491. $sessionkeyModel = new \app\common\model\UserSessionkey();
  492. $openidInfo = $sessionkeyModel->where(["openid" => $openid])->find();
  493. $openid = $openidInfo['openid'];
  494. $session_key = $openidInfo['sessionkey'];
  495. // // 数据签名校验
  496. // $signature2 = sha1($rawData . $session_key);
  497. // if ($signature != $signature2) {
  498. // $this->error(__('数据签名验证失败'));
  499. // }
  500. // 根据加密数据和加密算法获取用户信息
  501. $pc = new WXBizDataCrypt($config["appid"], $session_key);
  502. $data = "";
  503. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  504. if ($errCode == 0) {
  505. $data = json_decode($data, true);
  506. // 用户登录逻辑 === 开始
  507. $userModel = new \app\common\model\User();
  508. $auth = \app\common\library\Auth::instance();
  509. if ($logintype == 1) { // 手机号登录
  510. $userInfo = $userModel->where(["mobile" => $data["purePhoneNumber"]])->find();
  511. // 用户信息不存在时使用
  512. $extend = ["mobile" => $data["purePhoneNumber"]];
  513. } else { // 微信授权openid登录
  514. $userInfo = $userModel->where(["openid" => $openid])->find();
  515. // 用户信息不存在时使用
  516. $extend = [
  517. 'openid' => $data['openId'],
  518. 'nickname' => $data['nickName'],
  519. 'avatar' => $data['avatarUrl'],
  520. 'gender' => $data['gender'],
  521. ];
  522. }
  523. // 判断用户是否已经存在
  524. if ($userInfo) { // 登录
  525. $user = \app\common\model\User::get($userInfo["id"]);
  526. if (!$user) {
  527. $this->error("网络错误!请稍后重试");
  528. }
  529. $user->save(["logintime" => time()]);
  530. $res = $auth->direct($user->id);
  531. $is_register = 0;
  532. } else { // 注册
  533. // 先随机一个用户名,随后再变更为u+数字id
  534. $username = Random::alnum(20);
  535. $password = Random::alnum(6);
  536. Db::startTrans();
  537. try {
  538. // 默认注册一个会员
  539. $result = $auth->register($username, $password, "", $extend);
  540. if (!$result) {
  541. return false;
  542. }
  543. $user = $auth->getUser();
  544. $fields = ['username' => 'u' . $user->id];
  545. // 更新会员资料
  546. $user = \app\common\model\User::get($user->id);
  547. $user->save($fields);
  548. Db::commit();
  549. } catch (PDOException $e) {
  550. Db::rollback();
  551. $auth->logout();
  552. return false;
  553. }
  554. // 写入登录Cookies和Token
  555. $res = $auth->direct($user->id);
  556. $is_register = 1;
  557. }
  558. $userInfo = $auth->getUserinfo();
  559. $userInfo["is_register"] = $is_register;
  560. if ($res) {
  561. $this->success("登录成功!", $userInfo);
  562. } else {
  563. $this->error("登录失败!");
  564. }
  565. // 用户登录逻辑 === 结束
  566. } else {
  567. $this->error("解密失败!", ["code" => $errCode]);
  568. }
  569. }
  570. /**
  571. * json 请求
  572. * @param $url
  573. * @return mixed
  574. */
  575. private function getJson($url)
  576. {
  577. $ch = curl_init();
  578. curl_setopt($ch, CURLOPT_URL, $url);
  579. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  580. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  581. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  582. $output = curl_exec($ch);
  583. curl_close($ch);
  584. return json_decode($output, true);
  585. }
  586. /**
  587. * 运营商一键登录
  588. */
  589. public function onLogin()
  590. {
  591. $accessToken = $this->request->param('accessToken');// 运营商预取号获取到的token
  592. $token = $this->request->param('tokenT');// 易盾返回的token
  593. if (!$accessToken || !$token) {
  594. $this->error("参数获取失败!");
  595. }
  596. $params = array(
  597. // 运营商预取号获取到的token
  598. "accessToken" => $accessToken,
  599. // 易盾返回的token
  600. "token" => $token
  601. );
  602. // 获取密钥配置
  603. $configInfo = config("onLogin");
  604. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  605. $ret = $onlogin->check($params);
  606. // $ret = [];
  607. // $ret["code"] = 200;
  608. // $ret["msg"] = "ok";
  609. // $ret["data"] = [
  610. // "phone" => "17574504021",
  611. // "resultCode" => 0
  612. // ];
  613. if ($ret["code"] == 200) {
  614. $data = $ret["data"];
  615. $phone = $data["phone"];
  616. if (empty($phone)) {
  617. // 取号失败,建议进行二次验证,例如短信验证码
  618. $this->error("取号登录失败,请用验证码方式登录!");
  619. } else {
  620. // 取号成功, 执行登录等流程
  621. // 用户登录逻辑 === 开始
  622. $userModel = new \app\common\model\User();
  623. $auth = \app\common\library\Auth::instance();
  624. $userInfo = $userModel->where(["mobile" => $phone])->find();
  625. // 用户信息不存在时使用
  626. $extend = ["mobile" => $phone];
  627. // 判断用户是否已经存在
  628. if ($userInfo) { // 登录
  629. $user = \app\common\model\User::get($userInfo["id"]);
  630. if (!$user) {
  631. $this->error("网络错误!请稍后重试");
  632. }
  633. if ($user->status != 'normal') {
  634. $this->error(__('Account is locked'));
  635. }
  636. $user->save(["logintime" => time()]);
  637. $res = $auth->direct($user->id);
  638. $is_register = 0;
  639. } else { // 注册
  640. // 先随机一个用户名,随后再变更为u+数字id
  641. $username = Random::alnum(20);
  642. $password = Random::alnum(6);
  643. Db::startTrans();
  644. try {
  645. // 默认注册一个会员
  646. $result = $auth->register($username, $password, "", $extend);
  647. if (!$result) {
  648. return false;
  649. }
  650. $user = $auth->getUser();
  651. $fields = ['username' => 'u' . $user->id];
  652. // 更新会员资料
  653. $user = \app\common\model\User::get($user->id);
  654. $user->save($fields);
  655. Db::commit();
  656. } catch (PDOException $e) {
  657. Db::rollback();
  658. $auth->logout();
  659. return false;
  660. }
  661. // 写入登录Cookies和Token
  662. $res = $auth->direct($user->id);
  663. $is_register = 1;
  664. }
  665. $userInfo["userinfo"] = $auth->getUserinfo();
  666. $userInfo["is_register"] = $is_register;
  667. if ($res) {
  668. $this->success("登录成功!", $userInfo);
  669. } else {
  670. $this->error("登录失败!");
  671. }
  672. // 用户登录逻辑 === 结束
  673. }
  674. } else {
  675. $this->error("登录失败,请用验证码方式登录!");
  676. }
  677. }
  678. /**
  679. * 注销账号
  680. *
  681. * @param string $mobile 手机号
  682. * @param string $captcha 验证码
  683. */
  684. public function cancleUser()
  685. {
  686. $user = $this->auth->getUser();
  687. $user->status = -1;
  688. //unset($user->power);
  689. $user->save();
  690. $this->auth->logout();
  691. $this->success("账号注销成功!");
  692. }
  693. /**
  694. * 获取昵称
  695. * @return string
  696. */
  697. public function getNickName()
  698. {
  699. $result['nickname'] = get_rand_nick_name();
  700. $this->success('获取成功',$result);
  701. }
  702. //微信登录,预先假注册
  703. public function wechatlogin(){
  704. $code = $this->request->param('code','');
  705. if(!$code){
  706. $this->error(__('Invalid parameters'));
  707. }
  708. //微信
  709. $wechat = new Wechat();
  710. $wxuserinfo = $wechat->getwxuserinfo($code);
  711. Log::error('code:'.$code.',wxuserinfo:'.json_encode($wxuserinfo));
  712. if(!$wxuserinfo){
  713. $this->error('openid获取失败');
  714. }
  715. $openid = $wxuserinfo['openid'];
  716. $user = Db::name('user')->where('openid',$openid)->find();
  717. if ($user) {
  718. if ($user['status'] == -1) {
  719. $this->error('账户已注销');
  720. }
  721. if ($user['status'] != 1) {
  722. $this->error(__('Account is locked'));
  723. }
  724. //如果已经有账号则直接登录
  725. $ret = $this->auth->direct($user['id']);
  726. $is_register = 0;
  727. $userInfo = $this->auth->getUserinfo();
  728. } else {
  729. //记录code和openid,绑定手机号的时候更新openid
  730. $wechatCodeData = [
  731. 'code' => $code,
  732. 'openid' => $openid,
  733. 'createtime' => time(),
  734. ];
  735. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  736. if (empty($wechatCode)) {
  737. Db::name('wechat_code')->insertGetId($wechatCodeData);
  738. } else {
  739. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  740. }
  741. $ret = true;
  742. $is_register = 1;
  743. $userInfo = [];
  744. $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
  745. $this->success('获取信息成功', $data, 2);
  746. }
  747. if ($ret) {
  748. $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
  749. $this->success(__('Logged in successful'), $data);
  750. } else {
  751. $this->error($this->auth->getError());
  752. }
  753. }
  754. //获取openid
  755. public function getopenid() {
  756. //code
  757. $code = $this->request->post('code', '', 'trim');// code值
  758. if (!$code) {
  759. $this->error(__('Invalid parameters'));
  760. }
  761. $config = config('wxMiniProgram');
  762. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  763. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  764. $openidInfo = json_decode($openidInfo,true);
  765. if(!isset($openidInfo['openid'])) {
  766. $this->error('用户openid获取失败', $openidInfo);
  767. }
  768. $user = Db::name('user')->where('mini_openid',$openidInfo['openid'])->find();
  769. //$openidInfo['mobile'] = isset($user['mobile']) ? $user['mobile'] : '';
  770. $this->success('获取成功', $openidInfo);
  771. }
  772. //生成海报
  773. public function createposter() {
  774. // $image = input('image', '', 'trim');
  775. // if (!$image) {
  776. // $this->error('您的网络开小差啦~');
  777. // }
  778. $image = config('site.intro_imges');
  779. $haibao = $this->haibao($this->auth->id,['invite_no'=>$this->auth->invite_no, 'background' => $image]);
  780. return $haibao;
  781. // $this->success('success', $haibao);
  782. }
  783. //海报
  784. public function haibao($player_id,$data){
  785. //下载页二维码,没必要保留
  786. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  787. $params = [
  788. 'text' => $httpStr.'/index/index/download?code=' . $data['invite_no'],
  789. 'size' => 90,
  790. 'logo' => false,
  791. 'label' => false,
  792. 'padding' => 0,
  793. ];
  794. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  795. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  796. mk_dir($qrcode_path);
  797. $download_qrcode = $qrcode_path.'/download'.$player_id.'.png';
  798. $qrCode->writeFile($download_qrcode);
  799. //海报
  800. $result['url'] = $this->createhaibao($download_qrcode,$player_id,$data);
  801. $this->success('获取成功',$result);
  802. }
  803. public function createhaibao($download_qrcode,$player_id,$sub_data){
  804. //二维码
  805. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  806. $download_qrcode= $httpStr.'/'.$download_qrcode;
  807. $data = [
  808. /*[
  809. "left" => "15px",
  810. "top" => "400px",
  811. "type" => "img",
  812. "width" => "58px",
  813. "height" => "58px",
  814. "src" => one_domain_image($this->auth->avatar),
  815. ],
  816. [
  817. "left" => "81px",
  818. "top" => "400px",
  819. "type" => "nickname",
  820. "width" => "80px",
  821. "height" => "24px",
  822. "size" => "12px",
  823. "color" => "#000",
  824. "content" => $this->auth->nickname
  825. ],
  826. [
  827. "left" => "81px",
  828. "top" => "400px",
  829. "type" => "nickname",
  830. "width" => "80px",
  831. "height" => "24px",
  832. "size" => "12px",
  833. "color" => "#000",
  834. "content" => $this->auth->invite_no
  835. ],*/
  836. [
  837. "left" => "18px",
  838. "top" => "410px",
  839. "type" => "text",
  840. "width" => "80px",
  841. "height" => "24px",
  842. "size" => "16px",
  843. "color" => "#123354",
  844. "content" => 'GG语音'
  845. ],
  846. [
  847. "left" => "18px",
  848. "top" => "445px",
  849. "type" => "text",
  850. "width" => "80px",
  851. "height" => "24px",
  852. "size" => "10px",
  853. "color" => "#123354",
  854. "content" => '更多现金奖励等你来拿'
  855. ],
  856. [
  857. "left" => "210px",
  858. "top" => "385px",
  859. "type" => "img",
  860. "width" => "95px",
  861. "height" => "95px",
  862. "src" => $download_qrcode//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  863. ]
  864. ];
  865. $data = json_encode($data, 320);
  866. $poster = [
  867. 'id' => $player_id,
  868. 'title' => 'GG语音',
  869. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  870. 'bg_image' => $sub_data['background'] ? cdnurl($sub_data['background']) : '/assets/img/inviteposter.png',
  871. 'data' => $data,
  872. 'status' => 'normal',
  873. 'weigh' => 0,
  874. 'createtime' => 1653993709,
  875. 'updatetime' => 1653994259,
  876. ];
  877. $image = new \addons\poster\library\Image();
  878. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  879. if (!$imgurl) {
  880. $this->error('生成海报出错');
  881. }
  882. // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  883. return $httpStr.'/' . $imgurl;
  884. }
  885. //申请真人认证
  886. public function realauth()
  887. {
  888. try {
  889. $realName = $this->request->param('real_name','');
  890. $idCard = $this->request->param('id_card','');
  891. if ($this->auth->is_auth == 2) {
  892. $this->error('您已经真人认证过了~');
  893. }
  894. if (empty($realName)) {
  895. throw new Exception('请输入姓名');
  896. }
  897. if (empty($idCard)) {
  898. throw new Exception('请输入身份证号');
  899. }
  900. $userAuthWhere['user_id'] = ['neq',$this->auth->id];
  901. $userAuthWhere['idcard'] = $idCard;
  902. // 获取用户已经实名认证的数量
  903. $authenticatedCount = model('UserAuth')->where(['idcard' => $idCard])->count();
  904. // 如果已经实名认证的数量不超过3个
  905. if ($authenticatedCount > 3) {
  906. throw new Exception('您当前身份证号已经认证过3次');
  907. }
  908. /*$userAuth = model('UserAuth')->where($userAuthWhere)->find();
  909. if (!empty($userAuth)) {
  910. throw new Exception('该身份证号已被其他用户实名验证过');
  911. }*/
  912. /*$userService = new UserService();
  913. $faceParams = [
  914. 'real_name' => $realName,
  915. 'id_card' => $idCard,
  916. 'user_id' => $this->auth->id,
  917. ];
  918. $res = $userService->faceAuth($faceParams);
  919. if (!$res['status']) {
  920. $this->error('您的网络开小差啦5~');
  921. }
  922. $rs = $res['data'];
  923. if (!$rs || $rs['code'] != 0) {
  924. $this->error('您的网络开小差啦6~');
  925. }
  926. $user_auth = [
  927. 'user_id' => $this->auth->id,
  928. 'realname' => $realName,
  929. 'idcard' => $idCard,
  930. 'certify_id' => isset($rs['result']['faceId']) ? $rs['result']['faceId'] : '',
  931. 'out_trade_no' => isset($rs['result']['orderNo']) ? $rs['result']['orderNo'] : '',
  932. 'status' => 0,
  933. 'createtime' => time(),
  934. 'updatetime' => time()
  935. ];*/
  936. $userService = new UserService();
  937. $aliParams = [
  938. 'id_card' => $idCard,
  939. 'real_name' => $realName,
  940. ];
  941. $aliCheckRes = $userService->aliCheck($aliParams);
  942. if (!$aliCheckRes['status']) {
  943. throw new Exception($aliCheckRes['msg']);
  944. }
  945. $user_auth = [
  946. 'user_id' => $this->auth->id,
  947. 'realname' => $realName,
  948. 'idcard' => $idCard,
  949. 'certify_id' => '',
  950. 'out_trade_no' => '',
  951. 'status' => 1,
  952. 'createtime' => time(),
  953. ];
  954. //开启事务
  955. Db::startTrans();
  956. //查询是否认证过
  957. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  958. if ($info) {
  959. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  960. } else {
  961. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  962. }
  963. if (!$auth_rs) {
  964. Db::rollback();
  965. $this->error('您的网络开小差啦7~');
  966. }
  967. //修改用户表认证状态
  968. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', 2);
  969. if ($user_rs === false) {
  970. Db::rollback();
  971. $this->error('您的网络开小差啦8~');
  972. }
  973. Db::commit();
  974. /*$return_data = [
  975. 'face_id' => $user_auth['certify_id'],
  976. 'order_no' => $user_auth['out_trade_no'],
  977. 'user_id' => (string)$this->auth->id,
  978. 'nonce' => $rs['nonce'],
  979. 'sign' => $rs['sign'],
  980. ];*/
  981. $this->success('验证成功');
  982. } catch (Exception $e) {
  983. $this->error($e->getMessage());
  984. }
  985. }
  986. //查询真人认证结果
  987. public function getrealauthresult() {
  988. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  989. if (!$user_auth) {
  990. $this->success('尚未认证');
  991. }
  992. if ($user_auth['status'] == 1) {
  993. $this->success('真人认证通过');
  994. }
  995. if (!$user_auth['certify_id']) {
  996. $this->success('请先进行真人认证');
  997. }
  998. $tencentConfig = config('tencent_yun');
  999. $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
  1000. $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
  1001. //获取token
  1002. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
  1003. $token_result = file_get_contents($token_url);
  1004. if (!$token_result) {
  1005. $this->error('您的网络开小差啦1~');
  1006. }
  1007. $token_result = json_decode($token_result, true);
  1008. if ($token_result['code'] != 0) {
  1009. $this->error('您的网络开小差啦2~');
  1010. }
  1011. $token = $token_result['access_token'];
  1012. //获取签名鉴权参数ticket
  1013. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
  1014. $ticket_result = file_get_contents($ticket_url);
  1015. if (!$ticket_result) {
  1016. $this->error('您的网络开小差啦3~');
  1017. }
  1018. $ticket_result = json_decode($ticket_result, true);
  1019. if ($ticket_result['code'] != 0) {
  1020. $this->error('您的网络开小差啦4~');
  1021. }
  1022. $ticket = $ticket_result['tickets'][0]['value'];
  1023. //获取签名
  1024. $sign_data = [
  1025. 'wbappid' => $sercrtId,
  1026. 'orderNo' => $user_auth['out_trade_no'],
  1027. 'version' => '1.0.0',
  1028. 'ticket' => $ticket,
  1029. 'nonce' => Random::alnum(32)
  1030. ];//p($sign_data);
  1031. asort($sign_data); //p($sign_data);//排序
  1032. $sign_string = join('', $sign_data);//p($sign_string);
  1033. $sign = sha1($sign_string);//p($sign);
  1034. //人脸核身结果查询
  1035. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  1036. $data = [
  1037. 'appId' => $sercrtId,
  1038. 'version' => '1.0.0',
  1039. 'nonce' => $sign_data['nonce'],
  1040. 'orderNo' => $user_auth['out_trade_no'],
  1041. 'sign' => $sign
  1042. ];
  1043. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1044. if (!$rs) {
  1045. $this->error('您的网络开小差啦5~');
  1046. }
  1047. $rs = json_decode($rs, true);
  1048. if (!$rs || $rs['code'] != 0) {
  1049. $this->error($rs['msg']);
  1050. }
  1051. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  1052. $edit_data['status'] = 2;
  1053. $msg = '真人认证成功';
  1054. } else {
  1055. $edit_data['status'] = -1;
  1056. $edit_data['certify_id'] = '';
  1057. $edit_data['out_trade_no'] = '';
  1058. $msg = '真人认证失败';
  1059. }
  1060. $edit_data['updatetime'] = time();
  1061. //开启事务
  1062. Db::startTrans();
  1063. //修改认证信息
  1064. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  1065. if (!$result) {
  1066. Db::rollback();
  1067. $this->error('查询认证结果失败2');
  1068. }
  1069. //修改用户信息
  1070. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', $edit_data['status']);
  1071. if (!$rs) {
  1072. Db::rollback();
  1073. $this->error('查询认证结果失败3');
  1074. }
  1075. if ($edit_data['status'] == 2) { //通过
  1076. //tag任务赠送金币
  1077. //真人认证奖励
  1078. /*$task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  1079. if($task_rs === false){
  1080. Db::rollback();
  1081. $this->error('完成任务赠送奖励失败');
  1082. }*/
  1083. //系统消息
  1084. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  1085. } else {
  1086. //系统消息
  1087. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  1088. }
  1089. Db::commit();
  1090. $this->success($msg);
  1091. }
  1092. /**
  1093. * 微信注册来的,绑定手机号
  1094. *
  1095. * @ApiMethod (POST)
  1096. * @param string $mobile 手机号
  1097. * @param string $captcha 验证码
  1098. */
  1099. public function bindmobile()
  1100. {
  1101. Db::startTrans();
  1102. try {
  1103. $code = $this->request->param('code');
  1104. $mobile = $this->request->param('mobile');
  1105. $captcha = $this->request->param('captcha');
  1106. if (!$mobile || !$captcha || !$code) {
  1107. throw new Exception(__('Invalid parameters'));
  1108. }
  1109. if (!Validate::regex($mobile, "^1\d{10}$")) {
  1110. throw new Exception(__('Mobile is incorrect'));
  1111. }
  1112. $result = Sms::check($mobile, $captcha, 'changemobile');
  1113. if (!$result) {
  1114. throw new Exception(__('Captcha is incorrect'));
  1115. }
  1116. $where['mobile'] = $mobile;
  1117. $userData = model('User')->where($where)->find();//老用户
  1118. $wechatCodeWhere['code'] = $code;
  1119. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  1120. if (empty($wechatCode)) {
  1121. throw new Exception('请先微信登录');
  1122. }
  1123. if (!empty($userData)) {
  1124. if (empty($userData['openid'])) {
  1125. model('User')->update(['openid' => $wechatCode['openid']],$where);//老用户更新openid
  1126. } else {
  1127. if ($userData['openid'] != $wechatCode['openid']) {
  1128. throw new Exception('该手机号已被其他用户绑定');
  1129. }
  1130. }
  1131. $ret = $this->auth->direct($userData['id']);
  1132. } else {
  1133. $extend = [
  1134. 'openid' => $wechatCode['openid'],
  1135. ];
  1136. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
  1137. }
  1138. if (!$ret) {
  1139. throw new Exception($this->auth->getError());
  1140. }
  1141. Sms::flush($mobile, 'changemobile');
  1142. Db::commit();
  1143. $this->success('success',$this->userInfo('return'));
  1144. } catch (Exception $e) {
  1145. Db::rollback();
  1146. $this->error($e->getMessage());
  1147. }
  1148. }
  1149. /**
  1150. * 手机注册来的,绑定微信
  1151. *
  1152. * @ApiMethod (POST)
  1153. * @param string $wechat_openid
  1154. */
  1155. public function bindopenid()
  1156. {
  1157. Db::startTrans();
  1158. try {
  1159. $code = $this->request->param('code','');
  1160. if(!$code){
  1161. throw new Exception(__('Invalid parameters'));
  1162. }
  1163. //微信
  1164. $wechat = new Wechat();
  1165. $openid = $wechat->getOpenid($code);
  1166. if(!$openid){
  1167. throw new Exception('openid获取失败');
  1168. }
  1169. $user = model('User')->find($this->auth->id);
  1170. if(!empty($user['openid']) && $openid != $user['openid']){
  1171. throw new Exception('已经绑定了微信号');
  1172. }
  1173. $otherUserWhere['openid'] = $openid;
  1174. $otherUserWhere['id'] = ['neq',$this->auth->id];
  1175. $otherUser = model('User')->where($otherUserWhere)->find();
  1176. if (!empty($otherUser)) {
  1177. throw new Exception('该微信已被其他用户绑定过');
  1178. }
  1179. $user->openid = $openid;
  1180. $userRes = $user->save();
  1181. if (!$userRes) {
  1182. throw new Exception('绑定微信失败');
  1183. }
  1184. Db::commit();
  1185. $this->success('success',$this->userInfo('return'));
  1186. } catch (Exception $e) {
  1187. Db::rollback();
  1188. $this->error($e->getMessage());
  1189. }
  1190. }
  1191. //用户详细资料
  1192. public function userInfo($type = 1){
  1193. $info = $this->auth->getUserinfo();
  1194. if($type == 'return'){
  1195. return $info;
  1196. }
  1197. $this->success(__('success'),$info);
  1198. }
  1199. /**
  1200. * 微信解绑
  1201. * @return void
  1202. */
  1203. public function clearopenid()
  1204. {
  1205. try {
  1206. $userId = $this->auth->id;
  1207. $where['id'] = $userId;
  1208. $data['openid'] = '';
  1209. $user = model('User')->update($data,$where);
  1210. $this->success('解绑成功');
  1211. } catch (Exception $e) {
  1212. $this->error($e->getMessage());
  1213. }
  1214. }
  1215. }