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

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