25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

287 satır
9.2 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. $password = $this->request->post('password', "");
  121. // $avatar = $this->request->post('avatar', $userInfo['avatar']);
  122. if (!$password){
  123. $this->error(__('请填写密码'), 0);
  124. }
  125. $user = \app\common\model\User::get($this->auth->id);
  126. $pwd= \app\common\library\Auth::instance()->getEncryptPassword($password, $user->salt);
  127. $user->password = $pwd;
  128. if ($user->save()) {
  129. $this->success(__('Modified'), 1);
  130. } else {
  131. $this->error(__('Fail'), 0);
  132. }
  133. }
  134. /**
  135. * 登录状态
  136. */
  137. public function status()
  138. {
  139. $this->success('', $this->auth->isLogin());
  140. }
  141. /**
  142. * 微信小程序登录
  143. */
  144. public function authSession()
  145. {
  146. $platform = $this->request->header('platform');
  147. switch ($platform) {
  148. case 'MP-WEIXIN':
  149. $code = $this->request->get('code');
  150. $data = Wechat::authSession($code);
  151. // 如果有手机号码,自动登录
  152. if (isset($data['userInfo']['mobile']) && (!empty($data['userInfo']['mobile']) || $data['userInfo']['mobile'] != '')) {
  153. $this->auth->direct($data['userInfo']['id']);
  154. if ($this->auth->isLogin()) {
  155. $data['userInfo']['token'] = $this->auth->getToken();
  156. // 支付的时候用
  157. Cache::set('openid_' . $data['userInfo']['id'], $data['openid'], 7200);
  158. }
  159. }
  160. break;
  161. default:
  162. $data = [];
  163. }
  164. $this->success('', $data);
  165. }
  166. /**
  167. * 微信小程序消息解密
  168. */
  169. public function decryptData()
  170. {
  171. $iv = $this->request->post('iv');
  172. $encryptedData = $this->request->post('encryptedData');
  173. $app = Wechat::initEasyWechat('miniProgram');
  174. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  175. $this->success('', $decryptedData);
  176. }
  177. /**
  178. * 微信小程序通过授权手机号登录
  179. */
  180. public function loginForWechatMini()
  181. {
  182. $iv = $this->request->post('iv');
  183. $encryptedData = $this->request->post('encryptedData');
  184. $app = Wechat::initEasyWechat('miniProgram');
  185. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  186. if (isset($decryptedData['phoneNumber'])) {
  187. $openid = Session::get('openid');
  188. // 看看有没有这个mobile的用户
  189. $user = \addons\unishop\model\User::getByMobile($decryptedData['phoneNumber']);
  190. if ($user) {
  191. // 有 处理:1,把;user_extend对应的user删除;2,把user_extend表的user_id字段换成已存在的用户id
  192. $userExtend = UserExtend::getByOpenid($openid);
  193. if ($userExtend) {
  194. if ($userExtend['user_id'] != $user->id) {
  195. \addons\unishop\model\User::destroy($userExtend['user_id']);
  196. $userExtend->user_id = $user->id;
  197. $userExtend->save();
  198. }
  199. } else {
  200. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  201. }
  202. } else {
  203. // 没有
  204. $userExtend = UserExtend::getByOpenid($openid);
  205. if ($userExtend) {
  206. $user = \addons\unishop\model\User::get($userExtend->user_id);
  207. $user->mobile = $decryptedData['phoneNumber'];
  208. $user->save();
  209. } else {
  210. $params = [
  211. 'level' => 1,
  212. 'score' => 0,
  213. 'jointime' => time(),
  214. 'joinip' => $_SERVER['REMOTE_ADDR'],
  215. 'logintime' => time(),
  216. 'loginip' => $_SERVER['REMOTE_ADDR'],
  217. 'prevtime' => time(),
  218. 'status' => 'normal',
  219. 'avatar' => '',
  220. 'username' => __('Tourist'),
  221. 'mobile' => $decryptedData['phoneNumber']
  222. ];
  223. $user = \addons\unishop\model\User::create($params, true);
  224. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  225. }
  226. }
  227. $userInfo['id'] = $user->id;
  228. $userInfo['openid'] = $openid;
  229. $userInfo['mobile'] = $user->mobile;
  230. $userInfo['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($user->avatar);
  231. $userInfo['username'] = $user->username;
  232. $this->auth->direct($userInfo['id']);
  233. if ($this->auth->isLogin()) {
  234. $userInfo['token'] = $this->auth->getToken();
  235. // 支付的时候用
  236. Cache::set('openid_' . $userInfo['id'], $openid, 7200);
  237. }
  238. $this->success('', $userInfo);
  239. } else {
  240. $this->error(__('Logged in failed'));
  241. }
  242. }
  243. public function checkRedis(){
  244. $redis = new Redis();
  245. $a=$redis->handler->set("test_redis",1,86400);
  246. $b=$redis->handler->get("test_redis");
  247. $this->success('', $b);
  248. }
  249. }