Membersecurity.php 23 KB

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