Login.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\shop\controller;
  11. use app\Controller;
  12. use app\model\store\Store;
  13. use app\model\system\Site;
  14. use app\model\system\User as UserModel;
  15. use app\model\web\Config as ConfigModel;
  16. use app\model\web\DiyView as DiyViewModel;
  17. use extend\QRcode as QRcodeExtend;
  18. use think\captcha\facade\Captcha as ThinkCaptcha;
  19. use think\facade\Cache;
  20. /**
  21. * 登录
  22. * Class Login
  23. * @package app\shop\controller
  24. */
  25. class Login extends Controller
  26. {
  27. protected $app_module = "shop";
  28. protected $login_module;
  29. protected $site_id;
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. //检测基础登录
  34. $this->site_id = request()->siteid();
  35. if (empty($this->site_id)) {
  36. $this->site_id = input("site_id", 0);
  37. request()->siteid($this->site_id);
  38. }
  39. $this->assign('app_module', $this->app_module);
  40. // 加载自定义图标库
  41. $diy_view = new DiyViewModel();
  42. $diy_icon_url = $diy_view->getIconUrl()[ 'data' ];
  43. $this->assign('load_diy_icon_url', $diy_icon_url);
  44. }
  45. /**
  46. * 登录首页
  47. * @return mixed
  48. */
  49. public function login()
  50. {
  51. $site_id = request()->siteid();
  52. $config_model = new ConfigModel();
  53. $config_info = $config_model->getCaptchaConfig();
  54. $config = $config_info[ 'data' ][ 'value' ];
  55. $this->assign('shop_login', $config[ 'shop_login' ]);
  56. if (request()->isAjax()) {
  57. $username = input("username", '');
  58. $password = input("password", '');
  59. $login_module = input("login_module", 'shop');
  60. $user_model = new UserModel();
  61. if ($config[ "shop_login" ] == 1) {
  62. $captcha_result = $this->checkCaptcha();
  63. //验证码
  64. if ($captcha_result[ "code" ] != 0) {
  65. return $captcha_result;
  66. }
  67. }
  68. $result = $user_model->login($username, $password, $login_module, $site_id);
  69. return $result;
  70. } else {
  71. $this->assign("menu_info", [ 'title' => "登录" ]);
  72. //平台配置信息
  73. $site_model = new Site();
  74. $shop_info = $site_model->getSiteInfo([ [ 'site_id', '=', $site_id ] ], 'site_name,logo,seo_keywords,seo_description, create_time');
  75. $this->assign("shop_info", $shop_info[ 'data' ]);
  76. $this->assign('domain', $_SERVER[ 'SERVER_NAME' ]);
  77. //加载版权信息
  78. $copyright = $config_model->getCopyright();
  79. $this->assign('copyright', $copyright[ 'data' ][ 'value' ]);
  80. //获取其他端 访问二维码
  81. $addon = [];
  82. $resultData = [];
  83. if (addon_is_exit('mobileshop', $site_id)) {
  84. $config_model = new \addon\mobileshop\model\Config();
  85. $addon[ 'mobileshop' ] = $config_model->getMShopDomainName($site_id);
  86. $addon[ 'weapp' ] = $config_model->getWeappConfig($site_id);
  87. if ($addon[ 'mobileshop' ][ 'code' ] == 0 && !empty($addon[ 'mobileshop' ][ 'data' ])) {
  88. $path = 'upload/qrcode/shop' . '/';
  89. $name = "shop_qrcode_" . $site_id . '_' . 'mobileshop' . '.png';
  90. $filename = $path . $name;
  91. if (!file_exists($path)) {
  92. mkdir($path, intval('0755', 8), true);
  93. }
  94. if (!file_exists($filename)) {
  95. $url = $addon[ 'mobileshop' ][ 'data' ][ 'value' ][ 'domain_name_mobileshop' ];
  96. QRcodeExtend::png($url, $filename, 'L', 4, 1);
  97. }
  98. $resultData[ 0 ][ 'message' ] = 'H5端';
  99. $resultData[ 0 ][ 'img' ] = $filename;
  100. }
  101. if ($addon[ 'weapp' ][ 'code' ] == 0 && !empty($addon[ 'weapp' ][ 'data' ][ 'value' ]) && !empty($addon[ 'weapp' ][ 'data' ][ 'value' ][ 'qrcode' ])) {
  102. $resultData[ 1 ][ 'message' ] = '小程序端';
  103. $resultData[ 1 ][ 'img' ] = $addon[ 'weapp' ][ 'data' ][ 'value' ][ 'qrcode' ];
  104. }
  105. }
  106. // 验证码
  107. $captcha = $this->captcha()[ 'data' ];
  108. $this->assign('site_id', $site_id);
  109. $this->assign("captcha", $captcha);
  110. $this->assign('port_data', $resultData);
  111. $config_model = new ConfigModel();
  112. $base = $config_model->getStyle($this->site_id);
  113. $this->assign('base', $base);
  114. if (addon_is_exit('store', $this->site_id)) {
  115. $store_list = ( new Store() )->getStoreList([ [ 'site_id', '=', $this->site_id ] ], 'store_name,store_id')[ 'data' ];
  116. $this->assign('store_list', $store_list);
  117. $this->assign('store_is_exit', 1);
  118. }
  119. return $this->fetch("login/login");
  120. }
  121. }
  122. /**
  123. * 退出操作
  124. */
  125. public function logout()
  126. {
  127. $site_id = request()->siteid();
  128. $user_model = new UserModel();
  129. $login_module = $user_model->loginModule($this->app_module, $site_id);
  130. $user_model->clearLogin($login_module, $site_id);
  131. $this->redirect(url("shop/login/login"));
  132. }
  133. /**
  134. * 验证码
  135. */
  136. public function captcha()
  137. {
  138. $captcha_data = ThinkCaptcha::create(null, true);
  139. $captcha_id = md5(uniqid(null, true));
  140. // 验证码10分钟有效
  141. Cache::set($captcha_id, $captcha_data[ 'code' ], 600);
  142. return success(0, '', [ 'id' => $captcha_id, 'img' => $captcha_data[ 'img' ] ]);
  143. }
  144. /**
  145. * 验证码验证
  146. */
  147. public function checkCaptcha()
  148. {
  149. $captcha = input('captcha', '');
  150. $captcha_id = input('captcha_id', '');
  151. if (empty($captcha)) return error(-1, '请输入验证码');
  152. $captcha_data = Cache::pull($captcha_id);
  153. if (empty($captcha_data)) return error('', '验证码已失效');
  154. if ($captcha != $captcha_data) return error(-1, '验证码错误');
  155. return success();
  156. }
  157. /**
  158. * 清理缓存
  159. */
  160. public function clearCache()
  161. {
  162. Cache::clear();
  163. return success('', '缓存更新成功', '');
  164. }
  165. /**
  166. * 修改密码
  167. * */
  168. public function modifyPassword()
  169. {
  170. if (request()->isAjax()) {
  171. $site_id = request()->siteid();
  172. $user_model = new UserModel();
  173. $uid = $user_model->uid($this->app_module, $site_id);
  174. $old_pass = input('old_pass', '');
  175. $new_pass = input('new_pass', '123456');
  176. $condition = [
  177. [ 'uid', '=', $uid ],
  178. [ 'password', '=', data_md5($old_pass) ],
  179. [ 'site_id', '=', $site_id ]
  180. ];
  181. $res = $user_model->modifyAdminUserPassword($condition, $new_pass);
  182. return $res;
  183. }
  184. }
  185. }