Login.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. if(empty($userInfo)){
  154. $params = [$unionid];
  155. $score = Db::query('select sum(score) score from eb_score_record where uniond_id = ? and `status` = 0', $params);
  156. if(empty($score)){
  157. $info = array('unionid' => $unionid,'integral' => 0);
  158. }else{
  159. $integral = $score[0]['score'];
  160. if(empty($integral)){
  161. $integral = 0;
  162. }
  163. $info = array('unionid' => $unionid,'integral' => $integral);
  164. }
  165. }else{
  166. $info = array('unionid' => $unionid,'integral' => $userInfo['integral']);
  167. }
  168. return app('json')->success($info);
  169. }
  170. private function checkLock($orderId, $unionId, $score,$title,$mark,$integration_status,$status){
  171. // 2. 准备 INSERT IGNORE SQL 语句
  172. // 使用 IGNORE 关键字,如果 order_id 主键冲突,则忽略本次插入
  173. $sql = "INSERT IGNORE INTO `eb_score_record` (`order_id`, `create_time`, `uniond_id`, `score`,`title`,`mark`,`integration_status`,`status`) VALUES (?, NOW(), ?, ?,?,?,?,?)";
  174. // 3. 准备绑定的参数,防止SQL注入
  175. $params = [$orderId, $unionId, $score,$title,$mark,$integration_status,$status];
  176. try {
  177. // 4. 执行 SQL
  178. $affectedRows = Db::execute($sql, $params);
  179. // 5. 判断执行结果
  180. if ($affectedRows > 0) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. } catch (\Exception $e) {
  186. Log::error($e->getMessage());
  187. return false;
  188. }
  189. }
  190. public function addScore(){
  191. [$unionid, $score, $integration_status,$title,$mark,$order_id,$time,$sign] = $this->request->postMore([
  192. ['unionid', ''],
  193. ['score', ''],
  194. ['integration_status', ''],
  195. ['title', ''],
  196. ['mark', ''],
  197. ['order_id', ''],
  198. ['time', ''],
  199. ['sign', ''],
  200. ], true);
  201. $isRight = $this->validateRequest($time,$sign);
  202. if(!$isRight){
  203. return app('json')->fail("无权限");
  204. }
  205. $isLock = CacheService::setMutex($order_id,5);
  206. if(!$isLock){
  207. return app('json')->fail("同一订单号请勿提交频繁");
  208. }
  209. $userService = app()->make(UserServices::class);
  210. $userInfo = $userService->getUserScore($unionid);
  211. if(empty($userInfo)){//代表用户不存在
  212. $canAdd = $this->checkLock($order_id,$unionid,$score,$title,$mark,$integration_status,0);
  213. if(!$canAdd){
  214. return app('json')->fail("流水号已经存在");
  215. }
  216. $code = 2;//代表暂存
  217. }else{
  218. $uid = $userInfo['uid'];//用户存在的话
  219. $canAdd = $this->checkLock($order_id,$unionid,$score,$title,$mark,$integration_status,1);
  220. if(!$canAdd){
  221. return app('json')->fail("流水号已经存在。");
  222. }
  223. $result = $this->doAddScore($uid,$score,$integration_status,$title,$mark);
  224. if($result){
  225. $code = 1;
  226. $params = [$order_id];
  227. Db::execute('update `eb_score_record` set `status` = 0 where `order_id` = ?', $params);
  228. }else{
  229. $code = 0;
  230. }
  231. }
  232. $info = array('unionid' => $unionid);
  233. $info['code'] = $code;
  234. return app('json')->success($info);
  235. }
  236. private function doAddScore($uid,$score,$integration_status,$title,$mark){
  237. $userService = app()->make(UserServices::class);
  238. $data = array('integration' => $score,'integration_status'=>$integration_status);
  239. $data['title'] = $title;
  240. $data['mark'] = $mark;
  241. $data['is_other'] = true;
  242. $result = $userService->addScore($uid,$data);
  243. return $result;
  244. }
  245. }