Login.php 16 KB

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