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

242 lines
9.0 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
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(['miniUpload']), function (Grid $grid) {
  24. $grid->disableDeleteButton();
  25. $grid->disableRowSelector();
  26. //如果是审核页面,多加where条件判断
  27. if (strpos(Route::current()->uri, 'audit')) {
  28. $grid->model()->where('status', UserStatus::UNAUDITED);
  29. }
  30. $grid->model()->where('id', '>', 1); //隐藏ID=1的领峰云管理员
  31. $grid->column('id')->sortable();
  32. $grid->column('username');
  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('business_license')->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. ->using(UserStatus::array())
  44. ->dot([
  45. UserStatus::NORMAL => 'success',
  46. UserStatus::UNAUDITED => '',
  47. UserStatus::REFUSE => 'danger',
  48. UserStatus::DISABLED => 'warning',
  49. ], 'primary')
  50. ->if(fn() => $this->status == UserStatus::UNAUDITED)
  51. ->display('')
  52. ->then(function ($column) {
  53. $column->append((new AuditAgent(null, 1))->setKey($this->id))->append('&nbsp;');
  54. $column->append((new AuditAgent(null, 2))->setKey($this->id));
  55. });
  56. $grid->filter(function (Grid\Filter $filter) {
  57. $filter->panel();
  58. $filter->equal('id')->width(2);
  59. $filter->like('company_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('credit_codes');
  86. $show->field('logo')->image('', 80, 80);
  87. $show->field('address');
  88. $show->field('business_license')->image('', 80, 80);
  89. $show->field('director');
  90. $show->field('contact_phone');
  91. $show->field('legal_persona_name');
  92. $show->field('legal_persona_wechat');
  93. //$show->field('rate');
  94. $show->field('agentInfo.about', '关于我们')
  95. ->unescape()
  96. ->as(function ($v) {
  97. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->about);
  98. });
  99. $show->field('agentInfo.reg_protocol', '注册协议')
  100. ->unescape()
  101. ->as(function ($v) {
  102. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->reg_protocol);
  103. });
  104. $show->field('agentInfo.buy_protocol', '购买协议')
  105. ->unescape()
  106. ->as(function ($v) {
  107. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->buy_protocol);
  108. });
  109. $show->field('created_at');
  110. $show->field('updated_at');
  111. });
  112. }
  113. /**
  114. * Make a form builder.
  115. *
  116. * @return Form
  117. */
  118. protected function form()
  119. {
  120. return Form::make(new Agent('agentInfo'), function (Form $form) {
  121. $form->disableDeleteButton();
  122. $form->display('id');
  123. //新增
  124. if ($form->isCreating()) {
  125. $form->text('username')->required();
  126. $form->text('password')->required();
  127. }
  128. else if ($form->isEditing()) {
  129. $form->display('username');
  130. $form->text('password')->customFormat(fn() => '');
  131. }
  132. $form->text('name')->required();
  133. $form->display('appid')->help('无需填写,注册小程序后会自动生成');
  134. // $form->text('appsecret')->help('无需填写,注册小程序后会自动生成');
  135. // $form->text('mchid')->required();
  136. // $form->text('mchkey')->required();
  137. $form->select('status')
  138. ->default(UserStatus::NORMAL)
  139. ->options(UserStatus::array())
  140. ->required();
  141. $form->radio('type')
  142. ->options(AgentType::array())
  143. ->default(AgentType::OPERATOR)
  144. ->required();
  145. $form->text('company_name')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:海南易游天下供应链有限公司');
  146. $form->text('credit_codes')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:91440300577652919M');
  147. $form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();
  148. $form->image('logo')->removable(false)->uniqueName();
  149. $form->text('address');
  150. $form->image('business_license')->removable(false)->uniqueName();
  151. $form->text('director');
  152. $form->text('contact_phone');
  153. $form->text('legal_persona_name')->required()->maxLength(20)->help('很重要,注册小程序时需要用到,用于实名认证');
  154. $form->text('legal_persona_wechat')->required()->maxLength(100)->help('很重要,用于接收实名认证信息,不能是手机号或QQ号,微信号请前往微信“我的”页面查看');
  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. $allow_null = ['avatar', 'remember_token', 'appid', 'appsecret', 'mchid', 'mchkey'];
  176. foreach($form->input() as $k => $v) {
  177. if (is_null($v) && !in_array($k, $allow_null)) {
  178. $form->$k = '';
  179. } else if (is_array($v)) {
  180. foreach ($v as $k2 => &$v2) {
  181. if (is_null($v2)) {
  182. $v2 = ''; //不能用$form->$k[$k2] = '',会报错,只能通过引用修改
  183. } else {
  184. $v2 = preg_replace('/<script.*?>.*?<\/script>/is', '', $v2);
  185. }
  186. }
  187. }
  188. }
  189. })->saved(function (Form $form, $result) {
  190. //如果状态是正常,插入初始数据
  191. if ($result && $form->status == UserStatus::NORMAL) {
  192. (new AuditAgent)->setKey($form->getKey())->pass();
  193. //如果是供应商版旅行社,同时插入供应商会员表
  194. if ($form->type == AgentType::SUPPLIER) {
  195. if (Supplier::query()->where('username', $form->model()->username)->doesntExist()) {
  196. DB::beginTransaction();
  197. try {
  198. //插入用户表
  199. $supplier_id = Supplier::query()->insertGetId([
  200. 'username' => $form->model()->username,
  201. 'password' => $form->model()->password, //密码不用转换,原样插入
  202. 'name' => $form->model()->name,
  203. 'avatar' => $form->model()->avatar,
  204. 'status' => $form->model()->status,
  205. 'company_name' => $form->model()->company_name,
  206. 'logo' => $form->model()->logo,
  207. 'address' => $form->model()->address,
  208. 'business_license' => $form->model()->business_license,
  209. 'director' => $form->model()->director,
  210. 'contact_phone' => $form->model()->contact_phone,
  211. //'rate' => $form->model()->password,
  212. ]);
  213. //插入权限表
  214. DB::table(config('admin-supplier.database.role_users_table'))
  215. ->insertOrIgnore(['role_id' => 2, 'user_id' => $supplier_id]);
  216. DB::commit();
  217. } catch (\Exception $e) {
  218. DB::rollBack();
  219. return $form->response()->error('保存成功,但插入供应商会员失败,失败为:' . $e->getMessage());
  220. }
  221. }
  222. }
  223. }
  224. });
  225. }
  226. }