LoginController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace app\controller;
  3. use app\business\LoginBusiness;
  4. use app\business\SmsBusiness;
  5. use app\business\StreamBusiness;
  6. use Illuminate\Support\Arr;
  7. use Respect\Validation\Validator;
  8. use support\Db;
  9. use support\Redis;
  10. use support\Request;
  11. use hg\apidoc\annotation as Apidoc;
  12. use Webman\Captcha\CaptchaBuilder;
  13. use Webman\Captcha\PhraseBuilder;
  14. #[Apidoc\Title("登陆注册")]
  15. #[Apidoc\Group("Login")]
  16. #[Apidoc\Sort(1)]
  17. class LoginController
  18. {
  19. #[Apidoc\Title("登录")]
  20. #[Apidoc\Url("api/login/login.html")]
  21. #[Apidoc\Method("POST")]
  22. #[Apidoc\Param("mobile", type: "int", require: true, desc: "账号/手机号", mock: 16500000000)]
  23. #[Apidoc\Param("app", type: "int", require: true, desc: "是否为APP", mock: '')]
  24. #[Apidoc\Param("password", type: "string", require: true, desc: "密码(6-16字母加数字)位密码", mock: 123456)]
  25. #[Apidoc\Param("code", type: "int", require: true, desc: "验证码")]
  26. #[Apidoc\Param("key", type: "string", require: true, desc: "验证码的key")]
  27. public function login(Request $request)
  28. {
  29. $param = $request->param_data;
  30. Db::beginTransaction();
  31. try {
  32. $param = Arr::only($param, ['mobile', 'password', 'app', 'code', 'key']);
  33. Validator::input($param, [
  34. 'mobile' => Validator::notEmpty()->intType()->setName('手机号'),
  35. 'password' => Validator::notEmpty()->stringType()->Length(6, 15)->alnum()->setName('密码'),
  36. 'code' => Validator::notEmpty()->intType()->setName('验证码'),
  37. 'key' => Validator::notEmpty()->stringType()->setName('验证码标识'),
  38. ]);
  39. /* if(empty(preg_match('/^1[3-9]\d{9}$/', $param['mobile']))){
  40. throw new \Exception('请输入正确的手机号');
  41. }*/
  42. if (strtolower($param['code']) != Redis::get($param['key'])) {
  43. throw new \Exception('验证码错误!');
  44. }
  45. $param['last_ip'] = $request->getRealIp($safe_mode = true);
  46. $token = LoginBusiness::login($param);
  47. Redis::del($param['key']);
  48. } catch (\Throwable $exception) {
  49. Db::rollBack();
  50. return error($exception->getMessage());
  51. }
  52. Db::commit();
  53. return success([
  54. 'token' => $token
  55. ]);
  56. }
  57. #[Apidoc\Title("注册")]
  58. #[Apidoc\Url("api/login/register.html")]
  59. #[Apidoc\Method("POST")]
  60. // #[Apidoc\Header("token",type: "string",require: true,desc: "身份令牌Token",mock: "@token")]
  61. #[Apidoc\Param("mobile", type: "int", require: true, desc: "账号/手机号", mock: 15800000000)]
  62. #[Apidoc\Param("password", type: "string", require: true, desc: "密码(6-16字母加数字)位密码", mock: 123456)]
  63. #[Apidoc\Param("confirm_password", type: "string", require: true, desc: "确认密码(6-16字母加数字)位密码", mock: 123456)]
  64. #[Apidoc\Param("invitation_code", type: "int", require: true, desc: "邀请码", mock: 100001)]
  65. #[Apidoc\Param("code", type: "int", require: true, desc: "验证码")]
  66. #[Apidoc\Param("key", type: "string", require: true, desc: "验证码的key")]
  67. public function register(Request $request)
  68. {
  69. $param = $request->param_data;
  70. Db::beginTransaction();
  71. try {
  72. $param = Arr::only($param, ['mobile', 'password', 'confirm_password', 'invitation_code', 'code', 'key']);
  73. Validator::input($param, [
  74. 'mobile' => Validator::notEmpty()->intType()->setName('手机号'),
  75. 'password' => Validator::notEmpty()->stringType()->Length(6, 15)->alnum()->setName('密码'),
  76. 'confirm_password' => Validator::notEmpty()->stringType()->Length(6, 15)->alnum()->setName('确认密码'),
  77. 'invitation_code' => Validator::notEmpty()->intType()->setName('邀请码'),
  78. 'code' => Validator::notEmpty()->intType()->setName('验证码'),
  79. 'key' => Validator::notEmpty()->stringType()->setName('验证码标识'),
  80. ]);
  81. /* if(empty(preg_match('/^1[3-9]\d{9}$/', $param['mobile']))){
  82. throw new \Exception('请输入正确的手机号');
  83. }
  84. */
  85. if (strtolower($param['code']) != Redis::get($param['key'])) {
  86. throw new \Exception('验证码错误!');
  87. }
  88. Redis::del($param['key']);
  89. if ($param['password'] != $param['confirm_password']) {
  90. throw new \Exception('两次密码不一致!');
  91. }
  92. $param['last_ip'] = $request->getRealIp($safe_mode = true);
  93. $param['join_ip'] = $param['last_ip'];
  94. $token = LoginBusiness::register($param);
  95. } catch (\Throwable $exception) {
  96. Db::rollBack();
  97. return error($exception->getMessage());
  98. }
  99. Db::commit();
  100. return success([
  101. 'token' => $token
  102. ]);
  103. }
  104. // #[Apidoc\Title("注册领取")]
  105. // #[Apidoc\Url("api/login/register_receive.html")]
  106. // #[Apidoc\Method("POST")]
  107. // #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  108. // public function register_receive(Request $request)
  109. // {
  110. // $param = $request->all();
  111. // Db::beginTransaction();
  112. // try {
  113. // $user = Db::table('wa_users')->where('id', $request->user_data['id'])->first();
  114. // $system = Db::table('wa_system')->first();
  115. // /** @var $has 查是否已经领取 */
  116. // $has = Db::table('wa_stream')
  117. // ->where('user_id', $request->user_data['id'])
  118. // ->where('type', streamType1)
  119. // ->where('mold', moldType5)
  120. // ->exists();
  121. // if (!empty($has)) {
  122. // throw new \Exception('已经领取该奖励了,不能重复领取!');
  123. // }
  124. //
  125. // /** 注册赠送 */
  126. // if (!empty($system) && !empty($user) && !empty($system->register_award)) {//注册赠送
  127. // StreamBusiness::addStream($request->user_data['id'], $system->register_award, streamType1, moldType5, moldTypefild5);
  128. // }
  129. // } catch (\Throwable $exception) {
  130. // Db::rollBack();
  131. // return error($exception->getMessage());
  132. // }
  133. // Db::commit();
  134. // return success([], '领取成功');
  135. // }
  136. #[Apidoc\Title("验证码")]
  137. #[Apidoc\Url("api/login/authccode.html")]
  138. #[Apidoc\Method("POST")]
  139. // #[Apidoc\Header("token",type: "string",require: true,desc: "身份令牌Token",mock: "@token")]
  140. #[Apidoc\Returned(name: "img", type: "blob", require: true, desc: '图形验证码', default: 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA7')]
  141. #[Apidoc\Returned(name: "key", type: "string", require: true, desc: '验证码KEY', default: 'authccode172036979253337')]
  142. public function authccode()
  143. {
  144. $phraseBuilder = new PhraseBuilder(4, '123456789');
  145. $builder = new CaptchaBuilder(null, $phraseBuilder);
  146. // 生成验证码
  147. $builder->build(100, 29);
  148. $key = 'authccode' . time() . mt_rand(10000, 99999);
  149. Redis::setEx($key, 600, strtolower($builder->getPhrase()));
  150. $img_content = $builder->inline();
  151. return success(['img' => $img_content, 'key' => $key]);
  152. }
  153. #[Apidoc\Title("邀请规则")]
  154. #[Apidoc\Url("api/login/invite.html")]
  155. #[Apidoc\Method("POST")]
  156. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  157. #[Apidoc\Returned(name: "invite", type: "array", desc: '活动规则', default: '', children: [
  158. ['name' => "id", 'type' => 'int', 'require' => true, 'default' => 1, 'desc' => '规则ID'],
  159. ['name' => "num", 'type' => 'int', 'require' => true, 'default' => 1, 'desc' => '邀请人'],
  160. ['name' => "money", 'type' => 'int', 'require' => true, 'default' => 38, 'desc' => 'USD '],
  161. ['name' => "complete", 'type' => 'int', 'require' => true, 'default' => 0, 'desc' => '完成人数'],
  162. ['name' => "has", 'type' => 'int', 'require' => true, 'default' => false, 'desc' => '是否领奖 true 已领奖 false 未领奖'],
  163. ])]
  164. #[Apidoc\Returned(name: "uuid", type: "string", require: true, desc: '邀请码', default: '10001')]
  165. #[Apidoc\Returned(name: "is_num", type: "string", require: true, desc: '累计邀请人数', default: '10001')]
  166. #[Apidoc\Returned(name: "gift_gold", type: "int", require: true, desc: '已助力脱贫次数', default: '1')]
  167. #[Apidoc\Returned(name: "raffle_num", type: "int", require: true, desc: '可助力**次', default: '1')]
  168. #[Apidoc\Returned(name: "invitation_award", type: "int", require: true, desc: '每助力脱贫一次获得', default: '1')]
  169. public function invite(Request $request)
  170. {
  171. $invite = Db::table('wa_invite')->where('status', 1)
  172. ->orderBy('sort')->get()->toArray();
  173. foreach ($invite as $k => $v) {
  174. $invite[$k]->complete = $request->user_data['is_num'];
  175. $invite[$k]->has = Db::table('wa_stream')
  176. ->where('user_id', $request->user_data['id'])
  177. ->where('type', streamType3)
  178. ->where('source_id', $v->id)->exists();
  179. }
  180. $system = Db::table('wa_system')->first();
  181. return success([
  182. 'invite' => $invite,
  183. 'uuid' => $request->user_data['uuid'],
  184. 'is_num' => $request->user_data['new_is_num'],
  185. 'raffle_num' => $request->user_data['raffle_num'],
  186. 'gift_gold' => $system->gift_gold,
  187. 'boost_gift' => $system->boost_gift,
  188. 'original_boost_gift' => $system->original_boost_gift,
  189. ]);
  190. }
  191. #[Apidoc\Title("邀请领奖")]
  192. #[Apidoc\Url("api/login/invite_receive.html")]
  193. #[Apidoc\Method("POST")]
  194. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  195. #[Apidoc\Param("id", type: "int", require: true, desc: '邀请规则ID', mock: 1)]
  196. public function invite_receive(Request $request)
  197. {
  198. $param = $request->param_data;
  199. Db::beginTransaction();
  200. try {
  201. $param = Arr::only($param, ['id']);
  202. Validator::input($param, [
  203. 'id' => Validator::notEmpty()->intType()->setName('标识'),
  204. ]);
  205. if (!empty(Redis::get($param['id'] . $request->user_data['id']))) {
  206. throw new \Exception('请不要连续操作');
  207. }
  208. Redis::setEx($param['id'] . $request->user_data['id'], 5, $request->user_data['id']);
  209. $inviteData = Db::table('wa_invite')->where('id', $param['id'])->first();
  210. if (empty($inviteData)) {
  211. throw new \Exception('非法操作');
  212. }
  213. if ($request->user_data['new_is_num'] < $inviteData->num) {
  214. throw new \Exception('未达到,邀请规则!');
  215. }
  216. $has = Db::table('wa_stream')
  217. ->where('user_id', $request->user_data['id'])
  218. ->where('type', streamType3)
  219. ->where('source_id', $param['id'])
  220. ->exists();
  221. if (!empty($has)) {
  222. throw new \Exception('已领取过奖励!');
  223. }
  224. if (!empty($inviteData->money)) {
  225. StreamBusiness::addStream($request->user_data['id'], $inviteData->money, streamType3, moldType1, moldTypefild1, $inviteData->id);
  226. }
  227. if (!empty($inviteData->money_two)) {
  228. StreamBusiness::addStream($request->user_data['id'], $inviteData->money_two, streamType3, moldType5, moldTypefild5, $inviteData->id);
  229. }
  230. // if (!empty($inviteData->money_one)) {
  231. // for ($i = 1; $i <= $inviteData->money_one; $i++) {
  232. // $goodsdata = Db::table('wa_sign_goods')->where('id',23)->first();
  233. // $signId = Db::table('wa_sign_record')->insertGetId([
  234. // 'uid' => $request->user_data['id'],
  235. // 'goods_id' => $goodsdata->id,
  236. // 'num' => 1,
  237. // 'money' => 0,
  238. // 'type' => $goodsdata->type,
  239. // 'status' => 1,
  240. // 'order_no' => date('YmdHis') . mt_rand(1000, 9999),
  241. // 'created_at' => date('Y-m-d H:i:s'),
  242. // 'updated_at' => date('Y-m-d H:i:s'),
  243. // ]);
  244. // $thisDay = date('Y-m-d H:i:s');
  245. // /** @var $futureDay 未来时间 */
  246. // $futureDay = futureDay(999);
  247. // if (!empty($goodsdata->highest_pay_price)) {
  248. // Db::table('wa_cron_task_sign_two')->insert([
  249. // 'user_id' => $request->user_data['id'],
  250. // 'goods_id' => $goodsdata->id,
  251. // 'order_id' => $signId,
  252. // 'money' => 0,
  253. // 'bonus' => 0,
  254. // 'goods_type' => $goodsdata->type,
  255. // 'day_dividend_time' => strtotime(date('Y-m-d', strtotime('+1 days')) . ' 01:00:00'),
  256. // 'dividend_time' => strtotime($futureDay),
  257. // 'created_at' => $thisDay,
  258. // 'updated_at' => $thisDay,
  259. // 'my_good_id' => $signId,
  260. // 'highest_pay_price' => $goodsdata->highest_pay_price,
  261. // 'day' => $goodsdata->day,
  262. // 'bl' => $goodsdata->bl,
  263. // 'progress' => $goodsdata->progress,
  264. // ]);
  265. // }
  266. // }
  267. // }
  268. } catch (\Throwable $exception) {
  269. Db::rollBack();
  270. return error($exception->getMessage());
  271. }
  272. Db::commit();
  273. return success([], '领取成功');
  274. }
  275. #[Apidoc\Title("判断当前用户是否存在是否实名")]
  276. #[Apidoc\Url("api/login/sign_in.html")]
  277. #[Apidoc\Method("POST")]
  278. #[Apidoc\Param("mobile", type: "int", require: true, desc: "账号/手机号", mock: 15800000000)]
  279. #[Apidoc\Returned(name: "is_sign_in", type: "int", require: true, desc: '是否实名 1=未实名,2=实名', default: '1')]
  280. public function signIn(Request $request)
  281. {
  282. $param = $request->param_data;
  283. Db::beginTransaction();
  284. try {
  285. $param = Arr::only($param, ['mobile']);
  286. Validator::input($param, [
  287. 'mobile' => Validator::notEmpty()->intType()->setName('手机号'),
  288. ]);
  289. $userlist = Db::table('wa_users')->where('mobile', $param['mobile'])->first();
  290. if (!$userlist) {
  291. throw new \Exception('请输入正确手机号!');
  292. }
  293. $is_sign_in = 1;
  294. if ($userlist->is_autonym == 1) {
  295. $is_sign_in = 2;
  296. }
  297. $data = ['is_sign_in' => $is_sign_in];
  298. } catch (\Throwable $exception) {
  299. Db::rollBack();
  300. return error($exception->getMessage());
  301. }
  302. Db::commit();
  303. return success($data, '请求成功');
  304. }
  305. #[Apidoc\Title("未登陆修改密码")]
  306. #[Apidoc\Url("api/login/passsave.html")]
  307. #[Apidoc\Method("POST")]
  308. #[Apidoc\Param("mobile", type: "int", require: true, desc: "手机号", mock: 15800000000)]
  309. #[Apidoc\Param("number", type: "string", require: true, desc: "身份证", mock: 1123)]
  310. #[Apidoc\Param("password", type: "int", require: true, desc: "密码", mock: 123456)]
  311. public function passsave(Request $request)
  312. {
  313. $param = $request->param_data;
  314. Db::beginTransaction();
  315. try {
  316. Validator::input($param, [
  317. 'mobile' => Validator::notEmpty()->intType()->setName('手机号'),
  318. 'password' => Validator::notEmpty()->setName('密码'),
  319. ]);
  320. $userlist = Db::table('wa_users')->where('mobile', $param['mobile'])->first();
  321. if (!$userlist) {
  322. throw new \Exception('请输入正确手机号!');
  323. }
  324. if ($userlist->is_autonym == 1) {
  325. if (empty($param['number'])) {
  326. throw new \Exception('请输入身份证号码!');
  327. }
  328. $identity = Db::table('wa_user_identity')->where('uid', $userlist->id)->where('number', $param['number'])->first();
  329. if (!$identity) {
  330. throw new \Exception('输入的身份证号码和实名信息不一致!');
  331. }
  332. }
  333. Db::table('wa_users')->where('id', $userlist->id)->update(['password' => md5($param['password'])]);
  334. } catch (\Throwable $exception) {
  335. Db::rollBack();
  336. return error($exception->getMessage());
  337. }
  338. Db::commit();
  339. return success([], '修改成功');
  340. }
  341. #[Apidoc\Title("参与助力")]
  342. #[Apidoc\Url("api/login/boost.html")]
  343. #[Apidoc\Method("POST")]
  344. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  345. public function boost(Request $request)
  346. {
  347. $param = $request->param_data;
  348. Db::beginTransaction();
  349. try {
  350. $userdata = Db::table('wa_users')->where('id', $request->user_data['id'])->first();
  351. if ($userdata->raffle_num <= 0) {
  352. throw new \Exception('助力次数不足!');
  353. }
  354. if (!empty(Redis::get('111' . $request->user_data['id']))) {
  355. throw new \Exception('慢点操作!');
  356. }
  357. Redis::setEx('111' . $request->user_data['id'], 5, $request->user_data['id']);
  358. $system = Db::table('wa_system')->first();
  359. if (!empty($system->boost_gift)) {
  360. StreamBusiness::addStream($request->user_data['id'], $system->boost_gift, streamType11, moldType1, moldTypefild1);
  361. }
  362. Db::table('wa_users')->where('id', $request->user_data['id'])->decrement('raffle_num', 1);
  363. Db::table('wa_system')->where('id', 1)->increment('gift_gold', 3);
  364. } catch (\Throwable $exception) {
  365. Db::rollBack();
  366. return error($exception->getMessage());
  367. }
  368. Db::commit();
  369. return success([], '领取成功');
  370. }
  371. }