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

264 lines
10 KiB

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