Login.php 16 KB

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