海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSettled\Controllers;
  3. use App\Admin\Repositories\Supplier;
  4. use App\Common\UserStatus;
  5. use App\Models\AdminSetting;
  6. use App\Models\SettledOrder;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Widgets\Alert;
  11. use EasyWeChat\Factory;
  12. use Illuminate\Support\Facades\Config;
  13. use Illuminate\Support\Facades\Storage;
  14. class SupplierController extends AdminController
  15. {
  16. protected $title = '供应商入驻';
  17. public function __construct()
  18. {
  19. admin_inject_section(Admin::SECTION['LEFT_SIDEBAR_MENU'], '');
  20. app('view')->prependNamespace('admin', resource_path('views/admin'));
  21. }
  22. protected function grid()
  23. {
  24. config::set('admin.helpers.enable', false);
  25. $tip = '你可能需要
  26. <a href="' . admin_url('supplier/create') . '" style="color:#ef5228;">供应商入驻</a>
  27. <a href="' . admin_url('agent/create') . '" style="color:#ef5228;">代理商入驻</a>
  28. <a href="' . admin_url('guide/create') . '" style="color:#ef5228;">地接入驻</a>';
  29. return Alert::make($tip, '页面不存在')->danger();
  30. }
  31. /**
  32. * Make a show builder.
  33. *
  34. * @param mixed $id
  35. */
  36. protected function detail($id)
  37. {
  38. config::set('admin.helpers.enable', false);
  39. return Alert::make('', '页面不存在')->danger();
  40. }
  41. /**
  42. * Make a form builder.
  43. *
  44. * @return Form
  45. */
  46. protected function form()
  47. {
  48. config::set('admin.helpers.enable', false);
  49. return Form::make(new Supplier(), function (Form $form) {
  50. $form->disableListButton();
  51. $form->disableViewCheck();
  52. $form->disableCreatingCheck();
  53. $form->disableEditingCheck();
  54. $form->disableDeleteButton();
  55. $form->text('username')->required();
  56. $form->password('password')->minLength(6)->required();
  57. $form->text('name')->required();
  58. $form->text('company_name')->required();
  59. $form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();
  60. $form->image('license')->required()->removable(false)->uniqueName();
  61. $form->text('address')->required();
  62. $form->image('business_license')->required()->removable(false)->uniqueName();
  63. $form->text('credit_codes')->required();
  64. $form->text('corporate_account')->required()->help('非常重要,该账号为默认提现账号');
  65. $form->text('deposit_bank')->required();
  66. $form->text('director')->required();
  67. $form->text('contact_phone')->required()->maxLength(13);
  68. $contract_template = Storage::disk('public')->url(AdminSetting::val('settled_supplier_contract'));
  69. $form->image('contract')->required()->uniqueName()
  70. ->help('请先 <a target="_blank" href="' . $contract_template . '" download="【易游天下】供应商入驻合同模板">下载入驻合同</a> 并打印,填写完毕之后盖好公章再上传。');
  71. $agreement_template = Storage::disk('public')->url(AdminSetting::val('settled_supplier_agreement'));
  72. $form->checkbox('agreement', '')->required()
  73. ->options([1 => '我已认真阅读并完全认同'])->customFormat(fn() => '1111111')
  74. ->script('$(function(){
  75. $(".field_agreement").parent().css("margin-right", 0).after(\'《<a target="_blank" href="'.$agreement_template.'" download="入驻协议">入驻协议</a>》\');
  76. });');
  77. Admin::js('@qrcode');
  78. })->saving(function (Form $form) {
  79. //禁止编辑
  80. if ($form->isEditing()) {
  81. return $form->response()->error('服务器出错了~~');
  82. }
  83. if (empty(array_filter($form->agreement))) {
  84. return $form->response()->error('你必须同意入驻协议才能入驻');
  85. }
  86. $form->deleteInput('agreement');
  87. //判断账号是否唯一
  88. if ($form->repository()->model()->where('username', $form->username)->exists()) {
  89. return $form->response()->error($form->username . ' 的账号已经存在');
  90. }
  91. //过滤null字段
  92. foreach($form->input() as $k => $v) {
  93. if (is_null($form->$k)) {
  94. $form->$k = '';
  95. }
  96. }
  97. //不允许编辑的字段
  98. $form->ignore(['id', 'remember_token', 'rate', 'created_at', 'created_at', 'deleted_at', 'deleted_at']);
  99. //处理特殊字段
  100. $form->hidden(['status', 'rate']);
  101. $form->status = UserStatus::UNAUDITED;
  102. $form->rate = 5;
  103. })->saved(function (Form $form) {
  104. $pay = (new SupplierController)->payConfig($form->repository()->model());
  105. if (empty($pay['code_url'])) {
  106. if (isset($pay['result_code'], $pay['err_code_des']) && $pay['result_code'] != 'SUCCESS') {
  107. $msg = $pay['err_code_des'];
  108. } else {
  109. $msg = $pay['return_msg'] ?? '获取支付信息失败';
  110. }
  111. $js = "Dcat.swal.info('支付:$msg', null);";
  112. } else {
  113. $ajax_url = admin_url('is_pay');
  114. $cost = AdminSetting::val('settled_supplier_cost');
  115. $js = <<<JS
  116. Dcat.swal.info(
  117. '<div style="margin-top:1rem;"><div id="qrcode"></div><p style="text-align:center;">入驻费:¥{$cost}元</p></div>',
  118. '<b style="color:red">请微信扫码支付,请勿关闭页面</b>',
  119. {
  120. type: null,
  121. imageWidth: 240,
  122. imageHeight: 240,
  123. animation: false,
  124. // confirmButtonText: '我已完成支付,刷新',
  125. showConfirmButton: false,
  126. allowOutsideClick: false,
  127. allowEscapeKey: false,
  128. onOpen: function () {
  129. $('#qrcode').qrcode({text:'{$pay["code_url"]}', width:240, height:240});
  130. if (window.timer) {
  131. clearInterval(window.timer);
  132. }
  133. window.timer = setInterval(function () {
  134. $.ajax({
  135. url: '$ajax_url',
  136. data: {
  137. username: '{$form->repository()->model()->username}'
  138. },
  139. success: function (res) {
  140. if (res == 1) {
  141. clearInterval(window.timer);
  142. Dcat.swal.success('支付成功,请联系平台审核!', null, {
  143. onClose: function () {
  144. window.location.reload();
  145. }
  146. }).then(() => {
  147. window.location.reload();
  148. });
  149. }
  150. }
  151. });
  152. }, 1000);
  153. }
  154. });
  155. JS;
  156. }
  157. return $form->response()->success('操作成功,请等待管理员审核')->script($js);
  158. })->deleting(function (Form $form) {
  159. return $form->response()->error('服务器出错了~~');
  160. });
  161. }
  162. //付款
  163. private function payConfig($supplier)
  164. {
  165. $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey', 'settled_supplier_cost']);
  166. $config = [
  167. 'app_id' => $setting['payee_appid'],
  168. 'mch_id' => $setting['payee_mchid'],
  169. 'key' => $setting['payee_mchkey'],
  170. 'notify_url' => route('wxpay_settled_notify'),
  171. ];
  172. $app = Factory::payment($config);
  173. //生成订单号
  174. list($micro, $sec) = explode(' ', microtime());
  175. $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
  176. $order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999);
  177. //保存订单记录
  178. SettledOrder::insertOrIgnore([
  179. 'order_no' => $order_no,
  180. 'user_type' => 1,
  181. 'username' => $supplier->username,
  182. 'money' => $setting['settled_supplier_cost'],
  183. 'status' => 0,
  184. 'created_at' => now(),
  185. 'updated_at' => now(),
  186. ]);
  187. return $app->order->unify([
  188. 'product_id' => $supplier->id,
  189. 'attach' => $supplier->username,
  190. 'body' => mb_strcut($supplier->company_name . ' 供应商入驻', 0, 127),
  191. 'out_trade_no' => $order_no,
  192. 'total_fee' => round($setting['settled_supplier_cost'] * 100), //支付金额单位为分
  193. 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
  194. ]);
  195. }
  196. //ajax回调,判断是否已支付
  197. public function isPay(): int
  198. {
  199. $username = request()->input('username');
  200. $order = SettledOrder::where(['username' => $username, 'user_type' => 1])->first();
  201. if (!$order) {
  202. return 0;
  203. }
  204. return (int)$order->status;
  205. }
  206. }