UserEnter.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\merchant;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\special\Lecturer;
  14. use app\admin\model\special\Lecturer as LecturerModel;
  15. use app\admin\model\merchant\UserEnter as UserEnterModel;
  16. use app\admin\model\merchant\Merchant as MerchantModel;
  17. use app\admin\model\merchant\MerchantAdmin as MerchantAdminModel;
  18. use app\merchant\model\merchant\MerchantMenus;
  19. use app\admin\model\user\User;
  20. use service\JsonService;
  21. use service\FormBuilder as Form;
  22. use think\Url;
  23. /**
  24. * 讲师申请控制器
  25. */
  26. class UserEnter extends AuthController
  27. {
  28. /**
  29. * 讲师申请列表展示
  30. * @return
  31. * */
  32. public function index()
  33. {
  34. return $this->fetch();
  35. }
  36. /**
  37. * 讲师申请列表获取
  38. * @return
  39. * */
  40. public function lecturer_enter_list()
  41. {
  42. $where = parent::getMore([
  43. ['page', 1],
  44. ['limit', 20],
  45. ['title', ''],
  46. ]);
  47. return JsonService::successlayui(UserEnterModel::getLecturerList($where));
  48. }
  49. /**
  50. * 删除讲师申请
  51. * @param int $id 修改的主键
  52. * @return json
  53. * */
  54. public function delete($id = 0)
  55. {
  56. if (!$id) return JsonService::fail('缺少参数');
  57. if (UserEnterModel::delLecturer($id))
  58. return JsonService::successful('删除成功');
  59. else
  60. return JsonService::fail(UserEnterModel::getErrorInfo('删除失败'));
  61. }
  62. /**查看申请
  63. * @param int $id
  64. * @return mixed
  65. */
  66. public function see($id = 0)
  67. {
  68. $this->assign(['id' => $id]);
  69. return $this->fetch();
  70. }
  71. /**申请数据
  72. * @param $id
  73. * @throws \think\exception\DbException
  74. */
  75. public function getUserEnter($id)
  76. {
  77. $data = UserEnterModel::get($id);
  78. $data['address'] = $data['province'] . $data['city'] . $data['district'] . $data['address'];
  79. $data['label'] = json_decode($data['label']);
  80. if ($data['charter']) {
  81. $data['charter'] = json_decode($data['charter']);
  82. } else {
  83. $data['charter'] = [];
  84. }
  85. return JsonService::successful($data);
  86. }
  87. /**不通过
  88. * @param $id
  89. * @throws \think\exception\DbException
  90. */
  91. public function fail($id)
  92. {
  93. $fail_msg = parent::postMore([
  94. ['message', ''],
  95. ]);
  96. if (!UserEnterModel::be(['id' => $id, 'status' => 0])) return JsonService::fail('操作记录不存在或状态错误!');
  97. $enter = UserEnterModel::get($id);
  98. if (!$enter) return JsonService::fail('操作记录不存!');
  99. if ($enter->status != 0) return Json::fail('您已审核,请勿重复操作');
  100. UserEnterModel::beginTrans();
  101. $res = UserEnterModel::changeFail($id, $enter['uid'], $fail_msg['message'], $enter);
  102. if ($res) {
  103. UserEnterModel::commitTrans();
  104. return JsonService::successful('操作成功!');
  105. } else {
  106. UserEnterModel::rollbackTrans();
  107. return JsonService::fail('操作失败!');
  108. }
  109. }
  110. /**通过
  111. * @param $id
  112. * @throws \think\exception\DbException
  113. */
  114. public function succ($id)
  115. {
  116. if (!UserEnterModel::be(['id' => $id, 'status' => 0])) return JsonService::fail('操作记录不存在或状态错误!');
  117. $enter = UserEnterModel::get($id);
  118. if (!$enter) return JsonService::fail('操作记录不存!');
  119. if ($enter->status != 0) return Json::fail('您已审核,请勿重复操作');
  120. UserEnterModel::beginTrans();
  121. $res = UserEnterModel::changeSuccess($id, $enter['uid'], $enter);
  122. if ($res) {
  123. UserEnterModel::commitTrans();
  124. return JsonService::successful('操作成功!');
  125. } else {
  126. UserEnterModel::rollbackTrans();
  127. return JsonService::fail('操作失败!');
  128. }
  129. }
  130. /**生成讲师后台
  131. * @return mixed
  132. */
  133. public function create($id)
  134. {
  135. $enter = UserEnterModel::get($id);
  136. $this->assign([
  137. 'title' => '添加讲师后台',
  138. 'enter' => json_encode($enter),
  139. 'action' => Url::build('save'),
  140. 'menus' => json(MerchantMenus::ruleList())->getContent()
  141. ]);
  142. return $this->fetch();
  143. }
  144. /**
  145. * 添加讲师商户
  146. */
  147. public function save()
  148. {
  149. $data = parent::postMore([
  150. 'account',
  151. ['id', 0],
  152. ['uid', 0],
  153. 'conf_pwd',
  154. 'pwd',
  155. 'mer_name',
  156. 'real_name',
  157. 'mer_phone',
  158. 'mer_special_divide',
  159. 'mer_store_divide',
  160. 'mer_event_divide',
  161. 'mer_data_divide',
  162. 'mer_test_divide',
  163. 'gold_divide',
  164. 'mark',
  165. 'mer_address',
  166. ['checked_menus', [], '', 'rules'],
  167. ['is_source', 0],
  168. ['is_audit', 0],
  169. ['status', 0]
  170. ]);
  171. if (!is_array($data['rules']) || !count($data['rules'])) return JsonService::fail('请选择最少一个权限');
  172. foreach ($data['rules'] as &$v) {
  173. $pid = MerchantMenus::where('id', $v)->value('pid');
  174. if (!in_array($pid, $data['rules'])) $data['rules'][] = $pid;
  175. }
  176. $data['rules'] = implode(',', $data['rules']);
  177. if (!$data['account']) return JsonService::fail('请输入讲师后台账号');
  178. if (MerchantAdminModel::where('account', trim($data['account']))->where('is_del', 0)->count()) return JsonService::fail('商户账号已存在,请使用别的商户账号注册');
  179. if (!$data['pwd']) return JsonService::fail('请输入讲师后台登陆密码');
  180. if ($data['pwd'] != $data['conf_pwd']) return JsonService::fail('两次输入密码不想同');
  181. if (!$data['mer_name']) return JsonService::fail('请输入讲师后台名称');
  182. if (!$data['uid']) return JsonService::fail('请输入绑定的用户ID');
  183. $user = User::where('uid', $data['uid'])->find();
  184. if (!$user) {
  185. return JsonService::fail('绑定的用户不存在');
  186. } else {
  187. if ($user['business'] == 1) {
  188. return JsonService::fail('该用户已是讲师');
  189. }
  190. }
  191. $data['pwd'] = trim(md5($data['pwd']));
  192. $data['reg_time'] = time();
  193. $data['add_time'] = time();
  194. $data['reg_admin_id'] = $this->adminId;
  195. $admin = array();
  196. $admin['account'] = trim($data['account']);
  197. $admin['pwd'] = $data['pwd'];
  198. $enter = UserEnterModel::get($data['id']);
  199. $data['mer_avatar'] = $enter['merchant_head'];
  200. $data['estate'] = 1;
  201. UserEnterModel::where('id', $data['id'])->update(['status' => 2]);
  202. unset($data['id']);
  203. unset($data['conf_pwd']);
  204. unset($data['account']);
  205. unset($data['pwd']);
  206. MerchantModel::beginTrans();
  207. $res = MerchantModel::set($data);
  208. $res1 = false;
  209. if ($res) {
  210. $admin['uid'] = $data['uid'];
  211. $admin['mer_id'] = $res->id;
  212. $admin['real_name'] = $data['mer_name'];
  213. $admin['rules'] = $data['rules'];
  214. $admin['phone'] = $data['mer_phone'];
  215. $admin['add_time'] = time();
  216. $admin['status'] = 1;
  217. $admin['level'] = 0;
  218. $res1 = MerchantAdminModel::set($admin);
  219. }
  220. $res2 = false;
  221. if ($res1 && $res) {
  222. $res2 = Lecturer::addLecturer($enter, $res->id);
  223. }
  224. $bool = false;
  225. if ($res1 && $res && $res2) $bool = true;
  226. MerchantModel::checkTrans($bool);
  227. if ($bool) {
  228. MerchantModel::where('id', $res->id)->update(['lecturer_id' => $res2->id]);
  229. User::where('uid', $data['uid'])->update(['business' => 1]);
  230. return JsonService::successful('添加讲师后台成功!');
  231. } else {
  232. return JsonService::successful('添加讲师后台失败!');
  233. }
  234. }
  235. }