User.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. <?php
  2. namespace app\common\model;
  3. use app\common\model\Order;
  4. use app\common\model\Sms;
  5. use Firebase\JWT\JWT;
  6. use app\common\model\UserIntegralLog;
  7. use think\Model;
  8. use app\common\library\Common;
  9. use think\facade\Validate;
  10. use think\Request;
  11. use think\Db;
  12. use function foo\func;
  13. use app\common\library\WxService;
  14. use app\common\library\WxPay;
  15. use app\common\library\AliPay;
  16. use EasyWeChat\Factory;
  17. header('Access-Control-Allow-Origin: *');
  18. /**
  19. * 会员模型
  20. */
  21. class User Extends Model
  22. {
  23. // 开启自动写入时间戳字段
  24. protected $autoWriteTimestamp = 'TIMESTAMP';
  25. // 定义时间戳字段名
  26. protected $createTime = 'create_at';
  27. protected $updateTime = 'update_at';
  28. // 追加属性
  29. protected $append = [
  30. ];
  31. protected $table = 'q_user';
  32. /**
  33. * 注册
  34. */
  35. public static function register($phone,$password,$ver_code,$invite_code){
  36. $phone_use = self::where('phone',$phone)->find();
  37. if ($phone_use){
  38. return Common::return_error('手机号已注册!');
  39. }
  40. //获取最后的验证码
  41. $time = time()-90;
  42. $sms = Sms::where(['mobile' => $phone, 'event' => 'register'])
  43. ->where('createtime','>',$time)
  44. ->order('id', 'DESC')
  45. ->find();
  46. if (!$sms || $sms->code != $ver_code){
  47. return Common::return_error('短信验证码不正确!');
  48. }
  49. $data['phone'] = $phone;
  50. $data['fid'] = $invite_code ? $invite_code : 0;
  51. $data['password'] = md5($password);
  52. $data['ip'] = request()->ip();
  53. Db::startTrans();
  54. try {
  55. self::create($data);
  56. if ($invite_code){
  57. CouponUser::create([
  58. 'user_id'=>$invite_code,
  59. 'coupon_title'=>'满100可用',
  60. 'coupon_price'=>50,
  61. 'use_min_price'=>100,
  62. 'add_time'=>time(),
  63. 'end_time'=>time()+(30*24*60*60),
  64. ]);
  65. }
  66. Db::commit();
  67. return Common::return_success('注册成功');
  68. }catch (Exception $e) {
  69. Db::rollback();
  70. return Common::return_error('注册失败');
  71. }
  72. }
  73. /**
  74. * 登录
  75. */
  76. public static function login($phone,$password,$type,$ver_code,$come){
  77. $phone_use = self::where('phone',$phone)->find();
  78. if (!$phone_use){
  79. return Common::return_error('手机号未注册!');
  80. }
  81. if ($phone_use['status']!=1){
  82. return Common::return_error('账号已禁用!');
  83. }
  84. if ($type==1){
  85. if ($phone_use['password'] != md5($password)){
  86. return Common::return_error('密码错误!');
  87. }
  88. }elseif ($type==2){
  89. //获取最后的验证码
  90. $time = time()-90;
  91. $sms = Sms::where(['mobile' => $phone, 'event' => 'login'])
  92. ->where('createtime','>',$time)
  93. ->order('id', 'DESC')
  94. ->find();
  95. if (!$sms || $sms->code != $ver_code){
  96. return Common::return_error('短信验证码不正确!');
  97. }
  98. }
  99. if ($come==2){
  100. $info = $phone_use;
  101. if (!$info['skills_label']){
  102. $info['skills_label'] = [];
  103. }else{
  104. $info['skills_label'] = explode(',', $info['skills_label']);
  105. }
  106. if (!$info['good_type']){
  107. $info['good_type'] = [];
  108. }else{
  109. $info['good_type'] = explode(',', $info['good_type']);
  110. }
  111. if (!$info['good_style']){
  112. $info['good_style'] = [];
  113. }else{
  114. $info['good_style'] = explode(',', $info['good_style']);
  115. }
  116. if (!$info['related_certificate']){
  117. $info['related_certificate'] = [];
  118. }else{
  119. $info['related_certificate'] = explode(',', $info['related_certificate']);
  120. }
  121. if (!$info['works']){
  122. $info['works'] = [];
  123. }else{
  124. $info['works'] = explode(',', $info['works']);
  125. }
  126. $data['info'] = $phone_use;
  127. if ($phone_use['type']==1 && $phone_use['audit']==0){
  128. $data['type'] = 1;
  129. }elseif ($phone_use['type']==1 && $phone_use['audit']==1){
  130. $data['type'] = 2;
  131. }elseif ($phone_use['type']==1 && $phone_use['audit']==3){
  132. $data['type'] = 3;
  133. }else{
  134. $data['type'] = 4;
  135. }
  136. $data['token'] = "";
  137. if ($data['type']==4){
  138. self::where('id',$phone_use['id'])->update(['ip'=>request()->ip()]);
  139. LoginLog::create(['user_id'=>$phone_use['id'],'ip'=>request()->ip()]);
  140. $token = JWT::encode($phone_use,config('jwt.key'));
  141. $data['token'] = $token;
  142. }
  143. return Common::return_success('成功',$data);
  144. }
  145. self::where('id',$phone_use['id'])->update(['ip'=>request()->ip()]);
  146. LoginLog::create(['user_id'=>$phone_use['id'],'ip'=>request()->ip()]);
  147. $token = JWT::encode($phone_use,config('jwt.key'));
  148. $data['token'] = $token;
  149. return Common::return_success('登录成功',$data);
  150. }
  151. /**
  152. * 微信授权登录
  153. * @param $code
  154. */
  155. public static function wechatLogin($code){
  156. $appid = Config::get_values('wechat_appid');
  157. $secret = Config::get_values('wechat_appsecret');
  158. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
  159. $result = Common::httpcurl($url);
  160. if (isset($result['access_token'])){
  161. $access_token=$result['access_token'];
  162. $openid=$result['openid'];
  163. $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
  164. $resinfos =Common::httpcurl($urltoc);
  165. //数据库是否已注册
  166. $user = self::where('status',1)->where('openid',$resinfos['openid'])->find();
  167. if ($user){
  168. self::where('id',$user['id'])->update(['ip'=>request()->ip()]);
  169. LoginLog::create(['user_id'=>$user['id'],'ip'=>request()->ip()]);
  170. $token = JWT::encode($user,config('jwt.key'));
  171. return Common::return_success('登录成功',['token'=>$token]);
  172. }else{
  173. return Common::return_error('未注册!');
  174. }
  175. }else{
  176. return Common::return_error('获取access_token失败');
  177. }
  178. }
  179. /**
  180. * 获取微信session_key
  181. */
  182. public static function getSessionKey($code){
  183. $appid = Config::get_values('small_wechat_id');
  184. $secret = Config::get_values('small_wechat_appsecret');
  185. echo $appid."<br />";
  186. echo $secret;
  187. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
  188. $session_key = Common::curlRequest($url);
  189. print_r($session_key);die;
  190. return $session_key;
  191. }
  192. /**
  193. * 微信登录绑定手机号
  194. */
  195. public static function wechatLoginBindPhone($code,$rawData,$phone,$ver_code,$password){
  196. $phone_use = self::where('phone',$phone)->find();
  197. if ($phone_use && $phone_use['openid']!=''){
  198. return Common::return_error('手机号已被绑定!');
  199. }
  200. // if (!$phone_use['status']){
  201. // return Common::return_error('账号已禁用!');
  202. // }
  203. //获取最后的验证码
  204. $time = time()-90;
  205. $sms = Sms::where(['mobile' => $phone, 'event' => 'bindwechat'])
  206. ->where('createtime','>',$time)
  207. ->order('id', 'DESC')
  208. ->find();
  209. if (!$sms || $sms->code != $ver_code){
  210. return Common::return_error('短信验证码不正确!');
  211. }
  212. $appid = Config::get_values('wechat_appid');
  213. $secret = Config::get_values('wechat_appsecret');
  214. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
  215. $result = Common::httpcurl($url);
  216. if (isset($result['access_token'])){
  217. $access_token=$result['access_token'];
  218. $openid=$result['openid'];
  219. $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
  220. $resinfos = Common::httpcurl($urltoc);
  221. $openid = $resinfos['openid'];
  222. if (!$phone_use){
  223. $data['phone'] = $phone;
  224. $data['nickname'] = $resinfos['nickname'];
  225. $data['headimg'] = $resinfos['headimgurl'];
  226. $data['openid'] = $openid;
  227. $data['password'] = md5($password);
  228. $data['ip'] = request()->ip();
  229. Db::startTrans();
  230. try {
  231. $user = self::create($data);
  232. Db::commit();
  233. $userId =$user->id;
  234. LoginLog::create(['user_id'=>$userId,'ip'=>request()->ip()]);
  235. $user = self::where('id',$userId)->find();
  236. $token = JWT::encode($user,config('jwt.key'));
  237. return Common::return_success('授权成功',['token'=>$token]);
  238. }catch (Exception $e) {
  239. Db::rollback();
  240. return Common::return_error('失败');
  241. }
  242. }else{
  243. $da['openid'] = $openid;
  244. if (!$phone_use['headimg']){
  245. $da['headimg'] = $resinfos['headimgurl'];
  246. }
  247. if (!$phone_use['nickname']){
  248. $da['nickname'] = $resinfos['nickname'];
  249. }
  250. if (self::where('id',$phone_use['id'])->update($da)){
  251. LoginLog::create(['user_id'=>$phone_use['id'],'ip'=>request()->ip()]);
  252. $token = JWT::encode($phone_use,config('jwt.key'));
  253. return Common::return_success('绑定成功',['token'=>$token]);
  254. }else{
  255. return Common::return_error('失败');
  256. }
  257. }
  258. }else{
  259. return Common::return_error('获取access_token失败');
  260. }
  261. }
  262. /**
  263. * 商家注册第一步
  264. */
  265. public static function designer_register_one($phone,$ver_code){
  266. $phone_use = self::where('phone',$phone)->find();
  267. if ($phone_use){
  268. return Common::return_error('手机号已注册!');
  269. }
  270. //获取最后的验证码
  271. $time = time()-90;
  272. $sms = Sms::where(['mobile' => $phone, 'event' => 'register'])
  273. ->where('createtime','>',$time)
  274. ->order('id', 'DESC')
  275. ->find();
  276. if (!$sms || $sms->code != $ver_code){
  277. return Common::return_error('短信验证码不正确!');
  278. }
  279. return Common::return_success('验证成功');
  280. }
  281. /**
  282. * 商家注册第二步
  283. */
  284. public static function designer_register_two($data){
  285. $phone_use = self::where('phone',$data['phone'])->find();
  286. if ($phone_use && $phone_use['type']==2){
  287. return Common::return_error('手机号已注册!');
  288. }
  289. $data['audit'] = 1;
  290. Db::startTrans();
  291. try {
  292. self::where('phone',$data['phone'])->update($data);
  293. Db::commit();
  294. return Common::return_success('申请成功,等待审核');
  295. }catch (Exception $e) {
  296. Db::rollback();
  297. return Common::return_error('申请失败');
  298. }
  299. }
  300. /**
  301. * 忘记密码
  302. */
  303. public static function forgotPassword($phone,$password,$ver_code){
  304. $phone_use = self::where('phone',$phone)->find();
  305. if (!$phone_use){
  306. return Common::return_error('手机号未注册!');
  307. }
  308. //获取最后的验证码
  309. $time = time()-90;
  310. $sms = Sms::where(['mobile' => $phone, 'event' => 'forgetpwd'])
  311. ->where('createtime','>',$time)
  312. ->order('id', 'DESC')
  313. ->find();
  314. if (!$sms || $sms->code != $ver_code){
  315. return Common::return_error('短信验证码不正确!');
  316. }
  317. $phone_use->password = md5($password);
  318. Db::startTrans();
  319. try {
  320. $phone_use->save();
  321. Db::commit();
  322. return Common::return_success('修改成功');
  323. }catch (Exception $e) {
  324. Db::rollback();
  325. return Common::return_error('修改失败');
  326. }
  327. }
  328. /**
  329. * 修改密码
  330. */
  331. public static function changePassword($user_id,$password,$ver_code){
  332. $phone_use = self::where('id',$user_id)->find();
  333. //获取最后的验证码
  334. $time = time()-90;
  335. $sms = Sms::where(['mobile' => $phone_use['phone'], 'event' => 'changepwd'])
  336. ->where('createtime','>',$time)
  337. ->order('id', 'DESC')
  338. ->find();
  339. if (!$sms || $sms->code != $ver_code){
  340. return Common::return_error('短信验证码不正确!');
  341. }
  342. $phone_use->password = md5($password);
  343. Db::startTrans();
  344. try {
  345. $phone_use->save();
  346. Db::commit();
  347. return Common::return_success('修改成功');
  348. }catch (Exception $e) {
  349. Db::rollback();
  350. return Common::return_error('修改失败');
  351. }
  352. }
  353. /**
  354. * 我的收货地址列表
  355. */
  356. public static function MyAddress($user_id,$Nowpage,$limits){
  357. $list = Address::where('is_del',0)
  358. ->where('user_id',$user_id)
  359. ->page($Nowpage,$limits)
  360. ->order('is_default desc,id desc')
  361. ->select();
  362. $data['count'] = count($list);
  363. $data['list'] = $list;
  364. return Common::return_success('成功',$data);
  365. }
  366. /**
  367. * 添加,修改收货地址
  368. */
  369. public static function AddEditAddress($user_id,$data,$address_id){
  370. if ($address_id){
  371. $address = Address::where('id',$address_id)->where('user_id',$user_id)->find();
  372. if (!$address) return Common::return_error('地址不存在');
  373. //编辑
  374. if ($address->save($data)){
  375. if ($data['is_default']==1){
  376. Address::where('user_id',$user_id)
  377. ->where('is_del',0)
  378. ->where('id','neq',$address_id)
  379. ->update(['is_default'=>0]);
  380. }
  381. return Common::return_success('编辑成功');
  382. }else{
  383. return Common::return_error('编辑失败');
  384. }
  385. }else{
  386. $data['user_id'] = $user_id;
  387. $addre = Address::create($data);
  388. if ($addre){
  389. $address_id = $addre->id;
  390. if ($data['is_default']==1){
  391. Address::where('user_id',$user_id)
  392. ->where('is_del',0)
  393. ->where('id','neq',$address_id)
  394. ->update(['is_default'=>0]);
  395. }
  396. return Common::return_success('添加成功');
  397. }else{
  398. return Common::return_error('添加失败');
  399. }
  400. }
  401. }
  402. /**
  403. * 设为默认地址
  404. */
  405. public static function SetAddressDefault($user_id,$address_id){
  406. $address = Address::where('id',$address_id)
  407. ->where('user_id',$user_id)
  408. ->find();
  409. if (!$address) return Common::return_error('地址不存在');
  410. if ($address->save(['is_default'=>1])){
  411. Address::where('user_id',$user_id)
  412. ->where('is_del',0)
  413. ->where('id','neq',$address_id)
  414. ->update(['is_default'=>0]);
  415. return Common::return_success('设置成功');
  416. }else{
  417. return Common::return_error('设置失败');
  418. }
  419. }
  420. /**
  421. * 删除收货地址
  422. */
  423. public static function DelAddress($user_id,$address_id){
  424. $address = Address::where('id',$address_id)->where('user_id',$user_id)->find();
  425. if (!$address) return Common::return_error('地址不存在');
  426. if ($address->save(['is_del'=>time()])){
  427. return Common::return_success('删除成功');
  428. }else{
  429. return Common::return_error('删除失败');
  430. }
  431. }
  432. /**
  433. * 编辑用户资料
  434. */
  435. public static function EditUserInfo($user_id,$data){
  436. if (self::where('id',$user_id)->update($data)){
  437. return Common::return_success('编辑成功');
  438. }else{
  439. return Common::return_error('编辑失败');
  440. }
  441. }
  442. /**
  443. * 申请成为设计师
  444. */
  445. public static function applyDesigner($user_id,$data){
  446. $data['type'] = 1;
  447. $data['audit'] = 1;
  448. Db::startTrans();
  449. try {
  450. self::where('id',$user_id)->update($data);
  451. Db::commit();
  452. return Common::return_success('提交成功');
  453. }catch (Exception $e) {
  454. Db::rollback();
  455. return Common::return_error('提交失败');
  456. }
  457. }
  458. /**
  459. * 投诉
  460. */
  461. public static function Message($user_id,$content){
  462. $data['content'] = $content;
  463. $data['user_id'] = $user_id;
  464. if (Message::create($data)) return Common::return_success('提交成功');
  465. return Common::return_error('提交失败');
  466. }
  467. /**
  468. * 时间管理
  469. */
  470. public static function timeList($user_id){
  471. $time = time();
  472. //组合数据
  473. $date = [];
  474. for ($i=1; $i<=15; $i++){
  475. $date[$i] = date('Y-m-d' ,strtotime( $i .' days', $time));
  476. }
  477. $array = [];
  478. foreach ($date as &$v){
  479. $da = UserTime::where('user_id',$user_id)->where('time',$v)->find();
  480. if ($da){
  481. $array2['switch'] = $da['switch'];
  482. $array2['price'] = $da['price'];
  483. }else{
  484. $array2['switch'] = 2;
  485. $array2['price'] = 0;
  486. }
  487. $array2['date'] = $v;
  488. array_push($array,$array2);
  489. }
  490. return Common::return_success('成功',$array);
  491. }
  492. /**
  493. * 时间设置开关
  494. */
  495. // public static function timeSwitch($user_id,$date,$switch,$price){
  496. // $info = UserTime::where('user_id',$user_id)->where('time',$date)->find();
  497. // if ($info){
  498. // if (UserTime::where('id',$info['id'])->update(['switch'=>$switch,'price'=>$price])){
  499. // return Common::return_success('成功');
  500. // }else{
  501. // return Common::return_error('失败');
  502. // }
  503. // }else{
  504. // if (UserTime::create(
  505. // [
  506. // 'user_id'=>$user_id,
  507. // 'time'=>$date,
  508. // 'switch'=>$switch,
  509. // 'price'=>$price
  510. // ]
  511. // )){
  512. // return Common::return_success('成功');
  513. // }else{
  514. // return Common::return_error('失败');
  515. // }
  516. // }
  517. // }
  518. /**
  519. * 时间设置开关
  520. */
  521. public static function timeSwitch($user_id,$json){
  522. foreach ($json as &$v){
  523. $info = UserTime::where('user_id',$user_id)->where('time',$v['date'])->find();
  524. if ($info){
  525. UserTime::where('id',$info['id'])->update(['switch'=>$v['switch'],'price'=>$v['price']]);
  526. }else{
  527. UserTime::create(
  528. [
  529. 'user_id'=>$user_id,
  530. 'time'=>$v['date'],
  531. 'switch'=>$v['switch'],
  532. 'price'=>$v['price']
  533. ]);
  534. }
  535. }
  536. return Common::return_success('成功');
  537. }
  538. /**
  539. * 余额提现
  540. */
  541. public static function userWithdraw($user_id,$money,$withdraw_type){
  542. $userinfo = self::where('id',$user_id)->find();
  543. if ($userinfo['money']<$money)
  544. return Common::return_error('余额不足');
  545. $data['user_id'] = $user_id;
  546. $order_no = Common::getNewOrderId($user_id);
  547. $data['order_no'] = $order_no;
  548. $data['withdraw_type'] = $withdraw_type;
  549. $data['price'] = $money;
  550. Db::startTrans();
  551. try {
  552. UserWithdraw::create($data);
  553. User::money($money,$user_id,$withdraw_type=='weixin' ? '微信提现' : '支付宝提现' .$money.'元');
  554. Db::commit();
  555. return Common::return_success('提交成功');
  556. }catch (Exception $e) {
  557. Db::rollback();
  558. return Common::return_error('提交失败');
  559. }
  560. }
  561. /**
  562. * 消费明细
  563. */
  564. public static function moneyRecord($user_id,$Nowpage,$limits){
  565. //提现记录
  566. $withdraw = UserWithdraw::where('user_id',$user_id)->field('withdraw_type,paid,why,create_at,price')->select();
  567. if (count($withdraw)>0){
  568. $withdraw = $withdraw->toArray();
  569. foreach ($withdraw as &$v){
  570. $v['price'] = '-'.$v['price'];
  571. $v['project_name'] = '';
  572. }
  573. }else{
  574. $withdraw = [];
  575. }
  576. //收益记录
  577. //$record = MoneyLog::where('user_id',$user_id)->where('title','订单收益')->select();
  578. $record = MoneyLog::where('user_id',$user_id)->whereIn('title','订单收益,系统增加,,系统减少,加班费收益')->select();
  579. if (count($record)>0){
  580. $record = $record->toArray();
  581. $array = [];
  582. foreach ($record as &$v){
  583. $array2['withdraw_type'] = 'sy';
  584. $array2['paid'] = 1;
  585. $array2['why'] = '';
  586. $array2['create_at'] = $v['create_at'];
  587. if ($v['pm']==1){
  588. $array2['price'] = '+'.$v['change_money'];
  589. }else{
  590. $array2['price'] = '-'.$v['change_money'];
  591. }
  592. if ($v['link_id']!=0){
  593. $project_name = Order::where('id',$v['link_id'])->value('project_name');
  594. }else{
  595. $project_name = '';
  596. }
  597. $array2['project_name'] = $project_name;
  598. array_push($array,$array2);
  599. }
  600. }else{
  601. $array = [];
  602. }
  603. $list = array_merge($withdraw,$array);
  604. $data['count'] = count($list);
  605. array_multisort(array_column($list,'create_at'),SORT_DESC,$list);
  606. $list = array_slice($list,($Nowpage-1)*$limits,$limits);
  607. $data['list'] = $list;
  608. return Common::return_success('成功',$data);
  609. }
  610. /**
  611. * 获取首页搜索日期列表
  612. */
  613. public static function searchDateList(){
  614. $time = time();
  615. //组合数据
  616. $date = [];
  617. for ($i=1; $i<=15; $i++){
  618. $date[$i-1]['date'] = date('Y-m-d' ,strtotime( $i .' days', $time));
  619. $date[$i-1]['time'] = date('m-d' ,strtotime( $i .' days', $time));
  620. }
  621. return Common::return_success('成功',$date);
  622. }
  623. /**
  624. * 首页设计师列表
  625. */
  626. public static function Designer($user_id,$Nowpage,$limits,$design_classifi,$keywords,$lat,$log,$city,$distance,$price,$evaluation,$date,$price_section_start,$price_section_end){
  627. // self::RecentlyPrice(); //更新价格
  628. $count = self::designerByWhere($user_id,$design_classifi,$keywords,$city,$date,$price_section_start,$price_section_end)->count();
  629. if ($count){
  630. $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'];
  631. if ($lat && $log){
  632. $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';
  633. }
  634. $list = self::designerByWhere($user_id,$design_classifi,$keywords,$city,$date,$price_section_start,$price_section_end)
  635. ->field($field)
  636. ->when($distance,function ($query) use ($distance,$lat,$log){
  637. if ($lat && $log){
  638. $query->order('distance '.$distance);
  639. }
  640. })
  641. ->when($price,function ($query) use ($price){
  642. $query->order('price '.$price);
  643. })
  644. ->when($evaluation,function ($query) use ($evaluation){
  645. $query->order('evaluation '.$evaluation);
  646. })
  647. ->page($Nowpage,$limits)
  648. ->select();
  649. $time = time();
  650. //组合数据
  651. $date = [];
  652. for ($i=1; $i<=15; $i++){
  653. $date[$i] = date('Y-m-d' ,strtotime( $i .' days', $time));
  654. }
  655. foreach ($list as &$v){
  656. if ($v['works']){
  657. $v['works'] = explode(',',$v['works']);
  658. }else{
  659. $v['works'] = [];
  660. }
  661. if ($v['skills_label']){
  662. $v['skills_label'] = explode(',',$v['skills_label']);
  663. }else{
  664. $v['skills_label'] = [];
  665. }
  666. if ($v['good_type']){
  667. $v['good_type'] = explode(',',$v['good_type']);
  668. }else{
  669. $v['good_type'] = [];
  670. }
  671. if ($v['good_style']){
  672. $v['good_style'] = explode(',',$v['good_style']);
  673. }else{
  674. $v['good_style'] = [];
  675. }
  676. $v['name'] = $v['nickname'];
  677. $datee = Works::where('user_id',$v['id'])
  678. ->where('is_del',1)
  679. ->group('date')
  680. ->order('id desc')
  681. ->column('date');
  682. if ($datee){
  683. $arr = [];
  684. foreach ($datee as &$a){
  685. $array22['date'] = $a;
  686. $array22['imgs'] = Works::where('user_id',$v['id'])
  687. ->where('date',$a)
  688. ->where('is_del',1)
  689. ->field('id,img')
  690. ->select();
  691. array_push($arr,$array22);
  692. }
  693. }else{
  694. $arr = [];
  695. }
  696. $v['works_list'] = $arr;
  697. $array = [];
  698. foreach ($date as &$a){
  699. $da = UserTime::where('user_id',$v['id'])->where('time',$a)->find();
  700. if ($da){
  701. $order = Order::where('designer_id',$v['id'])
  702. ->where('yy_date',$a)
  703. ->where('is_del',1)
  704. ->whereIn('status','1,2')
  705. ->count();
  706. if ($order){
  707. $array2['switch'] = 2;
  708. }else{
  709. $array2['switch'] = $da['switch'];
  710. }
  711. $array2['price'] = $da['price'];
  712. }else{
  713. $array2['switch'] = 2;
  714. $array2['price'] = 0;
  715. }
  716. $array2['date'] = $a;
  717. array_push($array,$array2);
  718. }
  719. $v['date_list'] = $array;
  720. }
  721. }else{
  722. $list = [];
  723. }
  724. return Common::return_success('成功',compact('count','list'));
  725. }
  726. /**
  727. * 首页设计师列表where条件
  728. */
  729. public static function designerByWhere($user_id,$design_classifi,$keywords,$city,$date,$price_section_start,$price_section_end){
  730. $query = self::where('status',1)
  731. ->when($design_classifi,function ($query) use ($design_classifi){
  732. $query->where('design_classifi',$design_classifi);
  733. })
  734. ->when($keywords,function ($query) use ($keywords){
  735. $query->whereLike('name|nickname|skills_label|design_classifi|good_type|good_style','%'.$keywords.'%');
  736. })
  737. ->when($city,function ($query) use ($city){
  738. //$query->where('city',$city);
  739. $query->whereRaw('city like "%'.$city.'%" or province like "%'.$city.'%"');
  740. })
  741. ->when($date,function ($query) use ($date){
  742. $ids1 = UserTime::where('time',$date)
  743. ->where('switch',1)
  744. ->column('user_id');
  745. $ids2 = Order::where('yy_date',$date)
  746. ->where('is_del',1)
  747. ->whereIn('status','1,2')
  748. ->column('designer_id');
  749. $ids = array_diff($ids1,$ids2); //对比两数组差异
  750. $query->whereIn('id',$ids);
  751. })
  752. ->when($price_section_start,function ($query) use ($price_section_start,$price_section_end){
  753. if ($price_section_start && $price_section_end){
  754. $query->where('price','>=',$price_section_start)->where('price','<=',$price_section_end);
  755. }
  756. })
  757. ->where('price','>',0)
  758. //->where('id','neq',$user_id)
  759. ->where('type',2);
  760. return $query;
  761. }
  762. /**
  763. * 获取设计师最近的价格显示
  764. */
  765. public static function RecentlyPrice(){
  766. self::where('status',1)->where('type',2)->chunk(50,function ($list){
  767. $time = time();
  768. //组合数据
  769. $date = [];
  770. for ($i=1; $i<=7; $i++){
  771. $date[$i] = date('Y-m-d' ,strtotime( $i .' days', $time));
  772. }
  773. foreach ($list as &$v){
  774. $price = UserTime::where('user_id',$v['id'])
  775. ->where('switch',1)
  776. ->order('time asc')
  777. ->whereBetween('time',[$date[1],$date[7]])
  778. ->limit(1)
  779. ->value('price');
  780. if (!$price){
  781. $price = 0;
  782. }
  783. self::where('id',$v['id'])->update(['price'=>$price]);
  784. }
  785. });
  786. }
  787. /**
  788. * 获取可使用优惠券列表
  789. */
  790. public static function getCouponList($user_id,$money){
  791. CouponUser::couponsTimeOut($user_id);
  792. $list = CouponUser::where('user_id',$user_id)
  793. ->where('use_min_price','<=',$money)
  794. ->where('end_time','>',time())
  795. ->where('status',0)
  796. ->select();
  797. if ($list){
  798. foreach ($list as &$v){
  799. $v['add_time'] = date('Y-m-d',$v['add_time']);
  800. $v['end_time'] = date('Y-m-d',$v['end_time']);
  801. }
  802. }
  803. return Common::return_success('成功',$list);
  804. }
  805. /**
  806. * 下单时间段列表
  807. */
  808. public static function periodTimeList(){
  809. $list = PeriodTime::where('is_del',1)->select();
  810. return Common::return_success('成功',$list);
  811. }
  812. /**
  813. * 下单
  814. */
  815. public static function PlaceOrder($user_id,$address_id,$designer_id,$yy_date,$project_name,$coupon_id,$pay_type,$period_id){
  816. // if ($pay_type=='weixin'){
  817. // return Common::return_error('暂时无法使用微信支付');
  818. // }
  819. $orde = Order::where('user_id',$user_id)->where('is_del',1)->where('overtime_pay_status',1)->find();
  820. if ($orde) return Common::return_error('有未支付的加班费订单,无法下单');
  821. $designer = self::where('id',$designer_id)
  822. ->where('type',2)
  823. ->where('status',1)
  824. ->find();
  825. if (!$designer) return Common::return_error('设计师不存在');
  826. $address = Address::where('id',$address_id)
  827. ->where('user_id',$user_id)
  828. ->where('is_del',0)
  829. ->find();
  830. if (!$address) return Common::return_error('地址不存在');
  831. $time = UserTime::where('user_id',$designer_id)
  832. ->where('time',$yy_date)
  833. //->where('switch',1)
  834. ->find();
  835. $order = Order::where('designer_id',$designer_id)
  836. ->where('is_del',1)
  837. ->whereIn('status','1,2')
  838. ->where('yy_date',$yy_date)
  839. ->find();
  840. $period = PeriodTime::where('id',$period_id)->find();
  841. if ($order) return Common::return_error('预约日期设计师已接单');
  842. if ($time && $time['switch']==2) return Common::return_error('预约日期设计师未开启接单');
  843. //价格
  844. $price = $designer['price'];
  845. $coupon_price = 0;
  846. $pay_price = $price;
  847. if ($coupon_id){
  848. $coupon = CouponUser::where('id',$coupon_id)
  849. ->where('user_id',$user_id)
  850. ->where('status',0)
  851. ->find();
  852. if (!$coupon) return Common::return_error('优惠券不存在');
  853. $coupon_price = $coupon['coupon_price'];
  854. $pay_price = bcsub($pay_price,$coupon_price,2);
  855. }else{
  856. $coupon_id = 0;
  857. }
  858. //订单号
  859. $order_no = Common::getNewOrderId($user_id);
  860. $data = [
  861. 'order_no'=>$order_no,
  862. 'user_id'=>$user_id,
  863. 'designer_id'=>$designer_id,
  864. 'address_id'=>$address_id,
  865. 'address_jd'=>$address['jd'],
  866. 'address_wd'=>$address['wd'],
  867. 'name'=>$address['name'],
  868. 'phone'=>$address['phone'],
  869. 'province'=>$address['province'],
  870. 'city'=>$address['city'],
  871. 'area'=>$address['area'],
  872. 'address'=>$address['address'],
  873. 'yy_date'=>$yy_date,
  874. 'servicing_time'=>$period['start'].'-'.$period['end'],
  875. 'project_name'=>$project_name,
  876. 'coupon_id'=>$coupon_id,
  877. 'pay_type'=>$pay_type,
  878. 'price'=>$price,
  879. 'coupon_price'=>$coupon_price,
  880. 'pay_price'=>$pay_price,
  881. ];
  882. Db::startTrans();
  883. try {
  884. $order = Order::create($data);
  885. $order_id = $order->id;
  886. Common::order_status($order_id,'订单生成');
  887. //优惠券确定
  888. if ($coupon_id){
  889. CouponUser::where('id',$coupon_id)->update(['status'=>1,'use_time'=>time()]);
  890. }
  891. //先模拟下单成功
  892. // $da['pay_time'] = time();
  893. // $da['status'] = 1;
  894. // Order::where('id',$order['id'])->update($da);
  895. // Db::commit();
  896. // return Common::return_success('成功');
  897. switch ($pay_type){
  898. case 'weixin':
  899. $wx = new WxPay();//实例化微信torganizationid支付控制器
  900. $body = '订单号' . $order_no;//支付说明
  901. $total_fee = $pay_price * 100;//支付金额(乘以100)
  902. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/pay/pay_order';//回调地址
  903. $out_trade_no = $order_no;//订单号
  904. $config = $wx->retrunconfig();
  905. try{
  906. $app = Factory::payment($config);
  907. $result = $app->order->unify([
  908. 'body' => $body,
  909. 'out_trade_no' => $out_trade_no,
  910. 'total_fee' => $total_fee,
  911. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  912. 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
  913. ]);
  914. $jssdk = $app->jssdk;
  915. $order1 = $jssdk->appConfig($result['prepay_id']);
  916. Order::where('order_no',$order_no)->update(['wx_order'=>json_encode($order1,true)]);
  917. Db::commit();
  918. $retrun_data['order_no'] = $order_no;
  919. $retrun_data['pay'] = $order1;
  920. return Common::return_success('成功',$retrun_data);
  921. }catch (Exception $e){
  922. Db::rollback();
  923. return Common::return_error($order['return_msg']);
  924. }
  925. break;
  926. case 'zfb':
  927. $zfb = new AliPay();//实例化支付宝支付控制器
  928. $body = '设计下单支付';//支付说明
  929. $out_trade_no = $order_no;//订单号
  930. $total_fee = $pay_price;//支付金额
  931. $notify_url = 'http://'.$_SERVER['SERVER_NAME'].'/api/pay/alipay_order';//回调地址
  932. $order = $zfb->aliPay($body, $total_fee, $out_trade_no, $notify_url);//调用支付宝支付的方法
  933. $retrun_data['order_no'] = $order_no;
  934. $retrun_data['pay'] = $order;
  935. Db::commit();
  936. return Common::return_success('成功',$retrun_data);
  937. break;
  938. }
  939. }catch (Exception $e) {
  940. Db::rollback();
  941. return Common::return_error('失败');
  942. }
  943. }
  944. /**
  945. * 用户端订单列表
  946. */
  947. public static function userOrderList($user_id,$status,$keywords,$Nowpage,$limits){
  948. $count = self::orderByWhere($status,$user_id,$keywords)->count();
  949. if ($count){
  950. $list = self::orderByWhere($status,$user_id,$keywords)
  951. ->page($Nowpage,$limits)
  952. ->order('id desc')
  953. ->select();
  954. }else{
  955. $list = [];
  956. }
  957. return Common::return_success('成功',compact('count','list'));
  958. }
  959. /**
  960. * 订单条件
  961. */
  962. public static function orderByWhere($status,$user_id,$keywords){
  963. $query = Order::where('user_id',$user_id)
  964. ->where('is_del',1)
  965. ->when($status,function ($query) use ($status){
  966. if ($status!='all'){
  967. $query->where('status',$status);
  968. }
  969. })
  970. ->when($keywords,function ($query) use ($keywords){
  971. $query->whereLike('project_name|order_no','%'.$keywords.'%');
  972. });
  973. if ($status==='0'){
  974. $query = $query->where('status',$status);
  975. }
  976. return $query;
  977. }
  978. /**
  979. * 订单支付
  980. */
  981. public static function payOrder($user_id,$order_no){
  982. $order = Order::where('order_no', $order_no)->where('user_id', $user_id)->find();
  983. if (!$order)
  984. return Common::return_error('订单不存在');
  985. $order = $order->toArray();
  986. if ($order['status'])
  987. return Common::return_error('订单已支付');
  988. $designer = self::where('id',$order['designer_id'])
  989. ->where('type',2)
  990. ->where('status',1)
  991. ->find();
  992. if (!$designer) return Common::return_error('设计师不存在');
  993. $time = UserTime::where('user_id',$order['designer_id'])
  994. ->where('time',$order['yy_date'])
  995. ->where('switch',1)
  996. ->find();
  997. $order2 = Order::where('designer_id',$order['designer_id'])
  998. ->where('is_del',1)
  999. ->whereIn('status','1,2')
  1000. ->where('yy_date',$order['yy_date'])
  1001. ->find();
  1002. if (!$time || $order2) return Common::return_error('预约日期设计师未开启接单或已接单');
  1003. if ($order['coupon_id']){
  1004. $coupon = CouponUser::where('id',$order['coupon_id'])
  1005. ->where('user_id',$user_id)
  1006. ->where('status',0)
  1007. ->find();
  1008. if (!$coupon) return Common::return_error('优惠券不存在或已使用');
  1009. }
  1010. $pay_type = $order['pay_type'];
  1011. $pay_price = $order['pay_price'];
  1012. Db::startTrans();
  1013. switch ($pay_type){
  1014. case 'weixin':
  1015. $wx = new WxPay();//实例化微信torganizationid支付控制器
  1016. $body = '订单号' . $order_no;//支付说明
  1017. $total_fee = $pay_price * 100;//支付金额(乘以100)
  1018. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/pay/pay_order';//回调地址
  1019. $out_trade_no = $order_no;//订单号
  1020. $config = $wx->retrunconfig();
  1021. try{
  1022. $app = Factory::payment($config);
  1023. $result = $app->order->unify([
  1024. 'body' => $body,
  1025. 'out_trade_no' => $out_trade_no,
  1026. 'total_fee' => $total_fee,
  1027. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  1028. 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
  1029. ]);
  1030. $jssdk = $app->jssdk;
  1031. $order1 = $jssdk->appConfig($result['prepay_id']);
  1032. Order::where('order_no',$order_no)->update(['wx_order'=>json_encode($order1,true)]);
  1033. Db::commit();
  1034. $retrun_data['order_no'] = $order_no;
  1035. $retrun_data['pay'] = $order1;
  1036. // Common::order_status($order['id'],'订单支付成功');
  1037. return Common::return_success('成功',$retrun_data);
  1038. }catch (Exception $e){
  1039. Db::rollback();
  1040. return Common::return_error($order['return_msg']);
  1041. }
  1042. break;
  1043. case 'zfb':
  1044. $zfb = new AliPay();//实例化支付宝支付控制器
  1045. $body = '设计下单支付';//支付说明
  1046. $out_trade_no = $order_no;//订单号
  1047. $total_fee = $pay_price;//支付金额
  1048. $notify_url = 'http://'.$_SERVER['SERVER_NAME'].'/api/pay/alipay_order';//回调地址
  1049. $order = $zfb->aliPay($body, $total_fee, $out_trade_no, $notify_url);//调用支付宝支付的方法
  1050. $retrun_data['order_no'] = $order_no;
  1051. $retrun_data['pay'] = $order;
  1052. Db::commit();
  1053. return Common::return_success('成功',$retrun_data);
  1054. break;
  1055. }
  1056. }
  1057. /**
  1058. * 订单支付加班费
  1059. */
  1060. public static function overtimePayOrder($user_id,$order_no,$pay_type){
  1061. // if ($pay_type=='weixin'){
  1062. // return Common::return_error('暂时无法使用微信支付');
  1063. // }
  1064. $order = Order::where('order_no', $order_no)->where('user_id', $user_id)->find();
  1065. if (!$order)
  1066. return Common::return_error('订单不存在');
  1067. $order = $order->toArray();
  1068. if (in_array($order['overtime_pay_status'],['0','2']))
  1069. return Common::return_error('没有加班费或加班费已支付');
  1070. $pay_price = $order['overtime_pay'];
  1071. Db::startTrans();
  1072. switch ($pay_type){
  1073. case 'weixin':
  1074. $wx = new WxPay();//实例化微信torganizationid支付控制器
  1075. $body = '订单号' . $order['overtime_pay_order_no'];//支付说明
  1076. $total_fee = $pay_price * 100;//支付金额(乘以100)
  1077. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/pay/overtime_pay_order';//回调地址
  1078. $out_trade_no = $order['overtime_pay_order_no'];//订单号
  1079. $config = $wx->retrunconfig();
  1080. try{
  1081. $app = Factory::payment($config);
  1082. $result = $app->order->unify([
  1083. 'body' => $body,
  1084. 'out_trade_no' => $out_trade_no,
  1085. 'total_fee' => $total_fee,
  1086. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  1087. 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型
  1088. ]);
  1089. $jssdk = $app->jssdk;
  1090. $order1 = $jssdk->appConfig($result['prepay_id']);
  1091. Order::where('order_no',$order_no)->update(['overtime_pay_type'=>$pay_type,'overtime_wx_order'=>json_encode($order1,true)]);
  1092. Db::commit();
  1093. $retrun_data['order_no'] = $out_trade_no;
  1094. $retrun_data['pay'] = $order1;
  1095. return Common::return_success('成功',$retrun_data);
  1096. }catch (Exception $e){
  1097. Db::rollback();
  1098. return Common::return_error($order['return_msg']);
  1099. }
  1100. break;
  1101. case 'zfb':
  1102. $zfb = new AliPay();//实例化支付宝支付控制器
  1103. $body = '设计下单支付';//支付说明
  1104. $out_trade_no = $order['overtime_pay_order_no'];//订单号
  1105. $total_fee = $pay_price;//支付金额
  1106. $notify_url = 'http://'.$_SERVER['SERVER_NAME'].'/api/pay/overtime_alipay_order';//回调地址
  1107. $order = $zfb->aliPay($body, $total_fee, $out_trade_no, $notify_url);//调用支付宝支付的方法
  1108. $retrun_data['order_no'] = $out_trade_no;
  1109. $retrun_data['pay'] = $order;
  1110. Order::where('order_no',$order_no)->update(['overtime_pay_type'=>$pay_type]);
  1111. Db::commit();
  1112. return Common::return_success('成功',$retrun_data);
  1113. break;
  1114. }
  1115. }
  1116. /**
  1117. * 订单详情
  1118. */
  1119. public static function orderDetail($order_no){
  1120. $order = Order::where('order_no',$order_no)->find();
  1121. if (!$order)
  1122. return Common::return_error('订单不存在');
  1123. $designer =self::where('id',$order['designer_id'])->field('id,name,headimg,evaluation,works,design_classifi')->find();
  1124. if ($designer['works']){
  1125. $designer['works'] = explode(',',$designer['works']);
  1126. }else{
  1127. $designer['works'] = [];
  1128. }
  1129. $order['designer'] =$designer;
  1130. $order['pay_time'] = date('Y-m-d H:i:s',$order['pay_time']);
  1131. return Common::return_success('成功',$order);
  1132. }
  1133. /**
  1134. * 取消订单
  1135. */
  1136. public static function cancelOrder($user_id,$order_no){
  1137. $order = Order::where('order_no',$order_no)->where('user_id',$user_id)->find();
  1138. if (!$order)
  1139. return Common::return_error('订单不存在');
  1140. if ($order['status'])
  1141. return Common::return_error('订单无法取消');
  1142. Db::startTrans();
  1143. try {
  1144. $order->status = 5;
  1145. $order->remark = '用户取消订单';
  1146. $order->save();
  1147. if ($order->coupon_id){
  1148. CouponUser::where('id',$order->coupon_id)->update(['status'=>0]);
  1149. }
  1150. Db::commit();
  1151. Common::order_status($order->id,'订单取消成功');
  1152. return Common::return_success('取消成功');
  1153. }catch (Exception $e) {
  1154. Db::rollback();
  1155. return Common::return_error('取消失败');
  1156. }
  1157. }
  1158. /**
  1159. * 删除订单
  1160. */
  1161. public static function delOrder($user_id,$order_no){
  1162. $order = Order::where('order_no',$order_no)->where('user_id',$user_id)->find();
  1163. if (!$order)
  1164. return Common::return_error('订单不存在');
  1165. Db::startTrans();
  1166. try {
  1167. $order->is_del = 0;
  1168. $order->del_time = time();
  1169. $order->save();
  1170. Db::commit();
  1171. return Common::return_success('删除成功');
  1172. }catch (Exception $e) {
  1173. Db::rollback();
  1174. return Common::return_error('删除失败');
  1175. }
  1176. }
  1177. /**
  1178. * 订单评价
  1179. */
  1180. public static function evaluationOrder($user_id,$order_no,$evaluation,$evaluation_content){
  1181. $order = Order::where('order_no',$order_no)->where('user_id',$user_id)->find();
  1182. if (!$order)
  1183. return Common::return_error('订单不存在');
  1184. if ($order['status']!=3)
  1185. return Common::return_error('订单状态错误');
  1186. Db::startTrans();
  1187. try {
  1188. $order->status = 4;
  1189. $order->evaluation = $evaluation;
  1190. $order->evaluation_time = time();
  1191. $order->evaluation_content = $evaluation_content;
  1192. $order->save();
  1193. Db::commit();
  1194. $sum = Order::where('designer_id',$order['designer_id'])->where('status',4)->sum('evaluation');
  1195. $count = Order::where('designer_id',$order['designer_id'])->where('status',4)->count();
  1196. $eva = ceil($sum/$count);
  1197. User::where('id',$order['designer_id'])->update(['evaluation'=>$eva]);
  1198. Common::order_status($order->id,'订单评价成功');
  1199. return Common::return_success('评价成功');
  1200. }catch (Exception $e) {
  1201. Db::rollback();
  1202. return Common::return_error('评价失败');
  1203. }
  1204. }
  1205. /**
  1206. * 用户端我的评价
  1207. */
  1208. public static function userEvaluationList($user_id,$Nowpage,$limits){
  1209. $where = [
  1210. 'a.user_id'=>$user_id,
  1211. 'a.status'=>4,
  1212. 'a.is_del'=>1
  1213. ];
  1214. $count = Order::where($where)
  1215. ->alias('a')
  1216. ->join('q_user b','a.user_id=b.id')
  1217. ->join('q_user c','a.designer_id=c.id')
  1218. ->count();
  1219. if ($count){
  1220. $list = Order::where($where)
  1221. ->alias('a')
  1222. ->join('q_user b','a.user_id=b.id')
  1223. ->join('q_user c','a.designer_id=c.id')
  1224. ->page($Nowpage,$limits)
  1225. ->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')
  1226. ->order('a.id desc')
  1227. ->select();
  1228. foreach ($list as &$v){
  1229. $v['evaluation_time'] = date('Y-m-d',$v['evaluation_time']);
  1230. }
  1231. }else{
  1232. $list = [];
  1233. }
  1234. return Common::return_success('成功',compact('count','list'));
  1235. }
  1236. /**
  1237. * 设计师端我的评价
  1238. */
  1239. public static function designerEvaluationList($user_id,$Nowpage,$limits){
  1240. $where = [
  1241. 'a.designer_id'=>$user_id,
  1242. 'a.status'=>4,
  1243. 'a.is_del'=>1
  1244. ];
  1245. $count = Order::where($where)
  1246. ->alias('a')
  1247. ->join('q_user b','a.user_id=b.id')
  1248. ->count();
  1249. if ($count){
  1250. $list = Order::where($where)
  1251. ->alias('a')
  1252. ->join('q_user b','a.user_id=b.id')
  1253. ->page($Nowpage,$limits)
  1254. ->field('a.id,a.order_no,a.evaluation,a.evaluation_time,a.evaluation_content,b.name,b.nickname,b.headimg')
  1255. ->order('a.id desc')
  1256. ->select();
  1257. foreach ($list as &$v){
  1258. $v['evaluation_time'] = date('Y-m-d',$v['evaluation_time']);
  1259. }
  1260. }else{
  1261. $list = [];
  1262. }
  1263. return Common::return_success('成功',compact('count','list'));
  1264. }
  1265. /**
  1266. * 设计师端添加作品
  1267. */
  1268. public static function addWorks($user_id,$imgs){
  1269. $data = [
  1270. 'user_id'=>$user_id,
  1271. 'date'=>date('Y-m-d')
  1272. ];
  1273. foreach ($imgs as &$v){
  1274. $data['img'] = $v;
  1275. Works::create($data);
  1276. }
  1277. return Common::return_success('成功');
  1278. }
  1279. /**
  1280. * 设计师端作品管理
  1281. */
  1282. public static function workList($user_id){
  1283. $date = Works::where('user_id',$user_id)
  1284. ->where('is_del',1)
  1285. ->group('date')
  1286. ->order('id desc')
  1287. ->column('date');
  1288. if ($date){
  1289. $array = [];
  1290. foreach ($date as &$v){
  1291. $array2['date'] = $v;
  1292. $array2['imgs'] = Works::where('user_id',$user_id)
  1293. ->where('date',$v)
  1294. ->where('is_del',1)
  1295. ->field('id,img')
  1296. ->select();
  1297. array_push($array,$array2);
  1298. }
  1299. }else{
  1300. $array = [];
  1301. }
  1302. return Common::return_success('成功',$array);
  1303. }
  1304. /**
  1305. * 设计师端删除作品
  1306. */
  1307. public static function delWorks($user_id,$ids){
  1308. if (Works::whereIn('id',$ids)->where('user_id',$user_id)->update(['is_del'=>0])){
  1309. return Common::return_success('删除成功');
  1310. }else{
  1311. return Common::return_error('删除失败');
  1312. }
  1313. }
  1314. /**
  1315. * 设计师端首页
  1316. */
  1317. public static function designerIndex($user_id){
  1318. $list = Order::where('designer_id',$user_id)
  1319. ->where('is_del',1)
  1320. ->whereIn('status','1,2')
  1321. ->order('id desc')
  1322. ->limit(2)
  1323. ->select();
  1324. return Common::return_success('成功',$list);
  1325. }
  1326. /**
  1327. * 用户端订单列表
  1328. */
  1329. public static function designerOrderList($user_id,$status,$keywords,$Nowpage,$limits){
  1330. $count = self::designerOrderByWhere($status,$user_id,$keywords)->count();
  1331. if ($count){
  1332. $list = self::designerOrderByWhere($status,$user_id,$keywords)
  1333. ->page($Nowpage,$limits)
  1334. ->order('id desc')
  1335. ->select();
  1336. }else{
  1337. $list = [];
  1338. }
  1339. return Common::return_success('成功',compact('count','list'));
  1340. }
  1341. /**
  1342. * 订单条件
  1343. */
  1344. public static function designerOrderByWhere($status,$user_id,$keywords){
  1345. $query = Order::where('designer_id',$user_id)
  1346. ->where('is_del',1)
  1347. ->when($status,function ($query) use ($status){
  1348. if (in_array($status,['1','2'])){
  1349. $query->where('status',$status);
  1350. }elseif ($status==3){
  1351. $query->whereIn('status','3,4');
  1352. }
  1353. })
  1354. ->when($keywords,function ($query) use ($keywords){
  1355. $query->whereLike('project_name|order_no','%'.$keywords.'%');
  1356. });
  1357. if ($status===0){
  1358. $query = $query->where('status',$status);
  1359. }
  1360. return $query;
  1361. }
  1362. /**
  1363. * 设计师开始打卡
  1364. */
  1365. public static function clockInStart($user_id,$order_no,$start_address){
  1366. $order = Order::where('order_no',$order_no)->where('designer_id',$user_id)->find();
  1367. if (!$order)
  1368. return Common::return_error('订单不存在');
  1369. if ($order['status']!=1)
  1370. return Common::return_error('订单状态错误');
  1371. Db::startTrans();
  1372. try {
  1373. $order->status = 2;
  1374. $order->start_time = date('Y-m-d H:i:s');
  1375. $order->start_address = $start_address;
  1376. $order->save();
  1377. Db::commit();
  1378. Common::order_status($order->id,'设计师开始打卡成功');
  1379. return Common::return_success('打卡成功');
  1380. }catch (Exception $e) {
  1381. Db::rollback();
  1382. return Common::return_error('打卡失败');
  1383. }
  1384. }
  1385. /**
  1386. * 设计师结束打卡
  1387. */
  1388. public static function clockInEnd($user_id,$order_no,$end_address){
  1389. $order = Order::where('order_no',$order_no)->where('designer_id',$user_id)->find();
  1390. if (!$order)
  1391. return Common::return_error('订单不存在');
  1392. if ($order['status']!=2)
  1393. return Common::return_error('订单状态错误');
  1394. Db::startTrans();
  1395. try {
  1396. $order->status = 3;
  1397. $order->end_time = date('Y-m-d H:i:s');
  1398. $order->end_address = $end_address;
  1399. //判断是否产生加班费
  1400. $servicing = explode('-',$order['servicing_time']);
  1401. $end_time = strtotime($order['yy_date'].' '.$servicing[1]);
  1402. if (time()>$end_time){
  1403. $poor_minutes = floor((time()-$end_time)%86400/60);
  1404. //获取加班费每分钟多钱
  1405. $user_overtime_pay = self::where('id',$user_id)->value('overtime_pay');
  1406. if ($user_overtime_pay>0){
  1407. $price = $user_overtime_pay;
  1408. }else{
  1409. $price = $order['price'] * 0.1; //订单金额10%的加班费
  1410. }
  1411. $overtime_pay = sprintf("%.2f", ($price/60) * $poor_minutes); //四舍五入保留两位小数
  1412. $order->overtime_pay = $overtime_pay;
  1413. $order->overtime_pay_order_no = Common::getNewOrderId($order['designer_id']);
  1414. $order->overtime_pay_status = 1;
  1415. }
  1416. $designer = self::where('id',$order['designer_id'])->field('earnings_ratio,margin')->find();
  1417. //获取设计师收益金额
  1418. $designer_money = sprintf("%.2f",$order['pay_price'] * ($designer['earnings_ratio']/100));
  1419. $order->designer_money = $designer_money;
  1420. if ($designer['margin']<2000){
  1421. $designer_margin_money = 20;
  1422. $designer_real_money = $designer_money-20;
  1423. }else{
  1424. $designer_margin_money = 0;
  1425. $designer_real_money = $designer_money;
  1426. }
  1427. $order->designer_margin_money = $designer_margin_money;
  1428. $order->designer_real_money = $designer_real_money;
  1429. $order->designer_earnings_ratio = $designer['earnings_ratio'];
  1430. $order->save();
  1431. User::money($designer_real_money,$order['designer_id'],'订单收益',1,$order['id']);
  1432. if ($designer_margin_money>0){
  1433. User::margin($designer_margin_money,$order['designer_id'],'订单抵押金',1);
  1434. }
  1435. Common::order_status($order->id,'设计师结束打卡成功');
  1436. Db::commit();
  1437. return Common::return_success('打卡成功');
  1438. }catch (Exception $e) {
  1439. Db::rollback();
  1440. return Common::return_error('打卡失败');
  1441. }
  1442. }
  1443. /**
  1444. * 设计师结束工作上传作品
  1445. */
  1446. public static function orderUploading($user_id,$order_no,$works){
  1447. $order = Order::where('order_no',$order_no)->where('designer_id',$user_id)->find();
  1448. if (!$order)
  1449. return Common::return_error('订单不存在');
  1450. // if ($order['status']!=3)
  1451. // return Common::return_error('订单状态错误');
  1452. Db::startTrans();
  1453. try {
  1454. $order->works = implode(',',$works);
  1455. $order->save();
  1456. Db::commit();
  1457. Common::order_status($order->id,'设计师上传作品成功');
  1458. return Common::return_success('上传成功');
  1459. }catch (Exception $e) {
  1460. Db::rollback();
  1461. return Common::return_error('上传失败');
  1462. }
  1463. }
  1464. /**
  1465. * 优惠券列表
  1466. */
  1467. public static function couponList($user_id,$Nowpage,$limits){
  1468. CouponUser::couponsTimeOut($user_id);
  1469. $count = CouponUser::where('user_id',$user_id)
  1470. ->where('status',0)
  1471. ->count();
  1472. if ($count){
  1473. $list = CouponUser::where('user_id',$user_id)
  1474. ->where('status',0)
  1475. ->order('id desc')
  1476. ->page($Nowpage,$limits)
  1477. ->select();
  1478. foreach ($list as &$v){
  1479. $v['add_time'] = date('Y-m-d',$v['add_time']);
  1480. $v['end_time'] = date('Y-m-d',$v['end_time']);
  1481. }
  1482. }else{
  1483. $list = [];
  1484. }
  1485. return Common::return_success('成功',compact('count','list'));
  1486. }
  1487. /**
  1488. * 公告列表
  1489. */
  1490. public static function notice(){
  1491. $list = Notice::where('status',1)
  1492. ->where('is_del',1)
  1493. ->where('is_show',1)
  1494. ->order('id desc')
  1495. ->select();
  1496. return Common::return_success('成功',$list);
  1497. }
  1498. /**
  1499. * 根据手机号获取用户信息
  1500. */
  1501. public static function getByMobile($phone){
  1502. $userinfo = self::where('phone',$phone)->find();
  1503. return $userinfo;
  1504. }
  1505. /**
  1506. * 获取用户信息
  1507. */
  1508. public static function getUserInfo($id){
  1509. $info = self::where('id',$id)->field('password',true)->find();
  1510. if (!$info['skills_label']){
  1511. $info['skills_label'] = [];
  1512. }else{
  1513. $info['skills_label'] = explode(',', $info['skills_label']);
  1514. }
  1515. if (!$info['good_type']){
  1516. $info['good_type'] = [];
  1517. }else{
  1518. $info['good_type'] = explode(',', $info['good_type']);
  1519. }
  1520. if (!$info['good_style']){
  1521. $info['good_style'] = [];
  1522. }else{
  1523. $info['good_style'] = explode(',', $info['good_style']);
  1524. }
  1525. if (!$info['related_certificate']){
  1526. $info['related_certificate'] = [];
  1527. }else{
  1528. $info['related_certificate'] = explode(',', $info['related_certificate']);
  1529. }
  1530. if (!$info['works']){
  1531. $info['works'] = [];
  1532. }else{
  1533. $info['works'] = explode(',', $info['works']);
  1534. }
  1535. return $info;
  1536. }
  1537. /**
  1538. * 变更会员余额
  1539. * @param int $money 余额
  1540. * @param int $user_id 会员ID
  1541. * @param string $memo 备注
  1542. */
  1543. public static function money($money, $user_id, $memo, $pm = 0, $link_id = 0)
  1544. {
  1545. $user = self::get($user_id);
  1546. if ($user)
  1547. {
  1548. $before = $user->money;
  1549. if ($pm==1){
  1550. $after = $user->money + $money;
  1551. }else{
  1552. $after = $user->money - $money;
  1553. }
  1554. //更新会员信息
  1555. $user->save(['money' => $after]);
  1556. //写入日志
  1557. MoneyLog::create(['user_id' => $user_id,'pm' => $pm, 'change_money' => $money, 'before' => $before, 'after' => $after, 'title' => $memo,'link_id'=>$link_id]);
  1558. }
  1559. }
  1560. /**
  1561. * 变更会员余额
  1562. * @param int $money 余额
  1563. * @param int $user_id 会员ID
  1564. * @param string $memo 备注
  1565. */
  1566. public static function margin($money, $user_id, $memo, $pm = 0)
  1567. {
  1568. $user = self::get($user_id);
  1569. if ($user)
  1570. {
  1571. $before = $user->margin;
  1572. if ($pm==1){
  1573. $after = $user->margin + $money;
  1574. }else{
  1575. $after = $user->margin - $money;
  1576. }
  1577. //更新会员信息
  1578. $user->save(['margin' => $after]);
  1579. //写入日志
  1580. MarginLog::create(['user_id' => $user_id,'pm' => $pm, 'change_money' => $money, 'before' => $before, 'after' => $after, 'title' => $memo]);
  1581. }
  1582. }
  1583. /**
  1584. * 十分钟未支付,取消订单
  1585. */
  1586. public static function systemCancelOrder(){
  1587. Order::where('status',0)->chunk(20,function ($list){
  1588. foreach ($list as &$v) {
  1589. $seven = strtotime($v['create_at'])+(10*60);
  1590. if ($seven<time()){
  1591. Order::where('id',$v['id'])->update(
  1592. [
  1593. 'status'=>5,
  1594. 'cancel_reason'=>'订单超时自动取消'
  1595. ]
  1596. );
  1597. }
  1598. }
  1599. },'id','asc');
  1600. }
  1601. }