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

243 lines
9.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 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('name');
  34. $grid->column('type')->using(AgentType::array());
  35. $grid->column('company_name');
  36. $grid->column('logo')->image('', 60, 60);
  37. $grid->column('address');
  38. $grid->column('license_pic')->image('', 60, 60);
  39. $grid->column('director');
  40. $grid->column('contact_phone');
  41. //$grid->column('rate')->editable()->help('分成百分比,如10%,则输入10');
  42. $grid->column('created_at');
  43. $grid->column('status', '状态')
  44. ->using(UserStatus::array())
  45. ->dot([
  46. UserStatus::NORMAL => 'success',
  47. UserStatus::UNAUDITED => '',
  48. UserStatus::REFUSE => 'danger',
  49. UserStatus::DISABLED => 'warning',
  50. ], 'primary')
  51. ->if(fn() => $this->status == UserStatus::UNAUDITED)
  52. ->display('')
  53. ->then(function ($column) {
  54. $column->append((new AuditAgent(null, 1))->setKey($this->id))->append('&nbsp;');
  55. $column->append((new AuditAgent(null, 2))->setKey($this->id));
  56. });
  57. $grid->filter(function (Grid\Filter $filter) {
  58. $filter->panel();
  59. $filter->equal('id')->width(2);
  60. $filter->like('name')->width(3);
  61. $filter->equal('status', '用户状态')->select(UserStatus::array())->width(2);
  62. });
  63. });
  64. }
  65. /**
  66. * Make a show builder.
  67. *
  68. * @param mixed $id
  69. *
  70. * @return Show
  71. */
  72. protected function detail($id)
  73. {
  74. return Show::make($id, new Agent('agentInfo'), function (Show $show) {
  75. $show->disableDeleteButton();
  76. $show->field('id');
  77. $show->field('username');
  78. $show->field('name');
  79. $show->field('appid');
  80. // $show->field('appsecret');
  81. // $show->field('mchid');
  82. // $show->field('mchkey');
  83. $show->field('status')->using(UserStatus::array());
  84. $show->field('type')->using(AgentType::array());
  85. $show->field('company_name');
  86. $show->field('credit_codes');
  87. $show->field('logo')->image('', 80, 80);
  88. $show->field('address');
  89. $show->field('license_pic')->image('', 80, 80);
  90. $show->field('director');
  91. $show->field('contact_phone');
  92. $show->field('legal_persona_name');
  93. $show->field('legal_persona_wechat');
  94. //$show->field('rate');
  95. $show->field('agentInfo.about', '关于我们')
  96. ->unescape()
  97. ->as(function ($v) {
  98. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->about);
  99. });
  100. $show->field('agentInfo.reg_protocol', '注册协议')
  101. ->unescape()
  102. ->as(function ($v) {
  103. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->reg_protocol);
  104. });
  105. $show->field('agentInfo.buy_protocol', '购买协议')
  106. ->unescape()
  107. ->as(function ($v) {
  108. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->buy_protocol);
  109. });
  110. $show->field('created_at');
  111. $show->field('updated_at');
  112. });
  113. }
  114. /**
  115. * Make a form builder.
  116. *
  117. * @return Form
  118. */
  119. protected function form()
  120. {
  121. return Form::make(new Agent('agentInfo'), function (Form $form) {
  122. $form->disableDeleteButton();
  123. $form->display('id');
  124. //新增
  125. if ($form->isCreating()) {
  126. $form->text('username')->required();
  127. $form->text('password')->required();
  128. }
  129. else if ($form->isEditing()) {
  130. $form->display('username');
  131. $form->text('password')->customFormat(fn() => '');
  132. }
  133. $form->text('name')->required();
  134. $form->display('appid')->help('无需填写,注册小程序后会自动生成');
  135. // $form->text('appsecret')->help('无需填写,注册小程序后会自动生成');
  136. // $form->text('mchid')->required();
  137. // $form->text('mchkey')->required();
  138. $form->select('status')
  139. ->default(UserStatus::NORMAL)
  140. ->options(UserStatus::array())
  141. ->required();
  142. $form->radio('type')
  143. ->options(AgentType::array())
  144. ->default(AgentType::OPERATOR)
  145. ->required();
  146. $form->text('company_name')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:海南易游天下供应链有限公司');
  147. $form->text('credit_codes')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:91440300577652919M');
  148. $form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();
  149. $form->image('logo')->removable(false)->uniqueName();
  150. $form->text('address');
  151. $form->image('license_pic')->removable(false)->uniqueName();
  152. $form->text('director');
  153. $form->text('contact_phone');
  154. $form->text('legal_persona_name')->required()->maxLength(20)->help('很重要,注册小程序时需要用到,用于实名认证');
  155. $form->text('legal_persona_wechat')->required()->maxLength(100)->help('很重要,用于接收实名认证信息,不能是手机号或QQ号,微信号请前往微信“我的”页面查看');
  156. //$form->number('rate')->min(0)->max(100)->help('分成百分比,如10%,则输入10');
  157. $form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]);
  158. $form->editor('agentInfo.reg_protocol', '注册协议');
  159. $form->editor('agentInfo.buy_protocol', '购买协议');
  160. })->saving(function (Form $form) {
  161. //判断账号是否唯一
  162. if ($form->isCreating()) {
  163. if ($form->repository()->model()->where('username', $form->username)->exists()) {
  164. return $form->response()->error($form->username . ' 的账号已经存在');
  165. }
  166. }
  167. ////抽成比例
  168. //if ($form->rate < 0 || $form->rate > 100) {
  169. // return $form->response()->error('抽成比例在 0 ~ 100 之间');
  170. //}
  171. //不允许编辑的字段
  172. if ($form->isEditing()) {
  173. $form->ignore(['id', 'username', 'created_at', 'updated_at', 'deleted_at']);
  174. }
  175. //过滤null字段
  176. $allow_null = ['avatar', 'remember_token', 'appid', 'appsecret', 'mchid', 'mchkey'];
  177. foreach($form->input() as $k => $v) {
  178. if (is_null($v) && !in_array($k, $allow_null)) {
  179. $form->$k = '';
  180. } else if (is_array($v)) {
  181. foreach ($v as $k2 => &$v2) {
  182. if (is_null($v2)) {
  183. $v2 = ''; //不能用$form->$k[$k2] = '',会报错,只能通过引用修改
  184. } else {
  185. $v2 = preg_replace('/<script.*?>.*?<\/script>/is', '', $v2);
  186. }
  187. }
  188. }
  189. }
  190. })->saved(function (Form $form, $result) {
  191. //如果状态是正常,插入初始数据
  192. if ($result && $form->status == UserStatus::NORMAL) {
  193. (new AuditAgent)->setKey($form->getKey())->pass();
  194. //如果是供应商版旅行社,同时插入供应商会员表
  195. if ($form->type == AgentType::SUPPLIER) {
  196. if (Supplier::query()->where('username', $form->model()->username)->doesntExist()) {
  197. DB::beginTransaction();
  198. try {
  199. //插入用户表
  200. $supplier_id = Supplier::query()->insertGetId([
  201. 'username' => $form->model()->username,
  202. 'password' => $form->model()->password, //密码不用转换,原样插入
  203. 'name' => $form->model()->name,
  204. 'avatar' => $form->model()->avatar,
  205. 'status' => $form->model()->status,
  206. 'company_name' => $form->model()->company_name,
  207. 'logo' => $form->model()->logo,
  208. 'address' => $form->model()->address,
  209. 'license_pic' => $form->model()->license_pic,
  210. 'director' => $form->model()->director,
  211. 'contact_phone' => $form->model()->contact_phone,
  212. //'rate' => $form->model()->password,
  213. ]);
  214. //插入权限表
  215. DB::table(config('admin-supplier.database.role_users_table'))
  216. ->insertOrIgnore(['role_id' => 2, 'user_id' => $supplier_id]);
  217. DB::commit();
  218. } catch (\Exception $e) {
  219. DB::rollBack();
  220. return $form->response()->error('保存成功,但插入供应商会员失败,失败为:' . $e->getMessage());
  221. }
  222. }
  223. }
  224. }
  225. });
  226. }
  227. }