## 会员管理
>[info] 功能:
1,显示数据:通过关联`wp_users_discount`查询 `wp_users` 会员管理表 显示会员
public function scopeDefaultWhere($query)
{
parent::scopeDefaultWhere($query);
$wq = request_data('wq');
if($wq){
$query->where(function($q) use($wq)
{
$q->where('phone', 'like', "%".$wq."%");
});
}
$in = \cs\login::login_user(2);
if($in){
$query->whereIn('users_id',$in);
}
}
>[info]2,插入数据:新建会员 ,编辑数据:修改会员信息,如果会员参与折扣 则把折扣数 插入到 `wp_users_discount` 表中
/*
* @desc 开启事务 保存表单数据
*
*/
static function saveForm(){
try{
DB::transaction(function()
{
$code = cache("sms_".post_data('phone'));
if($code != post_data('code') && post_data('code')){
exit(json_encode(['status'=>0,'msg'=>__('验证码有误,请重新输入') ]));
}
$data = post_data();
$user_id = login::getUid(null,1)?:cookie('users_id');
if($data['sex']== "男"){
$data['sex'] = 1 ;
}elseif($data['sex']== "女"){
$data['sex'] = 2 ;
}elseif($data['sex'] == "保密"){
$data['sex'] = 3 ;
}else{
$data['sex'] = 0;
}
if($data['status']==1 || $data['discount'])
{
if(!preg_match('/^(?:[1-9]\d|100)$/',$data['discount'])){
exit(json_encode(['status'=>0,'msg'=>__('折扣率格式错误,请输入10~100以内的数值') ]));
}
}
$data['bron'] = strtotime($data['bron']);
if($data['id']){
if($data['phone']){
$model = self::find($data['id']);
$list = self::where('users_id',$user_id)->get();
if($list){
foreach($list as $k=>$v){
if($v->wp_user_id != $model->wp_user_id && $v->wp_users->phone == $data['phone']){
$in[] = $v['wp_user_id'];
}
}
}
if($in){
exit(json_encode(['status'=>0,'msg'=>__('该手机号已存在')]));
}
$models = wp_users::where('id',$model->wp_user_id)->first();
$date['status'] = $data['status'];
$date['discount'] = $data['discount'];
unset($data['id']);
unset($data['get_code']);
unset($data['status']);
unset($data['discount']);
$models->data($data)->save();
$model->data($date)->save();
}else{
exit(json_encode(['status'=>0,'msg'=>__('会员手机号不能为空') ]));
}
}else{
if(!post_data('ucenter_name')){
exit(json_encode(['msg'=>__('用户名不能为空') ]));
}
if(!post_data('phone')){
exit(json_encode(['msg'=>__('手机号不能为空') ]));
}
if(!post_data('code')){
exit(json_encode(['msg'=>__('验证码不能为空') ]));
}
$wp_users = new wp_users;
$model = new self;
$phone = $wp_users->where('phone',$data['phone'])->first();
if($phone){
$new_phone = $model::where('users_id',$user_id)->where('wp_user_id',$phone->id)->first();
if($new_phone){
exit(json_encode(['status'=>0,'msg'=>__('该手机号已被注册')]));
}else{
$date['sex'] = $data['sex'];
$date['email'] = $data['email'];
$date['realname'] = $data['realname'];
$date['ucenter_name'] = $data['ucenter_name'];
$date['phone'] = $data['phone'];
unset($data['sex']);
unset($data['phone']);
unset($data['email']);
unset($data['realname']);
unset($data['ucenter_name']);
unset($data['code']);
unset($data['bron']);
$data['users_id'] = cookie('admin_level')==1?post_data('users_id'):$user_id;;
$data['wp_user_id'] = $phone->id;
$data['numbers'] = 'YF-'.time().cookie('admin_id');
$data['started'] = time();
$data['ended'] = time()+365*84600*5;
$model->data($data)->save();
$wp_users->where('id',$phone->id)->update($date);
}
}else{
$name = $data['ucenter_name'];
$passwd = '111111';
$rs = api::add($name,$passwd);
$data['ucenter_id'] = $rs['data']['id'];
$msg = $rs['msg'];
if($rs['status'] != 200){
exit(json_encode(['status'=>0,'msg'=>__($msg)]));
}
if($data['status']==1 || $data['discount'])
{
$date['status'] = $data['status'];
$date['discount'] = $data['discount'];
unset($data['status']);
unset($data['discount']);
}
$data['created'] = time();
$wp_users->data($data)->save();
$id = last_insert_id();
if($id){
$date['wp_user_id'] = $id;
$date['users_id'] = cookie('admin_level')==1?post_data('users_id'):$user_id;
$date['numbers'] = 'YF-'.time().cookie('admin_id');
$date['started'] = time();
$date['ended'] = time()+365*84600*5;
$model->insert($date);
}
}
}
});
}catch(Exception $e){
return false;
}
return true;
}
>[info] 3,关联`wp_users_discount` 会员折扣表 来查看会员在不同商家下的折扣情况
/*
* @desc 折扣卡
*/
public function wp_users_discount(){
return $this->belongsToMany('models\wp_users_discount','wp_user_id');
}