Agency.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\Area;
  5. use app\api\model\system\Message;
  6. use app\api\service\TencentCloudService;
  7. use app\common\controller\Backend;
  8. use fast\Random;
  9. use think\Db;
  10. use think\Exception;
  11. use think\exception\PDOException;
  12. use think\exception\ValidateException;
  13. use think\Validate;
  14. /**
  15. * 代理商申请
  16. *
  17. * @icon fa fa-circle-o
  18. */
  19. class Agency extends Backend
  20. {
  21. /**
  22. * Agency模型对象
  23. * @var \app\admin\model\Agency
  24. */
  25. protected $model = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\admin\model\Agency;
  30. $this->view->assign("statusList", $this->model->getStatusList());
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //当前是否为关联查询
  43. $this->relationSearch = false;
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags', 'trim']);
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $list = $this->model
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->visible(['id', 'name', 'mobile', 'id_card', 'description', 'license_images', 'status', 'updatetime']);
  58. }
  59. $result = array("total" => $list->total(), "rows" => $list->items());
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. public function add()
  65. {
  66. if (false === $this->request->isPost()) {
  67. return $this->view->fetch();
  68. }
  69. $params = $this->request->post('row/a');
  70. if (empty($params)) {
  71. $this->error(__('Parameter %s can not be empty', ''));
  72. }
  73. $params = $this->preExcludeFields($params);
  74. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. $result = false;
  78. Db::startTrans();
  79. try {
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  84. $this->model->validateFailException()->validate($validate);
  85. }
  86. $result = $this->model->allowField(true)->save($params);
  87. Db::commit();
  88. } catch (ValidateException | PDOException | Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result === false) {
  93. $this->error(__('No rows were inserted'));
  94. }
  95. $this->success();
  96. }
  97. public function check($id = null, $check = null)
  98. {
  99. if (!$id || !$check)
  100. $this->error("参数错误!");
  101. $agency = $this->model->where([
  102. "id" => $id,
  103. "status" => \E_MASSAGER_STATUS::Default,
  104. ])->find();
  105. if (!$agency)
  106. $this->error("申请记录不存在!");
  107. $is_pass = $check === "pass";
  108. Db::startTrans();
  109. try {
  110. $admin_id = null;
  111. if ($is_pass) {
  112. $save_admin = [
  113. "username" => $agency["name"],
  114. "nickname" => $agency["name"],
  115. "salt" => Random::alnum(),
  116. "avatar" => '/assets/img/avatar.png',
  117. "mobile" => $agency["mobile"],
  118. "createtime" => time(),
  119. "updatetime" => time(),
  120. "type" => \E_IDENTITY_TYPE::Agency,
  121. "profit_amount" => 0,
  122. "area_ids" => $agency["area_ids"]
  123. ];
  124. $save_admin['password'] = md5(md5("123456") . $save_admin['salt']);
  125. $areas = (new Area())->where("id", "in", explode(",", $agency["area_ids"]))->select();
  126. $city_codes = array_map(function ($data) {
  127. return $data["area_code"];
  128. }, $areas);
  129. $records = (new Admin())->checkByCityCodes($city_codes);
  130. if (count($records) > 0)
  131. throw new Exception("申请记录中管辖城市已经绑定了代理商,无法通过审核!");
  132. $save_admin["city_codes"] = join(",", $city_codes);
  133. $result = (new Admin())->validate('Admin.add')->allowField(true)->insert($save_admin, false, true);
  134. if (!$result) {
  135. exception($this->model->getError());
  136. }
  137. $admin_id = $result;
  138. model('AuthGroupAccess')->save([
  139. 'uid' => $result,
  140. 'group_id' => 7
  141. ]);
  142. }
  143. $this->model->update([
  144. "admin_id" => $admin_id,
  145. "updatetime" => time(),
  146. "status" => $is_pass ? 'allow' : 'reject'
  147. ], ["id" => $id]);
  148. Db::commit();
  149. } catch (Exception $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. }
  153. $valid = TencentCloudService::tencent_cloud_sms_send(TencentCloudService::$MASSAGE_APPLY, $agency["mobile"], [$is_pass ? "通过!" : "拒绝!"]);
  154. if ($valid->code()) {
  155. $this->success("审核成功!");
  156. }
  157. $this->error($valid->msg());
  158. }
  159. public function info($admin_id = null)
  160. {
  161. if (false === $this->request->isPost()) {
  162. $this->view->assign("row", $this->model->infoByAdminId($admin_id));
  163. return $this->view->fetch();
  164. }
  165. $params = $this->request->post('row/a');
  166. if (empty($params)) {
  167. $this->error(__('Parameter %s can not be empty', ''));
  168. }
  169. $params = $this->preExcludeFields($params);
  170. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  171. $params[$this->dataLimitField] = $this->auth->id;
  172. }
  173. $result = false;
  174. Db::startTrans();
  175. try {
  176. //是否采用模型验证
  177. if ($this->modelValidate) {
  178. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  179. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  180. $this->model->validateFailException()->validate($validate);
  181. }
  182. $result = $this->model->allowField(true)->save($params);
  183. Db::commit();
  184. } catch (ValidateException | PDOException | Exception $e) {
  185. Db::rollback();
  186. $this->error($e->getMessage());
  187. }
  188. if ($result === false) {
  189. $this->error(__('No rows were inserted'));
  190. }
  191. $this->success();
  192. }
  193. }