25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

286 lines
9.1 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/10/25
  6. * Time: 11:09 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\unishop\extend\Redis;
  10. use addons\unishop\extend\Wechat;
  11. use addons\unishop\model\UserExtend;
  12. use app\common\library\Sms;
  13. use think\Cache;
  14. use think\Session;
  15. use think\Validate;
  16. class User extends Base
  17. {
  18. protected $noNeedLogin = ['login', 'status', 'authSession', 'decryptData', 'register', 'resetpwd', 'loginForWechatMini','checkRedis'];
  19. /**
  20. * 会员登录
  21. *
  22. * @param string $account 账号
  23. * @param string $password 密码
  24. */
  25. public function login()
  26. {
  27. $mobile = $this->request->post('mobile');
  28. $password = $this->request->post('password');
  29. if (!$mobile || !$password) {
  30. $this->error(__('Invalid parameters'));
  31. }
  32. $ret = $this->auth->login($mobile, $password);
  33. if ($ret) {
  34. $data = $this->auth->getUserinfo();
  35. $data['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($data['avatar']);
  36. $this->success(__('Logged in successful'), $data);
  37. } else {
  38. $this->error($this->auth->getError());
  39. }
  40. }
  41. /**
  42. * 重置密码
  43. *
  44. * @param string $mobile 手机号
  45. * @param string $newpassword 新密码
  46. * @param string $captcha 验证码
  47. */
  48. public function resetpwd()
  49. {
  50. $mobile = $this->request->post("mobile");
  51. $newpassword = $this->request->post("password");
  52. $captcha = $this->request->post("captcha");
  53. if (!$newpassword || !$captcha) {
  54. $this->error(__('Invalid parameters'));
  55. }
  56. if (!Validate::regex($mobile, "^1\d{10}$")) {
  57. $this->error(__('Mobile is incorrect'));
  58. }
  59. $user = \app\common\model\User::getByMobile($mobile);
  60. if (!$user) {
  61. $this->error(__('User not found'));
  62. }
  63. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  64. if (!$ret) {
  65. $this->error(__('Captcha is incorrect'));
  66. }
  67. Sms::flush($mobile, 'resetpwd');
  68. //模拟一次登录
  69. $this->auth->direct($user->id);
  70. $ret = $this->auth->changepwd($newpassword, '', true);
  71. if ($ret) {
  72. $this->success(__('Reset password successful'), 1);
  73. } else {
  74. $this->error($this->auth->getError());
  75. }
  76. }
  77. /**
  78. * 注册会员
  79. *
  80. * @param string $username 用户名
  81. * @param string $password 密码
  82. * @param string $email 邮箱
  83. * @param string $mobile 手机号
  84. */
  85. public function register()
  86. {
  87. $this->error(__('暂未开放'));
  88. die();
  89. $username = $this->request->post('username');
  90. $password = $this->request->post('password');
  91. $mobile = $this->request->post('mobile');
  92. $captcha = $this->request->post("captcha");
  93. if (!$username || !$password) {
  94. $this->error(__('Invalid parameters'));
  95. }
  96. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  97. $this->error(__('Mobile is incorrect'));
  98. }
  99. $ret = Sms::check($mobile, $captcha, 'register');
  100. if (!$ret) {
  101. $this->error(__('Captcha is incorrect'));
  102. }
  103. Sms::flush($mobile, 'register');
  104. $avatar = \addons\unishop\model\Config::getByName('avatar')['value'] ?? '';
  105. $ret = $this->auth->register($username, $password, '', $mobile, ['avatar' => $avatar]);
  106. if ($ret) {
  107. $data = ['userinfo' => $this->auth->getUserinfo()];
  108. $this->success(__('Sign up successful'), $data);
  109. } else {
  110. $this->error($this->auth->getError());
  111. }
  112. }
  113. /**
  114. * 更改用户信息
  115. */
  116. public function edit()
  117. {
  118. $userInfo = $this->auth->getUserinfo();
  119. $username = $this->request->post('username', $userInfo['username']);
  120. $mobile = $this->request->post('mobile', $userInfo['mobile']);
  121. $avatar = $this->request->post('avatar', $userInfo['avatar']);
  122. $user = \app\common\model\User::get($this->auth->id);
  123. $user->username = $username;
  124. $user->mobile = $mobile;
  125. $user->avatar = $avatar;
  126. if ($user->save()) {
  127. $this->success(__('Modified'), 1);
  128. } else {
  129. $this->error(__('Fail'), 0);
  130. }
  131. }
  132. /**
  133. * 登录状态
  134. */
  135. public function status()
  136. {
  137. $this->success('', $this->auth->isLogin());
  138. }
  139. /**
  140. * 微信小程序登录
  141. */
  142. public function authSession()
  143. {
  144. $platform = $this->request->header('platform');
  145. switch ($platform) {
  146. case 'MP-WEIXIN':
  147. $code = $this->request->get('code');
  148. $data = Wechat::authSession($code);
  149. // 如果有手机号码,自动登录
  150. if (isset($data['userInfo']['mobile']) && (!empty($data['userInfo']['mobile']) || $data['userInfo']['mobile'] != '')) {
  151. $this->auth->direct($data['userInfo']['id']);
  152. if ($this->auth->isLogin()) {
  153. $data['userInfo']['token'] = $this->auth->getToken();
  154. // 支付的时候用
  155. Cache::set('openid_' . $data['userInfo']['id'], $data['openid'], 7200);
  156. }
  157. }
  158. break;
  159. default:
  160. $data = [];
  161. }
  162. $this->success('', $data);
  163. }
  164. /**
  165. * 微信小程序消息解密
  166. */
  167. public function decryptData()
  168. {
  169. $iv = $this->request->post('iv');
  170. $encryptedData = $this->request->post('encryptedData');
  171. $app = Wechat::initEasyWechat('miniProgram');
  172. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  173. $this->success('', $decryptedData);
  174. }
  175. /**
  176. * 微信小程序通过授权手机号登录
  177. */
  178. public function loginForWechatMini()
  179. {
  180. $iv = $this->request->post('iv');
  181. $encryptedData = $this->request->post('encryptedData');
  182. $app = Wechat::initEasyWechat('miniProgram');
  183. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  184. if (isset($decryptedData['phoneNumber'])) {
  185. $openid = Session::get('openid');
  186. // 看看有没有这个mobile的用户
  187. $user = \addons\unishop\model\User::getByMobile($decryptedData['phoneNumber']);
  188. if ($user) {
  189. // 有 处理:1,把;user_extend对应的user删除;2,把user_extend表的user_id字段换成已存在的用户id
  190. $userExtend = UserExtend::getByOpenid($openid);
  191. if ($userExtend) {
  192. if ($userExtend['user_id'] != $user->id) {
  193. \addons\unishop\model\User::destroy($userExtend['user_id']);
  194. $userExtend->user_id = $user->id;
  195. $userExtend->save();
  196. }
  197. } else {
  198. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  199. }
  200. } else {
  201. // 没有
  202. $userExtend = UserExtend::getByOpenid($openid);
  203. if ($userExtend) {
  204. $user = \addons\unishop\model\User::get($userExtend->user_id);
  205. $user->mobile = $decryptedData['phoneNumber'];
  206. $user->save();
  207. } else {
  208. $params = [
  209. 'level' => 1,
  210. 'score' => 0,
  211. 'jointime' => time(),
  212. 'joinip' => $_SERVER['REMOTE_ADDR'],
  213. 'logintime' => time(),
  214. 'loginip' => $_SERVER['REMOTE_ADDR'],
  215. 'prevtime' => time(),
  216. 'status' => 'normal',
  217. 'avatar' => '',
  218. 'username' => __('Tourist'),
  219. 'mobile' => $decryptedData['phoneNumber']
  220. ];
  221. $user = \addons\unishop\model\User::create($params, true);
  222. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  223. }
  224. }
  225. $userInfo['id'] = $user->id;
  226. $userInfo['openid'] = $openid;
  227. $userInfo['mobile'] = $user->mobile;
  228. $userInfo['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($user->avatar);
  229. $userInfo['username'] = $user->username;
  230. $this->auth->direct($userInfo['id']);
  231. if ($this->auth->isLogin()) {
  232. $userInfo['token'] = $this->auth->getToken();
  233. // 支付的时候用
  234. Cache::set('openid_' . $userInfo['id'], $openid, 7200);
  235. }
  236. $this->success('', $userInfo);
  237. } else {
  238. $this->error(__('Logged in failed'));
  239. }
  240. }
  241. public function checkRedis(){
  242. $redis = new Redis();
  243. $a=$redis->handler->set("test_redis",1,86400);
  244. $b=$redis->handler->get("test_redis");
  245. $this->success('', $b);
  246. }
  247. }