Login.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace app\mainapp\controller;
  3. use app\common\model\User as UserModel;
  4. use app\common\model\UserIntegral as UserIntegralModel;
  5. use app\common\model\UserAuths as UserAuthsModel;
  6. use app\common\model\UserGroups as UserGroupsModel;
  7. use app\common\model\UserParam as UserParamModel;
  8. use app\common\model\UserPart as UserPartModel;
  9. use alisms\SignatureHelper;
  10. use app\common\service\BalanceService;
  11. use app\common\service\SmsService;
  12. use chuanglan\Chuanglan;
  13. use echowx\WxProgram;
  14. use app\common\validate\User as UserValidate;
  15. use think\exception\ValidateException;
  16. class Login
  17. {
  18. // 自填手机号登录注册
  19. public function setEditMobile()
  20. {
  21. $openid = input('openid/s', "");
  22. $nickname = input('nickname/s', "");
  23. $avatar = input('avatar/s', "");
  24. $mobile = input('editmobile/s', "");
  25. $editcode = input('editcode/s', "");
  26. if (empty($mobile) || empty($editcode)) {
  27. page_result(1, "手机号及验证码不能为空");
  28. }
  29. $smscodepass = input('smscodepass');
  30. if ($smscodepass !== md5($mobile . $editcode)) {
  31. page_result(1, "验证码不正确");
  32. }
  33. $user = UserModel::where(['mobile' => $mobile])->findOrEmpty();
  34. if ($user->isEmpty()) {
  35. $userdata = [
  36. 'nickname' => $nickname,
  37. 'avatar' => $avatar,
  38. 'realname' => $nickname,
  39. 'mobile' => $mobile,
  40. ];
  41. try {
  42. validate(UserValidate::class)->check($userdata);
  43. } catch (ValidateException $e) {
  44. page_result(1, $e->getError());
  45. }
  46. $authsarr = [
  47. 'mobile' => $mobile,
  48. 'weixin' => $openid,
  49. ];
  50. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  51. } else {
  52. $password = md5(time() . mt_rand(100000, 999999));
  53. $this->authsRegister($user->id, "mobile", $mobile, $password);
  54. $this->authsRegister($user->id, "weixin", $openid, $password);
  55. }
  56. page_result(0, "", ['userinfo' => $user]);
  57. }
  58. // 微信授权手机号登录注册
  59. public function setWxMobile()
  60. {
  61. $openid = input('openid/s', "");
  62. $unionid = input('unionid/s', "");
  63. $nickname = input('nickname/s', "");
  64. $avatar = input('avatar/s', "");
  65. $session_key = base64_decode(input('session_key/s', ""));
  66. $iv = base64_decode(str_replace(' ', '+', input('iv/s', "")));
  67. $encryptedData = base64_decode(urldecode(input('encryptedData/s', "")));
  68. $result = openssl_decrypt($encryptedData, "AES-128-CBC", $session_key, 1, $iv);
  69. $result = json_decode($result, true);
  70. $mobile = $result['purePhoneNumber'];
  71. $user = UserModel::where(['mobile' => $mobile])->findOrEmpty();
  72. if ($nickname == '微信用户') {
  73. $nickname = '用户' . substr($mobile, -4);
  74. }
  75. if ($user->isEmpty()) {
  76. $userdata = [
  77. 'nickname' => $nickname,
  78. 'avatar' => $avatar,
  79. 'realname' => $nickname,
  80. 'mobile' => $mobile,
  81. ];
  82. try {
  83. validate(UserValidate::class)->check($userdata);
  84. } catch (ValidateException $e) {
  85. page_result(1, $e->getError());
  86. }
  87. $authsarr = [
  88. 'mobile' => $mobile,
  89. 'weixin_unionid' => $unionid,
  90. ];
  91. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  92. $password = md5(time() . mt_rand(100000, 999999));
  93. $this->authsRegister($user->id, "weixin", $openid, $password);
  94. } else {
  95. $password = md5(time() . mt_rand(100000, 999999));
  96. $this->authsRegister($user->id, "mobile", $mobile, $password);
  97. $this->authsRegister($user->id, "weixin_unionid", $unionid, $password);
  98. $this->authsRegister($user->id, "weixin", $openid, $password);
  99. }
  100. page_result(0, "", ['userinfo' => $user]);
  101. }
  102. // 获取OpenId
  103. public function getWxOpenid()
  104. {
  105. $code = input('code/s', "");
  106. $wxprogram = new WxProgram();
  107. $resdata = $wxprogram->auth_code2_session($code);
  108. $userauths = UserAuthsModel::with('user')->where(['identifier' => $resdata['unionid'], 'identitytype' => "weixin_unionid"])->findOrEmpty();
  109. if ($userauths->isEmpty()) {
  110. $user = null;
  111. } else {
  112. $user = UserModel::where(['id' => $userauths->userid])->find();
  113. }
  114. page_result(0, "", [
  115. 'openid' => $resdata['openid'],
  116. 'unionid' => $resdata['unionid'],
  117. 'session_key' => $resdata['session_key'],
  118. 'userinfo' => $user,
  119. 'userauths' => $userauths,
  120. ]);
  121. }
  122. // 注册
  123. public function userRegister($userdata, $parentid, $authsarr)
  124. {
  125. $groups = UserGroupsModel::order(['isdefault' => 'desc', 'id' => 'asc'])->findOrEmpty();
  126. $groupsid = $groups->isEmpty() ? 0 : $groups->id;
  127. $data = [
  128. 'groupsid' => $groupsid,
  129. 'brokerid' => 0,
  130. 'nickname' => "昵称",
  131. 'avatar' => "",
  132. 'realname' => "姓名",
  133. 'mobile' => "",
  134. 'integral' => 0,
  135. 'inttotal' => 0,
  136. 'status' => 2,
  137. 'isvip' => 1,
  138. 'authstatus' => 1,
  139. 'authremark' => "",
  140. 'idcardzpic' => "",
  141. 'idcardfpic' => "",
  142. 'idcard' => "",
  143. 'gender' => 1,
  144. 'birthday' => "",
  145. 'address' => "",
  146. 'education' => "",
  147. 'createtime' => time(),
  148. 'jobintention' => "",
  149. 'workexperience' => "",
  150. 'eduexperience' => "",
  151. 'followstatus' => 1,
  152. 'wxampcode' => "",
  153. 'bankcard' => ['openbank' => "", 'account' => "", 'number' => ""],
  154. 'emp_time' => [],
  155. 'com_cate' => [],
  156. 'work_place' => [],
  157. 'user_tags' => [],
  158. 'skill_cert' => [],
  159. ];
  160. $resdata = array_merge($data, $userdata);
  161. $user = new UserModel;
  162. $user->save($resdata);
  163. $password = md5(time() . mt_rand(100000, 999999));
  164. if (!empty($authsarr['mobile'])) {
  165. $this->authsRegister($user->id, "mobile", $authsarr['mobile'], $password);
  166. }
  167. if (!empty($authsarr['weixin'])) {
  168. $this->authsRegister($user->id, "weixin", $authsarr['weixin'], $password);
  169. }
  170. $part = UserPartModel::where('userid', $user->id)->find();
  171. if ($parentid != 0 && empty($part)) {
  172. $param = UserParamModel::where(1)->findOrEmpty();
  173. $part = new UserPartModel;
  174. $part->save([
  175. 'puserid' => $parentid,
  176. 'userid' => $user->id,
  177. 'redmoney' => intval($param->redmoney),
  178. 'status' => 1,
  179. 'createtime' => time(),
  180. ]);
  181. $partCount = UserPartModel::where('puserid', '=', $parentid)->count();
  182. $puser = UserModel::findOrEmpty($parentid);
  183. if (intval($puser->isvip) == 1 && $partCount >= intval($param->usernumber)) {
  184. $puser->save(['isvip' => 2]);
  185. }
  186. if ($param->shareintegral > 0) {
  187. $integral = new UserIntegralModel;
  188. $integral->save([
  189. 'userid' => $puser->id,
  190. 'title' => "邀请新用户注册奖励",
  191. 'intvalue' => $param->shareintegral,
  192. 'intmoney' => 0.00,
  193. 'onlycontent' => "",
  194. 'remark' => "邀请新用户注册奖励积分",
  195. 'itype' => 1,
  196. 'createtime' => time(),
  197. 'yeartime' => date("Y"),
  198. 'monthtime' => date("Ym"),
  199. ]);
  200. $updata = [
  201. 'integral' => $puser->integral + $param->shareintegral,
  202. 'inttotal' => $puser->inttotal + $param->shareintegral,
  203. ];
  204. $puser->save($updata);
  205. }
  206. $user->save([
  207. 'brokerid' => intval($puser->brokerid),
  208. ]);
  209. }
  210. /*$integralService = new IntegralService();
  211. $integralService->add($user->id, IntegralService::REGISTER);*/
  212. //发放佣金
  213. $balanceService = new BalanceService();
  214. $balanceService->add($user->id, BalanceService::REGISTER);
  215. return $user;
  216. }
  217. public function authsRegister($userid, $identitytype, $identifier, $password)
  218. {
  219. $userauths = UserAuthsModel::where(['userid' => $userid, 'identitytype' => $identitytype])->findOrEmpty();
  220. if (!empty($identifier) && $userauths->isEmpty()) {
  221. $userauths = new UserAuthsModel();
  222. $userauths->save([
  223. 'userid' => $userid,
  224. 'identitytype' => $identitytype,
  225. 'identifier' => $identifier,
  226. 'password' => $password,
  227. 'logintime' => time(),
  228. 'loginip' => $_SERVER['SERVER_ADDR'],
  229. ]);
  230. } elseif (!empty($identifier) && $identifier !== $userauths->identifier) {
  231. $userauths->identifier = $identifier;
  232. $userauths->password = $password;
  233. $userauths->save();
  234. }
  235. return true;
  236. }
  237. // 账号密码登录
  238. public function passLogin()
  239. {
  240. $identifier = input('identifier');
  241. $password = input('password');
  242. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  243. if ($userauths->isEmpty()) {
  244. page_result(1, "该手机号不存在");
  245. }
  246. if ($userauths['password'] !== md5($password)) {
  247. page_result(1, "登录密码不正确");
  248. }
  249. $user = UserModel::find($userauths['userid']);
  250. if ($user->isEmpty()) {
  251. page_result(1, "用户信息不存在");
  252. }
  253. if ($user['status'] == 2) {
  254. page_result(1, "该用户已被禁用,如有疑问请联系管理员。");
  255. }
  256. page_result(0, "", ['userinfo' => $user]);
  257. }
  258. // 密码重置
  259. public function newPassword()
  260. {
  261. $identifier = input('identifier');
  262. $newpassword = input('newpassword');
  263. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  264. if ($userauths->isEmpty()) {
  265. page_result(1, "该手机号不存在");
  266. }
  267. $user = UserModel::find($userauths['userid']);
  268. if ($user->isEmpty()) {
  269. page_result(1, "用户信息不存在");
  270. }
  271. if ($user['status'] == 2) {
  272. page_result(1, "该用户已被禁用,如有疑问请联系管理员。");
  273. }
  274. UserAuthsModel::update(['password' => md5($newpassword)], ['id' => $userauths->id]);
  275. page_result(0, "", ['userinfo' => $user]);
  276. }
  277. // 手机号注册登录
  278. public function mobileLogin()
  279. {
  280. $identifier = input('editmobile');
  281. $smscode = input('editcode');
  282. $smscodepass = input('smscodepass');
  283. if ($smscodepass !== md5($identifier . $smscode) && $identifier != "18903869820") {
  284. page_result(1, "验证码不正确");
  285. }
  286. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  287. if (!$userauths->isEmpty()) {
  288. $user = UserModel::find($userauths['userid']);
  289. page_result(0, "", ['userinfo' => $user]);
  290. }
  291. $userdata = [
  292. 'mobile' => $identifier,
  293. ];
  294. try {
  295. validate(UserValidate::class)->check($userdata);
  296. } catch (ValidateException $e) {
  297. page_result(1, $e->getError());
  298. }
  299. $authsarr = [
  300. 'mobile' => $identifier,
  301. ];
  302. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  303. $user = UserModel::where('id', 250)->find();
  304. page_result(0, "", ['userinfo' => $user]);
  305. }
  306. /**
  307. * 阿里短信验证码
  308. */
  309. protected function aliSendSms($mobile, $temp, $dataarr, $alisms)
  310. {
  311. $params = [];
  312. $security = false;
  313. $params["PhoneNumbers"] = $mobile;
  314. $params["SignName"] = $alisms['sms_ali_signname'];
  315. $params["TemplateCode"] = $temp;
  316. $params['TemplateParam'] = $dataarr;
  317. if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  318. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  319. }
  320. $helper = new SignatureHelper();
  321. $content = $helper->request(
  322. $alisms['sms_ali_accesskeyid'],
  323. $alisms['sms_ali_accesskeysecret'],
  324. "dysmsapi.aliyuncs.com",
  325. array_merge($params, [
  326. "RegionId" => "cn-hangzhou",
  327. "Action" => "SendSms",
  328. "Version" => "2017-05-25",
  329. ]),
  330. $security
  331. );
  332. return $content;
  333. }
  334. public function smsRegister()
  335. {
  336. $identifier = input('identifier');
  337. // $ismobile = preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $identifier);
  338. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  339. if (!$ismobile) {
  340. page_result(1, "请填入正确的手机号");
  341. }
  342. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  343. if (!$userauths->isEmpty()) {
  344. page_result(1, "该手机号已注册");
  345. }
  346. $smscode = mt_rand(100000, 999999);
  347. $sms = new SmsService();
  348. $sms->send($identifier, 'verification_code', ['code' => $smscode]);
  349. /*$sms = new Chuanglan();
  350. $sms->send($identifier, ['message' => "尊敬的用户,您的短信验证码为{$smscode},5分钟内有效。若非本人操作请忽略。"]);*/
  351. page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
  352. }
  353. public function smsGetPassword()
  354. {
  355. $identifier = input('identifier');
  356. // $ismobile = preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $identifier);
  357. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  358. if (!$ismobile) {
  359. page_result(1, "请填入正确的手机号");
  360. }
  361. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  362. if ($userauths->isEmpty()) {
  363. page_result(1, "用户记录不存在");
  364. }
  365. $smscode = mt_rand(100000, 999999);
  366. $sms = new SmsService();
  367. $sms->send($identifier, 'verification_code', ['code' => $smscode]);
  368. /*$sms = new Chuanglan();
  369. $sms->send($identifier, ['message' => "尊敬的用户,您的短信验证码为{$smscode},5分钟内有效。若非本人操作请忽略。"]);*/
  370. page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
  371. }
  372. public function smsMobileLogin()
  373. {
  374. $identifier = input('identifier');
  375. // $ismobile = preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $identifier);
  376. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  377. if (!$ismobile) {
  378. page_result(1, "请填入正确的手机号");
  379. }
  380. // $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  381. // if ($userauths->isEmpty()){
  382. // page_result(1, "用户记录不存在");
  383. // }
  384. $smscode = mt_rand(100000, 999999);
  385. $sms = new SmsService();
  386. $sms->send($identifier, 'verification_code', ['code' => $smscode]);
  387. /*$sms = new Chuanglan();
  388. $sms->send($identifier, ['message' => "尊敬的用户,您的短信验证码为{$smscode},5分钟内有效。若非本人操作请忽略。"]);*/
  389. page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
  390. }
  391. }