Membersecurity.php 23 KB

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