UserCarkController.php 19 KB

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