| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\CityModel;
- use App\Models\FansModel;
- use App\Models\FollowModel;
- use App\Models\MasterModel;
- use App\Models\MemberModel;
- use App\Models\SiyuanModel;
- use App\Models\TradeModel;
- use SimpleSoftwareIO\QrCode\Facades\QrCode;
- /**
- * 会员管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class MemberService
- * @package App\Services
- */
- class MemberService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * MemberService constructor.
- */
- public function __construct()
- {
- $this->model = new MemberModel();
- $this->cityModel = new CityModel();
- }
- /**
- * 静态入口
- * @return MemberService|null
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new MemberService();
- }
- return self::$instance;
- }
- /**
- * 添加会编辑会员
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function edit()
- {
- // 请求参数
- $data = request()->all();
- // 头像处理
- $avatar = trim($data['avatar']);
- if (strpos($avatar, "temp")) {
- $data['avatar'] = save_image($avatar, 'member');
- } else {
- $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
- }
- // 出生日期
- if (isset($data['birthday']) && $data['birthday']) {
- $data['birthday'] = strtotime($data['birthday']);
- }
- // 城市处理
- $city = isset($data['city']) ? $data['city'] : [3];
- if (!empty($data['city'])) {
- // 省份
- $data['province_id'] = $city[0];
- // 城市
- $data['city_id'] = $city[1];
- // 县区
- $data['district_id'] = $city[2];
- }
- unset($data['city']);
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 获取用户信息
- * @param $openid OPENID
- * @param array $field m用户表
- * @return array
- */
- public function getUserInfo($where, $field = [], $type=0)
- {
- $field = $field ? $field : ['m.id', 'm.openid', 'm.invite_id','m.birthday','m.age','m.gender','m.sign_time','m.intro','m.province_id','m.city_id','m.district_id', 'ms.type as mtype', 'yg.status as yigong_status','yg.on_siyuan as yg_on_siyuan','yg.siyuan_id','ms.siyuan','ms.on_siyuan as sr_on_siyuan', 'ms.master_type', 'ms.realname as master_name', 'ms.status as master_status', 'm.mobile', 'm.nickname', 'm.avatar', 'm.gender', 'm.member_level', 'm.is_vip','m.vip_expire', 'm.realname', 'm.balance', 'm.salary', 'm.merits_num', 'm.coupon', 'm.score', 'm.login_time','m.intro', 'm.status'];
- $info = $this->model::from('member as m')
- ->leftJoin('member_level as ml', 'ml.id', '=', 'm.member_level')
- ->leftJoin('master as ms', 'ms.user_id', '=', 'm.id')
- ->leftJoin('yigong as yg', 'yg.user_id', '=', 'm.id')
- ->select($field)
- ->where($where)
- ->where('m.status', '>', 0)
- ->first();
- $info = $info ? $info->toArray() : [];
- if (isset($info['avatar'])) {
- $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
- }
- if($info){
- // 僧人参数
- if(array_key_exists('mtype', $info)){
- $info['mtype'] = $info['mtype']? intval($info['mtype']) : 0;
- }
- if(array_key_exists('master_type', $info)){
- $info['master_type'] = $info['master_type']? intval($info['master_type']) : 0;
- }
- if(array_key_exists('master_status', $info)){
- $info['master_status'] = $info['master_status']? intval($info['master_status']) : 0;
- }
- if(array_key_exists('master_name', $info)){
- $info['master_name'] = $info['master_name']? trim($info['master_name']) : '';
- }
- if(array_key_exists('yigong_siyuan', $info)){
- $info['yigong_siyuan'] = $info['yigong_siyuan']? trim($info['yigong_siyuan']) : '';
- }
- if(array_key_exists('yg_on_siyuan', $info)){
- $info['yg_on_siyuan'] = $info['yg_on_siyuan']? intval($info['yg_on_siyuan']) : 0;
- }
- if(array_key_exists('yigong_status', $info)){
- $info['yigong_status'] = $info['yigong_status']? intval($info['yigong_status']) : 0;
- }
- if(array_key_exists('siyuan', $info)){
- $info['siyuan'] = $info['siyuan']? trim($info['siyuan']) : '';
- }
- if(array_key_exists('sr_on_siyuan', $info)){
- $info['sr_on_siyuan'] = $info['sr_on_siyuan']? intval($info['sr_on_siyuan']) : 0;
- }
- $info['yg_siyuan'] = '';
- if(array_key_exists('siyuan_id', $info) && $info['siyuan_id']){
- $info['yg_siyuan'] = $info['siyuan_id']? SiyuanModel::where('id', $info['siyuan_id'])->value('title') : '';
- }
- if($type == 2){
- $master = MasterModel::where(['user_id'=> $info['id'],'mark'=> 1])
- ->first();
- if($master){
- $master['idcard_front_preview'] = $master['idcard_front']? get_image_url($master['idcard_front']) : '';
- $master['idcard_back_preview'] = $master['idcard_back']? get_image_url($master['idcard_back']) : '';
- $master['description_text'] = $master['description']? str_replace("\n","<br>", $master['description']) : '';
- }
- $info['master'] = $master? $master : [];
- }
- if($type == 3){
- if(isset($info['sign_time'])){
- $info['is_sign'] = $info['sign_time']>strtotime(date('Y-m-d'))? 1 : 0;
- $gd = ConfigService::make()->getConfigByCode('sign_give_gd');
- $info['sign_gd'] = $gd? $gd : 0;
- }
- $info['rank'] = 0;
- if($info['merits_num']>0){
- $model = $this->model::from(\DB::raw(env('DB_PREFIX').'member as m,(select (@rank:=0)) as rank'))
- ->where(['mark'=> 1,'status'=> 1])
- ->where('merits_num','>',0)
- ->orderBy('merits_num', 'desc')
- ->orderBy('create_time', 'asc')
- ->select(['merits_num','mark','id',\DB::raw('(@rank:=@rank+1) as rank')]);
- $binds = $model->getBindings();
- $sql = str_replace('?', '%s', $model->toSql());
- $sql = sprintf($sql, ...$binds);
- $ranks = $this->model::from(\DB::raw("({$sql}) as a"))
- ->where(['id'=> $info['id']])
- ->select(['merits_num','id','rank'])
- ->first();
- $info['rank'] = isset($ranks['rank'])? $ranks['rank'] : 0;
- }
- }
- if($type == 1) {
- // 城市
- $cityData = [0, 0, 0];
- $cityNames = [];
- if (isset($info['province_id'])) {
- $info['province_name'] = $info['province_id'] ? CityService::make()->getName($info['province_id']) : '';
- $cityData[0] = $info['province_id'] ? $info['province_id'] : 0;
- $cityNames[] = $info['province_name'] ? $info['province_name'] : '';
- }
- if (isset($info['city_id'])) {
- $info['city_name'] = $info['city_id'] ? CityService::make()->getName($info['city_id']) : '';
- $openid = isset($info['openid']) ? $info['openid'] : '';
- $cityData[1] = $info['city_id'] ? $info['city_id'] : 0;
- $cityNames[] = $info['city_name'] ? $info['city_name'] : '';
- if (empty($info['city_name']) && $openid) {
- $info['city_name'] = FansModel::where(['openid' => $openid])->value('city');
- }
- }
- if (isset($info['district_id'])) {
- $info['district_name'] = $info['district_id'] ? CityService::make()->getName($info['district_id']) : '';
- $cityData[2] = $info['district_id'] ? $info['district_id'] : 0;
- $cityNames[] = $info['district_name'] ? $info['district_name'] : '';
- }
- if ($cityData) {
- $info['cityData'] = $cityData;
- $info['cityText'] = implode(' ', array_filter($cityNames));
- }
- if (is_empty($info['age'])) {
- if (!is_empty($info['birthday'])) {
- $year = date('Y', $info['birthday']);
- $info['age'] = date('Y') - $year > 0 ? date('Y') - $year : 0;
- } else {
- $info['age'] = 0;
- }
- }
- if (is_empty($info['birthday'])) {
- $info['birthday'] = 0;
- } else {
- $info['birthday'] = date('Y-m-d', $info['birthday']);
- }
- if (is_empty($info['intro'])) {
- $info['intro'] = '';
- }
- if (is_empty($info['realname'])) {
- $info['realname'] = '';
- }
- // 二维码
- $url = env('WEB_URL') . '/pages/entry/auth?sid=' . $info['id'];
- $qrcode = WechatService::makeNormalQrcode($url);
- $info['qrcode'] = $qrcode ? get_image_url($qrcode) : '';
- // 会员
- if($info['is_vip'] && $info['vip_expire'] >= time()){
- $info['vip_expire_text'] = date('Y-m-d H:i:s', $info['vip_expire']);
- }else{
- $info['is_vip'] = 0;
- $info['vip_expire_text'] = '';
- }
- }
- }
- return $info;
- }
- /**
- * 统计表数据
- * @param $dateType
- * @return array
- */
- public function tableData($dateType){
- $cacheKey = "caches:statistics:table_member_{$dateType}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $firstTime = strtotime(date('Y-m-d'));
- if($dateType == 0){
- $formatStr = "%Y-%m-%d %H";
- $firstTime = strtotime(date('Y-m-d'));
- for($i = 0; $i<24; $i++){
- $i = $i<10? '0'.$i : $i;
- $ks = date('Y-m-d')." {$i}:00";
- $datas[$ks] = [
- 'date'=> $i.'点',
- 'ks'=> $ks,
- 'value'=> 0,
- ];
- }
- }else if($dateType == 1){
- $formatStr = "%Y-%m-%d";
- $firstTime = strtotime('monday this week');
- $weeks = ['周一','周二','周三','周四','周五','周六','周日'];
- for($i = 0; $i<7; $i++){
- $ks = date('Y-m-d', $firstTime+$i*86400);
- $datas[$ks] = [
- 'date'=> $weeks[$i],
- 'ks'=> $ks,
- 'value'=> 0,
- ];
- }
- }else if($dateType == 2){
- $formatStr = "%Y-%m-%d";
- $firstTime = strtotime(date('Y-m-01'));
- $num = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
- for($i = 1; $i<=$num; $i++){
- $i = $i<10? '0'.$i : $i;
- $ks = date('Y-m')."-{$i}";
- $datas[$ks] = [
- 'date'=> $i.'号',
- 'ks'=> $ks,
- 'value'=> 0,
- ];
- }
- }else if($dateType == 3){
- $formatStr = "%Y-%m";
- $firstTime = strtotime(date('Y-01-01'));
- for($i = 1; $i<=12; $i++){
- $ks = date('Y')."-".($i<10? '0'.$i : $i);
- $datas[$ks] = [
- 'date'=> $i.'月',
- 'ks'=> $ks,
- 'value'=> 0,
- ];
- }
- }
- $counts = $this->model::from('member as a')
- ->where(['a.mark'=> 1,'a.status'=> 1])
- ->where('a.create_time','>=', $firstTime)
- ->where('a.create_time','<=', time())
- ->select([\DB::raw('count('.env('DB_PREFIX').'a.`id`) as value'),\DB::raw('FROM_UNIXTIME('.env('DB_PREFIX')."a.create_time,'{$formatStr}') as ks")])
- ->groupBy(\DB::raw('FROM_UNIXTIME('.env('DB_PREFIX')."a.create_time,'{$formatStr}')"))
- ->get();
- $counts = $counts? $counts->toArray() : [];
- if($counts){
- foreach ($counts as $v){
- if(isset($datas[$v['ks']])){
- $datas[$v['ks']]['value'] = $v['value'];
- }
- }
- }
- $datas = array_values($datas);
- RedisService::set($cacheKey, $datas, rand(5, 10));
- return $datas;
- }
- /**
- * 保存资料
- * @param $userId
- * @return array
- */
- public function saveInfo($userId){
- $params = request()->all();
- $memberInfo = $this->model::where(['id'=> $userId])->first();
- if(!$memberInfo){
- return message('用户账号不可操作', false);
- }
- if($params['avatar']){
- $memberInfo->avatar = trim($params['avatar']);
- }
- if($params['birthday']){
- $memberInfo->birthday = strtotime($params['birthday']);
- }
- if($params['age']){
- $memberInfo->age = strtotime($params['age']);
- }
- if(!is_empty($params['intro'])){
- $memberInfo->intro = trim($params['intro']);
- }
- if(!is_empty($params['mobile'])){
- $memberInfo->mobile = trim($params['mobile']);
- }
- if(!empty($params['cityCodes'])){
- $cityData = $params['cityCodes'];
- if(!is_empty($cityData[0]) && $cityData[0]){
- $memberInfo->province_id = $cityData[0];
- }
- if(!is_empty($cityData[1]) && $cityData[1]){
- $memberInfo->city_id = $cityData[1];
- }
- if(!is_empty($cityData[2]) && $cityData[2]){
- $memberInfo->district_id = $cityData[2];
- }
- }
- $memberInfo->nickname = trim($params['nickname']);
- $memberInfo->gender = intval($params['gender']);
- if($memberInfo->save()){
- return message('保存成功', true);
- }else{
- return message('保存失败', false);
- }
- }
- /**
- * 账号注销
- * @param $userId
- * @return array
- */
- public function logout($userId){
- $memberInfo = $this->model::where(['id'=> $userId,'mark'=> 1,'status'=> 1])->first();
- if(!$memberInfo){
- return message('用户账号不可操作', false);
- }
- $memberInfo->mark = 0;
- $memberInfo->status = 2;
- $memberInfo->black_remark = request()->get('remark','');
- if($memberInfo->save()){
- return message('操作成功', true);
- }else{
- return message('操作失败', false);
- }
- }
- /**
- * 邀请奖励
- * @param $userId 注册用户
- * @param $inviteId 邀请用户
- * @param string $nickname 注册用户昵称
- * @return bool
- */
- public function inviteAward($userId, $inviteId, $nickname=''){
- // 验证当前用户是否已经奖励过
- $cacheKey = "caches:invite:u{$userId}_s{$inviteId}";
- $check = TradeModel::where(['user_id'=> $userId,'type'=> 6,'source_uid'=> $inviteId,'status'=>1,'mark'=> 1])->value('id');
- if($check){
- RedisService::set($cacheKey.':error_catched', ['error'=>'当前奖励已发放','date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- // 验证当前邀请用户是否账户有效
- $inviteInfo = $this->model::where(['id'=> $inviteId,'mark'=> 1,'status'=> 1])
- ->select(['id','openid','nickname','score','coupon','merits_num'])
- ->first();
- if(!$inviteInfo){
- RedisService::set($cacheKey.':error_invite', ['error'=>'当前邀请用户状态异常','date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- // 是否到达人数限制
- $curTime = strtotime(date('Y-m-d'));
- $inviteConfig = ConfigService::make()->getConfigByGroup(14);
- $inviteLimit = isset($inviteConfig['invite_limit'])? $inviteConfig['invite_limit']['value'] : 0;
- $checkCount = TradeModel::where(['user_id'=> $userId,'type'=> 6,'status'=>1,'mark'=> 1])
- ->where('create_time','>=',$curTime)
- ->count('id');
- if($inviteLimit>0 && $checkCount>=$inviteLimit){
- RedisService::set($cacheKey.':error_limit', ['error'=>'今日奖励已到达限制人数:'.$inviteLimit,'date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- // 奖励花灯券
- \DB::beginTransaction();
- $coupon = isset($inviteConfig['invite_give_coupon'])? intval($inviteConfig['invite_give_coupon']['value']) : 0;
- if($coupon>0){
- // 账户
- if(!$this->model::where(['id'=> $inviteId])->increment('coupon', $coupon)){
- \DB::rollBack();
- RedisService::set($cacheKey.':error_coupon', ['error'=>'奖励花灯券账户更新失败:'.$coupon,'date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- $data = [
- 'user_id'=> $inviteId,
- 'source_uid'=> $userId,
- 'type'=> 6,
- 'coin_type'=> 1,
- 'pay_type'=> 4,
- 'money'=> $coupon,
- 'change_type'=> 1,
- 'balance'=> $inviteInfo->coupon,
- 'create_time'=> time(),
- 'remark'=> "邀请用户[{$nickname}],奖励{$coupon}花灯券",
- 'status'=> 1,
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- RedisService::set($cacheKey.':error_acccount_coupon', ['error'=>'奖励花灯券账户明细处理失败','data'=> $data,'date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- }
- // 奖励功德值
- $giveGd = isset($inviteConfig['invite_give_gd'])? intval($inviteConfig['invite_give_gd']['value']) : 0;
- if($giveGd>0){
- // 账户
- if(!$this->model::where(['id'=> $inviteId])->increment('merits_num', $giveGd)){
- \DB::rollBack();
- RedisService::set($cacheKey.':error_gd', ['error'=>'奖励功德值账户更新失败:'.$giveGd,'date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- $data = [
- 'user_id'=> $inviteId,
- 'source_uid'=> $userId,
- 'type'=> 6,
- 'coin_type'=> 4,
- 'pay_type'=> 4,
- 'money'=> $giveGd,
- 'change_type'=> 1,
- 'balance'=> $inviteInfo->merits_num,
- 'create_time'=> time(),
- 'remark'=> "邀请用户[{$nickname}],奖励{$giveGd}功德值",
- 'status'=> 1,
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- RedisService::set($cacheKey.':error_acccount_gd', ['error'=>'奖励花灯券账户明细处理失败','data'=> $data,'date'=> date('Y-m-d H:i:s')], 7200);
- return false;
- }
- }
- // 奖励积分
- \DB::commit();
- return true;
- }
- /**
- * 加入会员
- * @param $userId
- */
- public function buyVip($userId){
- $memberInfo = $this->model::where(['id'=> $userId,'mark'=> 1,'status'=> 1])
- ->select(['id','nickname','openid','coupon','mobile','is_vip','vip_expire'])
- ->first();
- if(!$memberInfo){
- return message('用户账户不可操作,请联系客服',false);
- }
- $params = request()->all();
- $receiveVipMessage = isset($params['receive_vip_message'])? $params['receive_vip_message'] : 0;
- if($receiveVipMessage>0 && empty($memberInfo->mobile)){
- return message('接收消息通知,请先到个人资料设置手机号码再尝试',false);
- }
- // 是否已经加入了会员
- $isVip = false;
- if($memberInfo->is_vip && $memberInfo->vip_expire>=time()){
- $isVip = true;
- }
- // 验证账户
- $config = ConfigService::make()->getConfigByGroup(15);
- $vipPrice = isset($config['vip_price'])? intval($config['vip_price']['value']) : 0;
- $vipGiveCoupon = isset($config['vip_give_coupon'])? intval($config['vip_give_coupon']['value']) : 0;
- if($vipPrice<=0){
- return message('当前会员价格参数设置错误,请联系客服',false);
- }
- if($vipPrice<=$vipGiveCoupon){
- return message('当前会员赠送参数设置错误,请联系客服',false);
- }
- if($memberInfo->coupon < $vipPrice){
- return message('您的账户花灯券不足,请先充值后重试',false,[],'10003');
- }
- // 操作账户
- \DB::beginTransaction();
- if(!$this->model::where(['id'=> $userId,'mark'=> 1])->decrement('coupon',($vipPrice - $vipGiveCoupon))){
- \DB::rollBack();
- return message('扣除用户账户失败,请刷新后重试',false);
- }
- // 更新会员有效期
- if($isVip){
- $vipExpire = $memberInfo->vip_expire+365*24*3600;
- }else{
- $vipExpire = time()+365*24*3600;
- }
- if(!$this->model::where(['id'=> $userId,'mark'=> 1])->update(['is_vip'=> 1,'receive_vip_message'=> $receiveVipMessage,'vip_expire'=> $vipExpire])){
- \DB::rollBack();
- return message('更新会员有效期失败,请刷新后重试',false);
- }
- // 账户明细
- $data = [
- 'user_id'=> $userId,
- 'type'=> 1,
- 'coin_type'=> 1,
- 'pay_type'=> 1,
- 'money'=> $vipPrice,
- 'change_type'=> 2,
- 'balance'=> $memberInfo->coupon,
- 'create_time'=> time(),
- 'remark'=> ($isVip? '续费':'加入')."会员扣除{$vipPrice}花灯券",
- 'status'=> 1,
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- return message('处理账户明细失败,请刷新后重试',false);
- }
- // 赠送
- if($vipGiveCoupon>0){
- $data = [
- 'user_id'=> $userId,
- 'type'=> 3,
- 'coin_type'=> 1,
- 'pay_type'=> 4,
- 'money'=> $vipGiveCoupon,
- 'change_type'=> 1,
- 'balance'=> ($memberInfo->coupon-$vipPrice),
- 'create_time'=> time()+1,
- 'remark'=> ($isVip? '续费':'加入')."会员赠送{$vipGiveCoupon}花灯券",
- 'status'=> 1,
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- return message('赠送券处理失败,请刷新后重试',false);
- }
- }
- \DB::commit();
- return message(($isVip? '续费':'加入').'会员成功',true);
- }
- /**
- * 签到
- * @param $userId
- * @return array
- */
- public function sign($userId){
- $memberInfo = $this->model::where(['id'=> $userId, 'mark'=>1 ,'status'=> 1])
- ->select(['id','openid','nickname','score','merits_num','sign_time'])
- ->first();
- if(!$memberInfo){
- return message('账户不可操作,请联系客服',false);
- }
- if($memberInfo->sign_time >= strtotime(date('Y-m-d'))){
- return message('您今天已签到过,请明天再来',false);
- }
- // 参数
- $score = ConfigService::make()->getConfigByCode('sign_give_score');
- $score = $score? $score : 0;
- // 处理
- \DB::beginTransaction();
- if($score>0){
- if(!$this->model::where(['id'=> $userId,'mark'=> 1])->increment('score', $score)){
- \DB::rollBack();
- return message('更新账户积分失败,请刷新后重试',false);
- }
- $data = [
- 'user_id'=> $userId,
- 'type'=> 3,
- 'coin_type'=> 3,
- 'pay_type'=> 4,
- 'money'=> $score,
- 'change_type'=> 1,
- 'balance'=> $memberInfo->score,
- 'create_time'=> time(),
- 'remark'=> "每日签到赠送{$score}积分",
- 'status'=> 1,
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- return message('积分奖励处理失败,请刷新后重试',false);
- }
- }
- $giveGd = ConfigService::make()->getConfigByCode('sign_give_gd');
- $giveGd = $giveGd? $giveGd : 0;
- if($giveGd>0){
- if(!$this->model::where(['id'=> $userId,'mark'=> 1])->increment('merits_num', $giveGd)){
- \DB::rollBack();
- return message('更新账户功德失败,请刷新后重试',false);
- }
- $data = [
- 'user_id'=> $userId,
- 'type'=> 3,
- 'coin_type'=> 4,
- 'pay_type'=> 4,
- 'money'=> $giveGd,
- 'change_type'=> 1,
- 'balance'=> $memberInfo->merits_num,
- 'create_time'=> time(),
- 'remark'=> "每日签到赠送{$giveGd}功德",
- 'status'=> 1,
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- return message('签到功德奖励处理失败,请刷新后重试',false);
- }
- }
- if(!$this->model::where(['id'=> $userId,'mark'=> 1])->update(['sign_time'=> time()])){
- \DB::rollBack();
- return message('签到处理失败,请刷新后重试',false);
- }
- \DB::commit();
- return message('签到成功',true);
- }
- /**
- * 获取功德榜列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function gdList($userId)
- {
- $params = request()->all();
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $dataList = $this->model::where(['mark'=>1,'status'=> 1])
- ->where('merits_num','>',0)
- ->select(['id', 'avatar', 'nickname','gender','merits_num','status','create_time'])
- ->orderBy('merits_num', 'desc')
- ->orderBy('create_time', 'asc')
- ->paginate($pageSize);
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- foreach ($dataList['data'] as &$item) {
- $item['avatar'] = $item['avatar']? get_image_url($item['avatar']) : '';
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
- }
- unset($item);
- }
- $meritsNum = $this->model::where(['id'=> $userId])->value('merits_num');
- $ranks['id'] = $userId;
- $ranks['rank'] = 0;
- $ranks['merits_num'] = $meritsNum>0? $meritsNum : 0;
- if($meritsNum>0){
- $model = $this->model::from(\DB::raw(env('DB_PREFIX').'member as m,(select (@rank:=0)) as rank'))
- ->where(['mark'=> 1,'status'=> 1])
- ->where('merits_num','>',0)
- ->orderBy('merits_num', 'desc')
- ->orderBy('create_time', 'asc')
- ->select(['merits_num','mark','id',\DB::raw('(@rank:=@rank+1) as rank')]);
- $binds = $model->getBindings();
- $sql = str_replace('?', '%s', $model->toSql());
- $sql = sprintf($sql, ...$binds);
- $ranks = $this->model::from(\DB::raw("({$sql}) as a"))
- ->where(['id'=> $userId])
- ->select(['merits_num','id','rank'])
- ->first();
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'ranks' => $ranks,
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * 关注
- * @param $userId
- * @return array
- */
- public function follow($userId){
- $params = request()->all();
- $followUid = isset($params['follow_uid'])? $params['follow_uid'] : 0;
- $status = isset($params['status'])? $params['status'] : 1;
- $status = $status<=0? 1 : $status;
- if(empty($followUid)){
- return message('参数错误', false);
- }
- if(!in_array($status, [1,2])){
- return message('参数错误', false);
- }
- $memberInfo = $this->model::where(['id'=> $userId, 'mark'=> 1,'status'=> 1])
- ->select(['id','openid','nickname','status'])
- ->first();
- if(!$memberInfo){
- return message('账户不可操作已冻结或不存在,请联系客服',false);
- }
- $followInfo = $this->model::where(['id'=> $followUid, 'mark'=> 1,'status'=> 1])
- ->select(['id','openid','nickname','status'])
- ->first();
- if(!$followInfo){
- return message('被关注用户不可操作或不存在',false);
- }
- $info = FollowModel::where(['user_id'=> $userId, 'follow_uid'=> $followUid])->select(['id','status'])->first();
- if($info && $info->status == 1 && $status == 1){
- return message('您已关注过', false);
- }else if($info && $info->status == 2 && $status == 2){
- return message('您已取消关注', false);
- }else if(!$info && $status == 2){
- return message('您未关注过', false);
- }
- // 处理
- if($info){
- $info->status = $status;
- $info->create_time = time();
- if($info->save()){
- return message($status == 1? '关注成功':'取消关注成功', true);
- }
- }else{
- $data = [
- 'user_id'=> $userId,
- 'follow_uid'=> $followUid,
- 'create_time'=> time(),
- 'status'=> 1,
- ];
- if(FollowModel::insertGetId($data)){
- return message('关注成功', true);
- }
- }
- return message('操作失败', false);
- }
- /**
- * 获取选项列表
- * @param int $num
- * @return array
- */
- public function getOptions(){
- $params = request()->all();
- $num = isset($params['num'])? $params['num'] : 0;
- $type = isset($params['type'])? $params['type'] : 0;
- $where = ['status'=>1,'mark'=> 1];
- if($type>0){
- $where['type'] = $type;
- }
- $datas = $this->model->where($where)
- ->select(['id','nickname','status'])
- ->limit($num? $num : 999999)
- ->get();
- return message(MESSAGE_OK, true, $datas);
- }
- /**
- * 关注的人
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function followList($userId)
- {
- $params = request()->all();
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $dataList = $this->model::from('member_follow as mf')
- ->leftJoin('member as m','mf.follow_uid','=','m.id')
- ->where(['m.mark'=>1,'m.status'=> 1,'mf.mark'=> 1,'mf.status'=> 1])
- ->select(['mf.id', 'mf.user_id','mf.follow_uid','m.avatar','m.nickname', 'm.merits_num','m.gender','m.status','m.create_time'])
- ->orderBy('m.merits_num', 'desc')
- ->orderBy('mf.create_time', 'asc')
- ->paginate($pageSize);
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- foreach ($dataList['data'] as &$item) {
- $item['avatar'] = $item['avatar']? get_image_url($item['avatar']) : '';
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
- }
- unset($item);
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * 感兴趣的人
- * @param $userId
- * @return array
- */
- public function recommand($userId){
- $params = request()->all();
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $type = isset($params['type'])? $params['type'] : 0;
- $where = ['m.mark'=>1,'m.status'=> 1];
- $dataList = $this->model::from('member as m')
- ->leftJoin('master as ms','ms.user_id','=','m.id')
- ->leftJoin('member_follow as mf','mf.follow_uid','=','m.id')
- ->where($where)
- ->where(function($query) use($type){
- if($type==1){
- $query->where('ms.type',1)->where(['ms.mark'=> 1,'ms.status'=> 1]);
- }else if($type == 2){
- $query->whereNotIn('ms.type',[1])->where(['ms.mark'=> 1,'ms.status'=> 1]);
- }
- })
- ->whereNotIn('m.id', [$userId])
- ->whereNotIn('m.id',function($query) use ($userId){
- $query->from('member_follow')->where(['user_id'=> $userId])->select(['follow_uid']);
- })
- ->select(['m.id', 'm.avatar','m.nickname','ms.realname as master_name','ms.type as mtype','m.gender','m.status','m.create_time'])
- ->orderBy('mf.create_time', 'asc')
- ->paginate($pageSize);
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- foreach ($dataList['data'] as &$item) {
- $item['avatar'] = $item['avatar']? get_image_url($item['avatar']) : '';
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
- }
- unset($item);
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * VIP 用户每月奖励
- * @return array
- */
- public function vipAward(){
- $cacheKey = "caches:vip:awards:logs:".date('YmdHi');
- $num = ConfigService::make()->getConfigByCode('vip_award_catch_num');
- $num = $num>10? $num : 200;
- // 有效会员
- $users = $this->model::where(['mark'=>1,'status'=>1])
- ->where('vip_expire','>=', time())
- ->where('give_expire','<', strtotime('Y-m-01'))
- ->select(['id','nickname','coupon','give_time','vip_expire'])
- ->limit($num)
- ->get();
- $giveNum = ConfigService::make()->getConfigByCode('vip_month_give_coupon');
- $giveNum = $giveNum? $giveNum : 0;
- RedisService::set($cacheKey.':data', ['users'=> $users,'give'=>$giveNum], 86400);
- if($users && $giveNum>0){
- $success = [];
- $fail = [];
- foreach ($users as $v){
- // 处理用户
- $result = $this->vipAwardCatch($v,$giveNum);
- if($result['success'] == true){
- $success[] = [
- 'id'=>$v['id'],
- 'result'=> $result,
- ];
- }else{
- $fail[] = [
- 'id'=>$v['id'],
- 'result'=> $result,
- ];
- }
- }
- }
- if($success || $fail){
- $msg = 'VIP每月奖励处理完成:成功'.count($success).'个,失败'.count($fail).'个';
- RedisService::set($cacheKey.':result',['success'=> $success,'fail'=> $fail,'result'=> $msg], 7200);
- return message($msg, true, ['success'=> $success,'fail'=> $fail]);
- }else{
- return message('VIP每月奖励处理失败或暂无用户需处理', false);
- }
- }
- /**
- * VIP奖励处理
- * @param $userInfo 用户信息,至少包含 id、coupon字段
- * @param $giveNum
- * @return array
- */
- public function vipAwardCatch($userInfo,$giveNum){
- $userId = $userInfo->id;
- $cacheKey = "caches:vip:awards:users:u_{$userId}";
- \DB::beginTransaction();
- // 扣除账户
- if(!MemberModel::where(['id'=> $userId,'mark'=> 1])->increment('coupon', $giveNum)){
- \DB::rollBack();
- RedisService::set($cacheKey.':error', ['info'=>$userInfo,'give'=> $giveNum, 'error'=> '更新账户失败'], 7200);
- return message('更新会员券账户失败',false);
- }
- // 明细
- $data = [
- 'user_id'=> $userId,
- 'type'=> 3,
- 'coin_type'=> 1,
- 'pay_type'=> 1,
- 'money'=> $giveNum,
- 'change_type'=> 1,
- 'balance'=> $userInfo->coupon,
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'remark'=> 'VIP会员每月赠送奖励',
- 'status'=> 1
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- RedisService::set($cacheKey.':error', ['info'=>$userInfo,'data'=> $data,'give'=> $giveNum, 'error'=> '交易明细处理失败'], 7200);
- return message( "交易处理失败,请刷新重试", false);
- }
- // 更新处理时间
- $this->model::where(['id'=> $userId,'mark'=>1])->update(['give_expire'=> time()]);
- \DB::commit();
- return message( "VIP用户[ID:{$userId}]每月奖励处理成功", true);
- }
- }
|