Membersecurity.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Membersecurity extends BaseMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberpoints.lang.php');
  18. }
  19. public function index()
  20. {
  21. $member_info = $this->member_info;
  22. $member_info['security_level'] = model('member')->getMemberSecurityLevel($member_info);
  23. View::assign('member_info', $member_info);
  24. /* 设置买家当前菜单 */
  25. $this->setMemberCurMenu('member_security');
  26. /* 设置买家当前栏目 */
  27. $this->setMemberCurItem('index');
  28. return View::fetch($this->template_dir . 'index');
  29. }
  30. /**
  31. * 绑定邮箱 - 发送邮件
  32. */
  33. public function send_bind_email()
  34. {
  35. $email = input('param.email');
  36. $membersecurity_validate = ds_validate('membersecurity');
  37. if (!$membersecurity_validate->scene('send_bind_email')->check(array('email' => $email))) {
  38. ds_json_encode(10001, $membersecurity_validate->getError());
  39. }
  40. $member_model = model('member');
  41. $condition = array();
  42. $condition[] = array('member_email', '=', $email);
  43. $condition[] = array('member_id', '<>', session('member_id'));
  44. $member_info = $member_model->getMemberInfo($condition, 'member_id');
  45. if ($member_info) {
  46. ds_json_encode(10001, lang('mailbox_has_been_used'));
  47. }
  48. //验证发送频率
  49. $verify_code_model = model('verify_code');
  50. $result = $verify_code_model->isVerifyCodeFrequant(5, 1);
  51. if (!$result['code']) {
  52. ds_json_encode(10001, $result['msg']);
  53. }
  54. $verify_code = $verify_code_model->genVerifyCode(5, 1);
  55. if (!$verify_code) {
  56. ds_json_encode(10001, lang('system_error'));
  57. }
  58. $uid = base64_encode(ds_encrypt(session('member_id') . ' ' . $email));
  59. $verify_url = HOME_SITE_URL . '/Login/bind_email.html?uid=' . $uid . '&hash=' . md5($verify_code);
  60. $mailtemplates_model = model('mailtemplates');
  61. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => 'bind_email'));
  62. $param = array();
  63. $param['site_name'] = config('ds_config.site_name');
  64. $param['user_name'] = session('member_name');
  65. $param['verify_url'] = $verify_url;
  66. $subject = ds_replace_text($tpl_info['mailmt_title'], $param);
  67. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  68. $message = htmlspecialchars_decode($message);
  69. $ob_email = new \sendmsg\Email();
  70. $result = $ob_email->send_sys_email($email, $subject, $message);
  71. if ($result) {
  72. $ip = request()->ip();
  73. $flag = $verify_code_model->addVerifyCode(array(
  74. 'verify_code_type' => 5,
  75. 'verify_code' => $verify_code,
  76. 'verify_code_user_type' => 1,
  77. 'verify_code_user_id' => session('member_id'),
  78. 'verify_code_user_name' => session('member_name'),
  79. 'verify_code_add_time' => TIMESTAMP,
  80. 'verify_code_ip' => $ip,
  81. ));
  82. if (!$flag) {
  83. ds_json_encode(10001, lang('system_error'));
  84. }
  85. $data = array();
  86. $data['member_email'] = $email;
  87. $data['member_emailbind'] = 0;
  88. $member_model->editMember(array('member_id' => session('member_id')), $data, session('member_id'));
  89. ds_json_encode(10000, lang('verify_mail_been_sent_mailbox'));
  90. } else {
  91. ds_json_encode(10001, lang('system_error'));
  92. }
  93. }
  94. public function auth()
  95. {
  96. $member_model = model('member');
  97. $type = input('param.type');
  98. if (!request()->isPost()) {
  99. if (!in_array($type, array('modify_pwd', 'modify_mobile', 'modify_email', 'modify_paypwd', 'pd_cash'))) {
  100. $this->redirect('Membersecurity/index');
  101. }
  102. //继承父类的member_info
  103. $member_info = $this->member_info;
  104. if (!$member_info) {
  105. $member_info = $member_model->getMemberInfo(array('member_id' => session('member_id')), 'member_email,member_emailbind,member_mobile,member_mobilebind');
  106. }
  107. //第一次绑定邮箱,不用发验证码,直接进下一步
  108. //第一次绑定手机,不用发验证码,直接进下一步
  109. if (($type == 'modify_email' && $member_info['member_emailbind'] == '0') || ($type == 'modify_mobile' && $member_info['member_mobilebind'] == '0')) {
  110. session('auth_' . $type, TIMESTAMP);
  111. /* 设置买家当前菜单 */
  112. $this->setMemberCurMenu('member_security');
  113. /* 设置买家当前栏目 */
  114. $this->setMemberCurItem($type);
  115. echo View::fetch($this->template_dir . $type);
  116. exit;
  117. }
  118. //修改密码、设置支付密码时,必须绑定邮箱或手机
  119. if (in_array($type, array('modify_pwd', 'modify_paypwd')) && $member_info['member_emailbind'] == '0' && $member_info['member_mobilebind'] == '0') {
  120. $this->error(lang('please_bind_email_phone_first'), 'membersecurity/index');
  121. }
  122. View::assign('member_info', $member_info);
  123. /* 设置买家当前菜单 */
  124. $this->setMemberCurMenu('member_security');
  125. /* 设置买家当前栏目 */
  126. $this->setMemberCurItem($type);
  127. return View::fetch($this->template_dir . 'auth');
  128. } else {
  129. if (!in_array($type, array('modify_pwd', 'modify_mobile', 'modify_email', 'modify_paypwd', 'pd_cash'))) {
  130. $this->redirect((string)url('Membersecurity/index'));
  131. }
  132. $verify_code = input('post.auth_code');
  133. $validate_data = array(
  134. 'verify_code' => $verify_code,
  135. );
  136. $verify_code_validate = ds_validate('verify_code');
  137. if (!$verify_code_validate->scene('verify_code_search')->check($validate_data)) {
  138. $this->error($verify_code_validate->getError());
  139. }
  140. $verify_code_model = model('verify_code');
  141. if (!$verify_code_model->getVerifyCodeInfo(array(array('verify_code_type', '=', 6), array('verify_code_user_type', '=', 1), array('verify_code_user_id', '=', session('member_id')), array('verify_code', '=', $verify_code), array('verify_code_add_time', '>', TIMESTAMP - VERIFY_CODE_INVALIDE_MINUTE * 60)))) {
  142. $this->error(lang('validation_fails'));
  143. }
  144. //当类型为提现 获取用户绑定的银行卡账户列表
  145. if ($type == 'pd_cash') {
  146. $memberbank_list = model('memberbank')->getMemberbankList(array('member_id' => session('member_id')));
  147. $member_wxinfo = unserialize($this->member_info['member_wxinfo']);
  148. if (!empty($member_wxinfo) && is_array($member_wxinfo) && isset($member_wxinfo['member_wxopenid']) && $member_wxinfo['member_wxopenid']) {
  149. if (empty($memberbank_list)) {
  150. $memberbank_list = array();
  151. }
  152. $memberbank_list[] = array('memberbank_id' => -1, 'memberbank_type' => 'weixin', 'memberbank_no' => $member_wxinfo['nickname'], 'member_wxinfo' => $member_wxinfo);
  153. }
  154. View::assign('memberbank_list', $memberbank_list);
  155. }
  156. session('auth_' . $type, TIMESTAMP);
  157. /* 设置买家当前菜单 */
  158. $this->setMemberCurMenu('member_security');
  159. /* 设置买家当前栏目 */
  160. $this->setMemberCurItem($type);
  161. return View::fetch($this->template_dir . $type);
  162. }
  163. }
  164. /**
  165. * 统一发送身份验证码
  166. */
  167. public function send_auth_code()
  168. {
  169. $type = input('param.type');
  170. if (!in_array($type, array('email', 'mobile')))
  171. exit();
  172. $member_model = model('member');
  173. $member_info = $member_model->getMemberInfoByID(session('member_id'));
  174. //验证发送频率
  175. $verify_code_model = model('verify_code');
  176. $result = $verify_code_model->isVerifyCodeFrequant(6, 1);
  177. if (!$result['code']) {
  178. exit(json_encode(array('state' => 'false', 'msg' => $result['msg'])));
  179. }
  180. $verify_code = $verify_code_model->genVerifyCode(6, 1);
  181. if (!$verify_code) {
  182. exit(json_encode(array('state' => 'false', 'msg' => lang('system_error'))));
  183. }
  184. $mailtemplates_model = model('mailtemplates');
  185. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => 'authenticate'));
  186. $param = array();
  187. $param['code'] = $verify_code;
  188. $ten_param = array($verify_code);
  189. $subject = ds_replace_text($tpl_info['mailmt_title'], $param);
  190. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  191. if ($type == 'email') {
  192. $email = new \sendmsg\Email();
  193. $result['state'] = $email->send_sys_email($member_info["member_email"], $subject, $message);
  194. } elseif ($type == 'mobile') {
  195. $smslog_param = array(
  196. 'ali_template_code' => $tpl_info['ali_template_code'],
  197. 'ali_template_param' => $param,
  198. 'ten_template_code' => $tpl_info['ten_template_code'],
  199. 'ten_template_param' => $ten_param,
  200. 'message' => $message,
  201. );
  202. $result = model('smslog')->sendSms($member_info["member_mobile"], $smslog_param, 5, $verify_code);
  203. }
  204. if ($result['state']) {
  205. $ip = request()->ip();
  206. $flag = $verify_code_model->addVerifyCode(array(
  207. 'verify_code_type' => 6,
  208. 'verify_code' => $verify_code,
  209. 'verify_code_user_type' => 1,
  210. 'verify_code_user_id' => session('member_id'),
  211. 'verify_code_user_name' => session('member_name'),
  212. 'verify_code_add_time' => TIMESTAMP,
  213. 'verify_code_ip' => $ip,
  214. ));
  215. if (!$flag) {
  216. exit(json_encode(array('state' => 'false', 'msg' => lang('system_error'))));
  217. }
  218. exit(json_encode(array('state' => 'true', 'msg' => lang('verification_code_has_been_sent'))));
  219. } else {
  220. exit(json_encode(array('state' => 'false', 'msg' => isset($result['message']) ? $result['message'] : lang('verification_code_sending_failed'))));
  221. }
  222. }
  223. /**
  224. * 修改密码
  225. */
  226. public function modify_pwd()
  227. {
  228. $member_model = model('member');
  229. //身份验证后,需要在30分钟内完成修改密码操作
  230. if (TIMESTAMP - session('auth_modify_pwd') > 1800) {
  231. ds_json_encode(10001, lang('operation_timed_out'));
  232. }
  233. if (!request()->isPost()) {
  234. exit();
  235. }
  236. $data = array(
  237. 'password' => input('post.password'),
  238. 'confirm_password' => input('post.confirm_password'),
  239. );
  240. $membersecurity_validate = ds_validate('membersecurity');
  241. if (!$membersecurity_validate->scene('modify_pwd')->check($data)) {
  242. ds_json_encode(10001, $membersecurity_validate->getError());
  243. }
  244. if ($data['password'] != $data['confirm_password']) {
  245. ds_json_encode(10001, lang('two_password_inconsistencies'));
  246. }
  247. //判断当前的密码是否和原密码相同
  248. $member_info = $member_model->getMemberInfo(array('member_id' => session('member_id')));
  249. if ($member_info['member_password'] == md5($data['password'])) {
  250. ds_json_encode(10001, lang('new_password_same'));
  251. }
  252. $update = $member_model->editMember(array('member_id' => session('member_id')), array('member_password' => md5($data['password'])), session('member_id'));
  253. $message = $update ? lang('password_modify_successfully') : 'operation_timed_out';
  254. session('auth_modify_pwd', NULL);
  255. if ($update) {
  256. ds_json_encode(10000, $message);
  257. } else {
  258. ds_json_encode(10001, $message);
  259. }
  260. }
  261. /**
  262. * 设置支付密码
  263. */
  264. public function modify_paypwd()
  265. {
  266. $member_model = model('member');
  267. //身份验证后,需要在30分钟内完成修改密码操作
  268. if (TIMESTAMP - session('auth_modify_paypwd') > 1800) {
  269. $this->error(lang('operation_timed_out'), (string)url('Membersecurity/auth', ['type' => 'modify_paypwd']));
  270. }
  271. if (!request()->isPost())
  272. exit();
  273. $data = array(
  274. 'password' => input('post.password'),
  275. 'confirm_password' => input('post.confirm_password'),
  276. );
  277. $membersecurity_validate = ds_validate('membersecurity');
  278. if (!$membersecurity_validate->scene('modify_paypwd')->check($data)) {
  279. ds_json_encode(10001, $membersecurity_validate->getError());
  280. }
  281. if ($data['password'] != $data['confirm_password']) {
  282. ds_json_encode(10001, lang('two_password_inconsistencies'));
  283. }
  284. $update = $member_model->editMember(array('member_id' => session('member_id')), array('member_paypwd' => md5($data['password'])), session('member_id'));
  285. $message = $update ? lang('password_set_successfully') : lang('password_setting_failed');
  286. session('auth_modify_paypwd', NULL);
  287. if ($update) {
  288. ds_json_encode(10000, $message);
  289. } else {
  290. ds_json_encode(10001, $message);
  291. }
  292. }
  293. /**
  294. * 绑定手机
  295. */
  296. public function modify_mobile()
  297. {
  298. $member_model = model('member');
  299. $member_model->getMemberInfoByID(session('member_id'));
  300. if (request()->isPost()) {
  301. $data = array(
  302. 'mobile' => input('post.mobile'),
  303. 'vcode' => input('post.vcode'),
  304. );
  305. $membersecurity_validate = ds_validate('membersecurity');
  306. if (!$membersecurity_validate->scene('modify_mobile')->check($data)) {
  307. ds_json_encode(10001, $membersecurity_validate->getError());
  308. }
  309. $verify_code = input('post.vcode');
  310. $validate_data = array(
  311. 'verify_code' => $verify_code,
  312. );
  313. $verify_code_validate = ds_validate('verify_code');
  314. if (!$verify_code_validate->scene('verify_code_search')->check($validate_data)) {
  315. ds_json_encode(10001, $verify_code_validate->getError());
  316. }
  317. $verify_code_model = model('verify_code');
  318. if (!$verify_code_model->getVerifyCodeInfo(array(array('verify_code_type', '=', 4), array('verify_code_user_type', '=', 1), array('verify_code_user_id', '=', session('member_id')), array('verify_code', '=', $verify_code), array('verify_code_add_time', '>', TIMESTAMP - VERIFY_CODE_INVALIDE_MINUTE * 60)))) {
  319. ds_json_encode(10001, lang('mobile_verification_code_error'));
  320. }
  321. $member_model->editMember(array('member_id' => session('member_id')), array('member_mobilebind' => 1), session('member_id'));
  322. ds_json_encode(10000, lang('phone_number_bound_successfully'));
  323. }
  324. }
  325. /**
  326. * 修改手机号 - 发送验证码
  327. */
  328. public function send_modify_mobile()
  329. {
  330. $mobile = input('param.mobile');
  331. $membersecurity_validate = ds_validate('membersecurity');
  332. if (!$membersecurity_validate->scene('send_modify_mobile')->check(array('mobile' => $mobile))) {
  333. exit(json_encode(array('state' => 'false', 'msg' => $membersecurity_validate->getError())));
  334. }
  335. $member_model = model('member');
  336. $condition = array();
  337. $condition[] = array('member_mobile', '=', $mobile);
  338. $condition[] = array('member_id', '<>', session('member_id'));
  339. $member_info = $member_model->getMemberInfo($condition, 'member_id');
  340. if ($member_info) {
  341. exit(json_encode(array('state' => 'false', 'msg' => lang('please_change_another_phone_number'))));
  342. }
  343. //验证发送频率
  344. $verify_code_model = model('verify_code');
  345. $result = $verify_code_model->isVerifyCodeFrequant(4, 1);
  346. if (!$result['code']) {
  347. exit(json_encode(array('state' => 'false', 'msg' => $result['msg'])));
  348. }
  349. $verify_code = $verify_code_model->genVerifyCode(4, 1);
  350. if (!$verify_code) {
  351. exit(json_encode(array('state' => 'false', 'msg' => lang('system_error'))));
  352. }
  353. $mailtemplates_model = model('mailtemplates');
  354. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => 'modify_mobile'));
  355. $param = array();
  356. $param['code'] = $verify_code;
  357. $ten_param = array($verify_code);
  358. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  359. $smslog_param = array(
  360. 'ali_template_code' => $tpl_info['ali_template_code'],
  361. 'ali_template_param' => $param,
  362. 'ten_template_code' => $tpl_info['ten_template_code'],
  363. 'ten_template_param' => $ten_param,
  364. 'message' => $message,
  365. );
  366. $result = model('smslog')->sendSms($mobile, $smslog_param, 4, $verify_code);
  367. if (!$result['state']) {
  368. exit(json_encode(array('state' => 'false', 'msg' => $result['message'])));
  369. }
  370. $ip = request()->ip();
  371. $flag = $verify_code_model->addVerifyCode(array(
  372. 'verify_code_type' => 4,
  373. 'verify_code' => $verify_code,
  374. 'verify_code_user_type' => 1,
  375. 'verify_code_user_id' => session('member_id'),
  376. 'verify_code_user_name' => session('member_name'),
  377. 'verify_code_add_time' => TIMESTAMP,
  378. 'verify_code_ip' => $ip,
  379. ));
  380. if (!$flag) {
  381. exit(json_encode(array('state' => 'false', 'msg' => lang('system_error'))));
  382. }
  383. $update = $member_model->editMember(array('member_id' => session('member_id')), array('member_mobile' => $mobile), session('member_id'));
  384. if (!$update) {
  385. exit(json_encode(array('state' => 'false', 'msg' => lang('modified_phone_same_original_one'))));
  386. } else {
  387. exit(json_encode(array('state' => 'true', 'msg' => lang('send_success'))));
  388. }
  389. }
  390. /**
  391. * 用户中心右边,小导航
  392. *
  393. * @param string $menu_type 导航类型
  394. * @param string $menu_key 当前导航的menu_key
  395. * @return
  396. */
  397. protected function getMemberItemList()
  398. {
  399. $menu_name = request()->action();
  400. switch ($menu_name) {
  401. case 'index':
  402. $menu_array = array(
  403. array(
  404. 'name' => 'index', 'text' => lang('account_security'),
  405. 'url' => (string)url('Membersecurity/index')
  406. )
  407. );
  408. return $menu_array;
  409. break;
  410. case 'modify_pwd':
  411. $menu_array = array(
  412. array(
  413. 'name' => 'index', 'text' => lang('account_security'),
  414. 'url' => (string)url('Membersecurity/index')
  415. ), array(
  416. 'name' => 'modify_pwd', 'text' => lang('change_login_password'),
  417. 'url' => (string)url('Membersecurity/auth', ['type' => 'modify_pwd'])
  418. ),
  419. );
  420. return $menu_array;
  421. break;
  422. case 'modify_email':
  423. $menu_array = array(
  424. array(
  425. 'name' => 'index', 'text' => lang('account_security'),
  426. 'url' => (string)url('Membersecurity/index')
  427. ), array(
  428. 'name' => 'modify_email', 'text' => lang('email_address_verification'),
  429. 'url' => (string)url('Membersecurity/auth', ['type' => 'modify_email'])
  430. ),
  431. );
  432. return $menu_array;
  433. break;
  434. case 'modify_mobile':
  435. $menu_array = array(
  436. array(
  437. 'name' => 'index', 'text' => lang('account_security'),
  438. 'url' => (string)url('Membersecurity/index')
  439. ), array(
  440. 'name' => 'modify_mobile', 'text' => lang('phone_verification'),
  441. 'url' => (string)url('Membersecurity/auth', ['type' => 'modify_mobile'])
  442. ),
  443. );
  444. return $menu_array;
  445. break;
  446. case 'modify_paypwd':
  447. $menu_array = array(
  448. array(
  449. 'name' => 'index', 'text' => lang('account_security'),
  450. 'url' => (string)url('Membersecurity/index')
  451. ), array(
  452. 'name' => 'modify_paypwd', 'text' => lang('set_payment_password'),
  453. 'url' => (string)url('Membersecurity/auth', ['type' => 'modify_paypwd'])
  454. ),
  455. );
  456. return $menu_array;
  457. break;
  458. case 'auth':
  459. $menu_array = array(
  460. array(
  461. 'name' => 'loglist', 'text' => lang('account_balance'),
  462. 'url' => (string)url('Predeposit/pd_log_list')
  463. ), array(
  464. 'name' => 'recharge_list', 'text' => lang('top_up_detail'),
  465. 'url' => (string)url('Predeposit/index')
  466. ), array(
  467. 'name' => 'cashlist', 'text' => lang('balance_withdrawal'),
  468. 'url' => (string)url('Predeposit/pd_cash_list')
  469. ), array(
  470. 'name' => 'pd_cash', 'text' => lang('withdrawal_application'),
  471. 'url' => (string)url('Membersecurity/auth', ['type' => 'pd_cash'])
  472. ),
  473. );
  474. return $menu_array;
  475. break;
  476. }
  477. }
  478. }