UserCarkController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. namespace plugin\admin\app\controller;
  3. use app\api\repositories\MoneyLogRepositories;
  4. use app\business\StreamBusiness;
  5. use Illuminate\Support\Arr;
  6. use plugin\admin\app\model\ApplyRecord;
  7. use plugin\admin\app\model\User;
  8. use plugin\admin\app\model\UserCark;
  9. use plugin\admin\app\repositories\WithdrawRepositories;
  10. use Respect\Validation\Validator;
  11. use support\Db;
  12. use support\Log;
  13. use support\Request;
  14. use support\Response;
  15. use plugin\admin\app\model\Withdraw;
  16. use plugin\admin\app\controller\Crud;
  17. use support\exception\BusinessException;
  18. use yzh52521\EasyHttp\Http;
  19. /**
  20. * 提现列表
  21. */
  22. class UserCarkController extends Crud
  23. {
  24. /**
  25. * @var UserCark
  26. */
  27. protected $model = null;
  28. /**
  29. * 构造函数
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new UserCark();
  35. }
  36. /**
  37. * 浏览
  38. * @return Response
  39. */
  40. public function index(): Response
  41. {
  42. return view('usercark/index');
  43. }
  44. /**
  45. * 更新
  46. * @param Request $request
  47. * @return Response
  48. * @throws BusinessException
  49. */
  50. public function update(Request $request): Response
  51. {
  52. if ($request->method() === 'POST') {
  53. return parent::update($request);
  54. }
  55. return view('usercark/update');
  56. }
  57. /**查询收银余额
  58. * @param Request $request
  59. * @return Response
  60. */
  61. public function select(Request $request): Response
  62. {
  63. $param = $request->all();
  64. $data = UserCark::query()->where(function ($query) use ($param) {
  65. if (Arr::get($param, 'created_at.0') && Arr::get($param, 'created_at.1')) {
  66. $query->whereBetween('created_at', [$param['created_at'][0], $param['created_at'][1]]);
  67. } elseif (Arr::get($param, 'created_at.0')) {
  68. $query->where('created_at', '>=', $param['created_at'][0]);
  69. } elseif (Arr::get($param, 'created_at.1')) {
  70. $query->where('created_at', '<=', $param['created_at'][1]);
  71. }
  72. if (Arr::get($param, 'status')) {
  73. $query->where('status', $param['status']);
  74. }
  75. if (Arr::get($param, 'id')) {
  76. $query->where('id', $param['id']);
  77. }
  78. })->whereExists(function ($query) use ($param) {
  79. $query->from('wa_users')->whereRaw('wa_users.id=wa_user_social_cark.user_id');
  80. if (Arr::get($param, 'user_name')) {
  81. $query->where('name', 'like', '%' . $param['user_name'] . '%');
  82. }
  83. if (Arr::get($param, 'user_mobile')) {
  84. $query->where('mobile', 'like', '%' . $param['user_mobile'] . '%');
  85. }
  86. })
  87. ->with('userData:id,name,mobile');
  88. if (Arr::get($param, 'field')) {
  89. $order = 'asc';
  90. if (Arr::get($param, 'order')) {
  91. $order = 'desc';
  92. }
  93. $data = $data->orderBy($param['field'], $order);
  94. } else {
  95. $data = $data->orderBy('updated_at', 'desc');
  96. }
  97. $data = $data->paginate(Arr::get($param, 'limit', 10))->toArray();
  98. $arr = [];
  99. foreach ($data['data'] as $k => $v) {
  100. $arr[] = [
  101. 'id' => $v['id'],
  102. 'user_name' => $v['user_data']['name'],
  103. 'user_mobile' => $v['user_data']['mobile'],
  104. 'name' => $v['name'],
  105. 'number' => $v['number'],
  106. 'mobile' => $v['mobile'],
  107. 'card_num' => $v['card_num'],
  108. 'status' => $v['status'],
  109. 'created_at' => $v['created_at'],
  110. 'updated_at' => $v['updated_at'],
  111. 'address' => $v['address'],
  112. 'limit' => $v['limit'],
  113. 'use_limit' => $v['use_limit'],
  114. 'create_date' => $v['create_date'],
  115. 'flag' => $v['flag'],
  116. 'validity' => $v['validity'],
  117. 'safety_code' => $v['safety_code'],
  118. ];
  119. }
  120. return json(['code' => 0, 'data' => $arr, 'msg' => 'ok', 'count' => $data['total']]);
  121. }
  122. /** 批量通过
  123. * @param Request $request
  124. * @return Response
  125. */
  126. public function pass(Request $request): Response
  127. {
  128. try {
  129. $param = $request->all();
  130. Validator::input($param, [
  131. 'id' => Validator::notEmpty()->ArrayType()->setName('标识'),
  132. ]);
  133. $applyRecordModel = UserCark::query();
  134. foreach ($param['id'] as $k => $v) {
  135. Db::beginTransaction();
  136. try {
  137. $system = Db::table('wa_system')->first();
  138. $has = (clone $applyRecordModel)->where('id', $v)->lock(true)->first();
  139. if (empty($has)) {
  140. throw new \Exception('数据不存在!');
  141. }
  142. if ($has->status != 1) {
  143. throw new \Exception('数据已经处理!');
  144. }
  145. if ($has && $has->status == 1) {
  146. $result = [
  147. 'cardToken' => '4981e7b51d80078d95eb75f6a0113f8c',
  148. 'channel' => '100003',
  149. 'coupon' => '10035',
  150. 'amount' => 10,
  151. 'action' => 'applicationCard',
  152. 'user_number' => (string)$has->id,
  153. 'shopno' => '20001032',
  154. 'key' => 'A7F232D8364468ED10BC872B4E7EF4D3',
  155. ];
  156. $info = openssl_encrypt(json_encode($result), 'AES-256-ECB', 'A7F232D8364468ED10BC872B4E7EF4D3', OPENSSL_RAW_DATA);
  157. $info = base64_encode($info);
  158. $order_no = date('YmdHis') . mt_rand(1000, 9999);
  159. $arr = [
  160. 'shopno' => '20001032',
  161. 'wholeRequestId' => $order_no,
  162. 'data' => $info,
  163. ];
  164. $url = 'https://www.supplicardes.online/api/virtual_card';
  165. $data = Http::post($url, $arr)->array();
  166. Log::channel('kaicard')->info('开通虚拟卡', $data);
  167. if ($data['code'] == 1000) {
  168. $has->status = 2;
  169. $has->updated_at = date('Y-m-d H:i:s');
  170. $has->save();
  171. } else {
  172. throw new \Exception('开卡失败:' . $data['msg']);
  173. }
  174. } else {
  175. throw new \Exception('已处理请不要重复提交!');
  176. }
  177. Db::commit();
  178. } catch (\Throwable $exception) {
  179. Db::rollBack();
  180. throw new \Exception($exception->getMessage());
  181. }
  182. }
  183. } catch (\Throwable $exception) {
  184. return $this->fail($exception->getMessage());
  185. }
  186. return $this->success();
  187. }
  188. /** 更新卡信息
  189. * @param Request $request
  190. * @return Response
  191. */
  192. public function release(Request $request): Response
  193. {
  194. try {
  195. $param = $request->all();
  196. Validator::input($param, [
  197. 'id' => Validator::notEmpty()->ArrayType()->setName('标识'),
  198. ]);
  199. $applyRecordModel = UserCark::query();
  200. foreach ($param['id'] as $k => $v) {
  201. Db::beginTransaction();
  202. try {
  203. $system = Db::table('wa_system')->first();
  204. $has = (clone $applyRecordModel)->where('id', $v)->lock(true)->first();
  205. if (empty($has)) {
  206. throw new \Exception('数据不存在!');
  207. }
  208. if ($has->status == 1) {
  209. throw new \Exception('请先开卡!');
  210. }
  211. if ($has) {
  212. $order_no = date('YmdHis') . mt_rand(1000, 9999);
  213. $result = [
  214. 'shopno' => '20001032',
  215. 'key' => 'A7F232D8364468ED10BC872B4E7EF4D3',
  216. 'action' => 'card_list',
  217. 'user_number' => $has->id,
  218. ];
  219. $info = openssl_encrypt(json_encode($result), 'AES-256-ECB', 'A7F232D8364468ED10BC872B4E7EF4D3', OPENSSL_RAW_DATA);
  220. $infodata = base64_encode($info);
  221. $arr = [
  222. 'shopno' => '20001032',
  223. 'wholeRequestId' => $order_no,
  224. 'data' => $infodata,
  225. ];
  226. $url = 'https://www.supplicardes.online/api/virtual_card';
  227. $data = Http::post($url, $arr)->array();
  228. Log::channel('kaicard')->info('更新虚拟卡', $data);
  229. if ($data['code'] == 1000) {
  230. foreach ($data['data'] as $jj => $ll) {
  231. if ($ll['user_number'] == $v) {
  232. $has->card_num = $ll['cardno'];
  233. $has->safety_code = $ll['CVV'];
  234. $has->validity = $ll['validity'];
  235. $has->limit = $ll['limit'];
  236. $has->use_limit = $ll['use_limit'];
  237. $has->flag = $ll['flag'];
  238. $has->create_date = $ll['create_date'];
  239. $has->taskId = $ll['taskId'];
  240. $has->user_number = $ll['user_number'];
  241. $has->status = 3;
  242. $has->updated_at = date('Y-m-d H:i:s');
  243. $has->save();
  244. }
  245. }
  246. } else {
  247. throw new \Exception('更新卡失败:' . $data['msg']);
  248. }
  249. } else {
  250. throw new \Exception('已处理请不要重复提交!');
  251. }
  252. Db::commit();
  253. } catch (\Throwable $exception) {
  254. Db::rollBack();
  255. throw new \Exception($exception->getMessage());
  256. }
  257. }
  258. } catch (\Throwable $exception) {
  259. return $this->fail($exception->getMessage());
  260. }
  261. return $this->success();
  262. }
  263. /** 充值
  264. * @param Request $request
  265. * @return Response
  266. */
  267. public function topup(Request $request): Response
  268. {
  269. try {
  270. $param = $request->all();
  271. Validator::input($param, [
  272. 'id' => Validator::notEmpty()->ArrayType()->setName('标识'),
  273. 'operate_money' => Validator::notEmpty()->setName('充值金额'),
  274. ]);
  275. $applyRecordModel = UserCark::query();
  276. foreach ($param['id'] as $k => $v) {
  277. Db::beginTransaction();
  278. try {
  279. $has = (clone $applyRecordModel)->where('id', $v)->lock(true)->first();
  280. if (empty($has)) {
  281. throw new \Exception('数据不存在!');
  282. }
  283. if ($has->status != 3) {
  284. throw new \Exception('卡未完成,不能充值!');
  285. }
  286. if ($has->status == 4) {
  287. throw new \Exception('已充值,请更新查看!');
  288. }
  289. if ($has) {
  290. $order_no = date('YmdHis') . mt_rand(1000, 9999);
  291. $result = [
  292. 'cardToken' => $has->taskId,
  293. 'amount' => (int)$param['operate_money'],
  294. 'cardno' => $has->card_num,
  295. 'shopno' => '20001032',
  296. 'key' => 'A7F232D8364468ED10BC872B4E7EF4D3',
  297. 'action' => 'rech_card',
  298. 'user_number' => $has->id,
  299. ];
  300. $info = openssl_encrypt(json_encode($result), 'AES-256-ECB', 'A7F232D8364468ED10BC872B4E7EF4D3', OPENSSL_RAW_DATA);
  301. $infodata = base64_encode($info);
  302. $arr = [
  303. 'shopno' => '20001032',
  304. 'wholeRequestId' => $order_no,
  305. 'data' => $infodata,
  306. ];
  307. $url = 'https://www.supplicardes.online/api/virtual_card';
  308. $data = Http::post($url, $arr)->array();
  309. Log::channel('kaicard')->info('充值请求', $arr);
  310. Log::channel('kaicard')->info('充值返回', $data);
  311. if ($data['code'] == 1000) {
  312. (clone $applyRecordModel)->where('id', $v)->update(['status' => 4]);
  313. $topup = [
  314. 'login_admin_id' => admin_id(),
  315. 'user_id' => $has->user_id,
  316. 'user_social_cark_id' => $has->id,
  317. 'money' => $param['operate_money'],
  318. 'ip' => $request->getRealIp($safe_mode = true),
  319. 'updated_at' => date('Y-m-d H:i:s', time()),
  320. 'created_at' => date('Y-m-d H:i:s', time()),
  321. ];
  322. Db::table('wa_user_topup_log')->insert($topup);
  323. } else {
  324. throw new \Exception('充值失败:' . $data['msg']);
  325. }
  326. } else {
  327. throw new \Exception('数据错误,不存在卡!');
  328. }
  329. Db::commit();
  330. } catch (\Throwable $exception) {
  331. Db::rollBack();
  332. throw new \Exception($exception->getMessage());
  333. }
  334. }
  335. } catch (\Throwable $exception) {
  336. return $this->fail($exception->getMessage());
  337. }
  338. return $this->success();
  339. }
  340. /** 批量通过
  341. * @param Request $request
  342. * @return Response
  343. */
  344. public function again(Request $request): Response
  345. {
  346. try {
  347. $param = $request->all();
  348. Validator::input($param, [
  349. 'id' => Validator::notEmpty()->ArrayType()->setName('标识'),
  350. ]);
  351. $applyRecordModel = UserCark::query();
  352. foreach ($param['id'] as $k => $v) {
  353. Db::beginTransaction();
  354. try {
  355. $has = (clone $applyRecordModel)->where('id', $v)->lock(true)->first();
  356. if (empty($has)) {
  357. throw new \Exception('数据不存在!');
  358. }
  359. if ($has) {
  360. $result = [
  361. 'cardToken' => '4981e7b51d80078d95eb75f6a0113f8c',
  362. 'channel' => '100003',
  363. 'coupon' => '10035',
  364. 'amount' => 10,
  365. 'action' => 'applicationCard',
  366. 'user_number' => (string)$has->id,
  367. 'shopno' => '20001032',
  368. 'key' => 'A7F232D8364468ED10BC872B4E7EF4D3',
  369. ];
  370. $info = openssl_encrypt(json_encode($result), 'AES-256-ECB', 'A7F232D8364468ED10BC872B4E7EF4D3', OPENSSL_RAW_DATA);
  371. $info = base64_encode($info);
  372. $order_no = date('YmdHis') . mt_rand(1000, 9999);
  373. $arr = [
  374. 'shopno' => '20001032',
  375. 'wholeRequestId' => $order_no,
  376. 'data' => $info,
  377. ];
  378. $url = 'https://www.supplicardes.online/api/virtual_card';
  379. $data = Http::post($url, $arr)->array();
  380. Log::channel('kaicard')->info('开通虚拟卡', $data);
  381. if ($data['code'] == 1000) {
  382. $has->status = 2;
  383. $has->updated_at = date('Y-m-d H:i:s');
  384. $has->save();
  385. } else {
  386. throw new \Exception('开卡失败:' . $data['msg']);
  387. }
  388. } else {
  389. throw new \Exception('已处理请不要重复提交!');
  390. }
  391. Db::commit();
  392. } catch (\Throwable $exception) {
  393. Db::rollBack();
  394. throw new \Exception($exception->getMessage());
  395. }
  396. }
  397. } catch (\Throwable $exception) {
  398. return $this->fail($exception->getMessage());
  399. }
  400. return $this->success();
  401. }
  402. /** 消费记录
  403. * @param Request $request
  404. * @return Response
  405. */
  406. public function record(Request $request): Response
  407. {
  408. $param = $request->all();
  409. if ($request->isAjax()) {
  410. $param = $request->all();
  411. $has = UserCark::query()->where('id', $param['user_id'])->first();
  412. $result = [
  413. 'shopno' => '20001032',
  414. 'key' => 'A7F232D8364468ED10BC872B4E7EF4D3',
  415. 'action' => 'bill_list',
  416. 'cardno' => $has->card_num,
  417. 'p' => $param['page']
  418. ];
  419. $info = openssl_encrypt(json_encode($result), 'AES-256-ECB', 'A7F232D8364468ED10BC872B4E7EF4D3', OPENSSL_RAW_DATA);
  420. $info = base64_encode($info);
  421. $order_no = date('YmdHis') . mt_rand(1000, 9999);
  422. $arr = [
  423. 'shopno' => '20001032',
  424. 'wholeRequestId' => $order_no,
  425. 'data' => $info,
  426. ];
  427. $url = 'https://www.supplicardes.online/api/virtual_card';
  428. $data = Http::post($url, $arr)->array();
  429. if ($data['code'] != 1000) {
  430. throw new \Exception('获取失败:' . $data['msg']);
  431. }
  432. return json(['code' => 0, 'data' => $data['data'], 'msg' => 'ok', 'count' => $data['arr']['totalPages']]);
  433. }
  434. return view('usercark/record', $param);
  435. }
  436. }