Login.php 15 KB

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