User.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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']])
  62. ->where(function ($query) {
  63. $query->where(['platform' => 'wxapp'])
  64. ->whereOr(['platform' => 'wechat', 'apptype' => 'miniapp']);
  65. })
  66. ->find();
  67. if ($third && $third['user_id'] == $this->auth->id) {
  68. $this->success("登录成功", ['userInfo' => $this->getUserInfo()]);
  69. }
  70. }
  71. }
  72. $platform = 'wechat';
  73. $result = [
  74. 'openid' => $json['openid'],
  75. 'unionid' => $json['unionid'] ?? '',
  76. 'userinfo' => [
  77. 'nickname' => $userInfo['nickName'],
  78. ],
  79. 'access_token' => $json['session_key'],
  80. 'refresh_token' => '',
  81. 'expires_in' => isset($json['expires_in']) ? $json['expires_in'] : 0,
  82. 'apptype' => 'miniapp'
  83. ];
  84. $extend = ['gender' => $userInfo['gender'], 'nickname' => $userInfo['nickName'], 'avatar' => $userInfo['avatarUrl']];
  85. $ret = Service::connect($platform, $result, $extend);
  86. if ($ret) {
  87. $this->success("登录成功", ['userInfo' => $this->getUserInfo()]);
  88. } else {
  89. $this->error("连接失败");
  90. }
  91. } else {
  92. $this->error("登录失败");
  93. }
  94. }
  95. return;
  96. }
  97. /**
  98. * 绑定账号
  99. */
  100. public function bind()
  101. {
  102. $account = $this->request->post("account");
  103. $password = $this->request->post("password");
  104. if (!$account || !$password) {
  105. $this->error("参数不正确");
  106. }
  107. $account = $this->request->post('account');
  108. $password = $this->request->post('password');
  109. $rule = [
  110. 'account' => 'require|length:3,50',
  111. 'password' => 'require|length:6,30',
  112. ];
  113. $msg = [
  114. 'account.require' => 'Account can not be empty',
  115. 'account.length' => 'Account must be 3 to 50 characters',
  116. 'password.require' => 'Password can not be empty',
  117. 'password.length' => 'Password must be 6 to 30 characters',
  118. ];
  119. $data = [
  120. 'account' => $account,
  121. 'password' => $password,
  122. ];
  123. $validate = new Validate($rule, $msg);
  124. $result = $validate->check($data);
  125. if (!$result) {
  126. $this->error(__($validate->getError()));
  127. return false;
  128. }
  129. $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
  130. $user = \app\common\model\User::get([$field => $account]);
  131. if (!$user) {
  132. $this->error('账号未找到');
  133. }
  134. $third = Third::where(['user_id' => $user->id])
  135. ->where(function ($query) {
  136. $query->where(['platform' => 'wxapp'])
  137. ->whereOr(['platform' => 'wechat', 'apptype' => 'miniapp']);
  138. })
  139. ->find();
  140. if ($third) {
  141. $this->error('账号已经绑定其他小程序账号');
  142. }
  143. $third = Third::where(['user_id' => $this->auth->id])
  144. ->where(function ($query) {
  145. $query->where(['platform' => 'wxapp'])
  146. ->whereOr(['platform' => 'wechat', 'apptype' => 'miniapp']);
  147. })
  148. ->find();
  149. if (!$third) {
  150. $this->error('未找到登录信息');
  151. }
  152. if ($this->auth->login($account, $password)) {
  153. $third->user_id = $this->auth->id;
  154. $third->save();
  155. $this->success("绑定成功", ['userInfo' => $this->getUserInfo()]);
  156. } else {
  157. $this->error($this->auth->getError());
  158. }
  159. }
  160. /**
  161. * 个人资料
  162. */
  163. public function profile()
  164. {
  165. $user = $this->auth->getUser();
  166. $username = $this->request->post('username');
  167. $nickname = $this->request->post('nickname');
  168. $bio = $this->request->post('bio');
  169. $avatar = $this->request->post('avatar');
  170. if (!$username || !$nickname) {
  171. $this->error("用户名和昵称不能为空");
  172. }
  173. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  174. if ($exists) {
  175. $this->error(__('Username already exists'));
  176. }
  177. $avatar = str_replace(cdnurl('', true), '', $avatar);
  178. $user->username = $username;
  179. $user->nickname = $nickname;
  180. $user->bio = $bio;
  181. $user->avatar = $avatar;
  182. $user->save();
  183. $this->success('', ['userInfo' => $this->getUserInfo()]);
  184. }
  185. /**
  186. * 保存头像
  187. */
  188. public function avatar()
  189. {
  190. $user = $this->auth->getUser();
  191. $avatar = $this->request->post('avatar');
  192. if (!$avatar) {
  193. $this->error("头像不能为空");
  194. }
  195. $avatar = str_replace(cdnurl('', true), '', $avatar);
  196. $user->avatar = $avatar;
  197. $user->save();
  198. $this->success('', ['userInfo' => $this->getUserInfo()]);
  199. }
  200. /**
  201. * 退出登录
  202. */
  203. public function logout()
  204. {
  205. $this->auth->logout();
  206. $this->success();
  207. }
  208. /**
  209. * 获取用户信息
  210. * @return array
  211. */
  212. protected function getUserInfo()
  213. {
  214. $userinfo = $this->auth->getUserInfo();
  215. $userinfo['avatar'] = cdnurl($userinfo['avatar'], true);
  216. $vip = get_addon_info('vip');
  217. $userinfo['is_install_vip'] = ($vip && $vip['state']);
  218. if (!$userinfo['is_install_vip']) {//禁用
  219. $userinfo['vip'] = 0;
  220. $userinfo['vipInfo'] = null;
  221. } else {
  222. $userinfo['vipInfo'] = \addons\vip\library\Service::getVipInfo($userinfo['id']) ?? null;
  223. if (empty($userinfo['vipInfo'])) {
  224. $userinfo['vip'] = 0;
  225. }
  226. }
  227. return $userinfo;
  228. }
  229. }