1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786 |
- <?php
- namespace app\common\model;
- use app\common\model\Order;
- use app\common\model\Sms;
- use Firebase\JWT\JWT;
- use app\common\model\UserIntegralLog;
- use think\Model;
- use app\common\library\Common;
- use think\facade\Validate;
- use think\Request;
- use think\Db;
- use function foo\func;
- use app\common\library\WxService;
- use app\common\library\WxPay;
- use app\common\library\AliPay;
- use EasyWeChat\Factory;
- header('Access-Control-Allow-Origin: *');
- /**
- * 会员模型
- */
- class User Extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'TIMESTAMP';
- // 定义时间戳字段名
- protected $createTime = 'create_at';
- protected $updateTime = 'update_at';
- // 追加属性
- protected $append = [
- ];
- protected $table = 'q_user';
- /**
- * 注册
- */
- public static function register($phone,$password,$ver_code,$invite_code){
- $phone_use = self::where('phone',$phone)->find();
- if ($phone_use){
- return Common::return_error('手机号已注册!');
- }
- //获取最后的验证码
- // $time = time()-90;
- // $sms = Sms::where(['mobile' => $phone, 'event' => 'register'])
- // ->where('createtime','>',$time)
- // ->order('id', 'DESC')
- // ->find();
- // if (!$sms || $sms->code != $ver_code){
- // return Common::return_error('短信验证码不正确!');
- // }
- $data['phone'] = $phone;
- $data['fid'] = $invite_code ? $invite_code : 0;
- $data['password'] = md5($password);
- $data['ip'] = request()->ip();
- Db::startTrans();
- try {
- self::create($data);
- if ($invite_code){
- CouponUser::create([
- 'user_id'=>$invite_code,
- 'coupon_title'=>'满100可用',
- 'coupon_price'=>50,
- 'use_min_price'=>100,
- 'add_time'=>time(),
- 'end_time'=>time()+(30*24*60*60),
- ]);
- }
- Db::commit();
- return Common::return_success('注册成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('注册失败');
- }
- }
- /**
- * 登录
- */
- public static function login($phone,$password,$type,$ver_code,$come){
- $phone_use = self::where('phone',$phone)->find();
- if (!$phone_use){
- return Common::return_error('手机号未注册!');
- }
- if ($phone_use['status']!=1){
- return Common::return_error('账号已禁用!');
- }
- if ($type==1){
- if ($phone_use['password'] != md5($password)){
- return Common::return_error('密码错误!');
- }
- }elseif ($type==2){
- //获取最后的验证码
- $time = time()-90;
- $sms = Sms::where(['mobile' => $phone, 'event' => 'login'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms->code != $ver_code){
- //return Common::return_error('短信验证码不正确!');
- }
- }
- if ($come==2){
- $info = $phone_use;
- if (!$info['skills_label']){
- $info['skills_label'] = [];
- }else{
- $info['skills_label'] = explode(',', $info['skills_label']);
- }
- if (!$info['good_type']){
- $info['good_type'] = [];
- }else{
- $info['good_type'] = explode(',', $info['good_type']);
- }
- if (!$info['good_style']){
- $info['good_style'] = [];
- }else{
- $info['good_style'] = explode(',', $info['good_style']);
- }
- if (!$info['related_certificate']){
- $info['related_certificate'] = [];
- }else{
- $info['related_certificate'] = explode(',', $info['related_certificate']);
- }
- if (!$info['works']){
- $info['works'] = [];
- }else{
- $info['works'] = explode(',', $info['works']);
- }
- $data['info'] = $phone_use;
- if ($phone_use['type']==1 && $phone_use['audit']==0){
- $data['type'] = 1;
- }elseif ($phone_use['type']==1 && $phone_use['audit']==1){
- $data['type'] = 2;
- }elseif ($phone_use['type']==1 && $phone_use['audit']==3){
- $data['type'] = 3;
- }else{
- $data['type'] = 4;
- }
- $data['token'] = "";
- if ($data['type']==4){
- self::where('id',$phone_use['id'])->update(['ip'=>request()->ip()]);
- LoginLog::create(['user_id'=>$phone_use['id'],'ip'=>request()->ip()]);
- $token = JWT::encode($phone_use,config('jwt.key'));
- $data['token'] = $token;
- }
- return Common::return_success('成功',$data);
- }
- self::where('id',$phone_use['id'])->update(['ip'=>request()->ip()]);
- LoginLog::create(['user_id'=>$phone_use['id'],'ip'=>request()->ip()]);
- $token = JWT::encode($phone_use,config('jwt.key'));
- $data['token'] = $token;
- return Common::return_success('登录成功',$data);
- }
- /**
- * 微信授权登录
- * @param $code
- */
- public static function wechatLogin($code){
- $appid = Config::get_values('wechat_appid');
- $secret = Config::get_values('wechat_appsecret');
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
- $result = Common::httpcurl($url);
- if (isset($result['access_token'])){
- $access_token=$result['access_token'];
- $openid=$result['openid'];
- $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
- $resinfos =Common::httpcurl($urltoc);
- //数据库是否已注册
- $user = self::where('status',1)->where('openid',$resinfos['openid'])->find();
- if ($user){
- self::where('id',$user['id'])->update(['ip'=>request()->ip()]);
- LoginLog::create(['user_id'=>$user['id'],'ip'=>request()->ip()]);
- $token = JWT::encode($user,config('jwt.key'));
- return Common::return_success('登录成功',['token'=>$token]);
- }else{
- return Common::return_error('未注册!');
- }
- }else{
- return Common::return_error('获取access_token失败');
- }
- }
- /**
- * 获取微信session_key
- */
- public static function getSessionKey($code){
- $appid = Config::get_values('small_wechat_id');
- $secret = Config::get_values('small_wechat_appsecret');
- echo $appid."<br />";
- echo $secret;
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
- $session_key = Common::curlRequest($url);
- print_r($session_key);die;
- return $session_key;
- }
- /**
- * 微信登录绑定手机号
- */
- public static function wechatLoginBindPhone($code,$rawData,$phone,$ver_code,$password){
- $phone_use = self::where('phone',$phone)->find();
- if ($phone_use && $phone_use['openid']!=''){
- return Common::return_error('手机号已被绑定!');
- }
- // if (!$phone_use['status']){
- // return Common::return_error('账号已禁用!');
- // }
- //获取最后的验证码
- $time = time()-90;
- $sms = Sms::where(['mobile' => $phone, 'event' => 'bindwechat'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms->code != $ver_code){
- return Common::return_error('短信验证码不正确!');
- }
- $appid = Config::get_values('wechat_appid');
- $secret = Config::get_values('wechat_appsecret');
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
- $result = Common::httpcurl($url);
- if (isset($result['access_token'])){
- $access_token=$result['access_token'];
- $openid=$result['openid'];
- $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
- $resinfos = Common::httpcurl($urltoc);
- $openid = $resinfos['openid'];
- if (!$phone_use){
- $data['phone'] = $phone;
- $data['nickname'] = $resinfos['nickname'];
- $data['headimg'] = $resinfos['headimgurl'];
- $data['openid'] = $openid;
- $data['password'] = md5($password);
- $data['ip'] = request()->ip();
- Db::startTrans();
- try {
- $user = self::create($data);
- Db::commit();
- $userId =$user->id;
- LoginLog::create(['user_id'=>$userId,'ip'=>request()->ip()]);
- $user = self::where('id',$userId)->find();
- $token = JWT::encode($user,config('jwt.key'));
- return Common::return_success('授权成功',['token'=>$token]);
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('失败');
- }
- }else{
- $da['openid'] = $openid;
- if (!$phone_use['headimg']){
- $da['headimg'] = $resinfos['headimgurl'];
- }
- if (!$phone_use['nickname']){
- $da['nickname'] = $resinfos['nickname'];
- }
- if (self::where('id',$phone_use['id'])->update($da)){
- LoginLog::create(['user_id'=>$phone_use['id'],'ip'=>request()->ip()]);
- $token = JWT::encode($phone_use,config('jwt.key'));
- return Common::return_success('绑定成功',['token'=>$token]);
- }else{
- return Common::return_error('失败');
- }
- }
- }else{
- return Common::return_error('获取access_token失败');
- }
- }
- /**
- * 商家注册第一步
- */
- public static function designer_register_one($phone,$ver_code){
- $phone_use = self::where('phone',$phone)->find();
- if ($phone_use){
- return Common::return_error('手机号已注册!');
- }
- //获取最后的验证码
- $time = time()-90;
- $sms = Sms::where(['mobile' => $phone, 'event' => 'register'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms->code != $ver_code){
- return Common::return_error('短信验证码不正确!');
- }
- return Common::return_success('验证成功');
- }
- /**
- * 商家注册第二步
- */
- public static function designer_register_two($data){
- $phone_use = self::where('phone',$data['phone'])->find();
- if ($phone_use && $phone_use['type']==2){
- return Common::return_error('手机号已注册!');
- }
- $data['audit'] = 1;
- Db::startTrans();
- try {
- self::where('phone',$data['phone'])->update($data);
- Db::commit();
- return Common::return_success('申请成功,等待审核');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('申请失败');
- }
- }
- /**
- * 忘记密码
- */
- public static function forgotPassword($phone,$password,$ver_code){
- $phone_use = self::where('phone',$phone)->find();
- if (!$phone_use){
- return Common::return_error('手机号未注册!');
- }
- //获取最后的验证码
- $time = time()-90;
- $sms = Sms::where(['mobile' => $phone, 'event' => 'forgetpwd'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms->code != $ver_code){
- return Common::return_error('短信验证码不正确!');
- }
- $phone_use->password = md5($password);
- Db::startTrans();
- try {
- $phone_use->save();
- Db::commit();
- return Common::return_success('修改成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('修改失败');
- }
- }
- /**
- * 修改密码
- */
- public static function changePassword($user_id,$password,$ver_code){
- $phone_use = self::where('id',$user_id)->find();
- //获取最后的验证码
- $time = time()-90;
- $sms = Sms::where(['mobile' => $phone_use['phone'], 'event' => 'changepwd'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms->code != $ver_code){
- return Common::return_error('短信验证码不正确!');
- }
- $phone_use->password = md5($password);
- Db::startTrans();
- try {
- $phone_use->save();
- Db::commit();
- return Common::return_success('修改成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('修改失败');
- }
- }
- /**
- * 我的收货地址列表
- */
- public static function MyAddress($user_id,$Nowpage,$limits){
- $list = Address::where('is_del',0)
- ->where('user_id',$user_id)
- ->page($Nowpage,$limits)
- ->order('is_default desc,id desc')
- ->select();
- $data['count'] = count($list);
- $data['list'] = $list;
- return Common::return_success('成功',$data);
- }
- /**
- * 添加,修改收货地址
- */
- public static function AddEditAddress($user_id,$data,$address_id){
- if ($address_id){
- $address = Address::where('id',$address_id)->where('user_id',$user_id)->find();
- if (!$address) return Common::return_error('地址不存在');
- //编辑
- if ($address->save($data)){
- if ($data['is_default']==1){
- Address::where('user_id',$user_id)
- ->where('is_del',0)
- ->where('id','neq',$address_id)
- ->update(['is_default'=>0]);
- }
- return Common::return_success('编辑成功');
- }else{
- return Common::return_error('编辑失败');
- }
- }else{
- $data['user_id'] = $user_id;
- $addre = Address::create($data);
- if ($addre){
- $address_id = $addre->id;
- if ($data['is_default']==1){
- Address::where('user_id',$user_id)
- ->where('is_del',0)
- ->where('id','neq',$address_id)
- ->update(['is_default'=>0]);
- }
- return Common::return_success('添加成功');
- }else{
- return Common::return_error('添加失败');
- }
- }
- }
- /**
- * 设为默认地址
- */
- public static function SetAddressDefault($user_id,$address_id){
- $address = Address::where('id',$address_id)
- ->where('user_id',$user_id)
- ->find();
- if (!$address) return Common::return_error('地址不存在');
- if ($address->save(['is_default'=>1])){
- Address::where('user_id',$user_id)
- ->where('is_del',0)
- ->where('id','neq',$address_id)
- ->update(['is_default'=>0]);
- return Common::return_success('设置成功');
- }else{
- return Common::return_error('设置失败');
- }
- }
- /**
- * 删除收货地址
- */
- public static function DelAddress($user_id,$address_id){
- $address = Address::where('id',$address_id)->where('user_id',$user_id)->find();
- if (!$address) return Common::return_error('地址不存在');
- if ($address->save(['is_del'=>time()])){
- return Common::return_success('删除成功');
- }else{
- return Common::return_error('删除失败');
- }
- }
- /**
- * 编辑用户资料
- */
- public static function EditUserInfo($user_id,$data){
- if (self::where('id',$user_id)->update($data)){
- return Common::return_success('编辑成功');
- }else{
- return Common::return_error('编辑失败');
- }
- }
- /**
- * 申请成为设计师
- */
- public static function applyDesigner($user_id,$data){
- $data['type'] = 1;
- $data['audit'] = 1;
- Db::startTrans();
- try {
- self::where('id',$user_id)->update($data);
- Db::commit();
- return Common::return_success('提交成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('提交失败');
- }
- }
- /**
- * 投诉
- */
- public static function Message($user_id,$content){
- $data['content'] = $content;
- $data['user_id'] = $user_id;
- if (Message::create($data)) return Common::return_success('提交成功');
- return Common::return_error('提交失败');
- }
- /**
- * 时间管理
- */
- public static function timeList($user_id){
- $time = time();
- //组合数据
- $date = [];
- for ($i=1; $i<=15; $i++){
- $date[$i] = date('Y-m-d' ,strtotime( $i .' days', $time));
- }
- $array = [];
- foreach ($date as &$v){
- $da = UserTime::where('user_id',$user_id)->where('time',$v)->find();
- if ($da){
- $array2['switch'] = $da['switch'];
- $array2['price'] = $da['price'];
- }else{
- $array2['switch'] = 2;
- $array2['price'] = 0;
- }
- $array2['date'] = $v;
- array_push($array,$array2);
- }
- return Common::return_success('成功',$array);
- }
- /**
- * 时间设置开关
- */
- // public static function timeSwitch($user_id,$date,$switch,$price){
- // $info = UserTime::where('user_id',$user_id)->where('time',$date)->find();
- // if ($info){
- // if (UserTime::where('id',$info['id'])->update(['switch'=>$switch,'price'=>$price])){
- // return Common::return_success('成功');
- // }else{
- // return Common::return_error('失败');
- // }
- // }else{
- // if (UserTime::create(
- // [
- // 'user_id'=>$user_id,
- // 'time'=>$date,
- // 'switch'=>$switch,
- // 'price'=>$price
- // ]
- // )){
- // return Common::return_success('成功');
- // }else{
- // return Common::return_error('失败');
- // }
- // }
- // }
- /**
- * 时间设置开关
- */
- public static function timeSwitch($user_id,$json){
- foreach ($json as &$v){
- $info = UserTime::where('user_id',$user_id)->where('time',$v['date'])->find();
- if ($info){
- UserTime::where('id',$info['id'])->update(['switch'=>$v['switch'],'price'=>$v['price']]);
- }else{
- UserTime::create(
- [
- 'user_id'=>$user_id,
- 'time'=>$v['date'],
- 'switch'=>$v['switch'],
- 'price'=>$v['price']
- ]);
- }
- }
- return Common::return_success('成功');
- }
- /**
- * 余额提现
- */
- public static function userWithdraw($user_id,$money,$withdraw_type){
- $userinfo = self::where('id',$user_id)->find();
- if ($userinfo['money']<$money)
- return Common::return_error('余额不足');
- $data['user_id'] = $user_id;
- $order_no = Common::getNewOrderId($user_id);
- $data['order_no'] = $order_no;
- $data['withdraw_type'] = $withdraw_type;
- $data['price'] = $money;
- Db::startTrans();
- try {
- UserWithdraw::create($data);
- User::money($money,$user_id,$withdraw_type=='weixin' ? '微信提现' : '支付宝提现' .$money.'元');
- Db::commit();
- return Common::return_success('提交成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('提交失败');
- }
- }
- /**
- * 消费明细
- */
- public static function moneyRecord($user_id,$Nowpage,$limits){
- //提现记录
- $withdraw = UserWithdraw::where('user_id',$user_id)->field('withdraw_type,paid,why,create_at,price')->select();
- if (count($withdraw)>0){
- $withdraw = $withdraw->toArray();
- foreach ($withdraw as &$v){
- $v['price'] = '-'.$v['price'];
- $v['project_name'] = '';
- }
- }else{
- $withdraw = [];
- }
- //收益记录
- //$record = MoneyLog::where('user_id',$user_id)->where('title','订单收益')->select();
- $record = MoneyLog::where('user_id',$user_id)->whereIn('title','订单收益,加班费收益')->select();
- if (count($record)>0){
- $record = $record->toArray();
- $array = [];
- foreach ($record as &$v){
- $array2['withdraw_type'] = 'sy';
- $array2['paid'] = 1;
- $array2['why'] = '';
- $array2['create_at'] = $v['create_at'];
- $array2['price'] = '+'.$v['change_money'];
- if ($v['link_id']!=0){
- $project_name = Order::where('id',$v['link_id'])->value('project_name');
- }else{
- $project_name = '';
- }
- $array2['project_name'] = $project_name;
- array_push($array,$array2);
- }
- }else{
- $array = [];
- }
- $list = array_merge($withdraw,$array);
- $data['count'] = count($list);
- array_multisort(array_column($list,'create_at'),SORT_DESC,$list);
- $list = array_slice($list,($Nowpage-1)*$limits,$limits);
- $data['list'] = $list;
- return Common::return_success('成功',$data);
- }
- /**
- * 获取首页搜索日期列表
- */
- public static function searchDateList(){
- $time = time();
- //组合数据
- $date = [];
- for ($i=1; $i<=15; $i++){
- $date[$i-1]['date'] = date('Y-m-d' ,strtotime( $i .' days', $time));
- $date[$i-1]['time'] = date('m-d' ,strtotime( $i .' days', $time));
- }
- return Common::return_success('成功',$date);
- }
- /**
- * 首页设计师列表
- */
- public static function Designer($user_id,$Nowpage,$limits,$design_classifi,$keywords,$lat,$log,$city,$distance,$price,$evaluation,$date,$price_section_start,$price_section_end){
- // self::RecentlyPrice(); //更新价格
- $count = self::designerByWhere($user_id,$design_classifi,$keywords,$city,$date,$price_section_start,$price_section_end)->count();
- if ($count){
- $field = ['id','phone','name','nickname','headimg','price','design_classifi','skills_label','good_type','good_style','evaluation','works','province','city','area','work_experience','work_year','signature'];
- if ($lat && $log){
- $field['ROUND(6378.138 * 2 * ASIN(SQRT(POW( SIN( ( ' . $lat . ' * PI( ) / 180 - wd * PI( ) / 180 ) / 2 ), 2 ) + COS( ' . $lat . ' * PI( ) / 180 ) * COS( wd * PI( ) / 180 ) * POW( SIN( ( ' . $log . ' * PI( ) / 180 - jd * PI( ) / 180 ) / 2 ), 2 ))) * 1000)'] = 'distance';
- }
- $list = self::designerByWhere($user_id,$design_classifi,$keywords,$city,$date,$price_section_start,$price_section_end)
- ->field($field)
- ->when($distance,function ($query) use ($distance,$lat,$log){
- if ($lat && $log){
- $query->order('distance '.$distance);
- }
- })
- ->when($price,function ($query) use ($price){
- $query->order('price '.$price);
- })
- ->when($evaluation,function ($query) use ($evaluation){
- $query->order('evaluation '.$evaluation);
- })
- ->page($Nowpage,$limits)
- ->select();
- $time = time();
- //组合数据
- $date = [];
- for ($i=1; $i<=15; $i++){
- $date[$i] = date('Y-m-d' ,strtotime( $i .' days', $time));
- }
- foreach ($list as &$v){
- if ($v['works']){
- $v['works'] = explode(',',$v['works']);
- }else{
- $v['works'] = [];
- }
- if ($v['skills_label']){
- $v['skills_label'] = explode(',',$v['skills_label']);
- }else{
- $v['skills_label'] = [];
- }
- if ($v['good_type']){
- $v['good_type'] = explode(',',$v['good_type']);
- }else{
- $v['good_type'] = [];
- }
- if ($v['good_style']){
- $v['good_style'] = explode(',',$v['good_style']);
- }else{
- $v['good_style'] = [];
- }
- $v['name'] = $v['nickname'];
- $datee = Works::where('user_id',$v['id'])
- ->where('is_del',1)
- ->group('date')
- ->order('id desc')
- ->column('date');
- if ($datee){
- $arr = [];
- foreach ($datee as &$a){
- $array22['date'] = $a;
- $array22['imgs'] = Works::where('user_id',$v['id'])
- ->where('date',$a)
- ->where('is_del',1)
- ->field('id,img')
- ->select();
- array_push($arr,$array22);
- }
- }else{
- $arr = [];
- }
- $v['works_list'] = $arr;
- $array = [];
- foreach ($date as &$a){
- $da = UserTime::where('user_id',$v['id'])->where('time',$a)->find();
- if ($da){
- $order = Order::where('designer_id',$v['id'])
- ->where('yy_date',$a)
- ->where('is_del',1)
- ->whereIn('status','1,2')
- ->count();
- if ($order){
- $array2['switch'] = 2;
- }else{
- $array2['switch'] = $da['switch'];
- }
- $array2['price'] = $da['price'];
- }else{
- $array2['switch'] = 2;
- $array2['price'] = 0;
- }
- $array2['date'] = $a;
- array_push($array,$array2);
- }
- $v['date_list'] = $array;
- }
- }else{
- $list = [];
- }
- return Common::return_success('成功',compact('count','list'));
- }
- /**
- * 首页设计师列表where条件
- */
- public static function designerByWhere($user_id,$design_classifi,$keywords,$city,$date,$price_section_start,$price_section_end){
- $query = self::where('status',1)
- ->when($design_classifi,function ($query) use ($design_classifi){
- $query->where('design_classifi',$design_classifi);
- })
- ->when($keywords,function ($query) use ($keywords){
- $query->whereLike('name|nickname|skills_label|design_classifi|good_type|good_style','%'.$keywords.'%');
- })
- ->when($city,function ($query) use ($city){
- //$query->where('city',$city);
- $query->whereRaw('city like "%'.$city.'%" or province like "%'.$city.'%"');
- })
- ->when($date,function ($query) use ($date){
- $ids1 = UserTime::where('time',$date)
- ->where('switch',1)
- ->column('user_id');
- $ids2 = Order::where('yy_date',$date)
- ->where('is_del',1)
- ->whereIn('status','1,2')
- ->column('designer_id');
- $ids = array_diff($ids1,$ids2); //对比两数组差异
- $query->whereIn('id',$ids);
- })
- ->when($price_section_start,function ($query) use ($price_section_start,$price_section_end){
- if ($price_section_start && $price_section_end){
- $query->where('price','>=',$price_section_start)->where('price','<=',$price_section_end);
- }
- })
- ->where('price','>',0)
- //->where('id','neq',$user_id)
- ->where('type',2);
- return $query;
- }
- /**
- * 获取设计师最近的价格显示
- */
- public static function RecentlyPrice(){
- self::where('status',1)->where('type',2)->chunk(50,function ($list){
- $time = time();
- //组合数据
- $date = [];
- for ($i=1; $i<=7; $i++){
- $date[$i] = date('Y-m-d' ,strtotime( $i .' days', $time));
- }
- foreach ($list as &$v){
- $price = UserTime::where('user_id',$v['id'])
- ->where('switch',1)
- ->order('time asc')
- ->whereBetween('time',[$date[1],$date[7]])
- ->limit(1)
- ->value('price');
- if (!$price){
- $price = 0;
- }
- self::where('id',$v['id'])->update(['price'=>$price]);
- }
- });
- }
- /**
- * 获取可使用优惠券列表
- */
- public static function getCouponList($user_id,$money){
- CouponUser::couponsTimeOut($user_id);
- $list = CouponUser::where('user_id',$user_id)
- ->where('use_min_price','<=',$money)
- ->where('end_time','>',time())
- ->where('status',0)
- ->select();
- if ($list){
- foreach ($list as &$v){
- $v['add_time'] = date('Y-m-d',$v['add_time']);
- $v['end_time'] = date('Y-m-d',$v['end_time']);
- }
- }
- return Common::return_success('成功',$list);
- }
- /**
- * 下单时间段列表
- */
- public static function periodTimeList(){
- $list = PeriodTime::where('is_del',1)->select();
- return Common::return_success('成功',$list);
- }
- /**
- * 下单
- */
- public static function PlaceOrder($user_id,$address_id,$designer_id,$yy_date,$project_name,$coupon_id,$pay_type,$period_id){
- // if ($pay_type=='weixin'){
- // return Common::return_error('暂时无法使用微信支付');
- // }
- $orde = Order::where('user_id',$user_id)->where('is_del',1)->where('overtime_pay_status',1)->find();
- if ($orde) return Common::return_error('有未支付的加班费订单,无法下单');
- $designer = self::where('id',$designer_id)
- ->where('type',2)
- ->where('status',1)
- ->find();
- if (!$designer) return Common::return_error('设计师不存在');
- $address = Address::where('id',$address_id)
- ->where('user_id',$user_id)
- ->where('is_del',0)
- ->find();
- if (!$address) return Common::return_error('地址不存在');
- $time = UserTime::where('user_id',$designer_id)
- ->where('time',$yy_date)
- //->where('switch',1)
- ->find();
- $order = Order::where('designer_id',$designer_id)
- ->where('is_del',1)
- ->whereIn('status','1,2')
- ->where('yy_date',$yy_date)
- ->find();
- $period = PeriodTime::where('id',$period_id)->find();
- if ($order) return Common::return_error('预约日期设计师已接单');
- if ($time && $time['switch']==2) return Common::return_error('预约日期设计师未开启接单');
- //价格
- $price = $designer['price'];
- $coupon_price = 0;
- $pay_price = $price;
- if ($coupon_id){
- $coupon = CouponUser::where('id',$coupon_id)
- ->where('user_id',$user_id)
- ->where('status',0)
- ->find();
- if (!$coupon) return Common::return_error('优惠券不存在');
- $coupon_price = $coupon['coupon_price'];
- $pay_price = bcsub($pay_price,$coupon_price,2);
- }else{
- $coupon_id = 0;
- }
- //订单号
- $order_no = Common::getNewOrderId($user_id);
- $data = [
- 'order_no'=>$order_no,
- 'user_id'=>$user_id,
- 'designer_id'=>$designer_id,
- 'address_id'=>$address_id,
- 'address_jd'=>$address['jd'],
- 'address_wd'=>$address['wd'],
- 'name'=>$address['name'],
- 'phone'=>$address['phone'],
- 'province'=>$address['province'],
- 'city'=>$address['city'],
- 'area'=>$address['area'],
- 'address'=>$address['address'],
- 'yy_date'=>$yy_date,
- 'servicing_time'=>$period['start'].'-'.$period['end'],
- 'project_name'=>$project_name,
- 'coupon_id'=>$coupon_id,
- 'pay_type'=>$pay_type,
- 'price'=>$price,
- 'coupon_price'=>$coupon_price,
- 'pay_price'=>$pay_price,
- ];
- Db::startTrans();
- try {
- $order = Order::create($data);
- $order_id = $order->id;
- Common::order_status($order_id,'订单生成');
- //优惠券确定
- if ($coupon_id){
- CouponUser::where('id',$coupon_id)->update(['status'=>1,'use_time'=>time()]);
- }
- //先模拟下单成功
- // $da['pay_time'] = time();
- // $da['status'] = 1;
- // Order::where('id',$order['id'])->update($da);
- // Db::commit();
- // return Common::return_success('成功');
- switch ($pay_type){
- case 'weixin':
- $wx = new WxPay();//实例化微信torganizationid支付控制器
- $body = '订单号' . $order_no;//支付说明
- $total_fee = $pay_price * 100;//支付金额(乘以100)
- $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/pay/pay_order';//回调地址
- $out_trade_no = $order_no;//订单号
- $config = $wx->retrunconfig();
- try{
- $app = Factory::payment($config);
- $result = $app->order->unify([
- 'body' => $body,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
- ]);
- $jssdk = $app->jssdk;
- $order1 = $jssdk->appConfig($result['prepay_id']);
- Order::where('order_no',$order_no)->update(['wx_order'=>json_encode($order1,true)]);
- Db::commit();
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order1;
- return Common::return_success('成功',$retrun_data);
- }catch (Exception $e){
- Db::rollback();
- return Common::return_error($order['return_msg']);
- }
- break;
- case 'zfb':
- $zfb = new AliPay();//实例化支付宝支付控制器
- $body = '设计下单支付';//支付说明
- $out_trade_no = $order_no;//订单号
- $total_fee = $pay_price;//支付金额
- $notify_url = 'http://'.$_SERVER['SERVER_NAME'].'/api/pay/alipay_order';//回调地址
- $order = $zfb->aliPay($body, $total_fee, $out_trade_no, $notify_url);//调用支付宝支付的方法
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order;
- Db::commit();
- return Common::return_success('成功',$retrun_data);
- break;
- }
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('失败');
- }
- }
- /**
- * 用户端订单列表
- */
- public static function userOrderList($user_id,$status,$keywords,$Nowpage,$limits){
- $count = self::orderByWhere($status,$user_id,$keywords)->count();
- if ($count){
- $list = self::orderByWhere($status,$user_id,$keywords)
- ->page($Nowpage,$limits)
- ->order('id desc')
- ->select();
- }else{
- $list = [];
- }
- return Common::return_success('成功',compact('count','list'));
- }
- /**
- * 订单条件
- */
- public static function orderByWhere($status,$user_id,$keywords){
- $query = Order::where('user_id',$user_id)
- ->where('is_del',1)
- ->when($status,function ($query) use ($status){
- if ($status!='all'){
- $query->where('status',$status);
- }
- })
- ->when($keywords,function ($query) use ($keywords){
- $query->whereLike('project_name|order_no','%'.$keywords.'%');
- });
- if ($status==='0'){
- $query = $query->where('status',$status);
- }
- return $query;
- }
- /**
- * 订单支付
- */
- public static function payOrder($user_id,$order_no){
- $order = Order::where('order_no', $order_no)->where('user_id', $user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- $order = $order->toArray();
- if ($order['status'])
- return Common::return_error('订单已支付');
- $designer = self::where('id',$order['designer_id'])
- ->where('type',2)
- ->where('status',1)
- ->find();
- if (!$designer) return Common::return_error('设计师不存在');
- $time = UserTime::where('user_id',$order['designer_id'])
- ->where('time',$order['yy_date'])
- ->where('switch',1)
- ->find();
- $order2 = Order::where('designer_id',$order['designer_id'])
- ->where('is_del',1)
- ->whereIn('status','1,2')
- ->where('yy_date',$order['yy_date'])
- ->find();
- if (!$time || $order2) return Common::return_error('预约日期设计师未开启接单或已接单');
- if ($order['coupon_id']){
- $coupon = CouponUser::where('id',$order['coupon_id'])
- ->where('user_id',$user_id)
- ->where('status',0)
- ->find();
- if (!$coupon) return Common::return_error('优惠券不存在或已使用');
- }
- $pay_type = $order['pay_type'];
- $pay_price = $order['pay_price'];
- Db::startTrans();
- switch ($pay_type){
- case 'weixin':
- $wx = new WxPay();//实例化微信torganizationid支付控制器
- $body = '订单号' . $order_no;//支付说明
- $total_fee = $pay_price * 100;//支付金额(乘以100)
- $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/pay/pay_order';//回调地址
- $out_trade_no = $order_no;//订单号
- $config = $wx->retrunconfig();
- try{
- $app = Factory::payment($config);
- $result = $app->order->unify([
- 'body' => $body,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
- ]);
- $jssdk = $app->jssdk;
- $order1 = $jssdk->appConfig($result['prepay_id']);
- Order::where('order_no',$order_no)->update(['wx_order'=>json_encode($order1,true)]);
- Db::commit();
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order1;
- // Common::order_status($order['id'],'订单支付成功');
- return Common::return_success('成功',$retrun_data);
- }catch (Exception $e){
- Db::rollback();
- return Common::return_error($order['return_msg']);
- }
- break;
- case 'zfb':
- $zfb = new AliPay();//实例化支付宝支付控制器
- $body = '设计下单支付';//支付说明
- $out_trade_no = $order_no;//订单号
- $total_fee = $pay_price;//支付金额
- $notify_url = 'http://'.$_SERVER['SERVER_NAME'].'/api/pay/alipay_order';//回调地址
- $order = $zfb->aliPay($body, $total_fee, $out_trade_no, $notify_url);//调用支付宝支付的方法
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order;
- Db::commit();
- return Common::return_success('成功',$retrun_data);
- break;
- }
- }
- /**
- * 订单支付加班费
- */
- public static function overtimePayOrder($user_id,$order_no,$pay_type){
- // if ($pay_type=='weixin'){
- // return Common::return_error('暂时无法使用微信支付');
- // }
- $order = Order::where('order_no', $order_no)->where('user_id', $user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- $order = $order->toArray();
- if (in_array($order['overtime_pay_status'],['0','2']))
- return Common::return_error('没有加班费或加班费已支付');
- $pay_price = $order['overtime_pay'];
- Db::startTrans();
- switch ($pay_type){
- case 'weixin':
- $wx = new WxPay();//实例化微信torganizationid支付控制器
- $body = '订单号' . $order['overtime_pay_order_no'];//支付说明
- $total_fee = $pay_price * 100;//支付金额(乘以100)
- $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/pay/overtime_pay_order';//回调地址
- $out_trade_no = $order['overtime_pay_order_no'];//订单号
- $config = $wx->retrunconfig();
- try{
- $app = Factory::payment($config);
- $result = $app->order->unify([
- 'body' => $body,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
- ]);
- $jssdk = $app->jssdk;
- $order1 = $jssdk->appConfig($result['prepay_id']);
- Order::where('order_no',$order_no)->update(['overtime_pay_type'=>$pay_type,'overtime_wx_order'=>json_encode($order1,true)]);
- Db::commit();
- $retrun_data['order_no'] = $out_trade_no;
- $retrun_data['pay'] = $order1;
- return Common::return_success('成功',$retrun_data);
- }catch (Exception $e){
- Db::rollback();
- return Common::return_error($order['return_msg']);
- }
- break;
- case 'zfb':
- $zfb = new AliPay();//实例化支付宝支付控制器
- $body = '设计下单支付';//支付说明
- $out_trade_no = $order['overtime_pay_order_no'];//订单号
- $total_fee = $pay_price;//支付金额
- $notify_url = 'http://'.$_SERVER['SERVER_NAME'].'/api/pay/overtime_alipay_order';//回调地址
- $order = $zfb->aliPay($body, $total_fee, $out_trade_no, $notify_url);//调用支付宝支付的方法
- $retrun_data['order_no'] = $out_trade_no;
- $retrun_data['pay'] = $order;
- Order::where('order_no',$order_no)->update(['overtime_pay_type'=>$pay_type]);
- Db::commit();
- return Common::return_success('成功',$retrun_data);
- break;
- }
- }
- /**
- * 订单详情
- */
- public static function orderDetail($order_no){
- $order = Order::where('order_no',$order_no)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- $designer =self::where('id',$order['designer_id'])->field('id,name,headimg,evaluation,works,design_classifi')->find();
- if ($designer['works']){
- $designer['works'] = explode(',',$designer['works']);
- }else{
- $designer['works'] = [];
- }
- $order['designer'] =$designer;
- $order['pay_time'] = date('Y-m-d H:i:s',$order['pay_time']);
- return Common::return_success('成功',$order);
- }
- /**
- * 取消订单
- */
- public static function cancelOrder($user_id,$order_no){
- $order = Order::where('order_no',$order_no)->where('user_id',$user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- if ($order['status'])
- return Common::return_error('订单无法取消');
- Db::startTrans();
- try {
- $order->status = 5;
- $order->remark = '用户取消订单';
- $order->save();
- if ($order->coupon_id){
- CouponUser::where('id',$order->coupon_id)->update(['status'=>0]);
- }
- Db::commit();
- Common::order_status($order->id,'订单取消成功');
- return Common::return_success('取消成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('取消失败');
- }
- }
- /**
- * 删除订单
- */
- public static function delOrder($user_id,$order_no){
- $order = Order::where('order_no',$order_no)->where('user_id',$user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- Db::startTrans();
- try {
- $order->is_del = 0;
- $order->del_time = time();
- $order->save();
- Db::commit();
- return Common::return_success('删除成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('删除失败');
- }
- }
- /**
- * 订单评价
- */
- public static function evaluationOrder($user_id,$order_no,$evaluation,$evaluation_content){
- $order = Order::where('order_no',$order_no)->where('user_id',$user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- if ($order['status']!=3)
- return Common::return_error('订单状态错误');
- Db::startTrans();
- try {
- $order->status = 4;
- $order->evaluation = $evaluation;
- $order->evaluation_time = time();
- $order->evaluation_content = $evaluation_content;
- $order->save();
- Db::commit();
- $sum = Order::where('designer_id',$order['designer_id'])->where('status',4)->sum('evaluation');
- $count = Order::where('designer_id',$order['designer_id'])->where('status',4)->count();
- $eva = ceil($sum/$count);
- User::where('id',$order['designer_id'])->update(['evaluation'=>$eva]);
- Common::order_status($order->id,'订单评价成功');
- return Common::return_success('评价成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('评价失败');
- }
- }
- /**
- * 用户端我的评价
- */
- public static function userEvaluationList($user_id,$Nowpage,$limits){
- $where = [
- 'a.user_id'=>$user_id,
- 'a.status'=>4,
- 'a.is_del'=>1
- ];
- $count = Order::where($where)
- ->alias('a')
- ->join('q_user b','a.user_id=b.id')
- ->join('q_user c','a.designer_id=c.id')
- ->count();
- if ($count){
- $list = Order::where($where)
- ->alias('a')
- ->join('q_user b','a.user_id=b.id')
- ->join('q_user c','a.designer_id=c.id')
- ->page($Nowpage,$limits)
- ->field('a.id,a.order_no,a.evaluation,a.evaluation_time,a.evaluation_content,b.name,b.nickname,b.headimg,c.name as designer_name')
- ->order('a.id desc')
- ->select();
- foreach ($list as &$v){
- $v['evaluation_time'] = date('Y-m-d',$v['evaluation_time']);
- }
- }else{
- $list = [];
- }
- return Common::return_success('成功',compact('count','list'));
- }
- /**
- * 设计师端我的评价
- */
- public static function designerEvaluationList($user_id,$Nowpage,$limits){
- $where = [
- 'a.designer_id'=>$user_id,
- 'a.status'=>4,
- 'a.is_del'=>1
- ];
- $count = Order::where($where)
- ->alias('a')
- ->join('q_user b','a.user_id=b.id')
- ->count();
- if ($count){
- $list = Order::where($where)
- ->alias('a')
- ->join('q_user b','a.user_id=b.id')
- ->page($Nowpage,$limits)
- ->field('a.id,a.order_no,a.evaluation,a.evaluation_time,a.evaluation_content,b.name,b.nickname,b.headimg')
- ->order('a.id desc')
- ->select();
- foreach ($list as &$v){
- $v['evaluation_time'] = date('Y-m-d',$v['evaluation_time']);
- }
- }else{
- $list = [];
- }
- return Common::return_success('成功',compact('count','list'));
- }
- /**
- * 设计师端添加作品
- */
- public static function addWorks($user_id,$imgs){
- $data = [
- 'user_id'=>$user_id,
- 'date'=>date('Y-m-d')
- ];
- foreach ($imgs as &$v){
- $data['img'] = $v;
- Works::create($data);
- }
- return Common::return_success('成功');
- }
- /**
- * 设计师端作品管理
- */
- public static function workList($user_id){
- $date = Works::where('user_id',$user_id)
- ->where('is_del',1)
- ->group('date')
- ->order('id desc')
- ->column('date');
- if ($date){
- $array = [];
- foreach ($date as &$v){
- $array2['date'] = $v;
- $array2['imgs'] = Works::where('user_id',$user_id)
- ->where('date',$v)
- ->where('is_del',1)
- ->field('id,img')
- ->select();
- array_push($array,$array2);
- }
- }else{
- $array = [];
- }
- return Common::return_success('成功',$array);
- }
- /**
- * 设计师端删除作品
- */
- public static function delWorks($user_id,$ids){
- if (Works::whereIn('id',$ids)->where('user_id',$user_id)->update(['is_del'=>0])){
- return Common::return_success('删除成功');
- }else{
- return Common::return_error('删除失败');
- }
- }
- /**
- * 设计师端首页
- */
- public static function designerIndex($user_id){
- $list = Order::where('designer_id',$user_id)
- ->where('is_del',1)
- ->whereIn('status','1,2')
- ->order('id desc')
- ->limit(2)
- ->select();
- return Common::return_success('成功',$list);
- }
- /**
- * 用户端订单列表
- */
- public static function designerOrderList($user_id,$status,$keywords,$Nowpage,$limits){
- $count = self::designerOrderByWhere($status,$user_id,$keywords)->count();
- if ($count){
- $list = self::designerOrderByWhere($status,$user_id,$keywords)
- ->page($Nowpage,$limits)
- ->order('id desc')
- ->select();
- }else{
- $list = [];
- }
- return Common::return_success('成功',compact('count','list'));
- }
- /**
- * 订单条件
- */
- public static function designerOrderByWhere($status,$user_id,$keywords){
- $query = Order::where('designer_id',$user_id)
- ->where('is_del',1)
- ->when($status,function ($query) use ($status){
- if (in_array($status,['1','2'])){
- $query->where('status',$status);
- }elseif ($status==3){
- $query->whereIn('status','3,4');
- }
- })
- ->when($keywords,function ($query) use ($keywords){
- $query->whereLike('project_name|order_no','%'.$keywords.'%');
- });
- if ($status===0){
- $query = $query->where('status',$status);
- }
- return $query;
- }
- /**
- * 设计师开始打卡
- */
- public static function clockInStart($user_id,$order_no,$start_address){
- $order = Order::where('order_no',$order_no)->where('designer_id',$user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- if ($order['status']!=1)
- return Common::return_error('订单状态错误');
- Db::startTrans();
- try {
- $order->status = 2;
- $order->start_time = date('Y-m-d H:i:s');
- $order->start_address = $start_address;
- $order->save();
- Db::commit();
- Common::order_status($order->id,'设计师开始打卡成功');
- return Common::return_success('打卡成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('打卡失败');
- }
- }
- /**
- * 设计师结束打卡
- */
- public static function clockInEnd($user_id,$order_no,$end_address){
- $order = Order::where('order_no',$order_no)->where('designer_id',$user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- if ($order['status']!=2)
- return Common::return_error('订单状态错误');
- Db::startTrans();
- try {
- $order->status = 3;
- $order->end_time = date('Y-m-d H:i:s');
- $order->end_address = $end_address;
- //判断是否产生加班费
- $servicing = explode('-',$order['servicing_time']);
- $end_time = strtotime($order['yy_date'].' '.$servicing[1]);
- if (time()>$end_time){
- $poor_minutes = floor((time()-$end_time)%86400/60);
- //获取加班费每分钟多钱
- $user_overtime_pay = self::where('id',$user_id)->value('overtime_pay');
- if ($user_overtime_pay>0){
- $price = $user_overtime_pay;
- }else{
- $price = $order['price'] * 0.1; //订单金额10%的加班费
- }
- $overtime_pay = sprintf("%.2f", ($price/60) * $poor_minutes); //四舍五入保留两位小数
- $order->overtime_pay = $overtime_pay;
- $order->overtime_pay_order_no = Common::getNewOrderId($order['designer_id']);
- $order->overtime_pay_status = 1;
- }
- $designer = self::where('id',$order['designer_id'])->field('earnings_ratio,margin')->find();
- //获取设计师收益金额
- $designer_money = sprintf("%.2f",$order['pay_price'] * ($designer['earnings_ratio']/100));
- $order->designer_money = $designer_money;
- if ($designer['margin']<2000){
- $designer_margin_money = 20;
- $designer_real_money = $designer_money-20;
- }else{
- $designer_margin_money = 0;
- $designer_real_money = $designer_money;
- }
- $order->designer_margin_money = $designer_margin_money;
- $order->designer_real_money = $designer_real_money;
- $order->designer_earnings_ratio = $designer['earnings_ratio'];
- $order->save();
- User::money($designer_real_money,$order['designer_id'],'订单收益',1,$order['id']);
- if ($designer_margin_money>0){
- User::margin($designer_margin_money,$order['designer_id'],'订单抵押金',1);
- }
- Common::order_status($order->id,'设计师结束打卡成功');
- Db::commit();
- return Common::return_success('打卡成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('打卡失败');
- }
- }
- /**
- * 设计师结束工作上传作品
- */
- public static function orderUploading($user_id,$order_no,$works){
- $order = Order::where('order_no',$order_no)->where('designer_id',$user_id)->find();
- if (!$order)
- return Common::return_error('订单不存在');
- // if ($order['status']!=3)
- // return Common::return_error('订单状态错误');
- Db::startTrans();
- try {
- $order->works = implode(',',$works);
- $order->save();
- Db::commit();
- Common::order_status($order->id,'设计师上传作品成功');
- return Common::return_success('上传成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('上传失败');
- }
- }
- /**
- * 优惠券列表
- */
- public static function couponList($user_id,$Nowpage,$limits){
- CouponUser::couponsTimeOut($user_id);
- $count = CouponUser::where('user_id',$user_id)
- ->where('status',0)
- ->count();
- if ($count){
- $list = CouponUser::where('user_id',$user_id)
- ->where('status',0)
- ->order('id desc')
- ->page($Nowpage,$limits)
- ->select();
- foreach ($list as &$v){
- $v['add_time'] = date('Y-m-d',$v['add_time']);
- $v['end_time'] = date('Y-m-d',$v['end_time']);
- }
- }else{
- $list = [];
- }
- return Common::return_success('成功',compact('count','list'));
- }
- /**
- * 公告列表
- */
- public static function notice(){
- $list = Notice::where('status',1)
- ->where('is_del',1)
- ->where('is_show',1)
- ->order('id desc')
- ->select();
- return Common::return_success('成功',$list);
- }
- /**
- * 根据手机号获取用户信息
- */
- public static function getByMobile($phone){
- $userinfo = self::where('phone',$phone)->find();
- return $userinfo;
- }
- /**
- * 获取用户信息
- */
- public static function getUserInfo($id){
- $info = self::where('id',$id)->field('password',true)->find();
- if (!$info['skills_label']){
- $info['skills_label'] = [];
- }else{
- $info['skills_label'] = explode(',', $info['skills_label']);
- }
- if (!$info['good_type']){
- $info['good_type'] = [];
- }else{
- $info['good_type'] = explode(',', $info['good_type']);
- }
- if (!$info['good_style']){
- $info['good_style'] = [];
- }else{
- $info['good_style'] = explode(',', $info['good_style']);
- }
- if (!$info['related_certificate']){
- $info['related_certificate'] = [];
- }else{
- $info['related_certificate'] = explode(',', $info['related_certificate']);
- }
- if (!$info['works']){
- $info['works'] = [];
- }else{
- $info['works'] = explode(',', $info['works']);
- }
- return $info;
- }
- /**
- * 变更会员余额
- * @param int $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function money($money, $user_id, $memo, $pm = 0, $link_id = 0)
- {
- $user = self::get($user_id);
- if ($user)
- {
- $before = $user->money;
- if ($pm==1){
- $after = $user->money + $money;
- }else{
- $after = $user->money - $money;
- }
- //更新会员信息
- $user->save(['money' => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id,'pm' => $pm, 'change_money' => $money, 'before' => $before, 'after' => $after, 'title' => $memo,'link_id'=>$link_id]);
- }
- }
- /**
- * 变更会员余额
- * @param int $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function margin($money, $user_id, $memo, $pm = 0)
- {
- $user = self::get($user_id);
- if ($user)
- {
- $before = $user->margin;
- if ($pm==1){
- $after = $user->margin + $money;
- }else{
- $after = $user->margin - $money;
- }
- //更新会员信息
- $user->save(['margin' => $after]);
- //写入日志
- MarginLog::create(['user_id' => $user_id,'pm' => $pm, 'change_money' => $money, 'before' => $before, 'after' => $after, 'title' => $memo]);
- }
- }
- /**
- * 十分钟未支付,取消订单
- */
- public static function systemCancelOrder(){
- Order::where('status',0)->chunk(20,function ($list){
- foreach ($list as &$v) {
- $seven = strtotime($v['create_at'])+(10*60);
- if ($seven<time()){
- Order::where('id',$v['id'])->update(
- [
- 'status'=>5,
- 'cancel_reason'=>'订单超时自动取消'
- ]
- );
- }
- }
- },'id','asc');
- }
- }
|