User copy.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\shop\controller\api;
  3. use addons\shop\model\Order;
  4. use think\Config;
  5. /**
  6. * 会员
  7. */
  8. class User extends Base
  9. {
  10. protected $noNeedLogin = ['getSigned'];
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. if (!Config::get('fastadmin.usercenter')) {
  15. $this->error(__('User center already closed'));
  16. }
  17. }
  18. /**
  19. * 个人中心
  20. */
  21. public function index()
  22. {
  23. $apptype = $this->request->param('apptype');
  24. $platform = $this->request->param('platform');
  25. $logincode = $this->request->param('logincode');
  26. $info = $this->auth->getUserInfo();
  27. $info['order'] = [
  28. 'created' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 0)->count(),
  29. 'paid' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 1)->where('shippingstate', 0)->count(),
  30. 'evaluate' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 1)->where('shippingstate', 2)->count()
  31. ];
  32. $info['avatar'] = cdnurl($info['avatar'], true);
  33. $signin = get_addon_info('signin');
  34. $info['is_install_signin'] = ($signin && $signin['state']);
  35. $firstlogin = $this->auth->jointime === $this->auth->logintime;
  36. //判断是否显示昵称更新提示
  37. $profilePrompt = false;
  38. $config = get_addon_config('shop');
  39. if ($config['porfilePrompt'] === 'firstlogin') {
  40. $profilePrompt = $this->auth->jointime === $this->auth->logintime;
  41. } elseif ($config['porfilePrompt'] === 'everylogin') {
  42. $profilePrompt = true;
  43. } elseif ($config['porfilePrompt'] === 'disabled') {
  44. $profilePrompt = false;
  45. }
  46. $showProfilePrompt = false;
  47. if ($profilePrompt) {
  48. $showProfilePrompt = !$info['nickname'] || stripos($info['nickname'], '微信用户') !== false || preg_match("/^\d{3}\*{4}\d{4}$/", $info['nickname']);
  49. }
  50. $openid = '';
  51. //如果有传登录code,则获取openid
  52. if ($logincode) {
  53. $json = (new \addons\shop\library\Wechat\Service())->getWechatSession($logincode);
  54. $openid = $json['openid'] ?? '';
  55. }
  56. $data['openid'] = $openid;
  57. $this->success('', [
  58. 'userInfo' => $info,
  59. 'openid' => $openid,
  60. 'showProfilePrompt' => $showProfilePrompt
  61. ]);
  62. }
  63. /**
  64. * 个人资料
  65. */
  66. public function profile()
  67. {
  68. $user = $this->auth->getUser();
  69. $username = $this->request->post('username');
  70. $nickname = $this->request->post('nickname');
  71. $bio = $this->request->post('bio');
  72. $avatar = $this->request->post('avatar');
  73. if (!$username || !$nickname) {
  74. $this->error("用户名和昵称不能为空");
  75. }
  76. if (strlen($bio) > 100) {
  77. $this->error("签名太长了!");
  78. }
  79. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  80. if ($exists) {
  81. $this->error(__('Username already exists'));
  82. }
  83. $avatar = str_replace(cdnurl('', true), '', $avatar);
  84. $user->username = $username;
  85. $user->nickname = $nickname;
  86. $user->bio = $bio;
  87. $user->avatar = $avatar;
  88. $user->save();
  89. $this->success('修改成功!');
  90. }
  91. /**
  92. * 保存头像
  93. */
  94. public function avatar()
  95. {
  96. $user = $this->auth->getUser();
  97. $avatar = $this->request->post('avatar');
  98. if (!$avatar) {
  99. $this->error("头像不能为空");
  100. }
  101. $avatar = str_replace(cdnurl('', true), '', $avatar);
  102. $user->avatar = $avatar;
  103. $user->save();
  104. $this->success('修改成功!');
  105. }
  106. /**
  107. * 注销登录
  108. */
  109. public function logout()
  110. {
  111. $this->auth->logout();
  112. $this->success(__('Logout successful'), ['__token__' => $this->request->token()]);
  113. }
  114. /**
  115. * 分享配置参数
  116. */
  117. public function getSigned()
  118. {
  119. $url = $this->request->param('url', '', 'trim');
  120. $js_sdk = new \addons\shop\library\Jssdk();
  121. $data = $js_sdk->getSignedPackage($url);
  122. $this->success('', $data);
  123. }
  124. }