Login.php 15 KB

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