User.php 53 KB

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