Login.php 16 KB

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