User.php 54 KB

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