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

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