BindingController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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("Stream")]
  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("mobile", type: "int", require: true, desc: "手机号", mock: 10)]
  26. #[Apidoc\Param("type", type: "int", require: true, desc: "类型:1=支付宝,2=微信 ", mock: 2)]
  27. #[Apidoc\Param("img", type: "int", require: false, desc: "图片 ", 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. 'type' => Validator::notEmpty()->setName('类型'),
  37. 'img' => Validator::notEmpty()->setName('类型'),
  38. ]);
  39. if(!empty($param['img'])){
  40. $base64Image = $param['img']; // 获取Base64字符串
  41. $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image)); // 移除数据URL的前缀并解码
  42. $imageName = 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 = '/upload/img/' . date('Ymd') . '/' . $imageName; // 指定保存路径和文件名
  52. // 保存图片到服务器
  53. file_put_contents($base_dir.$imagePath, $imageData);
  54. }else{
  55. $imagePath = '';
  56. }
  57. $data = [
  58. 'user_id' => $param['user_data']['id'],
  59. 'account_number' => $param['account_number'],
  60. 'img' => $imagePath,
  61. 'mobile' => $param['user_data']['mobile'],
  62. 'type' => $param['type'],
  63. 'created_at' => date('Y-m-d H:i:s'),
  64. 'updated_at' => date('Y-m-d H:i:s'),
  65. ];
  66. $binding = Db::table('wa_binding')
  67. ->where('user_id', $param['user_data']['id'])
  68. ->where('type', $param['type'])
  69. ->first();
  70. if (empty($binding)) {
  71. Db::table('wa_binding')->insert($data);
  72. } else {
  73. Db::table('wa_binding')->where('id', $binding->id)->update($data);
  74. }
  75. } catch (\Throwable $exception) {
  76. Db::rollBack();
  77. return error($exception->getMessage());
  78. }
  79. Db::commit();
  80. return adminsuccess($data,'绑定成功');
  81. }
  82. #[Apidoc\Title("绑定支付宝,绑定微信")]
  83. #[Apidoc\Url("api/binding/binding_new.html")]
  84. #[Apidoc\Method("POST")]
  85. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  86. #[Apidoc\Param("wechat_binding", type: "string", require: true, desc: "微信绑定账号", mock: 10)]
  87. #[Apidoc\Param("alipay_binding", type: "string", require: true, desc: "支付宝绑定账号", mock: 10)]
  88. public function addbinding_new(Request $request)
  89. {
  90. $param = $request->param_data;
  91. $param['user_data'] = $request->user_data;
  92. Db::beginTransaction();
  93. try {
  94. Validator::input($param, [
  95. 'wechat_binding' => Validator::notEmpty()->setName('微信绑定账号'),
  96. 'alipay_binding' => Validator::notEmpty()->setName('支付宝绑定账号'),
  97. ]);
  98. Db::table('wa_users')
  99. ->where('id', $param['user_data']['id'])
  100. ->update([
  101. 'wechat_binding' => $param['wechat_binding'],
  102. 'alipay_binding' => $param['alipay_binding'],
  103. 'updated_at' => date('Y-m-d H:i:s')
  104. ]);
  105. } catch (\Throwable $exception) {
  106. Db::rollBack();
  107. return error($exception->getMessage());
  108. }
  109. Db::commit();
  110. return success([], '绑定成功');
  111. }
  112. #[Apidoc\Title("绑定记录详情")]
  113. #[Apidoc\Url("api/binding/binding_details.html")]
  114. #[Apidoc\Method("POST")]
  115. #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
  116. #[Apidoc\Param("type", type: "int", require: true, desc: "类型:类型:1=支付宝,2=微信", mock: 2)]
  117. #[Apidoc\Returned(name: "account_number", type: "string", require: true, desc: '账号', default: '0.00')]
  118. #[Apidoc\Returned(name: "mobile", type: "string", require: true, desc: '手机号', default: '0.00')]
  119. public function binding_details(Request $request)
  120. {
  121. $param = $request->param_data;
  122. $param['user_data'] = $request->user_data;
  123. Db::beginTransaction();
  124. try {
  125. Validator::input($param, [
  126. 'type' => Validator::notEmpty()->setName('类型'),
  127. ]);
  128. $bindingdata = Db::table('wa_binding')
  129. ->where('user_id', $param['user_data']['id'])
  130. ->where('type', $param['type'])
  131. ->first();
  132. if (empty($bindingdata)) {
  133. $data = [
  134. 'id' => '',
  135. 'account_number' => '',
  136. 'mobile' => '',
  137. 'img' => '',
  138. ];
  139. } else {
  140. $data = [
  141. 'id' => $bindingdata->id,
  142. 'account_number' => $bindingdata->account_number,
  143. 'mobile' => $bindingdata->mobile,
  144. 'img' => imageToBase64($bindingdata->img),
  145. ];
  146. }
  147. } catch (\Throwable $exception) {
  148. Db::rollBack();
  149. return error($exception->getMessage());
  150. }
  151. Db::commit();
  152. return success($data);
  153. }
  154. }