Login.php 15 KB

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