Login.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. ];
  147. $resdata = array_merge($data, $userdata);
  148. $user = new UserModel;
  149. $user->save($resdata);
  150. $password = md5(time() . mt_rand(100000, 999999));
  151. if (!empty($authsarr['mobile'])) {
  152. $this->authsRegister($user->id, "mobile", $authsarr['mobile'], $password);
  153. }
  154. if (!empty($authsarr['weixin'])) {
  155. $this->authsRegister($user->id, "weixin", $authsarr['weixin'], $password);
  156. }
  157. if ($parentid != 0) {
  158. $param = UserParamModel::where(1)->findOrEmpty();
  159. $part = new UserPartModel;
  160. $part->save([
  161. 'puserid' => $parentid,
  162. 'userid' => $user->id,
  163. 'redmoney' => intval($param->redmoney),
  164. 'status' => 1,
  165. 'createtime' => time(),
  166. ]);
  167. $partCount = UserPartModel::where('puserid', '=', $parentid)->count();
  168. $puser = UserModel::findOrEmpty($parentid);
  169. if (intval($puser->isvip) == 1 && $partCount >= intval($param->usernumber)) {
  170. $puser->save(['isvip' => 2]);
  171. }
  172. if ($param->shareintegral > 0) {
  173. $integral = new UserIntegralModel;
  174. $integral->save([
  175. 'userid' => $puser->id,
  176. 'title' => "邀请新用户注册奖励",
  177. 'intvalue' => $param->shareintegral,
  178. 'intmoney' => 0.00,
  179. 'onlycontent' => "",
  180. 'remark' => "邀请新用户注册奖励积分",
  181. 'itype' => 1,
  182. 'createtime' => time(),
  183. 'yeartime' => date("Y"),
  184. 'monthtime' => date("Ym"),
  185. ]);
  186. $updata = [
  187. 'integral' => $puser->integral + $param->shareintegral,
  188. 'inttotal' => $puser->inttotal + $param->shareintegral,
  189. ];
  190. $puser->save($updata);
  191. }
  192. $user->save([
  193. 'brokerid' => intval($puser->brokerid),
  194. ]);
  195. }
  196. return $user;
  197. }
  198. public function authsRegister($userid, $identitytype, $identifier, $password)
  199. {
  200. $userauths = UserAuthsModel::where(['userid' => $userid, 'identitytype' => $identitytype])->findOrEmpty();
  201. if (!empty($identifier) && $userauths->isEmpty()) {
  202. $userauths = new UserAuthsModel();
  203. $userauths->save([
  204. 'userid' => $userid,
  205. 'identitytype' => $identitytype,
  206. 'identifier' => $identifier,
  207. 'password' => $password,
  208. 'logintime' => time(),
  209. 'loginip' => $_SERVER['SERVER_ADDR'],
  210. ]);
  211. } elseif (!empty($identifier) && $identifier !== $userauths->identifier) {
  212. $userauths->identifier = $identifier;
  213. $userauths->password = $password;
  214. $userauths->save();
  215. }
  216. return true;
  217. }
  218. // 账号密码登录
  219. public function passLogin()
  220. {
  221. $identifier = input('identifier');
  222. $password = input('password');
  223. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  224. if ($userauths->isEmpty()) {
  225. page_result(1, "该手机号不存在");
  226. }
  227. if ($userauths['password'] !== md5($password)) {
  228. page_result(1, "登录密码不正确");
  229. }
  230. $user = UserModel::find($userauths['userid']);
  231. if ($user->isEmpty()) {
  232. page_result(1, "用户信息不存在");
  233. }
  234. if ($user['status'] == 2) {
  235. page_result(1, "该用户已被禁用,如有疑问请联系管理员。");
  236. }
  237. page_result(0, "", ['userinfo' => $user]);
  238. }
  239. // 密码重置
  240. public function newPassword()
  241. {
  242. $identifier = input('identifier');
  243. $newpassword = input('newpassword');
  244. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  245. if ($userauths->isEmpty()) {
  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. UserAuthsModel::update(['password' => md5($newpassword)], ['id' => $userauths->id]);
  256. page_result(0, "", ['userinfo' => $user]);
  257. }
  258. // 手机号注册登录
  259. public function mobileLogin()
  260. {
  261. $identifier = input('editmobile');
  262. $smscode = input('editcode');
  263. $smscodepass = input('smscodepass');
  264. if ($smscodepass !== md5($identifier . $smscode) && $identifier != "18903869820") {
  265. page_result(1, "验证码不正确");
  266. }
  267. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  268. if (!$userauths->isEmpty()) {
  269. $user = UserModel::find($userauths['userid']);
  270. page_result(0, "", ['userinfo' => $user]);
  271. }
  272. $userdata = [
  273. 'mobile' => $identifier,
  274. ];
  275. try {
  276. validate(UserValidate::class)->check($userdata);
  277. } catch (ValidateException $e) {
  278. page_result(1, $e->getError());
  279. }
  280. $authsarr = [
  281. 'mobile' => $identifier,
  282. ];
  283. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  284. $user = UserModel::where('id', 250)->find();
  285. page_result(0, "", ['userinfo' => $user]);
  286. }
  287. /**
  288. * 阿里短信验证码
  289. */
  290. protected function aliSendSms($mobile, $temp, $dataarr, $alisms)
  291. {
  292. $params = [];
  293. $security = false;
  294. $params["PhoneNumbers"] = $mobile;
  295. $params["SignName"] = $alisms['sms_ali_signname'];
  296. $params["TemplateCode"] = $temp;
  297. $params['TemplateParam'] = $dataarr;
  298. if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  299. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  300. }
  301. $helper = new SignatureHelper();
  302. $content = $helper->request(
  303. $alisms['sms_ali_accesskeyid'],
  304. $alisms['sms_ali_accesskeysecret'],
  305. "dysmsapi.aliyuncs.com",
  306. array_merge($params, [
  307. "RegionId" => "cn-hangzhou",
  308. "Action" => "SendSms",
  309. "Version" => "2017-05-25",
  310. ]),
  311. $security
  312. );
  313. return $content;
  314. }
  315. public function smsRegister()
  316. {
  317. $identifier = input('identifier');
  318. // $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);
  319. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  320. if (!$ismobile) {
  321. page_result(1, "请填入正确的手机号");
  322. }
  323. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  324. if (!$userauths->isEmpty()) {
  325. page_result(1, "该手机号已注册");
  326. }
  327. $smscode = mt_rand(100000, 999999);
  328. $sms = new Chuanglan();
  329. $sms->send($identifier,['message'=>"尊敬的用户,您的短信验证码为{$smscode},5分钟内有效。若非本人操作请忽略。"]);
  330. page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
  331. }
  332. public function smsGetPassword()
  333. {
  334. $identifier = input('identifier');
  335. // $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);
  336. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  337. if (!$ismobile) {
  338. page_result(1, "请填入正确的手机号");
  339. }
  340. $userauths = UserAuthsModel::where(['identifier' => $identifier, 'identitytype' => "mobile"])->findOrEmpty();
  341. if ($userauths->isEmpty()) {
  342. page_result(1, "用户记录不存在");
  343. }
  344. $smscode = mt_rand(100000, 999999);
  345. $sms = new Chuanglan();
  346. $sms->send($identifier,['message'=>"尊敬的用户,您的短信验证码为{$smscode},5分钟内有效。若非本人操作请忽略。"]);
  347. page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
  348. }
  349. public function smsMobileLogin()
  350. {
  351. $identifier = input('identifier');
  352. // $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);
  353. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  354. if (!$ismobile) {
  355. page_result(1, "请填入正确的手机号");
  356. }
  357. // $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  358. // if ($userauths->isEmpty()){
  359. // page_result(1, "用户记录不存在");
  360. // }
  361. $smscode = mt_rand(100000, 999999);
  362. $sms = new Chuanglan();
  363. $sms->send($identifier,['message'=>"尊敬的用户,您的短信验证码为{$smscode},5分钟内有效。若非本人操作请忽略。"]);
  364. page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
  365. }
  366. }