Login.php 16 KB

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