UserCarkController.php 19 KB

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