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

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