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

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