海南旅游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.

234 lines
7.8 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
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
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
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Extensions\Grid\AuditAgent;
  4. use App\Admin\Repositories\Agent;
  5. use App\Common\AgentType;
  6. use App\Common\UserStatus;
  7. use App\Models\Supplier;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Route;
  14. class AgentController extends AdminController
  15. {
  16. /**
  17. * Make a grid builder.
  18. *
  19. * @return Grid
  20. */
  21. protected function grid()
  22. {
  23. return Grid::make(new Agent(), function (Grid $grid) {
  24. $grid->disableDeleteButton();
  25. //如果是审核页面,多加where条件判断
  26. if (strpos(Route::current()->uri, 'audit')) {
  27. $grid->model()->where('status', UserStatus::UNAUDITED);
  28. }
  29. $grid->column('id')->sortable();
  30. $grid->column('username');
  31. $grid->column('name');
  32. $grid->column('type')->using(AgentType::array());
  33. $grid->column('company_name');
  34. $grid->column('logo')->image('', 60, 60);
  35. $grid->column('address');
  36. $grid->column('license_pic')->image('', 60, 60);
  37. $grid->column('director');
  38. $grid->column('contact_phone');
  39. $grid->column('rate')->editable()->help('分成百分比,如10%,则输入10');
  40. $grid->column('created_at');
  41. $grid->column('status', '状态')
  42. ->if(fn() => $this->status == UserStatus::UNAUDITED)
  43. ->display('')
  44. ->then(function ($column) {
  45. $column->append((new AuditAgent(null, 1))->setKey($this->id))->append('&nbsp;');
  46. $column->append((new AuditAgent(null, 2))->setKey($this->id));
  47. })
  48. ->else()
  49. ->using(UserStatus::array())
  50. ->dot([
  51. UserStatus::NORMAL => 'success',
  52. UserStatus::UNAUDITED => '',
  53. UserStatus::REFUSE => 'danger',
  54. UserStatus::DISABLED => 'warning',
  55. ], 'primary');
  56. $grid->filter(function ($filter) {
  57. $filter->panel();
  58. $filter->equal('id')->width(2);
  59. $filter->like('name')->width(3);
  60. $filter->equal('status', '用户状态')->select(UserStatus::array())->width(2);
  61. });
  62. });
  63. }
  64. /**
  65. * Make a show builder.
  66. *
  67. * @param mixed $id
  68. *
  69. * @return Show
  70. */
  71. protected function detail($id)
  72. {
  73. return Show::make($id, new Agent('agentInfo'), function (Show $show) {
  74. $show->disableDeleteButton();
  75. $show->field('id');
  76. $show->field('username');
  77. $show->field('name');
  78. $show->field('appid');
  79. $show->field('appsecret');
  80. $show->field('mchid');
  81. $show->field('mchkey');
  82. $show->field('status')->using(UserStatus::array());
  83. $show->field('type')->using(AgentType::array());
  84. $show->field('company_name');
  85. $show->field('logo')->image('', 80, 80);
  86. $show->field('address');
  87. $show->field('license_pic')->image('', 80, 80);
  88. $show->field('director');
  89. $show->field('contact_phone');
  90. $show->field('rate');
  91. $show->field('agentInfo.about', '关于我们')
  92. ->unescape()
  93. ->as(function ($v) {
  94. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->about);
  95. });
  96. $show->field('agentInfo.reg_protocol', '注册协议')
  97. ->unescape()
  98. ->as(function ($v) {
  99. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->reg_protocol);
  100. });
  101. $show->field('agentInfo.buy_protocol', '购买协议')
  102. ->unescape()
  103. ->as(function ($v) {
  104. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->buy_protocol);
  105. });
  106. $show->field('created_at');
  107. $show->field('updated_at');
  108. });
  109. }
  110. /**
  111. * Make a form builder.
  112. *
  113. * @return Form
  114. */
  115. protected function form()
  116. {
  117. return Form::make(new Agent('agentInfo'), function (Form $form) {
  118. $form->disableDeleteButton();
  119. $form->display('id');
  120. //新增
  121. if ($form->isCreating()) {
  122. $form->text('username')->required();
  123. $form->text('password')->required();
  124. }
  125. else if ($form->isEditing()) {
  126. $form->display('username');
  127. $form->text('password')->customFormat(fn() => '');
  128. }
  129. $form->text('name')->required();
  130. $form->text('appid')->required();
  131. $form->text('appsecret')->required();
  132. $form->text('mchid')->required();
  133. $form->text('mchkey')->required();
  134. $form->select('status')
  135. ->default(UserStatus::NORMAL)
  136. ->options(UserStatus::array())
  137. ->required();
  138. $form->radio('type')
  139. ->options(AgentType::array())
  140. ->default(AgentType::OPERATOR)
  141. ->required();
  142. $form->text('company_name');
  143. $form->image('logo')->removable(false)->uniqueName();
  144. $form->text('address');
  145. $form->image('license_pic')->removable(false)->uniqueName();
  146. $form->text('director');
  147. $form->text('contact_phone');
  148. $form->number('rate')->min(0)->max(100)->help('分成百分比,如10%,则输入10');
  149. $form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]);
  150. $form->editor('agentInfo.reg_protocol', '注册协议');
  151. $form->editor('agentInfo.buy_protocol', '购买协议');
  152. })->saving(function (Form $form) {
  153. //判断账号是否唯一
  154. if ($form->isCreating()) {
  155. if ($form->repository()->model()->where('username', $form->username)->exists()) {
  156. return $form->response()->error($form->username . ' 的账号已经存在');
  157. }
  158. }
  159. //分成比例
  160. if ($form->rate < 0 || $form->rate > 100) {
  161. return $form->response()->error('分成比例在 0 ~ 100 之间');
  162. }
  163. //不允许编辑的字段
  164. if ($form->isEditing()) {
  165. $form->ignore(['id', 'username', 'created_at', 'updated_at', 'deleted_at']);
  166. }
  167. //过滤null字段
  168. foreach($form->input() as $k => $v) {
  169. if (is_null($v)) {
  170. $form->$k = '';
  171. } else if (is_array($v)) {
  172. foreach ($v as $k2 => &$v2) {
  173. if (is_null($v2)) {
  174. $v2 = ''; //不能用$form->$k[$k2] = '',会报错,只能通过引用修改
  175. } else {
  176. $v2 = preg_replace('/<script.*?>.*?<\/script>/is', '', $v2);
  177. }
  178. }
  179. }
  180. }
  181. })->saved(function (Form $form, $result) {
  182. //如果状态是正常,插入初始数据
  183. if ($result && $form->status == UserStatus::NORMAL) {
  184. (new AuditAgent)->setKey($form->getKey())->pass();
  185. //如果是供应商版旅行社,同时插入供应商会员表
  186. if ($form->type == AgentType::SUPPLIER) {
  187. if (Supplier::query()->where('username', $form->model()->username)->doesntExist()) {
  188. DB::beginTransaction();
  189. try {
  190. //插入用户表
  191. $supplier_id = Supplier::query()->insertGetId([
  192. 'username' => $form->model()->username,
  193. 'password' => $form->model()->password, //密码不用转换,原样插入
  194. 'name' => $form->model()->name,
  195. 'avatar' => $form->model()->avatar,
  196. 'status' => $form->model()->status,
  197. 'company_name' => $form->model()->company_name,
  198. 'logo' => $form->model()->logo,
  199. 'address' => $form->model()->address,
  200. 'license_pic' => $form->model()->license_pic,
  201. 'director' => $form->model()->director,
  202. 'contact_phone' => $form->model()->contact_phone,
  203. 'rate' => $form->model()->password,
  204. ]);
  205. //插入权限表
  206. DB::table(config('admin-supplier.database.role_users_table'))
  207. ->insertOrIgnore(['role_id' => 2, 'user_id' => $supplier_id]);
  208. DB::commit();
  209. } catch (\Exception $e) {
  210. DB::rollBack();
  211. return $form->response()->error('保存成功,但插入供应商会员失败,失败为:' . $e->getMessage());
  212. }
  213. }
  214. }
  215. }
  216. });
  217. }
  218. }