You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
13 KiB
357 lines
13 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\api\member;
|
|
|
|
use addon\zhjw\app\model\classes\Classes;
|
|
use addon\zhjw\app\model\courses\Courses;
|
|
use addon\zhjw\app\model\staff\Staff;
|
|
use addon\zhjw\app\model\students\Students;
|
|
use addon\zhjw\app\model\timetables\Timetables;
|
|
use addon\zhjw\app\model\venues\Venues;
|
|
use app\dict\sys\AppTypeDict;
|
|
use app\model\member\Member;
|
|
use app\service\core\member\CoreMemberService;
|
|
use core\base\BaseApiService;
|
|
use core\exception\ApiException;
|
|
use core\util\Barcode;
|
|
use core\util\TokenAuth;
|
|
use think\facade\Db;
|
|
use think\Model;
|
|
|
|
/**
|
|
* 会员服务层
|
|
* Class MemberService
|
|
* @package app\service\api\member
|
|
*/
|
|
class MemberService extends BaseApiService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new Member();
|
|
}
|
|
|
|
/**
|
|
* 新增会员
|
|
*/
|
|
public function add(array $data){
|
|
return $this->model->create($data)?->member_id ?? 0;
|
|
}
|
|
|
|
/**
|
|
* 更新会员
|
|
* @param array $data
|
|
* @return true
|
|
*/
|
|
public function edit(array $data)
|
|
{
|
|
$member = $this->findMemberInfo(['member_id' => $this->member_id]);
|
|
|
|
if($member->isEmpty()) throw new ApiException('MEMBER_NOT_EXIST');
|
|
$member->allowField(['nickname', 'headimg', 'birthday', 'sex', 'last_visit_time'])->save($data);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取会员信息
|
|
* @return array
|
|
*/
|
|
public function getInfo()
|
|
{
|
|
$field = 'member_id, username, member_no, mobile, register_channel, nickname, headimg, member_level, member_label, login_ip, login_type, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, point, balance, growth, is_member, member_time, is_del, province_id, city_id, district_id, address, location, money, money_get, wx_openid, weapp_openid, commission, commission_get, commission_cash_outing';
|
|
$info = $this->model->where([['member_id', '=', $this->member_id]])
|
|
->with(['member_level_name_bind'])
|
|
->field($field)
|
|
->append(['sex_name'])
|
|
->findOrEmpty()->toArray();
|
|
|
|
$role_id = get_user_type($this->member_id);
|
|
if($role_id == 1){
|
|
//学员信息
|
|
$staff_id = (new Staff())->where(['member_id' => $this->member_id])->value('id');
|
|
$data = (new Students())->where(['user_id' => $staff_id])->find();
|
|
$info['height'] = $data['height'];
|
|
$info['weight'] = $data['weight'];
|
|
$info['score'] = $data['score'];
|
|
$info['students_id'] = $data['id'];
|
|
$info['evaluation_time'] = $data['evaluation_time'];
|
|
|
|
$info['name'] = $data['name'] ?? '';
|
|
|
|
//课程
|
|
$classes = new Classes();
|
|
|
|
$classes_list = $classes
|
|
->alias("a")
|
|
->join(['school_venues' => 'b'],'a.venue_id = b.id','left')
|
|
->join(['school_timetables' => 'c'],'a.id = c.class_id','left')
|
|
->join(['school_courses' => 'd'],'c.courses_id = d.id','left')
|
|
->whereRaw("FIND_IN_SET(?, a.max_students)", [$info['students_id']])
|
|
->group("d.id")
|
|
->column("d.name");
|
|
$info['classes_list'] = implode(",", $classes_list);
|
|
|
|
$info['classes_count'] = count($classes_list);
|
|
|
|
//总课时数
|
|
$duration_count = $classes
|
|
->alias("a")
|
|
->join(['school_venues' => 'b'],'a.venue_id = b.id','left')
|
|
->join(['school_timetables' => 'c'],'a.id = c.class_id','left')
|
|
->join(['school_courses' => 'd'],'c.courses_id = d.id','left')
|
|
->whereRaw("FIND_IN_SET(?, a.max_students)", [$info['students_id']])
|
|
->group("d.id")
|
|
->sum("d.duration");
|
|
|
|
$sign_count = Db::name("zhjw_students_sign")->where(['students_id' => $info['students_id']])->sum("hour");
|
|
|
|
$info['sign_count'] = $sign_count;
|
|
|
|
$info['stay_sign_count'] = $duration_count - $sign_count;
|
|
}
|
|
|
|
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* 检测会员信息是否存在
|
|
* @return int
|
|
*/
|
|
public function getCount($condition)
|
|
{
|
|
return $this->model->where($condition)->count();
|
|
}
|
|
|
|
/**
|
|
* 会员中心信息
|
|
*/
|
|
public function center()
|
|
{
|
|
$field = 'member_id, username, member_no, mobile, register_channel, nickname, headimg, member_level, member_label, login_ip, login_type, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, point, balance, growth, is_member, member_time, is_del, province_id, city_id, district_id, address, location, money, money_get, commission, commission_get, commission_cash_outing';
|
|
return $this->model->where([['member_id', '=', $this->member_id]])->field($field)->append(['sex_name'])->findOrEmpty()->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取会员的模型对象(todo 慎用!!! 现主要用于登录)
|
|
* @param array $data
|
|
* @return Member|array|mixed|Model !!! 仔细看,返回值是模型对象 如果想要判断是否为空 请用 $member->isEmpty()
|
|
*/
|
|
public function findMemberInfo(array $data){
|
|
//会员账号
|
|
if(!empty($data['username']))
|
|
$where[] = ['username', '=', $data['username']];
|
|
//会员手机号
|
|
if(!empty($data['mobile']))
|
|
$where[] = ['mobile', '=', $data['mobile']];
|
|
//会员id
|
|
if(!empty($data['member_id']))
|
|
$where[] = ['member_id', '=', $data['member_id']];
|
|
//微信公众号openid
|
|
if(!empty($data['wx_openid']))
|
|
$where[] = ['wx_openid', '=', $data['wx_openid']];
|
|
//微信小程序openid
|
|
if(!empty($data['weapp_openid']))
|
|
$where[] = ['weapp_openid', '=', $data['weapp_openid']];
|
|
// 微信unionid
|
|
if(!empty($data['wx_unionid']))
|
|
$where[] = ['wx_unionid', '=', $data['wx_unionid']];
|
|
if(!empty($data['username|mobile']))
|
|
$where[] = ['username|mobile', '=', $data['username|mobile']];
|
|
if(empty($where)){
|
|
$where[] = ['member_id', '=', -1];
|
|
}
|
|
return $this->model->where($where)->findOrEmpty();
|
|
}
|
|
|
|
/**
|
|
* 通过对象修改会员信息
|
|
* @param $member
|
|
* @param $data
|
|
* @return void
|
|
*/
|
|
public function editByFind($member, $data){
|
|
return $member->save($data);
|
|
}
|
|
|
|
/**
|
|
* 修改字段
|
|
* @param string $field
|
|
* @param $data
|
|
* @return null
|
|
*/
|
|
public function modify(string $field, $data)
|
|
{
|
|
return (new CoreMemberService())->modify($this->member_id, $field, $data);
|
|
}
|
|
|
|
public function getQrcode(){
|
|
// 生成会员二维码
|
|
$qrcode_dir = 'upload/member/temp';
|
|
if (!is_dir($qrcode_dir)) mkdir($qrcode_dir, intval('0755', 8), true);
|
|
$id = "member-".$this->member_id;
|
|
$qrcode_path = "{$qrcode_dir}/order_qrcode_{$this->member_id}.png";
|
|
\core\util\QRcode::png($id, $qrcode_path, 'L', 16, 1);
|
|
|
|
// 生成会员条形码
|
|
$barcode_path = (new Barcode(14, $id))->generateBarcode($qrcode_dir, 2);
|
|
$detail = [];
|
|
$detail['verify_code_qrcode'] = image_to_base64($qrcode_path, true);
|
|
$detail['verify_code_barcode'] = image_to_base64($barcode_path);
|
|
return $detail;
|
|
}
|
|
|
|
/**
|
|
* 初始化会员数据
|
|
*/
|
|
public function initMemberData(){
|
|
if ($this->member_id) {
|
|
event("MemberLoginAfter", ['member_id' => $this->member_id]);
|
|
}
|
|
}
|
|
|
|
public function is_pass($data){
|
|
$password = $this->model->where([['member_id', '=', $this->member_id]])->value("password");
|
|
if (!check_password($data['password'], $password)){
|
|
return fail("密码不正确");
|
|
}
|
|
return success("密码正确");
|
|
}
|
|
|
|
public function set_pass($data){
|
|
if($data['old_password'] != $data['password']){
|
|
return fail("两次密码输入不一致");
|
|
}
|
|
$password_hash = create_password($data['password']);
|
|
$data = array(
|
|
'password' => $password_hash,
|
|
);
|
|
$member_info = $this->findMemberInfo([ 'member_id' => $this->member_id ]);
|
|
$this->editByFind($member_info, $data);
|
|
TokenAuth::clearToken($this->member_id, AppTypeDict::API, $this->request->apiToken());
|
|
return success("修改密码成功");
|
|
}
|
|
|
|
//课程列表
|
|
public function course_list($data){
|
|
$member_info = $this->getInfo();
|
|
$venues = new Venues();
|
|
$classes = new Classes();
|
|
|
|
$classes_list = $classes
|
|
->alias("a")
|
|
->join(['school_venues' => 'b'],'a.venue_id = b.id','left')
|
|
->join(['school_timetables' => 'c'],'a.id = c.class_id','left')
|
|
->join(['school_courses' => 'd'],'c.courses_id = d.id','left')
|
|
->where('a.start_date', '<=', $data['schedule_date'])
|
|
->where('a.end_date', '>=', $data['schedule_date'])
|
|
->where('a.status','<',3)
|
|
->whereRaw("FIND_IN_SET(?, a.max_students)", [$member_info['students_id']])
|
|
->field("a.id,a.status,a.venue_id,a.start_date,a.end_date,a.max_students,a.name,b.name as address,d.name as courses_name,
|
|
LENGTH(a.max_students) - LENGTH(REPLACE(a.max_students, ',', '')) + 1 as students_count,d.id as courses_id
|
|
")
|
|
->group("a.venue_id")
|
|
->select()->toArray();
|
|
|
|
|
|
|
|
|
|
|
|
$groupedClassesList = [];
|
|
$venue_ids = [];
|
|
foreach ($classes_list as $item) {
|
|
|
|
$has_sign = Db::name("zhjw_students_sign")
|
|
->alias("a")
|
|
->join(['school_member' => 'b'],'a.students_id = b.member_id','left')
|
|
->where(['a.courses_id' => $item['courses_id']])
|
|
->field("a.id,b.headimg")
|
|
->select();
|
|
$item['has_sign_count'] = count($has_sign);
|
|
$item['has_sign_list'] = $has_sign;
|
|
|
|
$venueId = $item['venue_id'];
|
|
$venue_ids[] = $venueId;
|
|
if (!isset($groupedClassesList[$venueId])) {
|
|
$groupedClassesList[$venueId] = [];
|
|
}
|
|
$groupedClassesList[$venueId][] = $item;
|
|
}
|
|
$venue_ids = array_unique($venue_ids);
|
|
$venue_list = $venues->where('id','in',$venue_ids)->select()->toArray();
|
|
return ['classes_list' => $groupedClassesList, 'venue_list' => $venue_list];
|
|
|
|
}
|
|
|
|
public function students_sign_list(){
|
|
$member_info = $this->getInfo();
|
|
$where['students_id'] = $member_info['students_id'];
|
|
$search_model = Db::name("zhjw_students_sign")->where($where)->order('create_time desc');
|
|
return $this->pageQuery($search_model);
|
|
}
|
|
|
|
public function set_feedback($data){
|
|
$member_info = $this->getInfo();
|
|
Db::name("feedback")->insert([
|
|
'students_id' => $member_info['students_id'],
|
|
'content' => $data['content'],
|
|
'images' => $data['images'],
|
|
'mailbox' => $data['mailbox'],
|
|
'create_time' => date("Y-m-d H:i:s", time()),
|
|
]);
|
|
return true;
|
|
}
|
|
|
|
public function index(){
|
|
$venues = new Venues();
|
|
$classes = new Classes();
|
|
|
|
$data = [];
|
|
$member_info = $this->getInfo();
|
|
$data['info'] = $member_info;
|
|
|
|
|
|
$classes_info = $classes
|
|
->alias("a")
|
|
->join(['school_venues' => 'b'],'a.venue_id = b.id','left')
|
|
->join(['school_timetables' => 'c'],'a.id = c.class_id','left')
|
|
->join(['school_courses' => 'd'],'c.courses_id = d.id','left')
|
|
->where('a.status','<',3)
|
|
->whereRaw("FIND_IN_SET(?, a.max_students)", [$member_info['students_id']])
|
|
->field("a.id,a.status,a.venue_id,a.start_date,a.end_date,a.max_students,a.name,b.name as address,d.name as courses_name,
|
|
LENGTH(a.max_students) - LENGTH(REPLACE(a.max_students, ',', '')) + 1 as students_count,d.id as courses_id
|
|
")
|
|
->order("a.end_date desc")
|
|
->group("a.venue_id")
|
|
->find();
|
|
|
|
|
|
|
|
$classes_info['sign_count'] = Db::name("zhjw_students_sign")
|
|
->alias("a")
|
|
->join(['school_member' => 'b'],'a.students_id = b.member_id','left')
|
|
->where(['a.courses_id' => $classes_info['courses_id']])
|
|
->count();
|
|
|
|
$classes_info['my_sign_count'] = Db::name("zhjw_students_sign")
|
|
->alias("a")
|
|
->join(['school_member' => 'b'],'a.students_id = b.member_id','left')
|
|
->where(['a.courses_id' => $classes_info['courses_id'],'a.students_id' => $member_info['students_id']])
|
|
->count();
|
|
|
|
$data['classes_info'] = $classes_info;
|
|
|
|
return $data;
|
|
}
|
|
|
|
|
|
}
|
|
|