Login.php 16 KB

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