Login.php 15 KB

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