Member.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-31 20:43
  10. // +----------------------------------------------------------------------
  11. namespace app\system\controller\v1;
  12. use app\BaseController;
  13. use app\lib\OrderLib;
  14. use app\model\api\PayTrade;
  15. use app\model\api\UserShop as UserShopModel;
  16. use app\model\api\Order as OrderModel;
  17. use app\model\api\ActivityOrder as ActivityOrderModel;
  18. use library\utils\weixinPay as wxpayApi;
  19. use app\model\api\User as UserModel;
  20. use app\model\api\UserDetail as UserDetailModel;
  21. use app\model\api\UserScoreDetail as UserScoreDetailModel;
  22. use library\services\UtilService;
  23. use app\Request;
  24. use think\facade\Db;
  25. class Member extends BaseController
  26. {
  27. /**
  28. * 会员列表
  29. * @param Request $request
  30. * @return mixed
  31. */
  32. public function list(Request $request)
  33. {
  34. $pageSize = 50;
  35. $post = UtilService::getMore([
  36. ['page',1],
  37. ['nickname',''],
  38. ['uip',''],
  39. ['uid',''],
  40. ['parent_uid',''],
  41. ['parent_uip',''],
  42. ['mobile',''],
  43. ['status',''],
  44. ['time',[]],
  45. ],$request);
  46. $where = [];
  47. //创建时间
  48. $startTime="";
  49. $endTime="";
  50. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  51. $startTime = strtotime($post['time'][0]);
  52. $endTime = strtotime($post['time'][1]);
  53. $where[]=["u.regtime","between","{$startTime},{$endTime}"];
  54. }
  55. if(!empty($post['nickname'])){
  56. $where[]=["u.nickname","like","%{$post['nickname']}%"];
  57. }
  58. if(!empty($post['uid'])){
  59. $where[]=["u.uid","=",$post['uid']];
  60. }
  61. if(!empty($post['mobile'])){
  62. $where[]=["u.mobile","=",$post['mobile']];
  63. }
  64. if(!empty($post['uip'])){
  65. $where[]=["u.uip","=",$post['uip']];
  66. }
  67. if(!empty($post['parent_uid'])){
  68. $where[]=["u.parent_uid","=",$post['parent_uid']];
  69. }else if(!empty($post['parent_uip'])){
  70. $m = Db::name("user")->where("uip",$post['parent_uip'])->find();
  71. if(!empty($m)) {
  72. $where[]=["u.parent_uid","=",$m['uid']];
  73. }
  74. }
  75. if(in_array((string)$post["status"],['1','0','-1'])){
  76. $where[]=["u.status","=",(int)$post["status"]];
  77. }
  78. $data = (new UserModel)
  79. ->field("u.uid,u.uip,u.nickname,u.regtime,u.score,u.money,u.status,u.avatar,u.ercode,u.mobile,u.parent_uid"
  80. . ",s.id as shop_id,s.name as shop_name,s.lx_name as shop_lx_name,s.address as shop_address,s.tel as shop_tel,s.longitude as shop_longitude,s.latitude as shop_latitude,IFNULL(s.is_show,0) as shop_is_show"
  81. . ",p.nickname as p_nickname,p.mobile as p_mobile,p.uip as p_uip"
  82. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount")
  83. ->alias("u")
  84. ->leftJoin("user p","p.uid = u.parent_uid")
  85. ->leftJoin("user_shop s","s.uid = u.uid")
  86. ->where($where)
  87. ->page((int)$post["page"], (int)$pageSize)
  88. ->order("u.uid","desc")
  89. ->select()
  90. ->toArray();
  91. $pageCount = (new UserModel)->alias("u")->where($where)->count();
  92. $data = empty($data)?[]:$data;
  93. foreach($data as $k=>$v){
  94. $data[$k]["regtime"] = date("Y-m-d H:i:s",$v["regtime"]);
  95. $data[$k]["mobile"] = empty($v["mobile"])?"未绑定":$v["mobile"];
  96. $data[$k]["p_mobile"] = empty($v["mobile"])?"未绑定":$v["p_mobile"];
  97. }
  98. return app('json')->success([
  99. 'list' => $data,
  100. 'pageCount' => $pageCount,
  101. 'pageSize' => $pageSize,
  102. 'page' => $post["page"],
  103. ]);
  104. }
  105. /**
  106. * 编辑
  107. */
  108. public function save(Request $request){
  109. $post = UtilService::getMore([
  110. ['uid','0'],
  111. ['status',''],
  112. ['shop_name',''],
  113. ['shop_tel',''],
  114. ['shop_lx_name',''],
  115. ['shop_address',''],
  116. ['shop_longitude',''],
  117. ['shop_latitude',''],
  118. ['shop_is_show',''],
  119. ],$request);
  120. $uid = (int)$post["uid"];
  121. if(empty($uid)){
  122. return app('json')->fail("参数错误");
  123. }
  124. unset($post["uid"]);
  125. $r=0;
  126. if(!in_array((string)$post["status"],['1','-1'])){
  127. return app('json')->success("状态错误");
  128. }
  129. //开启门店
  130. $shopData = (new UserShopModel)->where("uid",$uid)->find();
  131. $shopSave=[
  132. "name" => $post["shop_name"],
  133. "tel" => $post["shop_tel"],
  134. "lx_name" => $post["shop_lx_name"],
  135. "address" => $post["shop_address"],
  136. "longitude" => $post["shop_longitude"],
  137. "latitude" => $post["shop_latitude"],
  138. "is_show" => $post["shop_is_show"]==1?1:0,
  139. ];
  140. if($shopSave["is_show"]==1){
  141. if(empty($shopSave["name"])){
  142. return app('json')->fail("请输入门店名称");
  143. }
  144. if(empty($shopSave["tel"])){
  145. return app('json')->fail("请输入门店联系电话");
  146. }
  147. if(empty($shopSave["lx_name"])){
  148. return app('json')->fail("请输入门店联系人");
  149. }
  150. if(empty($shopSave["address"])){
  151. return app('json')->fail("请输入门店地址");
  152. }
  153. if(empty($shopData)){
  154. $shopSave["time"]=time();
  155. (new UserShopModel)->insert($shopSave);
  156. }else{
  157. (new UserShopModel)->where("id",$shopData["id"])->update($shopSave);
  158. }
  159. }else{
  160. if(!empty($shopData)){
  161. (new UserShopModel)->where("id",$shopData["id"])->update($shopSave);
  162. }
  163. }
  164. //用户信息
  165. $save=[];
  166. $save["status"] = $post["status"];
  167. $r = (new UserModel)->where("uid",$uid)->update($save);
  168. return app('json')->success("数据保存成功");
  169. }
  170. /**
  171. * 会员收益明细列表
  172. * @param Request $request
  173. * @return mixed
  174. */
  175. public function moneyDetail (Request $request)
  176. {
  177. $pageSize = 50;
  178. $post = UtilService::getMore(
  179. [
  180. ['page', '1'],
  181. ['uid', '0'],
  182. ['uip', ''],
  183. ['mobile', ''],
  184. ['time', []],
  185. ['type', ''],
  186. ['code', ''],
  187. ], $request
  188. );
  189. $where = [];
  190. if (!empty($post['uid'])){
  191. $where[]=["d.uid","=",$post['uid']];
  192. }
  193. if (!empty($post['uip'])) {
  194. $m = Db::name("user")->where("uip",$post['uip'])->find();
  195. if(!empty($m)) {
  196. $where[]=["d.uid","=",$m['uid']];
  197. }
  198. }
  199. if (!empty($post['mobile'])) {
  200. $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  201. if(!empty($m)) {
  202. $where[]=["d.uid","=",$m['uid']];
  203. }
  204. }
  205. $startTime = "";
  206. $endTime = "";
  207. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  208. $startTime = strtotime($post['time'][0]);
  209. $endTime = strtotime($post['time'][1]);
  210. $where[]=["d.time","between","{$startTime},{$endTime}"];
  211. }
  212. if (!empty($post['type']) && in_array(strval($post['type']), ["1","-1"])) {
  213. $where[]=["d.type","=",$post['type']];
  214. }
  215. if(!empty($post['code']) && $post['code']!="all"){
  216. $where[]=["d.code","=",$post['code']];
  217. }
  218. $data = (new UserDetailModel)
  219. ->alias("d")
  220. ->field("d.*,u.mobile,u.uip")
  221. ->leftJoin("user u", "u.uid = d.uid")
  222. ->where($where)
  223. ->page((int)$post["page"], $pageSize)
  224. ->order("d.id","desc")
  225. ->select()
  226. ->toArray();
  227. $pageCount = (new UserDetailModel)->alias("d")->where($where)->count();
  228. foreach($where as $k=>$v){
  229. if($v[0]=="d.type"){
  230. array_splice($where,$k,1);
  231. }
  232. }
  233. //收入
  234. $inWhere = $where;
  235. $inWhere[]=["d.type","=",1];
  236. $inTotal = (new UserDetailModel)->alias("d")->where($inWhere)->sum("d.into");
  237. //支出
  238. $outWhere = $where;
  239. $outWhere[]=["d.type","=",-1];
  240. $outTotal = (new UserDetailModel)->alias("d")->where($outWhere)->sum("d.into");
  241. $result = UtilService::getParam(
  242. [
  243. 'id',
  244. 'uid',
  245. 'uip',
  246. 'mobile',
  247. 'title',
  248. 'content',
  249. 'type',
  250. 'into',
  251. 'money',
  252. ['time', 'time', 'date("Y-m-d H:i:s",$1)'],
  253. ], $data);
  254. return app('json')->success([
  255. 'list' => $result,
  256. 'pageCount' => $pageCount,
  257. 'pageSize' => $pageSize,
  258. 'page' => $post['page'],
  259. 'inTotal' => empty($inTotal) ? 0 : floatval($inTotal),
  260. 'outTotal' => empty($outTotal)? 0 : floatval($outTotal),
  261. ]);
  262. }
  263. /**
  264. * 收益增/减
  265. * @param Request $request
  266. */
  267. public function moneyDetailSave(Request $request)
  268. {
  269. $post = UtilService::getMore([
  270. ['uid', '', 'empty', '参数错误'],
  271. ['type', '1'],
  272. ['into', '0', 'gt|0', '输入值错误'],
  273. ['othen', ''],
  274. ], $request);
  275. $userData = (new UserModel)->where("uid",$post["uid"])->find();
  276. if(empty($userData)){
  277. $userData = (new UserModel)->where("uip",$post["uip"])->find();
  278. if(empty($userData)){
  279. return app('json')->fail('用户不存在,请输入正确的用户uid');
  280. }
  281. }
  282. if(!in_array((string)$post["type"],["1","-1"])){
  283. return app('json')->fail('请选择增减');
  284. }
  285. if ($post['type'] == 1)
  286. $bool = (new UserDetailModel)->adminAddMoney($userData['uid'], $post['into'], $request->adminInfo['id'],$post['othen']);
  287. if ($post['type'] == -1)
  288. $bool = (new UserDetailModel)->adminCutMoney($userData['uid'], $post['into'], $request->adminInfo['id'],$post['othen']);
  289. if ($bool)
  290. return app('json')->success('操作成功');
  291. else
  292. return app('json')->fail('操作失败');
  293. }
  294. /**
  295. * 钻石收益明细列表
  296. * @param Request $request
  297. * @return mixed
  298. */
  299. public function scoreDetail (Request $request)
  300. {
  301. $pageSize = 50;
  302. $post = UtilService::getMore(
  303. [
  304. ['page', '1'],
  305. ['uid', '0'],
  306. ['uip', ''],
  307. ['mobile', ''],
  308. ['type', ''],
  309. ['code', ''],
  310. ['time', []],
  311. ], $request
  312. );
  313. $where = [];
  314. if (!empty($post['uid'])){
  315. $where[]=["d.uid","=",$post['uid']];
  316. }else if (!empty($post['uip'])) {
  317. $m = Db::name("user")->where("uip",$post['uip'])->find();
  318. if(!empty($m)) {
  319. $where[]=["d.uid","=",$m['uid']];
  320. }
  321. }else if (!empty($post['mobile'])) {
  322. $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  323. if(!empty($m)) {
  324. $where[]=["d.uid","=",$m['uid']];
  325. }
  326. }
  327. $startTime = "";
  328. $endTime = "";
  329. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  330. $startTime = strtotime($post['time'][0]);
  331. $endTime = strtotime($post['time'][1]);
  332. $where[]=["d.time","between","{$startTime},{$endTime}"];
  333. }
  334. if (!empty($post['type']) && in_array(strval($post['type']), ["1","-1"])) {
  335. $where[]=["d.type","=",$post['type']];
  336. }
  337. if (!empty($post['code'])) {
  338. $where[]=["d.code","=",$post['code']];
  339. }
  340. $data = (new UserScoreDetailModel)
  341. ->alias("d")
  342. ->field("d.*,u.mobile,u.uip")
  343. ->leftJoin("user u", "u.uid = d.uid")
  344. ->where($where)
  345. ->page((int)$post["page"], $pageSize)
  346. ->order("d.id","desc")
  347. ->select()
  348. ->toArray();
  349. $pageCount = (new UserScoreDetailModel)->alias("d")->where($where)->count();
  350. foreach($where as $k=>$v){
  351. if($v[0]=="d.type"){
  352. array_splice($where,$k,1);
  353. }
  354. }
  355. //收入
  356. $inWhere = $where;
  357. $inWhere[]=["d.type","=",1];
  358. $inTotal = (new UserScoreDetailModel)->alias("d")->where($inWhere)->sum("d.v");
  359. //支出
  360. $outWhere = $where;
  361. $outWhere[]=["d.type","=",-1];
  362. $outTotal = (new UserScoreDetailModel)->alias("d")->where($outWhere)->sum("d.v");
  363. $result = UtilService::getParam(
  364. [
  365. 'id',
  366. 'uid',
  367. 'uip',
  368. 'mobile',
  369. 'title',
  370. 'content',
  371. 'type',
  372. ['into','v'],
  373. 'money',
  374. ['time', 'time', 'date("Y-m-d H:i:s",$1)'],
  375. ], $data);
  376. return app('json')->success([
  377. 'list' => $result,
  378. 'pageCount' => $pageCount,
  379. 'pageSize' => $pageSize,
  380. 'page' => $post['page'],
  381. 'inTotal' => empty($inTotal) ? 0 : floatval($inTotal),
  382. 'outTotal' => empty($outTotal)? 0 : floatval($outTotal),
  383. ]);
  384. }
  385. /**
  386. * 积分增/减
  387. * @param Request $request
  388. */
  389. public function scoreDetailSave(Request $request)
  390. {
  391. $post = UtilService::getMore([
  392. ['uid', '', 'empty', '参数错误'],
  393. ['type', '1'],
  394. ['into', '0', 'gt|0', '输入值错误'],
  395. ['othen', ''],
  396. ], $request);
  397. $userData = (new UserModel)->where("uid",$post["uid"])->find();
  398. if(empty($userData)){
  399. $userData = (new UserModel)->where("uip",$post["uid"])->find();
  400. if(empty($userData)){
  401. return app('json')->fail('用户不存在,请输入正确的用户uid');
  402. }
  403. }
  404. if ($post['type'] == 1)
  405. $bool = (new UserScoreDetailModel)->adminAddMoney($userData['uid'], intval($post['into']), $request->adminInfo['id'],$post['othen']);
  406. if ($post['type'] == -1)
  407. $bool = (new UserScoreDetailModel)->adminCutMoney($userData['uid'], intval($post['into']), $request->adminInfo['id'],$post['othen']);
  408. if ($bool)
  409. return app('json')->success('操作成功');
  410. else
  411. return app('json')->fail('操作失败');
  412. }
  413. /**
  414. * 获取会员数据
  415. * @param Request $request
  416. * @return mixed
  417. */
  418. public function getItem(Request $request)
  419. {
  420. $post = UtilService::getMore([
  421. ['uid', 1],
  422. ]);
  423. $data = (new UserModel)
  424. ->alias("u")
  425. ->field("u.uid,u.mobile,u.nickname,u.avatar,u.status,u.regtime,u.lasttime,u.parent_uid.p.nickname as parent_nickname")
  426. ->leftJoin("user p","p.uid = u.parent_uid")
  427. ->where("u.uid",$post["uid"])
  428. ->find()
  429. ->toArray();
  430. if(!empty($data)){
  431. $data["regtime"] = date("Y-m-d H:i:s",$data["regtime"]);
  432. $data["lasttime"] = date("Y-m-d H:i:s",$data["lasttime"]);
  433. }
  434. return app('json')->success(empty($data) ? [] : $data);
  435. }
  436. public function shopSave(Request $request){
  437. $post = UtilService::getMore([
  438. ['id','','empty','参数错误'],
  439. ['name','','empty','请输入名称'],
  440. ['tel','','empty','请输入联系电话'],
  441. ['lx_name','','empty','请输入联系人'],
  442. ['address','','empty','请输入门店地址'],
  443. ['longitude',''],
  444. ['latitude',''],
  445. ['is_show',''],
  446. ],$request);
  447. $id = (int)$post["id"];
  448. if(empty($id)){
  449. return app('json')->fail("参数错误");
  450. }
  451. unset($post["id"]);
  452. $shopSave=[
  453. "name" => $post["name"],
  454. "tel" => $post["tel"],
  455. "address" => $post["address"],
  456. "lx_name" => $post["lx_name"],
  457. "longitude" => $post["longitude"],
  458. "latitude" => $post["latitude"],
  459. "is_show" => $post["is_show"]==1?1:0,
  460. ];
  461. if(empty($shopSave["name"])){
  462. return app('json')->fail("请输入门店名称");
  463. }
  464. if(empty($shopSave["tel"])){
  465. return app('json')->fail("请输入门店联系电话");
  466. }
  467. if(empty($shopSave["address"])){
  468. return app('json')->fail("请输入门店地址");
  469. }
  470. if(empty($shopSave["lx_name"])){
  471. return app('json')->fail("请输入门店联系人");
  472. }
  473. (new UserShopModel)->where("id",$id)->update($shopSave);
  474. return app('json')->success("数据保存成功");
  475. }
  476. public function shopList(Request $request){
  477. $pageSize = 50;
  478. $post = UtilService::getMore(
  479. [
  480. ['page', '1'],
  481. ['uid', ''],
  482. ['is_show', ''],
  483. ['mobile', ''],
  484. ['name', ''],
  485. ['pageSize', ''],
  486. ], $request);
  487. $where = [];
  488. if(!empty($post["pageSize"]) && $post["pageSize"]>50){
  489. $pageSize = (int)$post["pageSize"];
  490. }
  491. //用户
  492. if (!empty($post['uid'])){
  493. $where[]=["t.uid","=",$post['uid']];
  494. }else if (!empty($post['mobile'])) {
  495. $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  496. if(!empty($m)) {
  497. $where[]=["t.uid","=",$m['uid']];
  498. }
  499. }
  500. if(is_numeric($post['is_show']) && in_array((string)$post['is_show'],['0','1'])) {
  501. $where[]=["t.is_show","=",(int)$post['is_show']];
  502. }
  503. if(!empty($post['name'])){
  504. $where[]=["t.name","like","%{$post['name']}%"];
  505. }
  506. $data = (new UserShopModel)
  507. ->alias("t")
  508. ->field("t.*,u.nickname as nickname,u.uip as uip")
  509. ->leftJoin("user u" , "u.uid = t.uid")
  510. ->where($where)
  511. ->page((int)$post["page"], $pageSize)
  512. ->order("t.id","desc")
  513. ->select()
  514. ->toArray();
  515. $pageCount = (new UserShopModel)->alias("t")->where($where)->count();
  516. $result = UtilService::getParam(
  517. [
  518. 'id',
  519. 'uid',
  520. 'nickname',
  521. 'name',
  522. 'area',
  523. 'address',
  524. 'tel',
  525. 'lx_name',
  526. 'longitude',
  527. "latitude",
  528. 'is_show',
  529. ['time', 'time', function($item){
  530. return empty($item) ? "--" : date("Y-m-d H:i:s",$item);
  531. }],
  532. ], $data);
  533. return app('json')->success([
  534. 'list' => $result,
  535. 'pageCount' => $pageCount,
  536. 'pageSize' => $pageSize,
  537. 'page' => $post['page'],
  538. ]);
  539. }
  540. /**
  541. * 支付任务列表
  542. * @param \app\model\api\Request $request
  543. */
  544. public function payTradeList(Request $request){
  545. $pageSize = 50;
  546. $post = UtilService::getMore(
  547. [
  548. ['page', '1'],
  549. ['uid', ''],
  550. ['uip', ''],
  551. ['status', ''],
  552. ['mobile', ''],
  553. ['type', ''],
  554. ['pay_no', ''],
  555. ['order_id', ''],
  556. ['time', []],
  557. ], $request);
  558. $where = [];
  559. //用户
  560. if (!empty($post['uid'])){
  561. $where[]=["t.uid","=",$post['uid']];
  562. }else if (!empty($post['uip'])) {
  563. $m = Db::name("user")->where("uip",$post['uip'])->find();
  564. if(!empty($m)) {
  565. $where[]=["t.uid","=",$m['uid']];
  566. }
  567. }else if (!empty($post['mobile'])) {
  568. $m = Db::name("user")->where("mobile",$post['mobile'])->find();
  569. if(!empty($m)) {
  570. $where[]=["t.uid","=",$m['uid']];
  571. }
  572. }
  573. if(is_numeric($post['status']) && in_array((string)$post['status'],['0','1'])) {
  574. $where[]=["t.status","=",(int)$post['status']];
  575. }
  576. if(!empty($post['pay_type']) && in_array($post['pay_type'],['wxpay','system'])) {
  577. $where[]=["t.pay_type","=",$post['pay_type']];
  578. }
  579. if(!empty($post['type']) && in_array($post['type'],['pro','ip','activity'])) {
  580. $where[]=["t.type","=",$post['type']];
  581. }
  582. if(!empty($post['pay_no'])){
  583. $where[]=["t.pay_no","=",$post['pay_no']];
  584. }
  585. if(!empty($post['order_id'])){
  586. $where[]=["t.order_id","=",$post['order_id']];
  587. }
  588. $startTime = "";
  589. $endTime = "";
  590. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  591. $startTime = strtotime($post['time'][0]);
  592. $endTime = strtotime($post['time'][1]);
  593. $where[]=["t.time","between","{$startTime},{$endTime}"];
  594. }
  595. $data = (new PayTrade)
  596. ->alias("t")
  597. ->field("t.*,u.nickname as nickname,u.uip as uip,a.username as admin_name")
  598. ->leftJoin("user u" , "u.uid = t.uid")
  599. ->leftJoin("admin a", "a.id = t.admin_id")
  600. ->where($where)
  601. ->page((int)$post["page"], $pageSize)
  602. ->order("t.id","desc")
  603. ->select()
  604. ->toArray();
  605. $pageCount = (new PayTrade)->alias("t")->where($where)->count();
  606. $result = UtilService::getParam(
  607. [
  608. 'id',
  609. 'uid',
  610. 'uip',
  611. 'nickname',
  612. "admin_id",
  613. "admin_name",
  614. 'pay_no',
  615. 'out_trade_no',
  616. 'pay_no',
  617. 'order_id',
  618. 'pay_type',
  619. 'type',
  620. 'money',
  621. 'content',
  622. "d_json",
  623. 'status',
  624. 'mono',
  625. ['time', 'time', function($item){
  626. return empty($item) ? "--" : date("Y-m-d H:i:s",$item);
  627. }],
  628. ['pay_time' , 'pay_time', function($item){
  629. return empty($item) ? "--" : date("Y-m-d H:i:s",$item);
  630. }],
  631. ['pay_json', 'pay_json', function($item){
  632. if(empty($item)){
  633. return "";
  634. }
  635. $payData = json_decode($item,true);
  636. return $payData;
  637. }],
  638. ], $data);
  639. return app('json')->success([
  640. 'list' => $result,
  641. 'pageCount' => $pageCount,
  642. 'pageSize' => $pageSize,
  643. 'page' => $post['page'],
  644. ]);
  645. }
  646. /**
  647. * 搜索交易数据
  648. * @param Request $request
  649. */
  650. public function searchPayTrade(Request $request){
  651. $post = UtilService::getMore(
  652. [
  653. ['id', '','empty',''],
  654. ], $request);
  655. $data = (new PayTrade)->where("id",$post["id"])->find();
  656. if(empty($data)){
  657. return app("json")->fail("数据不存在");
  658. }
  659. if($data["status"]==1 && !empty($data["pay_json"])){
  660. return app("json")->success("交易支付成功", json_decode($data["pay_json"],true));
  661. }
  662. $out_trade_no = $data["out_trade_no"];
  663. //微信支付
  664. if($data["pay_type"]=="wxpay"){
  665. $wxpay = new wxpayApi();
  666. $res = $wxpay->searchOrder($out_trade_no);
  667. if(empty($res)){
  668. return app("json")->fail("查询失败:".$wxpay->errorMsg);
  669. }
  670. $resAr = json_decode($res,true);
  671. if(empty($resAr) || !is_array($resAr)){
  672. return app("json")->fail("查询失败");
  673. }
  674. if(empty($resAr["trade_state"])){
  675. return app("json")->fail("查询状态错误");
  676. }
  677. if($resAr["trade_state"]=="NOTPAY"){
  678. return app("json")->fail("未支付");
  679. }
  680. if($resAr["trade_state"]=="REFUND"){
  681. return app("json")->fail("转入退款");
  682. }
  683. if($resAr["trade_state"]=="CLOSED"){
  684. return app("json")->fail("已关闭");
  685. }
  686. if($resAr["trade_state"]=="REVOKED"){
  687. return app("json")->fail("已撤销(付款码支付)");
  688. }
  689. if($resAr["trade_state"]=="USERPAYING"){
  690. return app("json")->fail("用户支付中(付款码支付)");
  691. }
  692. if($resAr["trade_state"]=="PAYERROR"){
  693. return app("json")->fail("支付失败(其他原因,如银行返回失败)");
  694. }
  695. if($resAr["trade_state"]=="ACCEPT"){
  696. return app("json")->fail("已接收,等待扣款");
  697. }
  698. if($resAr["trade_state"]=="SUCCESS"){
  699. if(empty($resAr["amount"]) || empty($resAr["amount"]["total"])){
  700. return app("json")->fail("交易支付成功,但是查询金额信息");
  701. }
  702. $returnData = [
  703. 'totalMoney' =>$resAr["amount"]["total"]/100,
  704. 'payMoney' =>empty($resAr["amount"]["payer_total"])?'未返回':$resAr["amount"]["payer_total"]/100,
  705. 'payTradeNo' =>$resAr["transaction_id"],
  706. 'outTradeNo' =>$resAr["out_trade_no"],
  707. "tradeStatus"=>$resAr["trade_state"],
  708. "payTime" =>$resAr["success_time"],
  709. ];
  710. $save=[
  711. "status"=>1,
  712. "pay_json"=>json_encode($returnData),
  713. ];
  714. if(!empty($returnData["payTime"])){
  715. $save["pay_time"] = strtotime($returnData["payTime"]);
  716. }
  717. (new PayTrade)->where("id",$post["id"])->save($save);
  718. $saveOrderModel = null;
  719. if(!empty($data["o_id"]) && $data["status"]!=1){
  720. if($data["type"]=="activity"){
  721. $saveOrderModel = new ActivityOrderModel();
  722. }
  723. if($data["type"]=="pro" || $data["type"]=="ip"){
  724. $saveOrderModel = new OrderModel();
  725. }
  726. }
  727. if(!empty($saveOrderModel)){
  728. $orderData = $saveOrderModel->where("id",$data["o_id"])->find();
  729. if(!empty($orderData) && $orderData["status"]<1){
  730. $saveOrderModel->where("id",$data["o_id"])->update(["status"=>1,"pay_time"=>$save["pay_time"]]);
  731. }
  732. }
  733. return app("json")->success("交易支付成功",$returnData);
  734. }
  735. return app("json")->fail("支付状态未知");
  736. }
  737. return app("json")->fail("查询失败");
  738. }
  739. }