User.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace addons\cms\controller\wxapp;
  3. use addons\third\library\Service;
  4. use addons\third\model\Third;
  5. use app\common\library\Auth;
  6. use fast\Http;
  7. use think\Config;
  8. use think\Validate;
  9. /**
  10. * 会员
  11. */
  12. class User extends Base
  13. {
  14. protected $noNeedLogin = ['index', 'login'];
  15. protected $token = '';
  16. public function _initialize()
  17. {
  18. $this->token = $this->request->post('token');
  19. if ($this->request->action() == 'login' && $this->token) {
  20. $this->request->post(['token' => '']);
  21. }
  22. parent::_initialize();
  23. if (!Config::get('fastadmin.usercenter')) {
  24. $this->error(__('User center already closed'));
  25. }
  26. }
  27. /**
  28. * 登录
  29. */
  30. public function login()
  31. {
  32. if($this->auth->isLogin()){
  33. $this->success("登录成功", ['userInfo' => $this->getUserInfo()]);
  34. }
  35. $config = get_addon_config('cms');
  36. $code = $this->request->post("code");
  37. $rawData = $this->request->post("rawData", '', 'trim');
  38. if (!$code || !$rawData) {
  39. $this->error("参数不正确");
  40. }
  41. $third = get_addon_info('third');
  42. if (!$third || !$third['state']) {
  43. $this->error("请在后台插件管理安装并配置第三方登录插件");
  44. }
  45. $userInfo = (array)json_decode($rawData, true);
  46. $params = [
  47. 'appid' => $config['wxappid'],
  48. 'secret' => $config['wxappsecret'],
  49. 'js_code' => $code,
  50. 'grant_type' => 'authorization_code'
  51. ];
  52. $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
  53. if ($result['ret']) {
  54. $json = (array)json_decode($result['msg'], true);
  55. if (isset($json['openid'])) {
  56. //如果有传Token
  57. if ($this->token) {
  58. $this->auth->init($this->token);
  59. //检测是否登录
  60. if ($this->auth->isLogin()) {
  61. $third = Third::where(['openid' => $json['openid'], 'platform' => 'wxapp'])->find();
  62. if ($third && $third['user_id'] == $this->auth->id) {
  63. $this->success("登录成功", ['userInfo' => $this->getUserInfo()]);
  64. }
  65. }
  66. }
  67. $platform = 'wechat';
  68. $result = [
  69. 'openid' => $json['openid'],
  70. 'userinfo' => [
  71. 'nickname' => $userInfo['nickName'],
  72. ],
  73. 'access_token' => $json['session_key'],
  74. 'refresh_token' => '',
  75. 'expires_in' => isset($json['expires_in']) ? $json['expires_in'] : 0,
  76. 'apptype'=>'miniapp'
  77. ];
  78. $extend = ['gender' => $userInfo['gender'], 'nickname' => $userInfo['nickName'], 'avatar' => $userInfo['avatarUrl']];
  79. $ret = Service::connect($platform, $result, $extend);
  80. if ($ret) {
  81. $this->success("登录成功", ['userInfo' => $this->getUserInfo()]);
  82. } else {
  83. $this->error("连接失败");
  84. }
  85. } else {
  86. $this->error("登录失败");
  87. }
  88. }
  89. return;
  90. }
  91. /**
  92. * 绑定账号
  93. */
  94. public function bind()
  95. {
  96. $account = $this->request->post("account");
  97. $password = $this->request->post("password");
  98. if (!$account || !$password) {
  99. $this->error("参数不正确");
  100. }
  101. $account = $this->request->post('account');
  102. $password = $this->request->post('password');
  103. $rule = [
  104. 'account' => 'require|length:3,50',
  105. 'password' => 'require|length:6,30',
  106. ];
  107. $msg = [
  108. 'account.require' => 'Account can not be empty',
  109. 'account.length' => 'Account must be 3 to 50 characters',
  110. 'password.require' => 'Password can not be empty',
  111. 'password.length' => 'Password must be 6 to 30 characters',
  112. ];
  113. $data = [
  114. 'account' => $account,
  115. 'password' => $password,
  116. ];
  117. $validate = new Validate($rule, $msg);
  118. $result = $validate->check($data);
  119. if (!$result) {
  120. $this->error(__($validate->getError()));
  121. return false;
  122. }
  123. $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
  124. $user = \app\common\model\User::get([$field => $account]);
  125. if (!$user) {
  126. $this->error('账号未找到');
  127. }
  128. $third = Third::where(['user_id' => $user->id, 'platform' => 'wxapp'])->find();
  129. if ($third) {
  130. $this->error('账号已经绑定其他小程序账号');
  131. }
  132. $third = Third::where(['user_id' => $this->auth->id, 'platform' => 'wechat','apptype'=>'miniapp'])->find();
  133. if (!$third) {
  134. $this->error('未找到登录信息');
  135. }
  136. if ($this->auth->login($account, $password)) {
  137. $third->user_id = $this->auth->id;
  138. $third->save();
  139. $this->success("绑定成功", ['userInfo' => $this->getUserInfo()]);
  140. } else {
  141. $this->error($this->auth->getError());
  142. }
  143. }
  144. /**
  145. * 个人资料
  146. */
  147. public function profile()
  148. {
  149. $user = $this->auth->getUser();
  150. $username = $this->request->post('username');
  151. $nickname = $this->request->post('nickname');
  152. $bio = $this->request->post('bio');
  153. $avatar = $this->request->post('avatar');
  154. if (!$username || !$nickname) {
  155. $this->error("用户名和昵称不能为空");
  156. }
  157. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  158. if ($exists) {
  159. $this->error(__('Username already exists'));
  160. }
  161. $avatar = str_replace(cdnurl('', true), '', $avatar);
  162. $user->username = $username;
  163. $user->nickname = $nickname;
  164. $user->bio = $bio;
  165. $user->avatar = $avatar;
  166. $user->save();
  167. $this->success('', ['userInfo' => $this->getUserInfo()]);
  168. }
  169. /**
  170. * 保存头像
  171. */
  172. public function avatar()
  173. {
  174. $user = $this->auth->getUser();
  175. $avatar = $this->request->post('avatar');
  176. if (!$avatar) {
  177. $this->error("头像不能为空");
  178. }
  179. $avatar = str_replace(cdnurl('', true), '', $avatar);
  180. $user->avatar = $avatar;
  181. $user->save();
  182. $this->success('', ['userInfo' => $this->getUserInfo()]);
  183. }
  184. /**
  185. * 获取用户信息
  186. * @return array
  187. */
  188. protected function getUserInfo()
  189. {
  190. $userinfo = $this->auth->getUserInfo();
  191. $userinfo['avatar'] = cdnurl($userinfo['avatar'], true);
  192. $vip = get_addon_info('vip');
  193. $userinfo['is_install_vip'] = ($vip && $vip['state']);
  194. if(!$userinfo['is_install_vip']){//禁用
  195. $userinfo['vip'] = 0;
  196. $userinfo['vipInfo'] = null;
  197. }else{
  198. $userinfo['vipInfo'] = \addons\vip\library\Service::getVipInfo($userinfo['id']) ?? null;
  199. if(empty($userinfo['vipInfo'])){
  200. $userinfo['vip'] = 0;
  201. }
  202. }
  203. return $userinfo;
  204. }
  205. }