Login.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller;
  12. use app\services\user\UserServices;
  13. use crmeb\services\CacheService;
  14. use think\facade\App;
  15. use crmeb\utils\Captcha;
  16. use app\services\system\admin\SystemAdminServices;
  17. use think\facade\Log;
  18. use think\facade\Db;
  19. /**
  20. * 后台登陆
  21. * Class Login
  22. * @package app\adminapi\controller
  23. */
  24. class Login extends AuthController
  25. {
  26. /**
  27. * Login constructor.
  28. * @param App $app
  29. * @param SystemAdminServices $services
  30. */
  31. public function __construct(App $app, SystemAdminServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. protected function initialize()
  37. {
  38. // TODO: Implement initialize() method.
  39. }
  40. /**
  41. * 验证码
  42. * @return $this|\think\Response
  43. */
  44. public function captcha()
  45. {
  46. return app()->make(Captcha::class)->create();
  47. }
  48. /**
  49. * @return mixed
  50. */
  51. public function ajcaptcha()
  52. {
  53. $captchaType = $this->request->get('captchaType');
  54. return app('json')->success(aj_captcha_create($captchaType));
  55. }
  56. /**
  57. * 一次验证
  58. * @return mixed
  59. */
  60. public function ajcheck()
  61. {
  62. [$token, $pointJson, $captchaType] = $this->request->postMore([
  63. ['token', ''],
  64. ['pointJson', ''],
  65. ['captchaType', ''],
  66. ], true);
  67. try {
  68. aj_captcha_check_one($captchaType, $token, $pointJson);
  69. return app('json')->success();
  70. } catch (\Throwable $e) {
  71. return app('json')->fail(400336);
  72. }
  73. }
  74. /**
  75. * 登陆
  76. * @return mixed
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function login()
  82. {
  83. [$account, $password, $key, $captchaVerification, $captchaType] = $this->request->postMore([
  84. 'account',
  85. 'pwd',
  86. ['key', ''],
  87. ['captchaVerification', ''],
  88. ['captchaType', '']
  89. ], true);
  90. if ($captchaVerification != '') {
  91. try {
  92. aj_captcha_check_two($captchaType, $captchaVerification);
  93. } catch (\Throwable $e) {
  94. return app('json')->fail(400336);
  95. }
  96. }
  97. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 32) {
  98. return app('json')->fail(400762);
  99. }
  100. $this->validate(['account' => $account, 'pwd' => $password], \app\adminapi\validate\setting\SystemAdminValidata::class, 'get');
  101. $result = $this->services->login($account, $password, 'admin', $key);
  102. if (!$result) {
  103. $num = CacheService::get('login_captcha', 1);
  104. if ($num > 1) {
  105. return app('json')->fail(400140, ['login_captcha' => 1]);
  106. }
  107. CacheService::set('login_captcha', $num + 1, 60);
  108. return app('json')->fail(400140, ['login_captcha' => 0]);
  109. }
  110. CacheService::delete('login_captcha');
  111. return app('json')->success($result);
  112. }
  113. /**
  114. * 获取后台登录页轮播图以及LOGO
  115. * @return mixed
  116. */
  117. public function info()
  118. {
  119. return app('json')->success($this->services->getLoginInfo());
  120. }
  121. private function validateRequest($time,$sign) {
  122. $end_key = "hunantianmuzhineng_2025";
  123. // 2. 检查参数是否存在
  124. if ($time === null || $sign === null) {
  125. return false;
  126. }
  127. // 3. 验证时间戳有效性(可选但推荐)
  128. $currentTime = time();
  129. $timeDiff = abs($currentTime - (int)$time);
  130. $maxAllowedDiff = 300; // 允许的最大时间差(5分钟)
  131. if ($timeDiff > $maxAllowedDiff) {
  132. return false;
  133. }
  134. // 4. 计算服务端签名
  135. $serverSign = md5($time . $end_key);
  136. // 5. 安全比较签名(防止时序攻击)
  137. if (!hash_equals($serverSign, $sign)) {
  138. return false;
  139. }
  140. // 验证通过,继续后续业务逻辑
  141. return true;
  142. }
  143. public function getUserScore(){
  144. $unionid = $this->request->get('unionid');
  145. $time = $this->request->get('time');
  146. $sign = $this->request->get('sign');
  147. $isRight = $this->validateRequest($time,$sign);
  148. if(!$isRight){
  149. return app('json')->fail("无权限");
  150. }
  151. $userService = app()->make(UserServices::class);
  152. $userInfo = $userService->getUserScore($unionid);
  153. $info = array('unionid' => $unionid,'integral' => $userInfo['integral']);
  154. if(!$userInfo['uid']){
  155. $info['code'] = 0;
  156. }else{
  157. $info['code'] = 1;
  158. }
  159. return app('json')->success($info);
  160. }
  161. private function checkLock($orderId, $unionId, $score){
  162. // 2. 准备 INSERT IGNORE SQL 语句
  163. // 使用 IGNORE 关键字,如果 order_id 主键冲突,则忽略本次插入
  164. $sql = "INSERT IGNORE INTO `eb_score_record` (`order_id`, `create_time`, `uniond_id`, `score`) VALUES (?, NOW(), ?, ?)";
  165. // 3. 准备绑定的参数,防止SQL注入
  166. $params = [$orderId, $unionId, $score];
  167. try {
  168. // 4. 执行 SQL
  169. $affectedRows = Db::execute($sql, $params);
  170. // 5. 判断执行结果
  171. if ($affectedRows > 0) {
  172. return true;
  173. } else {
  174. return false;
  175. }
  176. } catch (\Exception $e) {
  177. Log::error($e->getMessage());
  178. return false;
  179. }
  180. }
  181. public function addScore(){
  182. [$unionid, $score, $integration_status,$title,$mark,$order_id,$time,$sign] = $this->request->postMore([
  183. ['unionid', ''],
  184. ['score', ''],
  185. ['integration_status', ''],
  186. ['title', ''],
  187. ['mark', ''],
  188. ['order_id', ''],
  189. ['time', ''],
  190. ['sign', ''],
  191. ], true);
  192. $isRight = $this->validateRequest($time,$sign);
  193. if(!$isRight){
  194. return app('json')->fail("无权限");
  195. }
  196. $canAdd = $this->checkLock($order_id,$unionid,$score);
  197. if(!$canAdd){
  198. return app('json')->fail("流水号已经存在");
  199. }
  200. $userService = app()->make(UserServices::class);
  201. $userInfo = $userService->getUserScore($unionid);
  202. $uid = $userInfo['uid'];
  203. $data = array('integration' => $score,'integration_status'=>$integration_status);
  204. $data['title'] = $title;
  205. $data['mark'] = $mark;
  206. $data['is_other'] = true;
  207. Log::error($data);
  208. $result = $userService->addScore($uid,$data);
  209. $info = array('unionid' => $unionid);
  210. if($result){
  211. $info['code'] = 1;
  212. }else{
  213. $info['code'] = 0;
  214. }
  215. return app('json')->success($info);
  216. }
  217. }