User.php 59 KB

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