User.php 63 KB

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