Login.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace app\mainapp\controller;
  3. use think\facade\Session;
  4. use app\common\model\Alisms as AlismsModel;
  5. use app\common\model\User as UserModel;
  6. use app\common\model\UserIntegral as UserIntegralModel;
  7. use app\common\model\UserAuths as UserAuthsModel;
  8. use app\common\model\UserGroups as UserGroupsModel;
  9. use app\common\model\UserParam as UserParamModel;
  10. use app\common\model\UserPart as UserPartModel;
  11. use app\common\model\Agent as AgentModel;
  12. use app\common\model\Broker as BrokerModel;
  13. use alisms\SignatureHelper;
  14. use echowx\WxProgram;
  15. use app\common\validate\User as UserValidate;
  16. use think\exception\ValidateException;
  17. class Login
  18. {
  19. // 自填手机号登录注册
  20. public function setEditMobile()
  21. {
  22. $openid = input('openid/s', "");
  23. $nickname = input('nickname/s', "");
  24. $avatar = input('avatar/s', "");
  25. $mobile = input('editmobile/s', "");
  26. $editcode = input('editcode/s', "");
  27. if (empty($mobile) || empty($editcode)){
  28. page_result(1, "手机号及验证码不能为空");
  29. }
  30. $smscodepass = input('smscodepass');
  31. if ($smscodepass!==md5($mobile.$editcode)){
  32. page_result(1, "验证码不正确");
  33. }
  34. $user = UserModel::where(['mobile'=>$mobile])->findOrEmpty();
  35. if ($user->isEmpty()){
  36. $userdata = array(
  37. 'nickname' => $nickname,
  38. 'avatar' => $avatar,
  39. 'realname' => $nickname,
  40. 'mobile' => $mobile
  41. );
  42. try {
  43. validate(UserValidate::class)->check($userdata);
  44. } catch (ValidateException $e) {
  45. page_result(1, $e->getError());
  46. }
  47. $authsarr = array(
  48. 'mobile' => $mobile,
  49. 'weixin' => $openid
  50. );
  51. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  52. } else {
  53. $password = md5(time().mt_rand(100000, 999999));
  54. $this->authsRegister($user->id, "mobile", $mobile, $password);
  55. $this->authsRegister($user->id, "weixin", $openid, $password);
  56. }
  57. page_result(0, "", array('userinfo'=>$user));
  58. }
  59. // 微信授权手机号登录注册
  60. public function setWxMobile()
  61. {
  62. $openid = input('openid/s', "");
  63. $nickname = input('nickname/s', "");
  64. $avatar = input('avatar/s', "");
  65. $session_key = base64_decode(input('session_key/s', ""));
  66. $iv = base64_decode(str_replace(' ','+',input('iv/s', "")));
  67. $encryptedData = base64_decode(urldecode(input('encryptedData/s', "")));
  68. $result = openssl_decrypt($encryptedData,"AES-128-CBC",$session_key,1,$iv);
  69. $result = json_decode($result, true);
  70. $mobile = $result['purePhoneNumber'];
  71. $user = UserModel::where(['mobile'=>$mobile])->findOrEmpty();
  72. if ($user->isEmpty()){
  73. $userdata = array(
  74. 'nickname' => $nickname,
  75. 'avatar' => $avatar,
  76. 'realname' => $nickname,
  77. 'mobile' => $mobile
  78. );
  79. try {
  80. validate(UserValidate::class)->check($userdata);
  81. } catch (ValidateException $e) {
  82. page_result(1, $e->getError());
  83. }
  84. $authsarr = array(
  85. 'mobile' => $mobile,
  86. 'weixin' => $openid
  87. );
  88. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  89. } else {
  90. $password = md5(time().mt_rand(100000, 999999));
  91. $this->authsRegister($user->id, "mobile", $mobile, $password);
  92. $this->authsRegister($user->id, "weixin", $openid, $password);
  93. }
  94. page_result(0, "", array('userinfo'=>$user));
  95. }
  96. // 获取OpenId
  97. public function getWxOpenid()
  98. {
  99. $code = input('code/s', "");
  100. $wxprogram = new WxProgram();
  101. $resdata = $wxprogram->auth_code2_session($code);
  102. $userauths = UserAuthsModel::with('user')->where(['identifier'=>$resdata['openid'],'identitytype'=>"weixin"])->findOrEmpty();
  103. if ($userauths->isEmpty()){
  104. $user = null;
  105. }else{
  106. $user = UserModel::where(['id'=>$userauths->userid])->find();
  107. }
  108. page_result(0, "", array(
  109. 'openid' => $resdata['openid'],
  110. 'session_key' => $resdata['session_key'],
  111. 'userinfo' => $user,
  112. 'userauths' => $userauths
  113. ));
  114. }
  115. // 注册
  116. public function userRegister($userdata, $parentid=0, $authsarr)
  117. {
  118. $groups = UserGroupsModel::order(['isdefault'=>'desc','id'=>'asc'])->findOrEmpty();
  119. $groupsid = $groups->isEmpty() ? 0 : $groups->id ;
  120. $data = array(
  121. 'groupsid' => $groupsid,
  122. 'brokerid' => 0,
  123. 'nickname' => "昵称",
  124. 'avatar' => "",
  125. 'realname' => "姓名",
  126. 'mobile' => "",
  127. 'integral' => 0,
  128. 'inttotal' => 0,
  129. 'status' => 2,
  130. 'isvip' => 1,
  131. 'authstatus' => 1,
  132. 'authremark' => "",
  133. 'idcardzpic' => "",
  134. 'idcardfpic' => "",
  135. 'idcard' => "",
  136. 'gender' => 1,
  137. 'birthday' => "",
  138. 'address' => "",
  139. 'education' => "",
  140. 'createtime' => time(),
  141. 'jobintention' => "",
  142. 'workexperience' => "",
  143. 'eduexperience' => "",
  144. 'followstatus' => 1,
  145. 'wxampcode' => "",
  146. 'bankcard' => array('openbank'=>"",'account'=>"",'number'=>"")
  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. if ($parentid!=0){
  159. $param = UserParamModel::where(1)->findOrEmpty();
  160. $part = new UserPartModel;
  161. $part->save([
  162. 'puserid' => $parentid,
  163. 'userid' => $user->id,
  164. 'redmoney' => intval($param->redmoney),
  165. 'status' => 1,
  166. 'createtime' => time()
  167. ]);
  168. $partCount = UserPartModel::where('puserid', '=', $parentid)->count();
  169. $puser = UserModel::findOrEmpty($parentid);
  170. if ( intval($puser->isvip)==1 && $partCount>=intval($param->usernumber) ){
  171. $puser->save(['isvip'=>2]);
  172. }
  173. if ($param->shareintegral>0){
  174. $integral = new UserIntegralModel;
  175. $integral->save([
  176. 'userid' => $puser->id,
  177. 'title' => "邀请新用户注册奖励",
  178. 'intvalue' => $param->shareintegral,
  179. 'intmoney' => 0.00,
  180. 'onlycontent' => "",
  181. 'remark' => "邀请新用户注册奖励积分",
  182. 'itype' => 1,
  183. 'createtime' => time(),
  184. 'yeartime' => date("Y"),
  185. 'monthtime' => date("Ym")
  186. ]);
  187. $updata = array(
  188. 'integral' => $puser->integral + $param->shareintegral,
  189. 'inttotal' => $puser->inttotal + $param->shareintegral
  190. );
  191. $puser->save($updata);
  192. }
  193. $user->save([
  194. 'brokerid' => intval($puser->brokerid)
  195. ]);
  196. }
  197. return $user;
  198. }
  199. public function authsRegister($userid, $identitytype, $identifier, $password)
  200. {
  201. $userauths = UserAuthsModel::where(['userid'=>$userid, 'identitytype'=>$identitytype])->findOrEmpty();
  202. if ( !empty($identifier) && $userauths->isEmpty() ){
  203. $userauths = new UserAuthsModel();
  204. $userauths->save([
  205. 'userid' => $userid,
  206. 'identitytype' => $identitytype,
  207. 'identifier' => $identifier,
  208. 'password' => $password,
  209. 'logintime' => time(),
  210. 'loginip' => $_SERVER['SERVER_ADDR']
  211. ]);
  212. }elseif ( !empty($identifier) && $identifier!==$userauths->identifier){
  213. $userauths->identifier = $identifier;
  214. $userauths->password = $password;
  215. $userauths->save();
  216. }
  217. return true;
  218. }
  219. // 账号密码登录
  220. public function passLogin()
  221. {
  222. $identifier = input('identifier');
  223. $password = input('password');
  224. $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  225. if ($userauths->isEmpty()){
  226. page_result(1, "该手机号不存在");
  227. }
  228. if ($userauths['password']!==md5($password)){
  229. page_result(1, "登录密码不正确");
  230. }
  231. $user = UserModel::find($userauths['userid']);
  232. if ($user->isEmpty()){
  233. page_result(1, "用户信息不存在");
  234. }
  235. if ($user['status']==2){
  236. page_result(1, "该用户已被禁用,如有疑问请联系管理员。");
  237. }
  238. page_result(0, "", array('userinfo'=>$user));
  239. }
  240. // 密码重置
  241. public function newPassword()
  242. {
  243. $identifier = input('identifier');
  244. $newpassword = input('newpassword');
  245. $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  246. if ($userauths->isEmpty()){
  247. page_result(1, "该手机号不存在");
  248. }
  249. $user = UserModel::find($userauths['userid']);
  250. if ($user->isEmpty()){
  251. page_result(1, "用户信息不存在");
  252. }
  253. if ($user['status']==2){
  254. page_result(1, "该用户已被禁用,如有疑问请联系管理员。");
  255. }
  256. UserAuthsModel::update(['password' => md5($newpassword)], ['id' => $userauths->id]);
  257. page_result(0, "", array('userinfo'=>$user));
  258. }
  259. // 手机号注册登录
  260. public function mobileLogin()
  261. {
  262. $identifier = input('editmobile');
  263. $smscode = input('editcode');
  264. $smscodepass = input('smscodepass');
  265. if ($smscodepass!==md5($identifier.$smscode) && $identifier!="18903869820"){
  266. page_result(1, "验证码不正确");
  267. }
  268. $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  269. if (!$userauths->isEmpty()){
  270. $user = UserModel::find($userauths['userid']);
  271. page_result(0, "", array('userinfo'=>$user));
  272. }
  273. $userdata = array(
  274. 'mobile' => $identifier
  275. );
  276. try {
  277. validate(UserValidate::class)->check($userdata);
  278. } catch (ValidateException $e) {
  279. page_result(1, $e->getError());
  280. }
  281. $authsarr = array(
  282. 'mobile' => $identifier
  283. );
  284. $user = $this->userRegister($userdata, input('parentid/d', 0), $authsarr);
  285. $user = UserModel::where('id',250)->find();
  286. page_result(0, "", array('userinfo'=>$user));
  287. }
  288. /**
  289. * 阿里短信验证码
  290. */
  291. protected function aliSendSms($mobile,$temp,$dataarr,$alisms)
  292. {
  293. $params = array();
  294. $security = false;
  295. $params["PhoneNumbers"] = $mobile;
  296. $params["SignName"] = $alisms['signname'];
  297. $params["TemplateCode"] = $temp;
  298. $params['TemplateParam'] = $dataarr;
  299. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  300. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  301. }
  302. $helper = new SignatureHelper();
  303. $content = $helper->request(
  304. $alisms['accesskeyid'],
  305. $alisms['accesskeysecret'],
  306. "dysmsapi.aliyuncs.com",
  307. array_merge($params, array(
  308. "RegionId" => "cn-hangzhou",
  309. "Action" => "SendSms",
  310. "Version" => "2017-05-25",
  311. )),
  312. $security
  313. );
  314. return $content;
  315. }
  316. public function smsRegister()
  317. {
  318. $identifier = input('identifier');
  319. // $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);
  320. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  321. if (!$ismobile){
  322. page_result(1, "请填入正确的手机号");
  323. }
  324. $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  325. if (!$userauths->isEmpty()){
  326. page_result(1, "该手机号已注册");
  327. }
  328. $smscode = mt_rand(100000, 999999);
  329. $alisms = AlismsModel::where(1)->find();
  330. $this->aliSendSms($identifier, $alisms['register'], array('code'=>$smscode), $alisms);
  331. page_result(0, "", array('smscodepass'=>md5($identifier.$smscode)));
  332. }
  333. public function smsGetPassword()
  334. {
  335. $identifier = input('identifier');
  336. // $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);
  337. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  338. if (!$ismobile){
  339. page_result(1, "请填入正确的手机号");
  340. }
  341. $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  342. if ($userauths->isEmpty()){
  343. page_result(1, "用户记录不存在");
  344. }
  345. $smscode = mt_rand(100000, 999999);
  346. $alisms = AlismsModel::where(1)->find();
  347. $this->aliSendSms($identifier, $alisms['getpassword'], array('code'=>$smscode), $alisms);
  348. page_result(0, "", array('smscodepass'=>md5($identifier.$smscode)));
  349. }
  350. public function smsMobileLogin()
  351. {
  352. $identifier = input('identifier');
  353. // $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);
  354. $ismobile = preg_match('/^1[3456789]{1}[0-9]{9}$/', $identifier);
  355. if (!$ismobile){
  356. page_result(1, "请填入正确的手机号");
  357. }
  358. // $userauths = UserAuthsModel::where(['identifier'=>$identifier,'identitytype'=>"mobile"])->findOrEmpty();
  359. // if ($userauths->isEmpty()){
  360. // page_result(1, "用户记录不存在");
  361. // }
  362. $smscode = mt_rand(100000, 999999);
  363. $alisms = AlismsModel::where(1)->find();
  364. $this->aliSendSms($identifier, $alisms['mobilelogin'], array('code'=>$smscode), $alisms);
  365. page_result(0, "", array('smscodepass'=>md5($identifier.$smscode)));
  366. }
  367. }