<?php
/**
* +----------------------------------------------------------------------
* | TickyPHP [ This is a freeware ]
* +----------------------------------------------------------------------
* | Copyright (c) 2015 All rights reserved.
* +----------------------------------------------------------------------
* | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
* +----------------------------------------------------------------------
* | SVN: $Id: Member.php 87573 2018-09-04 09:49:32 luomingui $
* +----------------------------------------------------------------------
* | 文件功能:对应的表名:tky_member
* +----------------------------------------------------------------------
*/
namespace application\member\controller;
use application\admin\controller\auth as auth;
use ticky\request;
use ticky\response;
use application\member\models\point;
class member extends auth {
public $member_status = array('0' => '待审核', '1' => '通过审核', '2' => '账号锁定');
public function index() {
$default_password = get_config('default_password');
$search = $this->search_frm();
$ret = db('member')->where($search['sql'])->newpage();
foreach ($ret['items'] as $row) {
if ($row['lastlogintime'] == '0') {
$row['lastlogintime'] = "未登陆";
} else {
$row['lastlogintime'] = date("Y-m-d H:i:s", $row['lastlogintime']);
}
$data[] = $row;
}
$this->assign('page', $ret['page']);
$this->assign('items', $data);
$this->assign('search', $search['arr']);
$this->assign('default_password', $default_password);
$this->display('member/index');
}
public function add() {
if (request::isPost()) {
$data = $this->post_frm();
$default_password = md5(encrypt(get_config('default_password')));
$data['password'] = $default_password;
$data['experience'] = $this->get_experience($data['groupid']);
$userid = db('member')->add($data);
if ($userid) {
db('member_profile')->add(array('uid' => $userid, 'realname' => $_POST['realname'])); }
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '添加成功']);
} else {
showmsg('添加成功', '/admin/member');
}
} else {
$this->assign('postUrl', '/member/member/add');
$this->assign('action', '添加');
$this->display('member/member_add');
}
}
public function update() {
if (request::isPost()) {
$id = request::post('uid', 0);
$data = $this->post_frm();
foreach ($data as $k => $v) {
if (!$v)
unset($data[$k]);
}
$this->db->table('member')->where(['uid' => $id])->update($data);
$profile = db('member_profile')->where(['uid' => $id])->find();
$profile_post_frm = $this->member_profile_post_frm();
$profile_post_frm['uid'] = $id;
if ($profile) {
foreach ($profile_post_frm as $k => $v) {
if (!$v)
unset($profile_post_frm[$k]);
}
db('member_profile')->where(['uid' => $id])->update($profile_post_frm);
} else {
db('member_profile')->add($profile_post_frm);
}
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '修改成功']);
} else {
showmsg('修改成功', '/admin/member');
}
} else {
$uid = request::get('id', 0);
$item = db('member')->where(['uid' => $uid])->find();
$profile = db('member_profile')->where(['uid' => $uid])->find();
$item['regdate'] = date("Y-m-d H:i:s", $item['regdate']);
if ($item['lastlogintime'] == '0') {
$item['lastlogintime'] = "未登陆";
} else {
$item['lastlogintime'] = date("Y-m-d H:i:s", $item['lastlogintime']);
}
if ($profile) {
$item = array_merge($item, $profile);
}
$this->assign('postUrl', '/member/member/update');
$this->assign('action', '修改');
$this->assign('item', $item);
$this->display('member/manage_edit');
}
}
public function delete() {
$id = request::post('id', 0);
$result = $this->db->table('member')->where(['uid' => $id])->delete();
if (request::isAjax()) {
if ($result) {
response::ajax(['code' => 200, 'msg' => '删除成功!']);
} else {
response::ajax(['code' => 403, 'msg' => '删除失败! id=' . $id]);
}
} else {
showmsg('删除成功', '/admin/member');
}
}
public function batchremove() {
$optype = request::post('optype', 'del');
$ids = request::post('ids', []);
$msg = "";
if ($optype == "del") {
$this->db->table('member')->where('uid in(' . implode(",", $ids) . ')')->delete();
$msg = "批量删除成功";
} else if ($optype == 'check') {
$msg = "批量设置待审核用户成功";
$this->db->table('member')->where('uid in(' . implode(",", $ids) . ')')->update(array('status' => '0'));
} else if ($optype == 'adopt') {
$msg = "批量通过审核成功";
$this->db->table('member')->where('uid in(' . implode(",", $ids) . ')')->update(array('status' => '1'));
} else if ($optype == 'lock') {
$msg = "批量锁定用户成功";
$this->db->table('member')->where('uid in(' . implode(",", $ids) . ')')->update(array('status' => '2'));
} else if ($optype == 'reset') {
$msg = "批量重置用户密码为" . get_config('default_password') . "成功";
$default_password = md5(encrypt(get_config('default_password')));
$this->db->table('member')->where('uid in(' . implode(",", $ids) . ')')->update(array('password' => $default_password));
}
header("location:/member/member");
}
private function search_frm() {
$where = '1=1 and adminid=0 and status=1';
$search = request::get('search', []);
$search['groupid'] = request::get('groupid', 0);
$type = request::get('type', '');
$keyword = request::get('keyword', '');
$search['type'] = $type;
if ($keyword != '') {
$search['keyword'] = $keyword;
switch ($type) {
case "username":
$where .= "and username LIKE '%" . $keyword . "%' ";
break;
case "uid":
$where .= "and uid = '" . $keyword . "' ";
break;
case "email":
$where .= "and email LIKE '%" . $keyword . "%' ";
break;
case "loginnum":
$where .= "and loginnum = " . $keyword . " ";
break;
}
}
if ($groupid = trim($search['groupid'])) {
$where .= "and groupid = '" . $groupid . "' ";
}
if ($status = trim($search['status'])) {
$where .= "and status = '" . $status . "' ";
}
if (isset($_GET['startdate']) && isset($_GET['enddate']) && $_GET['startdate']) {
$where .= " AND `regdate` >= '" . strtotime($_GET['startdate']) . "' AND `regdate` <= '" . strtotime($_GET['enddate']) . "' ";
$search['startdate'] = $_GET['startdate'];
$search['enddate'] = $_GET['enddate'];
} else {
$search['enddate'] = date('Y-m-d');
}
return [
'arr' => $search,
'sql' => $where
];
}
private function post_frm() {
$arr = array();
$arr['username'] = request::post('username', '');
$arr['password'] = request::post('password', '');
$arr['email'] = request::post('email', '');
$arr['avatarstatus'] = request::post('avatarstatus', '0');
$arr['score'] = request::post('score', '0');
$arr['experience'] = request::post('experience', '0');
$arr['regip'] = request::getClientIp();
$arr['regdate'] = time();
$arr['lastloginip'] = '';
$arr['lastlogintime'] = '';
$arr['loginnum'] = request::post('loginnum', '0');
$arr['adminid'] = request::post('adminid', '0');
$arr['groupid'] = request::post('groupid', '1');
$arr['timeoffset'] = request::post('timeoffset', '');
$arr['status'] = request::post('status', '0');
$arr['dateline'] = time();
return $arr;
}
private function member_profile_post_frm() {
$arr = array();
$arr['realname'] = request::post('realname', '');
$arr['gender'] = request::post('gender', '');
$arr['birthyear'] = request::post('birthyear', '');
$arr['birthmonth'] = request::post('birthmonth', '');
$arr['birthday'] = request::post('birthday', '');
$arr['constellation'] = request::post('constellation', '');
$arr['zodiac'] = request::post('zodiac', '');
$arr['idcardtype'] = request::post('idcardtype', '');
$arr['idcard'] = request::post('idcard', '');
$arr['address'] = request::post('address', '');
$arr['zipcode'] = request::post('zipcode', '');
$arr['nationality'] = request::post('nationality', '');
$arr['birthprovince'] = request::post('birthprovince', '');
$arr['birthcity'] = request::post('birthcity', '');
$arr['birthdist'] = request::post('birthdist', '');
$arr['birthcommunity'] = request::post('birthcommunity', '');
$arr['resideprovince'] = request::post('resideprovince', '');
$arr['residecity'] = request::post('residecity', '');
$arr['residedist'] = request::post('residedist', '');
$arr['residecommunity'] = request::post('residecommunity', '');
$arr['residesuite'] = request::post('residesuite', '');
$arr['graduateschool'] = request::post('graduateschool', '');
$arr['company'] = request::post('company', '');
$arr['education'] = request::post('education', '');
$arr['occupation'] = request::post('occupation', '');
$arr['position'] = request::post('position', '');
$arr['revenue'] = request::post('revenue', '');
$arr['affectivestatus'] = request::post('affectivestatus', '');
$arr['lookingfor'] = request::post('lookingfor', '');
$arr['bloodtype'] = request::post('bloodtype', '');
$arr['height'] = request::post('height', '');
$arr['weight'] = request::post('weight', '');
$arr['alipay'] = request::post('alipay', '');
$arr['icq'] = request::post('icq', '');
$arr['qq'] = request::post('qq', '');
$arr['yahoo'] = request::post('yahoo', '');
$arr['msn'] = request::post('msn', '');
$arr['taobao'] = request::post('taobao', '');
$arr['site'] = request::post('site', '');
$arr['bio'] = request::post('bio', '');
$arr['interest'] = request::post('interest', '');
return $arr;
}
private function get_experience($groupid) {
if ($groupid == 1)
return 0;
$member_group = get_groupinfo();
if ($member_group[$groupid - 1]) {
return $member_group[$groupid - 1]['experience'] + 1;
}
return 0;
}
public function reset_password() {
$user_id = request::get('user_id', 0);
$user = db('member')->where(array('uid' => $user_id))->find();
if (!$user) {
response::ajax(['code' => 301, 'msg' => '抱歉,该会员不存在', 'data' => $user]);
}
$default_password = md5(encrypt(get_config('default_password')));
$result = $this->db->table('member')->where(['uid' => $user_id])->update(['password' => $default_password]);
if ($result !== false) {
response::ajax(['code' => 200, 'msg' => '密码重置成功!']);
} else {
response::ajax(['code' => 500, 'msg' => '密码重置失败!']);
}
}
public function change_password() {
if (request::isPost()) {
$name = request::post('username', '');
$password = request::post('password', '');
$default_password = md5(encrypt($password));
$user = $this->db->table('member')->where(['username' => $name])->find();
if ($user) {
$result = $this->db->table('member')->where(['username' => $name])->update(['password' => $default_password]);
if ($result !== false) {
response::ajax(['code' => 200, 'msg' => '更新成功!']);
} else {
response::ajax(['code' => 500, 'msg' => '更新失败!']);
}
} else {
response::ajax(['code' => 301, 'msg' => '抱歉,该会员不存在']);
}
} else {
$this->display('member/change_password');
}
}
public function check() {
$ret = db('member')->where(array('status' => 0))->newpage();
foreach ($ret['items'] as $row) {
if ($row['lastlogintime'] == '0') {
$row['lastlogintime'] = "未登陆";
} else {
$row['lastlogintime'] = date("Y-m-d H:i:s", $row['lastlogintime']);
}
$data[] = $row;
}
$this->assign('page', $ret['page']);
$this->assign('items', $data);
$this->display('member/member_check');
}
public function recharge() {
if (request::isPost()) {
$username = isset($_POST['username']) && is_username($_POST['username']) ? trim($_POST['username']) : showmsg('用户名格式错误!');
$userinfo = db('member')->field('uid,email')->where(array('username' => $username))->find();
if ($userinfo) {
if ($_POST['unit']) {
point::instance()->point_add($_POST['type'], floatval($_POST['money']), 4, $userinfo['userid'], $username, 0, $_POST['remarks'], $_SESSION['adminname'], false);
} else {
point::instance()->point_spend($_POST['type'], floatval($_POST['money']), 4, $userinfo['userid'], $username, $_POST['remarks']);
}
if (isset($_POST['sendemail'])) {
$type = $_POST['type'] == '1' ? '积分' : '元';
$content = '您的账户于' . date('Y-m-d H:i:s', SYS_TIME) . '成功充值' . floatval($_POST['money']) . $type . ',详情请登录会员中心查看。';
sendmail($userinfo['email'], '充值到账通知', $content);
}
$op = $_POST['unit'] == '1' ? 'pay' : 'pay_spend';
showmsg('操作成功!', $op, 2);
} else {
showmsg('操作失败!');
}
} else {
$this->display('member/recharge');
}
}
}