12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790 |
- <?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'];
- if ($v['pm']==1){
- $array2['price'] = '+'.$v['change_money'];
- }else{
- $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');
- }
- }
|