BindingController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\controller;
  3. use app\business\GoodsBusiness;
  4. use app\business\LoginBusiness;
  5. use app\business\PayorderBusiness;
  6. use app\business\WithdrawBusiness;
  7. use Illuminate\Support\Arr;
  8. use Respect\Validation\Validator;
  9. use support\Db;
  10. use support\Redis;
  11. use support\Request;
  12. use hg\apidoc\annotation as Apidoc;
  13. use Webman\Captcha\CaptchaBuilder;
  14. use Webman\Captcha\PhraseBuilder;
  15. // #[Apidoc\Title("绑定")]
  16. #[Apidoc\Group("Binding")]
  17. #[Apidoc\Sort(5)]
  18. class BindingController
  19. {
  20. #[Apidoc\Title("绑定支付宝,绑定微信,绑定数字人民币 扫码支付")]
  21. #[Apidoc\Url("api/binding.html")]
  22. #[Apidoc\Method("POST")]
  23. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  24. #[Apidoc\Param("account_number", type: "int", require: true, desc: "账号", mock: 10)]
  25. #[Apidoc\Param("name", type: "int", require: true, desc: "姓名", mock: 10)]
  26. #[Apidoc\Param("baseimg", type: "int", require: true, desc: "图片base", mock: 10)]
  27. #[Apidoc\Param("type", type: "int", require: true, desc: "类型:1=绑定支付宝,2=绑定微信 ", mock: 2)]
  28. public function addbinding(Request $request)
  29. {
  30. $param = $request->all();
  31. $param['user_data'] = $request->user_data;
  32. Db::beginTransaction();
  33. try {
  34. Validator::input($param, [
  35. 'account_number' => Validator::notEmpty()->setName('账号'),
  36. 'name' => Validator::notEmpty()->setName('姓名'),
  37. 'type' => Validator::notEmpty()->setName('类型'),
  38. ]);
  39. if (Arr::get($param, 'baseimg')) {
  40. $base64Image_one = $param['baseimg']; // 获取Base64字符串
  41. $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image_one)); // 移除数据URL的前缀并解码
  42. $imageName = $request->user_data['id'] . time() . '.png'; // 生成一个唯一的文件名
  43. $data = '/upload/img/' . date('Ymd');
  44. $relative_dir = ltrim($data, '\\/');
  45. $admin_public_path = rtrim(config('app.public_path', ''), '\\/');
  46. $base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
  47. $full_dir = $base_dir . $relative_dir;
  48. if (!is_dir($full_dir)) {
  49. mkdir($full_dir, 0777, true);
  50. }
  51. $imagePath_one = '/upload/img/' . date('Ymd') . '/' . $imageName; // 指定保存路径和文件名
  52. // 保存图片到服务器
  53. file_put_contents($base_dir . $imagePath_one, $imageData);
  54. } else {
  55. $imagePath_one = '';
  56. }
  57. $data = [
  58. 'user_id' => $param['user_data']['id'],
  59. 'account_number' => $param['account_number'],
  60. 'name' => $param['name'],
  61. 'type' => $param['type'],
  62. 'created_at' => date('Y-m-d H:i:s'),
  63. 'updated_at' => date('Y-m-d H:i:s'),
  64. ];
  65. $binding = Db::table('wa_binding')
  66. ->where('user_id', $param['user_data']['id'])
  67. ->where('type', $param['type'])->first();
  68. if (empty($binding)) {
  69. $data['img'] = $imagePath_one;
  70. Db::table('wa_binding')->insert($data);
  71. } else {
  72. if (empty($param['baseimg'])) {
  73. $data['img'] = $binding->img;
  74. } else {
  75. $data['img'] = $imagePath_one;
  76. }
  77. Db::table('wa_binding')->where('id', $binding->id)->update($data);
  78. }
  79. } catch (\Throwable $exception) {
  80. Db::rollBack();
  81. return error($exception->getMessage());
  82. }
  83. Db::commit();
  84. return success();
  85. }
  86. #[Apidoc\Title("绑定记录详情")]
  87. #[Apidoc\Url("api/binding/binding_details.html")]
  88. #[Apidoc\Method("POST")]
  89. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  90. #[Apidoc\Param("type", type: "int", require: true, desc: "类型:1=绑定支付宝,2=绑定微信 ", mock: 2)]
  91. #[Apidoc\Returned(name: "account_number", type: "string", require: true, desc: '账号', default: '0.00')]
  92. #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '姓名', default: '0.00')]
  93. #[Apidoc\Returned(name: "img", type: "string", require: true, desc: '图片', default: '0.00')]
  94. public function binding_details(Request $request)
  95. {
  96. $param = $request->param_data;
  97. $param['user_data'] = $request->user_data;
  98. Db::beginTransaction();
  99. try {
  100. Validator::input($param, [
  101. 'type' => Validator::notEmpty()->setName('类型'),
  102. ]);
  103. $bindingdata = Db::table('wa_binding')
  104. ->where('user_id', $param['user_data']['id'])
  105. ->where('type', $param['type'])
  106. ->first();
  107. if (empty($bindingdata)) {
  108. $data = [
  109. 'id' => '',
  110. 'account_number' => '',
  111. 'name' => '',
  112. 'img' => '',
  113. 'type' => '',
  114. ];
  115. } else {
  116. $data = [
  117. 'id' => $bindingdata->id,
  118. 'account_number' => $bindingdata->account_number,
  119. 'name' => $bindingdata->name,
  120. 'img' => imageToBase64($bindingdata->img),
  121. 'type' => $bindingdata->type,
  122. ];
  123. }
  124. } catch (\Throwable $exception) {
  125. Db::rollBack();
  126. return error($exception->getMessage());
  127. }
  128. Db::commit();
  129. return success($data);
  130. }
  131. }