User.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:23
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use app\BaseController;
  14. use app\model\api\ContractComment as ContractCommentModel;
  15. use app\model\api\ContractRecord as UserContractRecordModel;
  16. use app\model\api\User as UserModel;
  17. use app\model\api\UserClock as UserClockModel;
  18. use app\model\api\UserDetail as UserDetailModel;
  19. use app\model\api\ServiceType as ServiceTypeModel;
  20. use app\model\api\InfoAudit;
  21. use app\model\api\TypeAudit;
  22. use app\model\api\UserAudit;
  23. use app\model\api\ServiceType;
  24. use app\model\api\ServiceLabel as ServiceLabelModel;
  25. use app\model\api\ServiceTimeType;
  26. use app\model\api\City as CityModel;
  27. use app\model\api\Sys as SysModel;
  28. use app\model\api\ShowTemplate;
  29. use app\model\api\ShowTemplateOrder;
  30. use app\model\api\UserShowTemplate;
  31. use app\model\api\UserWorkType;
  32. use app\model\api\PayTrade as PayTradeModel;
  33. use app\api\controller\pub;
  34. use app\Request;
  35. use library\services\UtilService;
  36. use library\utils\QRcodeComm;
  37. use library\utils\Qiniu;
  38. use library\utils\IdentityCard;
  39. use library\utils\WxpayV2 as wxpayApi;
  40. use think\db\exception\DbException;
  41. use library\lib\weixina;
  42. use think\facade\Db;
  43. use think\Image;
  44. use think\Route;
  45. use think\Collection;
  46. class User extends BaseController
  47. {
  48. /**
  49. * 获取用户详情
  50. * @param Request $request
  51. */
  52. public function userInfo(Request $request){
  53. //资料审核信息
  54. $infoAuditData = (new InfoAudit)->where("uid",$request->user["uid"])->order("id desc")->find();
  55. $userAuditData = (new UserAudit)->where("uid",$request->user["uid"])->order("id desc")->find();
  56. $typeAuditData = (new TypeAudit)->where("uid",$request->user["uid"])->order("id desc")->find();
  57. $data = [];
  58. $data['uid'] = $request->user['uid'];
  59. $data['nickname'] = $request->user['nickname'];
  60. $data['mobile'] = $request->user['mobile'];
  61. $data['avatar'] = $request->user['avatar'];
  62. $data['name'] = $request->user['name'];
  63. $data['sex'] = $request->user['sex'];
  64. $data['type'] = $request->user['type'];//用户类型
  65. $data['type_name'] = "";//用户类型名称
  66. $data['work_type_id'] = $request->user['work_type_id'];//用户职称类型
  67. $data['work_type_title'] = $request->user['work_type_title'];//用户职称类型名称
  68. $data['card_look_count'] = $request->user['card_look_count'];//用户名片浏览次数
  69. $data['info_audit_status'] = empty($infoAuditData)?-2:$infoAuditData["status"];
  70. $data['user_audit_status'] = empty($userAuditData)?-2:$userAuditData["status"];
  71. $data['type_audit_status'] = empty($typeAuditData)?-2:$typeAuditData["status"];
  72. //资料通过需要重新提交认证
  73. if(!empty($infoAuditData) && !empty($typeAuditData)){
  74. if($infoAuditData["status"]==1 && $typeAuditData["status"]==1 && $infoAuditData["admin_time"]>$typeAuditData["admin_time"]){
  75. $data['type_audit_status'] = -3;
  76. }
  77. }
  78. $data['show_template_code'] = "";
  79. $data['show_template_id'] = "";
  80. $tmpData = (new UserShowTemplate)
  81. ->field("t.id,t.code,t.title,t.is_init,b.is_default")
  82. ->alias("b")
  83. ->join("show_template t","t.id = b.show_template_id","left")
  84. ->where("b.uid",$request->user['uid'])
  85. ->order("b.is_default","desc")
  86. ->order("b.id","desc")
  87. ->find();
  88. if(!empty($tmpData)){
  89. $data['show_template_code'] = $tmpData["code"];
  90. $data['show_template_id'] = $tmpData["id"];
  91. $data['show_template_init'] = $tmpData["is_init"];
  92. $data['show_template_default'] = $tmpData["is_default"];
  93. }
  94. return app('json')->success($data);
  95. }
  96. /**
  97. * 获取用户子级列表
  98. * @param Request $request
  99. */
  100. public function getChildList(Request $request){
  101. $post = UtilService::getMore([
  102. ['page', 1],
  103. ['pageSize', 50],
  104. ], $request);
  105. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  106. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  107. $post["parent_uid"]= $request->user["uid"];
  108. $data = (new UserModel)->getDataList($post,"u.uid,u.mobile,u.nickname,u.avatar");
  109. $list = $data["list"];
  110. foreach ($list as &$item) {
  111. $userInfo = $this->getUserInfo($item["uid"]);
  112. $item["is_type_audit"] = $userInfo["is_type_audit"];
  113. $item["ancestral_place"] = $userInfo["ancestral_place"];
  114. $item["auth_info"] = $userInfo["auth_info"];
  115. $item["user_work_type_id"] = $userInfo["user_work_type_id"];
  116. }
  117. $data["list"] = $list;
  118. return app('json')->success($data);
  119. }
  120. /**
  121. * 获取用户总邀请人数排行榜
  122. * @param Request $request
  123. */
  124. public function getInviterRanking(Request $request)
  125. {
  126. $time = $request->param('time');
  127. $times = $request->param('times');
  128. $users = (new UserModel())
  129. ->where('parent_uid', '<>', 0)
  130. ->where('regtime', '>=', $time)
  131. ->where('regtime', '<=', $times)
  132. ->field('parent_uid as uid, count(*) as invite_count')
  133. ->group('parent_uid')
  134. ->order('invite_count', 'desc')
  135. ->select();
  136. $rankList = [];
  137. foreach ($users as $key => $value) {
  138. $userInfo = (new UserModel)->where('uid', $value['uid'])->find();
  139. if (!$userInfo['nickname']) {
  140. continue;
  141. }
  142. $auth = (new InfoAudit)->where('uid', $value['uid'])->find();
  143. $user_work_type_title = (new UserWorkType)->where('id', $auth['user_work_type_id'])->find();
  144. if ($auth) {
  145. $auth_info = $auth->toArray();
  146. } else {
  147. $auth_info = null;
  148. }
  149. $rankList[] = [
  150. 'rank' => $key + 1,
  151. 'uid' => $value['uid'],
  152. 'nickname' => $userInfo['nickname'],
  153. 'avatar' => $userInfo['avatar'],
  154. 'invite_count' => $value['invite_count'],
  155. 'is_type_audit' => $auth && $auth['status'] == 1 ? 1 : 0,
  156. 'ancestral_place' => $auth ? $auth['ancestral_place'] : '',
  157. 'auth_info' => $auth_info,
  158. 'user_work_type_id' => $user_work_type_title['title'],
  159. ];
  160. }
  161. $inviteCount = array_column($rankList, 'invite_count');
  162. $uid = array_column($rankList, 'uid');
  163. array_multisort($inviteCount, SORT_DESC, $uid, SORT_ASC, $rankList);
  164. $data['list'] = $rankList;
  165. return app('json')->success($data);
  166. }
  167. /**
  168. * 设置用户信息
  169. * @param Request $request
  170. */
  171. public function setUserInfo(Request $request){
  172. $post = UtilService::getMore([
  173. ['avatar',''],
  174. ['nickname',''],
  175. ['name',''],
  176. // ['mobile',''],
  177. ['sex', ''],
  178. ],$request);
  179. $save=[];
  180. if(!empty($post["avatar"])){
  181. $save["avatar"] = $post["avatar"];
  182. }
  183. if(!empty($post["nickname"])){
  184. $save["nickname"] = $post["nickname"];
  185. }
  186. if(!empty($post["name"])){
  187. $save["name"] = $post["name"];
  188. }
  189. // if(!empty($post["mobile"])){
  190. // if(is_mobile($post["mobile"])){
  191. // return app("json")->fail("请输入正确的手机号码");
  192. // }
  193. // $save["mobile"] = $post["mobile"];
  194. // }
  195. if(in_array((string)$post["sex"],["0","1","2"])){
  196. $save["sex"] = (int)$post["sex"];
  197. }
  198. if(empty($save)){
  199. return app("json")->fail("提交数据为空");
  200. }
  201. //是否需要审核
  202. $isInfoAudit = 0;
  203. if($isInfoAudit==0){
  204. $r = (new UserModel)->where("uid",$request->user["uid"])->update($save);
  205. if(!$r){
  206. return app("json")->fail("提交数据为空");
  207. }
  208. }else{
  209. $data = (new UserAudit)->where("uid",$request->user["uid"])->order("id desc")->find();
  210. if(!empty($data) && $data["status"]==0){
  211. return app('json')->fail("资料审核中,请勿重复提交");
  212. }
  213. $save["uid"] = $request->user["uid"];
  214. $save["status"] = 0;
  215. $r = (new UserAudit)->insert($save);
  216. if(!$r){
  217. return app("json")->fail("提交数据为空");
  218. }
  219. }
  220. return app("json")->success("提交成功");
  221. }
  222. /**
  223. * 绑定手机号
  224. * @param Request $request
  225. */
  226. public function bindMobile(Request $request){
  227. [$code] = UtilService::getMore([
  228. ['code', '', 'empty', 'code参数错误'],
  229. ], $request, true);
  230. if(!empty($request->user['mobile'])){
  231. return app('json')->fail("当前账户已绑定过手机号码");
  232. }
  233. $weixinA = new weixina();
  234. $data=$weixinA->getPhoneNumber($code);//新版
  235. if(empty($data) || empty($data['purePhoneNumber'])) {
  236. return app('json')->fail("绑定失败,请重新绑定01".$weixinA->error);
  237. }
  238. $save=[
  239. // "countryCode"=>empty($data['countryCode'])?"":$data['countryCode'],
  240. "mobile"=>$data['purePhoneNumber'],
  241. ];
  242. // if($request->user["nickname"]=="微信用户"){
  243. // $save["nickname"] = $data['purePhoneNumber'];
  244. // }
  245. $r = (new UserModel)->where('uid', $request->user['uid'])->update($save);
  246. if(!$r){
  247. return app('json')->fail("绑定失败,请重新绑定02");
  248. }
  249. $result = [];
  250. $result['nickname'] = $request->user['nickname'];
  251. $result['avatar'] = $request->user['avatar'];
  252. return app('json')->success($result);
  253. }
  254. /**
  255. * 获取名片信息
  256. * @param Request $request
  257. */
  258. public function getUserCardInfo(Request $request){
  259. $data = (new InfoAudit)->getItem(["uid"=>$request->user["uid"],"status"=>1]);
  260. if(empty($data)){
  261. return app('json')->fail("您还没有通过名片信息审核");
  262. }
  263. $typeData = (new TypeAudit)->where("uid",$request->user["uid"])->order("id","desc")->find();
  264. $data["is_type_audit"] = (empty($typeData) || $typeData["status"]!=1)?0:1;
  265. return app('json')->success($data);
  266. }
  267. /**
  268. * 提交资料审核
  269. * @param Request $request
  270. */
  271. public function subInfoAudit(Request $request){
  272. $data = (new InfoAudit)->where("uid",$request->user["uid"])->order("id desc")->find();
  273. if(!empty($data) && $data["status"]==0){
  274. return app('json')->fail("资料审核中,请勿重复提交");
  275. }
  276. $post = UtilService::getMore([
  277. ['avatar',''],
  278. ['name','','empty','请输入真实姓名'],
  279. ['mobile','','is_moblie','请输入正确的手机号码'],
  280. ['idcard','','empty','请输入正确的身份证号码'],
  281. ['birthday',''],
  282. ['service_type',''],
  283. ['service_price',''],
  284. ['service_min_price',''],
  285. ['service_max_price',''],
  286. ['service_area',[]],
  287. ['service_project',[]],
  288. ['service_intro_content',''],
  289. ['service_intro_imgs',[]],
  290. ['service_audit_imgs',[]],//此处为我的证书
  291. ['service_imgs',[]],//我的服务照片
  292. ['service_count',0],
  293. ['work_year',''],
  294. ['ancestral_place',''],
  295. ['education',''],
  296. ['minority',''],
  297. ['is_china',1],
  298. ['user_work_type_id','0'],//服务职称
  299. ['service_label',[]],//服务标签
  300. ],$request);
  301. if(!IdentityCard::isValid($post["idcard"])){
  302. return app('json')->fail("请输入正确的身份证号码");
  303. }
  304. $save=[];
  305. $save["avatar"] = empty($post["avatar"]) ? "" : $post["avatar"];
  306. $save["name"] = empty($post["name"]) ? "" : $post["name"];
  307. $save["mobile"] = empty($post["mobile"]) ? "" : $post["mobile"];
  308. $save["idcard"] = empty($post["idcard"]) ? "" : $post["idcard"];
  309. $save["service_count"] = $post['service_count'];
  310. // $save["birthday"] = empty($post["birthday"])? 0 : strtotime($post["birthday"]);
  311. $save["constellation"] = IdentityCard::get_starsign($post["idcard"]);//星座
  312. $save["twelve"] = IdentityCard::get_zodiac($post["idcard"]);//星座
  313. $save["birthday"] = strtotime(IdentityCard::get_birthday($post["idcard"]));
  314. //服务类型
  315. if(!empty($post["service_type"])){
  316. $timeTypeData = (new ServiceTimeType)->where("code",$post["service_type"])->where("status",1)->select();
  317. if(empty($timeTypeData)){
  318. return app('json')->fail("服务时长类型不存在");
  319. }
  320. $save["service_type"] = $post["service_type"];
  321. }
  322. //服务价格区间
  323. // $save["service_price"] = empty($post["service_price"]) ? "" : $post["service_price"];
  324. $save["service_min_price"] = empty($post["service_min_price"]) ? 0 : $post["service_min_price"];
  325. $save["service_max_price"] = empty($post["service_max_price"]) ? 0 : $post["service_max_price"];
  326. //服务区域
  327. $cityModel = new CityModel();
  328. if(!empty($post["service_area"]) && is_array($post["service_area"])){
  329. $saveAre=[];
  330. foreach($post["service_area"] as $v){
  331. $stc = str_replace(['省','市','区','县'],['','','',''],$v);
  332. $str = str_replace(['辖'],['市辖'],$stc);
  333. $arr = explode(",",$str);
  334. $city_id = $cityModel->where('merger_name','like',"%".$arr[1].",".$arr[2])->value('id');
  335. if(!$city_id) $city_id = $cityModel->where('merger_name','like',"%".$arr[0].",".$arr[1])->value('id');
  336. $saveAre[] = $city_id;
  337. }
  338. if(!empty($saveAre)){
  339. $save["service_area"] = implode(",", $saveAre);
  340. }
  341. }
  342. // //服务区域
  343. // $cityModel = new CityModel();
  344. // if(!empty($post["service_area"]) && is_array($post["service_area"])){
  345. // $saveAre=[];
  346. // foreach($post["service_area"] as $v){
  347. // @file_put_contents('dub.txt','1');
  348. // $arr = explode(",",$v);
  349. // $str1 = $arr[0].','.$arr[1];
  350. // @file_put_contents('dub.txt','2');
  351. // $stc = str_replace(['省','市','区','县'],['','','',''],$str1);
  352. // $str2 = $stc.",".$arr[2]!='市辖区'?str_replace(['省','市','区','县'],['','','',''],$arr[2]):'市辖区';
  353. // $arr3 = explode(",",$str2);
  354. // $city_id = $cityModel->where('merger_name','like',"%".$arr3[1].",".$arr3[2])->value('id');
  355. // @file_put_contents('dub.txt','3');
  356. // if(!$city_id)
  357. // {
  358. // $city_id = $cityModel->where('merger_name','like',"%".$arr3[0].",".$arr3[1])->value('id');
  359. // $city_id1 = $cityModel->where('merger_name','like',"%".$arr3[0].",".$arr3[1])->value('city_id');
  360. // $city_id2 = $cityModel->where('parent_id',$city_id1)->where('name','市辖区')->value('id');
  361. // if($city_id2>0) $city_id= $city_id2;
  362. // }
  363. // @file_put_contents('dub.txt','4');
  364. // $saveAre[] = $city_id;
  365. //
  366. // }
  367. // if(!empty($saveAre)){
  368. // $save["service_area"] = implode(",", $saveAre);
  369. // }
  370. // }
  371. //服务内容
  372. $serviceTypeModel = new ServiceTypeModel();
  373. if(!empty($post["service_project"]) && is_array($post["service_project"])){
  374. $saveCon=[];
  375. foreach($post["service_project"] as $k=>$v){
  376. if(empty(trim($v))){
  377. continue;
  378. }
  379. $itemData = $serviceTypeModel->where("id",(int)$v)->where("status",1)->find();
  380. if(!empty($itemData)){
  381. $saveCon[]=$itemData["id"];
  382. }
  383. }
  384. if(!empty($saveCon)){
  385. $save["service_project"] = implode(",", $saveCon);
  386. }
  387. }
  388. //服务标签
  389. $save["service_label"]="";
  390. $labelData = (new ServiceLabelModel)->getColumnList("id","id",$post["service_label"]);
  391. if(!empty($labelData)){
  392. $save["service_label"] = implode(",", $labelData);
  393. }
  394. $save["service_intro_content"] = empty($post["service_intro_content"])? "" : $post["service_intro_content"];
  395. $save["service_intro_imgs"] = empty($post["service_intro_imgs"]) ? "" : implode(",", $post["service_intro_imgs"]);
  396. $save["service_audit_imgs"] = empty($post["service_audit_imgs"]) ? "" : implode(",", $post["service_audit_imgs"]);
  397. $save["service_imgs"] = empty($post["service_imgs"]) ? "" : implode(",", $post["service_imgs"]);//我的服务展示
  398. $save['work_year'] = empty($post["work_year"]) ? "": $post["work_year"];
  399. $save['ancestral_place'] = empty($post["ancestral_place"])? "": $post["ancestral_place"];
  400. $save['education'] = empty($post["education"]) ? "": $post["education"];
  401. $save['minority'] = empty($post["minority"]) ? "": $post["minority"];
  402. $save["status"] = 1;
  403. $save["time"] = time();
  404. $save["uid"] = $request->user["uid"];
  405. $save['is_china'] = $post["is_china"];
  406. //服务职称
  407. $save["user_work_type_id"] = empty($post["user_work_type_id"]) ? 0: (int)$post["user_work_type_id"];
  408. if($save["user_work_type_id"]>0 && (new UserWorkType)->where("id",$post["user_work_type_id"])->where("status",1)->count()<=0){
  409. return app('json')->fail("当前类型不能选择");
  410. }
  411. //生成编码
  412. $r = (new InfoAudit)->insertGetId($save);
  413. if (!$r) {
  414. return app('json')->fail("系统错误");
  415. }
  416. $exist = (new InfoAudit)->where('uid', $save['uid'])->find();
  417. if ($exist && !empty($exist['no'])) {
  418. $no = $exist['no'];
  419. } else {
  420. $id = str_pad($r, 5, "0", STR_PAD_LEFT);
  421. $no = "MYJ" . date("ym") . $id;
  422. }
  423. (new InfoAudit)->where("id", $r)->update(["no" => $no]);
  424. (new InfoAudit)->where("id", $r)->update(["status" => 1]);
  425. return app('json')->success("提交成功,数据已通过审核");
  426. }
  427. /**
  428. * 平台认证
  429. * @param Request $request
  430. */
  431. public function subTypeAudit(Request $request){
  432. $data = (new TypeAudit)->where("uid",$request->user["uid"])->order("id desc")->find();
  433. if(!empty($data) && $data["status"]==0){
  434. return app('json')->fail("平台审核中,请勿重复提交");
  435. }
  436. $post = UtilService::getMore([
  437. ['service_audit_imgs',[],"empty","请上传服务认证截图"],
  438. ['store_name','','',''],
  439. ],$request);
  440. $save=[];
  441. $save["service_audit_imgs"] = is_array($post["service_audit_imgs"]) ? implode(",", $post["service_audit_imgs"]) : "";
  442. $save["store_name"] = empty($post["store_name"]) ? "" : $post["store_name"];
  443. $save["status"] = 0;
  444. $save["time"] = time();
  445. $save["uid"] = $request->user["uid"];
  446. if(empty($save["service_audit_imgs"])){
  447. return app('json')->fail("请上传服务认证截图");
  448. }
  449. $r = (new TypeAudit)->insertGetId($save);
  450. if(!$r){
  451. return app('json')->fail("系统错误");
  452. }
  453. $no = "CERT-".date("YmdHis")."-".$r;
  454. (new TypeAudit)->where("id",$r)->update(["no"=>$no]);
  455. return app('json')->success("提交成功,请耐心等待审核");
  456. }
  457. /**
  458. * 模板订单提交[第一步]
  459. * @param Request $request
  460. */
  461. public function subShowTemplateOrder(Request $request){
  462. [$id,$pay_type] = UtilService::getMore([
  463. ['id','','empty','参数错误'],
  464. ['pay_type','','empty','请选择支付方式'],
  465. ],$request,true);
  466. if(!in_array($pay_type,["wxpay","balance"])){
  467. return app('json')->fail('不支持该支付方式!');
  468. }
  469. $weixinConfig = (new SysModel)->getWeixinConfig();
  470. // if(empty($weixinConfig)){
  471. // return app('json')->fail('支付配置为空!');
  472. // }
  473. //缓存
  474. $redis = \think\facade\Cache::store('redis');
  475. $key = 'show_template_order_sub_' . $request->user['uid'];
  476. $bool = $redis->handler()->exists($key);
  477. if ($bool) {
  478. return app('json')->fail('请务重复操作,请稍等在重试!');
  479. }
  480. $redis->set($key,1,5);//5秒缓存
  481. $proData = (new ShowTemplate)->where("id",$id)->where("status",1)->find();
  482. if(empty($proData)){
  483. return app('json')->fail("当前模板已下架");
  484. }
  485. $proData = $proData->toArray();
  486. $count = (new UserShowTemplate)->where("uid",$request->user['uid'])->where("show_template_id",$id)->count();
  487. if($count>0){
  488. return app('json')->fail("您已购买过当前模板");
  489. }
  490. //验证
  491. $orderCount = (new ShowTemplateOrder)->where("uid",$request->user['uid'])->where("show_template_id",$id)->where("status",1)->count();
  492. if($orderCount>0){
  493. return app('json')->fail("您已下单该模板,请勿重复下单");
  494. }
  495. $price = floatval($proData["price"]);
  496. // if($price<0.01){
  497. // return app('json')->fail("模板信息错误");
  498. // }
  499. $allMoney = $price;//订单商品费用
  500. $total_money = $allMoney;//订单总费用
  501. $payMoney = $total_money;//订单需支付费用
  502. $payMoney = $payMoney <= 0 ? 0 : $payMoney;
  503. $nowTime = time();
  504. //余额支付验证
  505. if($pay_type=="balance" && $payMoney > $request->user["money"]){
  506. return app('json')->fail("当前余额不足");
  507. }
  508. //主订单数据
  509. $save=[];
  510. $save['uid'] = $request->user['uid'];
  511. $save['order_id'] = makeOrderId($request->user['uid'],"ST");
  512. $save['pay_type'] = $pay_type;//
  513. $save['total_money'] = $total_money;//订单总金额
  514. $save['all_money'] = $allMoney;//商品中金额
  515. $save['pay_money'] = $payMoney;//实际支付金额
  516. $save['balance'] = 0;//余额支付了多少(目前不用)
  517. $save['status'] = $payMoney <= 0 ? 1 : 0;
  518. $save['pay_time'] = $payMoney <= 0 ? time() : 0;
  519. $save['time'] = $nowTime;
  520. $save['show_template_id'] = $proData["id"];
  521. $save['show_template_title'] = $proData["title"];;
  522. $save['show_template_price'] = $proData["price"];
  523. $save['show_template_img'] = empty($proData["imgs"]) ? "" : explode(",", $proData["imgs"])[0];
  524. $save['show_template_code'] = $proData["code"];
  525. //余额实时支付
  526. if($pay_type=="balance"){
  527. $save['status'] = 1;
  528. $save['pay_time'] = time();
  529. }
  530. try{
  531. Db::startTrans();
  532. $o_id = (new ShowTemplateOrder)->insertGetId($save);
  533. if(empty($o_id)){
  534. return app('json')->fail("订单提交失败");
  535. }
  536. //不需要支付
  537. if($payMoney<=0 && $save['status']==1){
  538. //验证并绑定当前模板
  539. (new UserShowTemplate)->userBindTemp($save["show_template_id"],$save['uid']);
  540. Db::commit();
  541. return app("json")->success(["order_id"=>$save['order_id'],"money"=>$payMoney,"status"=>$save['status']]);
  542. }
  543. //余额支付
  544. if($pay_type=="balance"){
  545. //验证并绑定当前模板
  546. (new UserShowTemplate)->userBindTemp($save["show_template_id"],$save['uid']);
  547. //修改用户余额
  548. $res = (new UserDetailModel)->balancePay($request->user["uid"],$payMoney,"show_temp_pay",["to_id"=>$o_id]);
  549. if(!$res){
  550. Db::rollback();
  551. return app('json')->fail("余额支付失败");
  552. }
  553. Db::commit();
  554. return app("json")->success(["order_id"=>$save['order_id'],"money"=>$payMoney,"status"=>$save['status']]);
  555. }
  556. //微信支付
  557. if(empty($request->user['openid'])){
  558. Db::rollback();
  559. return app('json')->fail('用户还未绑定微信!');
  560. }
  561. //清理之前支付凭证 || 防止重复购买
  562. $payTrade = (new PayTradeModel)
  563. ->where("uid",$request->user["uid"])
  564. ->where("o_id",$o_id)
  565. ->where("type","temp")
  566. ->where("status",0)
  567. ->where("time","<",time() - 7*24*60*60)
  568. ->select()
  569. ->toArray();
  570. $wxpay = new wxpayApi();
  571. // $wxpay = new wxpayApi($weixinConfig);
  572. foreach ($payTrade as $v) {
  573. // if($v['pay_type'] == 'wxpay') {
  574. // $result = $wxpay->closeOrder($v['pay_no']);
  575. // }
  576. (new PayTradeModel)->where("id",$v['id'])->where("status",0)->delete();
  577. }
  578. $mtime = microtime(true)*10000;
  579. $payOn = "T".date("Ymd").$mtime .rand(100,999) . $request->user['uid'];
  580. $out_trade_no="";
  581. $payType = $save["pay_type"];
  582. //添加交易记录
  583. $trade = [
  584. 'uid' => $request->user['uid'],
  585. 'o_id' => $o_id,
  586. 'order_id' =>$save["order_id"],
  587. 'pay_no' => $payOn,
  588. 'out_trade_no' => empty($out_trade_no) ? $payOn : $out_trade_no,
  589. 'pay_type' => $payType,
  590. 'money' => $payMoney,
  591. 'type' => 'temp',
  592. 'd_json' => serialize(['orderId'=>$save["order_id"],"give_score"=>0]),
  593. 'time' => time(),
  594. 'status' => 0,
  595. ];
  596. $r=(new PayTradeModel)->insert($trade);
  597. if(!$r){
  598. Db::rollback();
  599. return app('json')->fail('支付信息获取失败!');
  600. }
  601. $clictip = get_client_ip();
  602. if(empty($clictip)){
  603. $clictip = $request->ip();
  604. }
  605. $payData = $wxpay->wxmpPay([
  606. 'body' => "微信小程序购买模板",
  607. 'out_trade_no' => $payOn,
  608. 'total' => $payMoney,
  609. 'openid' => $request->user['openid'],
  610. 'payer_client_ip' => $clictip,
  611. ]);
  612. if(empty($payData)){
  613. Db::rollback();
  614. return app('json')->fail($wxpay->errorMsg);
  615. }
  616. Db::commit();
  617. $redis->delete($key);
  618. return app('json')->success([
  619. 'jsApiParameters'=> $payData,
  620. 'pay_no' => $payOn,
  621. 'order_id' => $save["order_id"],
  622. "status" => $save['status'],
  623. "money" => $payMoney,
  624. ]);
  625. } catch (DbException $db){
  626. Db::rollback();
  627. return app('json')->fail("订单生成失败");
  628. }
  629. }
  630. /**
  631. * 获取模板购买订单详情
  632. * @param Request $request
  633. */
  634. public function payShowTemplateOrderInfo(Request $request){
  635. [$order_id] = UtilService::getMore([
  636. ['order_id','','empty','参数错误'],
  637. ],$request,true);
  638. $data = (new ShowTemplateOrder)
  639. ->field("order_id,total_money,all_money,pay_money,status,time")
  640. ->where("order_id",$order_id)
  641. ->where("uid",$request->user["uid"])
  642. ->find();
  643. if(empty($data)){
  644. return app('json')->fail('信息不存在!');
  645. }
  646. $data=$data->toArray();
  647. if($data["status"]==0 && $data["time"]<time() - 30*60){
  648. (new ShowTemplateOrder)->where("id",$data["id"])->where("uid",$request->user["uid"])->update(["status"=>-1]);
  649. $data["status"]==-1;
  650. }
  651. $data["time"] = date("Y-m-d H:i:s",$data["time"]);
  652. return app('json')->success($data);
  653. }
  654. /**
  655. * 用户模板列表
  656. * @param Request $request
  657. */
  658. public function userShowTemplateList(Request $request){
  659. $post = UtilService::getMore([
  660. ['page', 1],
  661. ['pageSize', 50],
  662. ], $request);
  663. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  664. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  665. $where=[];
  666. $where[]=["ut.uid","=",$request->user["uid"]];
  667. $totalCount = (new UserShowTemplate)->alias("ut")->where($where)->count();
  668. $data=null;
  669. if($totalCount>0){
  670. $data = (new UserShowTemplate)
  671. ->field("ut.id,t.id as show_template_id,t.title,t.price,t.old_price,t.status,t.imgs,t.is_hot,t.is_recommend,t.code,look_count,t.real_sales,t.unreal_sales,ut.is_default")
  672. ->alias("ut")
  673. ->join("show_template t", "t.id = ut.show_template_id","left")
  674. ->where($where)
  675. ->order("ut.is_default", "desc")
  676. ->order("ut.id", "desc")
  677. ->page($post["page"], $post["pageSize"])
  678. ->select();
  679. foreach($data as $k=>$v){
  680. $data[$k]["is_use"] = 1;//是否已经购买或者可以使用
  681. $data[$k]["imgs"] = getImageAr($v["imgs"]);
  682. $data[$k]["img"] = empty($data[$k]["imgs"]) ? "" : $data[$k]["imgs"][0];
  683. $data[$k]["sales_count"] = $data[$k]["real_sales"] + $data[$k]["unreal_sales"];
  684. unset($data[$k]["real_sales"]);
  685. unset($data[$k]["unreal_sales"]);
  686. }
  687. }
  688. $data = empty($data)?[]:$data;
  689. return app('json')->success(["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount]);
  690. }
  691. /**
  692. * 设置默认模板
  693. * @param Request $request
  694. */
  695. public function setShowTemplate(Request $request) {
  696. [$show_template_id] = UtilService::getMore([
  697. ['show_template_id', '', 'empty', '参数错误'],
  698. ], $request, true);
  699. (new UserShowTemplate)->where("uid", $request->user["uid"])->where("is_default", 1)->update(["is_default" => 0]);
  700. (new UserShowTemplate)->where("uid", $request->user["uid"])->where("show_template_id", $show_template_id)->update(["is_default" => 1]);
  701. return app('json')->success("设置成功");
  702. }
  703. /**
  704. * 获取邀请海报小程序码
  705. * @param Request $request
  706. */
  707. public function getWxmpInviteQrcode(Request $request){
  708. $inviteData = (new SysModel)->getDataInfo("invite");
  709. $bgimg = $inviteData["img"];
  710. if(!empty($request->user["invite_img"])){
  711. return app('json')->success(["qrcode"=>$request->user["invite_img"],"bgimg"=>$bgimg]);
  712. }
  713. $weixinA = new weixina();
  714. // $data = $weixinA->getUnlimitedQRCode("pages/index/index","scene=".$request->user["uid"]);
  715. $data = $weixinA->getUnlimitedQRCode("pages/index/index",$request->user["uid"]);
  716. // var_dump($data);
  717. if(empty($data)){
  718. return app('json')->fail("小程序码获取失败".$weixinA->error);
  719. }
  720. $base64 = 'data:'.$data['contentType'].';base64,'.base64_encode($data['buffer']);
  721. $res = $this->uploadImageBase64("wximg",$base64,1);
  722. if($res["code"]!=1){
  723. return app('json')->fail($res["msg"]);
  724. }
  725. (new UserModel)->where("uid",$request->user["uid"])->update(["invite_img"=>$res["url"]]);
  726. return app('json')->success(["qrcode"=>$res["url"],"bgimg"=>$bgimg]);
  727. }
  728. // public function getWxmpInviteQrcode2(Request $request){
  729. //
  730. // //获取小程序码
  731. // $weixinA = new weixina();
  732. // $data = $weixinA->getUnlimitedQRCode("pages/index/index","scene=".$request->user["uid"]);
  733. // if(empty($data)){
  734. // return app('json')->fail("小程序码获取失败".$weixinA->error);
  735. // }
  736. // $base64 = 'data:'.$data['contentType'].';base64,'.base64_encode($data['buffer']);
  737. // $code = "temp";
  738. // $sysData = (new SysModel())->where("id",1)->find();
  739. // $uploadConfig = config('filesystem');
  740. // //验证base64格式
  741. // preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64, $result);
  742. // if(!$result){
  743. // return ["code"=>-1,"msg"=>"base64格式格式错误"];
  744. // }
  745. // //验证图片后缀
  746. // if (!in_array($result[2], $uploadConfig['image']['ext'], true)) {
  747. // return ["code"=>-1,"msg"=>"不支持的图片格式"];
  748. // }
  749. // $rootPath = config('filesystem.disks.resource.root');
  750. // $hashName = $code.DIRECTORY_SEPARATOR.date('Ymd') . DIRECTORY_SEPARATOR . md5((string) microtime(true)).".".$result[2];
  751. // $filePath = $rootPath. DIRECTORY_SEPARATOR .$hashName;
  752. // $path = dirname($filePath);
  753. // // 检测目录
  754. // if (!is_dir($path)) {
  755. // if (!mkdir($path, 0777, true)) {
  756. // return ["code"=>-1,"msg"=>"生成目录失败"];
  757. // }
  758. // return ["code"=>-1,"msg"=>"上传目录不存在"];
  759. // }
  760. // //重名文件验证
  761. // if (is_file($filePath)) {
  762. // return ["code"=>-1,"msg"=>"文件已存在"];
  763. // }
  764. // if(!file_put_contents($filePath, base64_decode(str_replace($result[1], '', $base64)))){
  765. // return ["code"=>-1,"msg"=>"文件报错失败"];
  766. // }
  767. //
  768. // //获取海报背景
  769. // $inviteData = (new SysModel)->getDataInfo("invite");
  770. // if(empty($inviteData["img"])){
  771. // return ["code"=>-1,"msg"=>"海报背景不存在"];
  772. // }
  773. // $bgimg =explode("resource", $inviteData["img"])[1];
  774. // if(empty($bgimg)){
  775. // return ["code"=>-1,"msg"=>"海报背景不存在"];
  776. // }
  777. //
  778. // $savePath = $rootPath. DIRECTORY_SEPARATOR .$code.DIRECTORY_SEPARATOR.date('Ymd') . DIRECTORY_SEPARATOR . md5((string) microtime(true))."b".".jpg";
  779. //
  780. //
  781. //
  782. //
  783. // $bg = $rootPath.$bgimg;
  784. // $bgSize = getimagesize($bg);
  785. // $qrcodeSize = getimagesize($filePath);
  786. // $handle = Image::open($bg);
  787. // $handle->water($filePath,[($bgSize[0]-$qrcodeSize[0])/2,($bgSize[1]-$qrcodeSize[1])/2]);
  788. // $handle->save($savePath,"jpg",80);
  789. // $qiniu = new Qiniu;
  790. // $img_url = $qiniu->updateFile('img', $savePath, $savePath);
  791. // if (empty($img_url['url'])) {
  792. // return ["code"=>-1,"msg"=>"文件报错失败:".$qiniu->getError()];
  793. // }
  794. // @unlink($filePath);
  795. // @unlink($savePath);
  796. // return ["code"=>1,"url"=>str_replace("\\", "/", $img_url['url'])];
  797. // }
  798. //
  799. // public function checkImgPath(){
  800. //
  801. // }
  802. /**
  803. * 图片合成
  804. * @param type $bg
  805. * @param type $qrcode
  806. * @param type $savePath
  807. */
  808. public function createPosterImg($bg,$qrcode,$savePath){
  809. }
  810. /**
  811. * 获取小程序码
  812. * @param Request $request
  813. */
  814. public function getWxmpQrcode(Request $request){
  815. $weixinA = new weixina();
  816. $data = $weixinA->getUnlimitedQRCode("pages/index/index","a=1");
  817. if(empty($data)){
  818. return app('json')->fail("小程序码获取失败".$weixinA->error);
  819. }
  820. $base64 = 'data:'.$data['contentType'].';base64,'.base64_encode($data['buffer']);
  821. $res = $this->uploadImageBase64("wximg",$base64);
  822. if($res["code"]!=1){
  823. return app('json')->fail($res["msg"]);
  824. }
  825. return app('json')->success(["img"=>$res["data"]]);
  826. }
  827. /**
  828. *
  829. * @param Request $request
  830. */
  831. public function uploadBase64(Request $request){
  832. [$img,$code] = UtilService::getMore([
  833. ['img', '','empty',"请上传图片"],
  834. ['code', 'image'],
  835. ], $request,true);
  836. // $imgAr = explode("base64,",$img);
  837. // if(empty($imgAr)){
  838. // return app("json")->fail("请上传base64图片");
  839. // }
  840. // $imgBase64 = count($imgAr)==1?$imgAr[0]:$imgAr[1];
  841. // //判断字符串是否经过编码方法
  842. // if($imgBase64!=base64_encode(base64_decode($imgBase64))){
  843. // return app("json")->fail("请上传base64图片");
  844. // }
  845. // $base64 = "data:application/octet-stream;base64,".$imgBase64;//七牛
  846. $res = $this->uploadImageBase64($code,$img);
  847. if($res["code"]!=1){
  848. return app('json')->fail($res["msg"]);
  849. }
  850. return app('json')->success(["img"=>$res["data"]]);
  851. }
  852. /**
  853. * 图片上传
  854. * @param Request $request
  855. * @return type
  856. */
  857. public function upload(Request $request){
  858. $uploadConfig = config('filesystem');
  859. $sysData = (new SysModel())->where("id",1)->find();
  860. $file = $request->file('file');
  861. $code = $request->post("code","image");
  862. if(empty($file)) {
  863. return app('json')->fail("未上传文件");
  864. }
  865. $size = $file->getSize();
  866. if(!empty($size) && $size>$uploadConfig["image"]["size"]*1024*1024){
  867. return app('json')->fail("图片不能超过{$uploadConfig["image"]["size"]}M");
  868. }
  869. if(!in_array($code, $uploadConfig['image']['path'])){
  870. return app('json')->fail("上传路径错误");
  871. }
  872. $ext = $file->extension();
  873. if(!in_array($ext, $uploadConfig['image']['ext'])){
  874. return app('json')->fail("图片类型错误");
  875. }
  876. $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
  877. if(!$upPath){
  878. return app('json')->fail("上传失败01");
  879. }
  880. $savePath = $sysData['system_url'].'/resource/' . $upPath;
  881. $imgUrl = str_replace("\\", "/", $savePath);
  882. return app('json')->success(['img'=>$imgUrl,'size'=>$size]);
  883. }
  884. /**
  885. * 视频上传
  886. * @param Request $request
  887. * @return type
  888. */
  889. public function uploadVideo(Request $request){
  890. $uploadConfig = config('filesystem');
  891. $sysData = (new SysModel())->where("id",1)->find();
  892. $file = $request->file('file');
  893. $code = $request->post("code","video");
  894. if(empty($file)) {
  895. return app('json')->fail("未上传文件");
  896. }
  897. if(!in_array($code, $uploadConfig['video']['path'])){
  898. return app('json')->fail("上传路径错误");
  899. }
  900. $ext = $file->extension();
  901. if(!in_array($ext, $uploadConfig['video']['ext'])){
  902. return app('json')->fail("视频类型错误");
  903. }
  904. $upPath = \think\facade\Filesystem::disk('resource')->putFile( $code, $file);
  905. if(!$upPath){
  906. return app('json')->fail("上传失败01");
  907. }
  908. $savePath = $sysData['system_url'].'/resource/' . $upPath;
  909. $imgUrl = str_replace("\\", "/", $savePath);
  910. return app('json')->success(['video'=>$imgUrl]);
  911. }
  912. /**
  913. * base64文件上传
  914. * @param type $code
  915. * @param type $base64
  916. */
  917. public function uploadImageBase64($code,$base64,$isQiniu=0){
  918. $sysData = (new SysModel())->where("id",1)->find();
  919. $uploadConfig = config('filesystem');
  920. //验证base64格式
  921. preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64, $result);
  922. if(!$result){
  923. return ["code"=>-1,"msg"=>"base64格式格式错误"];
  924. }
  925. //验证图片后缀
  926. if (!in_array($result[2], $uploadConfig['image']['ext'], true)) {
  927. return ["code"=>-1,"msg"=>"不支持的图片格式"];
  928. }
  929. $rootPath = config('filesystem.disks.resource.root');
  930. $hashName = $code.DIRECTORY_SEPARATOR.date('Ymd') . DIRECTORY_SEPARATOR . md5((string) microtime(true)).".".$result[2];
  931. $filePath = $rootPath. DIRECTORY_SEPARATOR .$hashName;
  932. $path = dirname($filePath);
  933. // 检测目录
  934. if (!is_dir($path)) {
  935. if (!mkdir($path, 0777, true)) {
  936. return ["code"=>-1,"msg"=>"生成目录失败"];
  937. }
  938. return ["code"=>-1,"msg"=>"上传目录不存在"];
  939. }
  940. //重名文件验证
  941. if (is_file($filePath)) {
  942. return ["code"=>-1,"msg"=>"文件已存在"];
  943. }
  944. if(!file_put_contents($filePath, base64_decode(str_replace($result[1], '', $base64)))){
  945. return ["code"=>-1,"msg"=>"文件报错失败"];
  946. }
  947. //七牛上传
  948. if($isQiniu==1){
  949. $qiniu = new Qiniu;
  950. $img_url = $qiniu->updateFile('img', $filePath, $filePath);
  951. if (empty($img_url['url'])) {
  952. return ["code"=>-1,"msg"=>"文件报错失败:".$qiniu->getError()];
  953. }
  954. @unlink($filePath);
  955. return ["code"=>1,"url"=>str_replace("\\", "/", $img_url['url'])];
  956. }
  957. $savePath = $sysData['system_url'].config('filesystem.disks.resource.url').DIRECTORY_SEPARATOR.$hashName;
  958. return ["code"=>1,"url"=>str_replace("\\", "/", $savePath)];
  959. }
  960. /**
  961. * 七牛上传图片
  962. * @param Request $request
  963. */
  964. public function qiniuUpload(Request $request)
  965. {
  966. $file = $request->file('file');
  967. if (empty($file)) {
  968. return app('json')->fail("未上传文件");
  969. }
  970. $rootTmp = config('filesystem.disks.local.root') . '/' . \think\facade\Filesystem::putFile('tmp', $file);
  971. $image_size = @getimagesize($rootTmp);
  972. if ($image_size[0] > 1000) {
  973. $imgS = Image::open($rootTmp);
  974. $imgS->thumb(1000, $image_size[1]);
  975. $imgS->save($rootTmp);
  976. } else {
  977. if ($image_size[1] > 1000) {
  978. $imgS = Image::open($rootTmp);
  979. $imgS->thumb($image_size[0], 1000);
  980. $imgS->save($rootTmp);
  981. }
  982. }
  983. $qiniu = new Qiniu;
  984. $img_url = $qiniu->updateFile('img', $rootTmp, $rootTmp);
  985. if (empty($img_url['url'])) {
  986. return app('json')->fail($qiniu->getError());
  987. }
  988. @unlink($rootTmp);
  989. return app('json')->success(['img' => $img_url['url']]);
  990. }
  991. /**
  992. * 七牛上传token
  993. * @param Request $request
  994. */
  995. public function qiniuUploadToken(Request $request){
  996. [$bucket] = UtilService::getMore([
  997. ['bucket', ''],
  998. ], $request,true);
  999. $qiniu = new Qiniu;
  1000. $token = $qiniu->createUploadToken($bucket);
  1001. return app('json')->success(['token' => $token]);
  1002. }
  1003. /**
  1004. * 获取用户模板信息
  1005. * @param int $uid 用户ID
  1006. * @return array
  1007. */
  1008. private function getUserInfo($uid)
  1009. {
  1010. $userInfo = (new UserModel)->where('uid', $uid)->find();
  1011. $auth = (new InfoAudit)->where('uid', $uid)->find();
  1012. $user_work_type_title = (new UserWorkType)->where('id', $auth['user_work_type_id'])->find();
  1013. if ($auth) {
  1014. $auth_info = $auth->toArray();
  1015. } else {
  1016. $auth_info = null;
  1017. }
  1018. return [
  1019. 'is_type_audit' => $auth && $auth['status'] == 1 ? 1 : 0,
  1020. 'ancestral_place' => $auth ? $auth['ancestral_place'] : '',
  1021. 'auth_info' => $auth_info,
  1022. 'user_work_type_id' => $user_work_type_title['title'],
  1023. ];
  1024. }
  1025. /**
  1026. * 合约列表
  1027. * @param \app\Request $request
  1028. * @return mixed
  1029. */
  1030. public function getContractList(\think\Request $request)
  1031. {
  1032. // $pageSize = 50;
  1033. // $post = UtilService::getMore([
  1034. // ['page',1],
  1035. // ['pageSize',50],
  1036. // ['nickname',''],
  1037. //// ['uid',''],
  1038. // ['parent_uid',''],
  1039. // ['mobile',''],
  1040. // ['status',''],
  1041. // ['time',[]],
  1042. // ],$request);
  1043. //// $post['uid']=$request->user["uid"];
  1044. // $data = (new UserModel)->getDataList($post,"*",1);
  1045. // return app('json')->success([
  1046. // 'list' => $data["list"],
  1047. // 'pageCount' => $data["totalCount"],
  1048. // 'pageSize' => $data["pageSize"],
  1049. // 'page' => $data["page"],
  1050. // ]);
  1051. $post = UtilService::getMore([
  1052. ['page', 1],
  1053. ['pageSize', 50],
  1054. ['status',-2] //1未签约 2已签约 3已解约
  1055. ], $request);
  1056. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  1057. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  1058. $where=[];
  1059. $totalCount = (new UserContractRecordModel)->where($where)->count();
  1060. $uid= $request->user["uid"];
  1061. if ($uid>0){
  1062. $where[]=["uid","=",$uid];
  1063. }
  1064. if ($post["status"]!=-2){
  1065. $where[]=['status','=',$post["status"]];
  1066. }
  1067. $data=null;
  1068. if($totalCount>0){
  1069. $data = (new UserContractRecordModel)
  1070. // ->field("")
  1071. // ->alias("ut")
  1072. // ->join("show_template t", "t.id = show_template_id","left")
  1073. ->where($where)
  1074. // ->order("is_default", "desc")
  1075. ->order("id", "desc")
  1076. ->page($post["page"], $post["pageSize"])
  1077. ->select();
  1078. // var_dump((new UserContractRecordModel)->getLastSql());die();
  1079. foreach($data as $k=>$v){
  1080. $data[$k]["is_use"] = 1;//是否已经购买或者可以使用
  1081. switch ($data[$k]["status"]){
  1082. case 0:
  1083. $data[$k]["status_name"] = "未签约";
  1084. break;
  1085. case 1:
  1086. $data[$k]["status_name"] = "已签约";
  1087. break;
  1088. case -1:
  1089. $data[$k]["status_name"] = "已解约";
  1090. break;
  1091. }
  1092. // $data[$k]["nickname"] = (new \app\model\api\User())->where('uid',$v['to_uid'])->value('nickname');
  1093. }
  1094. }
  1095. $data = empty($data)?[]:$data;
  1096. return app('json')->success(["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount]);
  1097. }
  1098. /**
  1099. * 甲方创建合约
  1100. */
  1101. public function form_save(Request $request){
  1102. $post = UtilService::getMore([
  1103. ['id',0],
  1104. ['uid','0'], //甲方uid
  1105. ['name',''], //甲方姓名
  1106. ['phone',''], //甲方手机号
  1107. ['address',''], //甲方地址
  1108. ['card',''], //甲方身份证号
  1109. ['to_uid',0], //员工uid
  1110. ['to_name',''], //乙方姓名
  1111. ['price',0], //服务费
  1112. ['deposit',0], // 定金
  1113. ['balance',0], //余款
  1114. ['period',0], // 周期天数
  1115. ['start_time',''], // 开始时间
  1116. ['end_time',''], // 结束时间
  1117. ['mark',''] //备注
  1118. // ['uid_img',''] //甲方签名图片路径
  1119. // ['to_phone',''].
  1120. // ['to_address',''],
  1121. // ['to_card',''],
  1122. // ['data',[]],
  1123. // ['year',''],
  1124. // ['month',''],
  1125. // ['day',''],
  1126. ],$request);
  1127. $id = (int)$post["id"];
  1128. unset($post["id"]);
  1129. $uid = (int)$post["uid"];
  1130. if(empty($uid)){
  1131. return app('json')->fail("参数错误");
  1132. }
  1133. $post['start_time'] = strtotime($post['start_time']);
  1134. $post['end_time'] = strtotime($post['end_time']);
  1135. $start=(new UserContractRecordModel)->where('to_uid',$post['to_uid'])->where('status',1)->whereBetween('start_time',[$post['start_time'],$post['end_time']])->find();
  1136. $end=(new UserContractRecordModel)->where('to_uid',$post['to_uid'])->where('status',1)->whereBetween('end_time',[$post['start_time'],$post['end_time']])->find();
  1137. // $start2=(new UserContractRecordModel)->where('to_uid',$post['to_uid'])->where('status',0)->whereBetween('start_time',[$post['start_time'],$post['end_time']])->find();
  1138. // $end2=(new UserContractRecordModel)->where('to_uid',$post['to_uid'])->where('status',0)->whereBetween('end_time',[$post['start_time'],$post['end_time']])->find();
  1139. // if (!empty($start)||!empty($end)||!empty($start2)||!empty($end2)){
  1140. if (!empty($start)||!empty($end)){
  1141. return app('json')->fail("与对方其他合约时间有冲突,请与对方协商");
  1142. }
  1143. // $year = $post['year'];
  1144. // $month = $post['month'];
  1145. // $day = $post['day'];
  1146. //
  1147. //// 检查年月日是否为空
  1148. // if (empty($year) || empty($month) || empty($day)) {
  1149. // return app('json')->fail("日期参数错误");
  1150. // }
  1151. //
  1152. //// 创建 DateTime 对象
  1153. // $date = \DateTime::createFromFormat('Y-m-d', "$year-$month-$day");
  1154. //
  1155. //// 检查日期是否有效
  1156. // if (!$date || $date->format('Y-m-d') !== "$year-$month-$day") {
  1157. // return app('json')->fail("无效的日期");
  1158. // }
  1159. // unset($post['year']);
  1160. // unset($post['month']);
  1161. // unset($post['day']);
  1162. //// 转换为时间戳
  1163. // $timestamp = $date->getTimestamp();
  1164. //
  1165. // $post['create_time'] = $timestamp;
  1166. //用户信息
  1167. // if(!empty($post["uid_img"])){
  1168. // $post['status'] = 2;
  1169. // }else{
  1170. $post['status'] = 0;
  1171. // }
  1172. if ($id>0){
  1173. $r = (new UserContractRecordModel)->where("id",$id)->update($post);
  1174. }else{
  1175. $post['contract_no'] = makeOrderId($post['uid'],"CR");
  1176. $r = (new UserContractRecordModel)->save($post);
  1177. }
  1178. return app('json')->success("数据保存成功");
  1179. }
  1180. /**
  1181. * 甲方签约合约
  1182. */
  1183. public function check(Request $request){
  1184. $post = UtilService::getMore([
  1185. ['id',0], //合约id
  1186. ['uid_img',''], //甲方签名图片路径
  1187. ['check_time','']//签约时间
  1188. // ['to_phone',''].
  1189. // ['to_address',''],
  1190. // ['to_card',''],
  1191. // ['data',[]],
  1192. // ['year',''],
  1193. // ['month',''],
  1194. // ['day',''],
  1195. ],$request);
  1196. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  1197. if(empty($info)){
  1198. return app('json')->fail("合约不存在");
  1199. }
  1200. // 检查签约时间是否为空
  1201. if (empty($post['uid_img'])) {
  1202. return app('json')->fail("签字错误");
  1203. }
  1204. // 检查签约时间是否为空
  1205. if (empty($post['check_time'])) {
  1206. return app('json')->fail("日期参数错误");
  1207. }
  1208. $post['check_time'] = strtotime($post['check_time']);
  1209. // $post['status'] = 1;
  1210. $post['from_check'] = 1;
  1211. if ($info['to_check']==1){
  1212. $post['status']=1;
  1213. }
  1214. // $post['contract_no'] = makeOrderId($post['uid'],"CR");
  1215. $r = (new UserContractRecordModel)->where("id",$post["id"])->update($post);
  1216. return app('json')->success("数据保存成功");
  1217. }
  1218. // 乙方确认
  1219. public function to_check(Request $request){
  1220. $post = UtilService::getMore([
  1221. ['id','0'], //
  1222. ['to_uid_img',''], //乙方签名图片路径
  1223. ['to_phone',''], //乙方手机号
  1224. ['to_address',''], //乙方地址
  1225. ['to_card',''], //乙方身份证号
  1226. ['to_check_time','']//签约时间
  1227. ],$request);
  1228. $info = (new UserContractRecordModel)->where("id",$post["id"])->find();
  1229. if(empty($info)){
  1230. return app('json')->fail("合约不存在");
  1231. }
  1232. // 检查签约时间是否为空
  1233. if (empty($post['to_uid_img'])) {
  1234. return app('json')->fail("签字错误");
  1235. }
  1236. // 检查签约时间是否为空
  1237. if (empty($post['to_check_time'])) {
  1238. return app('json')->fail("日期参数错误");
  1239. }
  1240. $post['to_check_time'] = strtotime($post['to_check_time']);
  1241. $post['to_check'] = 1;
  1242. if ($info['from_check']==1){
  1243. $post['status']=1;
  1244. }
  1245. $r = (new UserContractRecordModel)->where("id",$post["id"])->update($post);
  1246. return app('json')->success("数据保存成功");
  1247. }
  1248. // 员工签到
  1249. public function clock_in(Request $request){
  1250. $post = UtilService::getMore([
  1251. ['contract_id','0'], //合约id
  1252. ['longitude',''], //经度
  1253. ['latitude',''], //纬度
  1254. // ['uid',0]
  1255. ],$request);
  1256. $uid = $request->user["uid"];
  1257. // $uid = (int)$post["uid"];
  1258. // 检查签约时间是否为空
  1259. if (empty($post['contract_id'])) {
  1260. return app('json')->fail("签字错误");
  1261. }
  1262. $nickname=\app\model\api\User::where('uid',$uid)->value('nickname');
  1263. $r = (new UserClockModel)->save([
  1264. 'uid'=>$uid,
  1265. 'nickname' => $nickname,
  1266. 'longitude'=>$post['longitude'],
  1267. 'latitude'=>$post['latitude'],
  1268. 'contract_id'=>$post['contract_id'],
  1269. 'create_time'=>time()
  1270. ]);
  1271. return app('json')->success("数据保存成功");
  1272. }
  1273. // 添加客户评论
  1274. public function comment(Request $request){
  1275. $post = UtilService::getMore([
  1276. ['contract_id','0'], //合约id
  1277. ['content',''], //评论内容
  1278. ['uid',0]
  1279. ],$request);
  1280. // $uid = $request->user["uid"];
  1281. $uid = $post['uid'];
  1282. $time=time();
  1283. $info = (new UserContractRecordModel)->where("id",$post["contract_id"])->where('start_time','>',$time)->where('end_time','<',$time)->find();
  1284. if (!empty($info)){
  1285. $info = $info->toArray();
  1286. }
  1287. if ($info['status']!=1){
  1288. return app('json')->fail("只能在签约期间才能发表评论");
  1289. }
  1290. if(empty($info)){
  1291. return app('json')->fail("没有签约中的合约");
  1292. }
  1293. // 检查签约时间是否为空
  1294. // if (empty($post['id'])) {
  1295. // return app('json')->fail("签字错误");
  1296. // }
  1297. $r = (new ContractCommentModel)->save([
  1298. 'uid'=>$uid,
  1299. 'contract_id'=>$post['contract_id'],
  1300. 'contract_no'=>$info['contract_no'],
  1301. 'to_uid'=>$info['to_uid'],
  1302. 'content'=>$post['content'],
  1303. 'create_time'=>time()
  1304. ]);
  1305. return app('json')->success("数据保存成功");
  1306. }
  1307. // 甲方主动解约
  1308. public function lift_contract(Request $request){
  1309. $post = UtilService::getMore([
  1310. ['contract_id','0'], //合约id
  1311. // ['uid',0]
  1312. ],$request);
  1313. // 检查签约时间是否为空
  1314. if (empty($post['contract_id'])) {
  1315. return app('json')->fail("没有该合约");
  1316. }
  1317. $r = (new UserContractRecordModel())->where('id',$post['contract_id'])->update([
  1318. 'status'=>-1,
  1319. 'delete_time'=>time()
  1320. ]);
  1321. return app('json')->success("解约成功");
  1322. }
  1323. }