Wechat.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\validate\Admin as AdminValidate;
  5. use app\common\controller\Backend;
  6. use think\App;
  7. use think\db\exception\PDOException;use think\facade\Config;
  8. use think\facade\Db;use think\Request;
  9. use think\exception\ValidateException;
  10. class Wechat extends Backend
  11. {
  12. protected $noNeedLogin=["wxlogin"];
  13. protected $noNeedPermission=["wxinfo","wxedit"];
  14. public function __construct(App $app) {parent::__construct($app);}
  15. public function initialize(){
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. }
  18. /**
  19. * 显示资源列表
  20. *
  21. * @return \think\Response
  22. */
  23. public function wxlogin()
  24. {
  25. $openMemberCenter = Config::get('buildadmin.open_member_center');
  26. if (!$openMemberCenter) {
  27. $this->error(__('Member center disabled'));
  28. }
  29. // 检查登录态
  30. if ($this->auth->isLogin()) {
  31. $this->success(__('You have already logged in. There is no need to log in again~'), [
  32. 'routePath' => '/user'
  33. ], 302);
  34. }
  35. if ($this->request->isPost()) {
  36. $params = $this->request->post(['openid','unionid',"nickname"=>"","avatar"=>"","mobile"=>"", "keep",
  37. 'registerType'=>"wx"]);
  38. $validate = new AdminValidate();
  39. try {
  40. $validate->scene("wechat")->check($params);
  41. } catch (ValidateException $e){
  42. $this->error($e->getMessage());
  43. }
  44. $res = $this->auth->isWxUser($params['openid'], $params['unionid'], (bool)$params['keep']);
  45. if (!$res) {
  46. $res = $this->auth->WxRegister( $params['openid'], $params['unionid'], $params['nickname'], $params['mobile'], $params['avatar']);
  47. }
  48. if (isset($res) && $res === true) {
  49. $this->success(__('Login succeeded!'), [
  50. 'userInfo' =>$this->auth->getInfo(),
  51. 'routePath' => '/user'
  52. ]);
  53. } else {
  54. $msg = $this->auth->getError();
  55. $msg = $msg ?: __('Check in failed, please try again or contact the website administrator~');
  56. $this->error($msg);
  57. }
  58. }
  59. $this->success('', [
  60. 'accountVerificationType' => get_account_verification_type()
  61. ]);
  62. }
  63. //微信端用户信息
  64. public function wxinfo(){
  65. $this->success('', $this->auth->getInfo());
  66. }
  67. //微信端用户信息补充
  68. public function wxedit(){
  69. $data = $this->request->post();
  70. if (!$data) {
  71. $this->error(__('Parameter %s can not be empty', ['']));
  72. }
  73. // 数据验证
  74. if ($this->modelValidate) {
  75. try {
  76. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->auth->getAdmin()));
  77. $validate = new $validate;
  78. $validate->scene('wxedit')->check($data);
  79. } catch (ValidateException $e) {
  80. $this->error($e->getMessage());
  81. }
  82. }
  83. $data = $this->excludeFields($data);
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. $result = $this->auth->getAdmin()->save($data);
  88. Db::commit();
  89. } catch (PDOException|\Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. if ($result !== false) {
  94. $this->success(__('Update successful'));
  95. } else {
  96. $this->error(__('No rows updated'));
  97. }
  98. }
  99. }