You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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