UserLevel.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace app\adminapi\controller\v1\user;
  3. use app\adminapi\controller\AuthController;
  4. use Exception;
  5. use FormBuilder\exception\FormBuilderException;
  6. use app\models\system\{SystemUserLevel, SystemUserTask};
  7. use crmeb\services\{FormBuilder as Form, UtilService};
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\facade\Route as Url;
  12. use crmeb\traits\CurdControllerTrait;
  13. /**
  14. * 会员设置
  15. * Class UserLevel
  16. * @package app\adminapi\controller\v1\user
  17. */
  18. class UserLevel extends AuthController
  19. {
  20. use CurdControllerTrait;
  21. /**
  22. * 会员详情
  23. * @param $id
  24. * @return mixed
  25. * @throws DataNotFoundException
  26. * @throws DbException
  27. * @throws ModelNotFoundException
  28. */
  29. public function read($id)
  30. {
  31. $info = SystemUserLevel::merSet($this->merId)->where('id', $id)->find();
  32. return $this->success(compact('info'));
  33. }
  34. /**
  35. * 获取添加资源表单
  36. *
  37. * @throws FormBuilderException
  38. * @throws Exception
  39. */
  40. public function create()
  41. {
  42. $where = UtilService::getMore(
  43. ['id', 0]
  44. );
  45. if ($where['id']) {
  46. $vipinfo = SystemUserLevel::merSet($this->merId)->where('id', $where['id'])->find();
  47. if (!$vipinfo) {
  48. return $this->fail('不存在等级');
  49. }
  50. $msg = '编辑会员等级';
  51. } else {
  52. $msg = '添加会员等级';
  53. }
  54. $field[] = Form::input('name', '等级名称', isset($vipinfo) ? $vipinfo->name : '')->col(Form::col(24));
  55. $field[] = Form::radio('is_forever', '是否为永久', isset($vipinfo) ? $vipinfo->is_forever : 0)->options([['label' => '永久', 'value' => 1], ['label' => '非永久', 'value' => 0]])->col(24);
  56. // $field[] = Form::number('money', '等级价格', isset($vipinfo) ? $vipinfo->money : 0)->min(0)->col(24);
  57. // $field[] = Form::radio('is_pay', '是否需要购买', isset($vipinfo) ? $vipinfo->is_pay : 0)->options([['label' => '需要', 'value' => 1], ['label' => '免费', 'value' => 0]])->col(24);
  58. $field[] = Form::number('valid_date', '有效时间(天)', isset($vipinfo) ? $vipinfo->valid_date : 0)->min(0)->col(8);
  59. $field[] = Form::number('grade', '等级', isset($vipinfo) ? $vipinfo->grade : 0)->min(0)->col(8);
  60. $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 0)->min(0)->col(8);
  61. $field[] = Form::frameImageOne('icon', '图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'icon')), isset($vipinfo) ? $vipinfo->icon : '')->icon('ios-add')->width('60%')->height('435px');
  62. $field[] = Form::frameImageOne('image', '会员背景', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), isset($vipinfo) ? $vipinfo->image : '')->icon('ios-add')->width('60%')->height('435px');
  63. $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  64. $field[] = Form::radio('show_commission', '是否显示佣金', isset($vipinfo) ? $vipinfo->show_commission : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  65. $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '');
  66. if ($where['id']) $field[] = Form::hidden('id', $where['id']);
  67. return $this->makePostForm($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
  68. }
  69. /**
  70. * 会员等级添加或者修改
  71. * @return mixed
  72. *
  73. * @throws Exception
  74. */
  75. public function save()
  76. {
  77. $data = UtilService::postMore([
  78. ['id', 0],
  79. ['name', ''],
  80. ['is_forever', 0],
  81. ['money', 0],
  82. ['is_pay', 0],
  83. ['valid_date', 0],
  84. ['grade', 0],
  85. ['discount', 0],
  86. ['icon', ''],
  87. ['image', ''],
  88. ['is_show', ''],
  89. ['show_commission', 1],
  90. ['explain', ''],
  91. ]);
  92. $id = $data['id'];
  93. $data['mer_id'] = $this->merId ?: '';
  94. if (!$data['name']) return $this->fail('请输入等级名称');
  95. if (!$data['grade']) return $this->fail('请输入等级');
  96. if (!$data['explain']) return $this->fail('请输入等级说明');
  97. if ($data['is_forever'] == 0 && !$data['valid_date']) return $this->fail('请输入有效时间(天)');
  98. // if ($data['is_pay']) return $this->fail('会员等级购买功能正在开发中,暂时关闭可购买功能!');
  99. // if ($data['is_pay'] && !$data['money']) return $this->fail('请输入购买金额');
  100. if (!$data['icon']) return $this->fail('请上传等级图标');
  101. if (!$data['image']) return $this->fail('请上传等级背景图标');
  102. if (!$id && SystemUserLevel::be(['mer_id' => $data['mer_id'], 'is_del' => 0, 'grade' => $data['grade']])) return $this->fail('已检测到您设置过的会员等级,此等级不可重复');
  103. SystemUserLevel::beginTrans();
  104. try {
  105. //修改
  106. if ($id) {
  107. if (SystemUserLevel::merSet($this->merId)->where('id', $id)->find())
  108. if (SystemUserLevel::edit($data, $id)) {
  109. SystemUserLevel::commitTrans();
  110. return $this->success('修改成功');
  111. } else {
  112. SystemUserLevel::rollbackTrans();
  113. return $this->fail('修改失败');
  114. }
  115. else return $this->fail('不存在等级');
  116. } else {
  117. //新增
  118. $data['add_time'] = time();
  119. if (SystemUserLevel::create($data)) {
  120. SystemUserLevel::commitTrans();
  121. return $this->success('添加成功');
  122. } else {
  123. SystemUserLevel::rollbackTrans();
  124. return $this->fail('添加失败');
  125. }
  126. }
  127. } catch (Exception $e) {
  128. SystemUserLevel::rollbackTrans();
  129. return $this->fail($e->getMessage());
  130. }
  131. }
  132. /**
  133. * 获取系统设置的vip列表
  134. * @param int page
  135. * @param int limit
  136. *
  137. * @return mixed
  138. * @throws Exception
  139. */
  140. public function get_system_vip_list()
  141. {
  142. $where = UtilService::getMore([
  143. ['page', 0],
  144. ['limit', 10],
  145. ['title', ''],
  146. ['is_show', ''],
  147. ]);
  148. $where['mer_id'] = $this->merId;
  149. return $this->success(SystemUserLevel::getSytemList($where));
  150. }
  151. /**
  152. * 删除会员等级
  153. * @param int $id
  154. *
  155. * @return
  156. * @throws DataNotFoundException
  157. * @throws DbException
  158. * @throws ModelNotFoundException
  159. */
  160. public function delete($id)
  161. {
  162. if (!$id || !SystemUserLevel::merSet($this->merId)->where('id', $id)->find()) {
  163. return $this->fail('参数错误');
  164. }
  165. if (SystemUserLevel::edit(['is_del' => 1], $id))
  166. return $this->success('删除成功');
  167. else
  168. return $this->fail('删除失败');
  169. }
  170. /**
  171. * 设置会员等级显示|隐藏
  172. *
  173. * @param string $is_show
  174. * @param string $id
  175. * @return mixed
  176. */
  177. public function set_show($is_show = '', $id = '')
  178. {
  179. if ($is_show == '' || $id == '') return $this->fail('缺少参数');
  180. $res = SystemUserLevel::merSet($this->merId)->where(['id' => $id])->update(['is_show' => (int)$is_show]);
  181. if ($res) {
  182. return $this->success($is_show == 1 ? '显示成功' : '隐藏成功');
  183. } else {
  184. return $this->fail($is_show == 1 ? '显示失败' : '隐藏失败');
  185. }
  186. }
  187. /**
  188. * 设置会员等级显示返佣|隐藏
  189. */
  190. public function set_show_commission($show_commission = '', $id = '')
  191. {
  192. if ($show_commission == '' || $id == '') return $this->fail('缺少参数');
  193. $res = SystemUserLevel::merSet($this->merId)->where(['id' => $id])->update(['show_commission' => (int)$show_commission]);
  194. if ($res) {
  195. return $this->success($show_commission == 1 ? '显示成功' : '隐藏成功');
  196. } else {
  197. return $this->fail($show_commission == 1 ? '显示失败' : '隐藏失败');
  198. }
  199. }
  200. /**
  201. * 等级列表快速编辑
  202. * field:value name:钻石会员/grade:8/discount:92.00
  203. * @return mixed
  204. * @throws Exception
  205. */
  206. public function set_value($id)
  207. {
  208. $data = UtilService::postMore([
  209. ['field', ''],
  210. ['value', '']
  211. ], $this->request);
  212. if ($data['field'] == '' || $data['value'] == '') return $this->fail('缺少参数');
  213. if (SystemUserLevel::merSet($this->merId)->where(['id' => $id])->update([$data['field'] => $data['value']]))
  214. return $this->success('保存成功');
  215. else
  216. return $this->fail('保存失败');
  217. }
  218. /**
  219. * 异步获取等级任务列表
  220. * @param int $level_id 会员id
  221. * @return mixed
  222. *
  223. * @throws Exception
  224. */
  225. public function get_task_list($level_id)
  226. {
  227. $where = UtilService::getMore([
  228. ['page', 1],
  229. ['limit', 10],
  230. ['name', ''],
  231. ['is_show', '']
  232. ], $this->request);
  233. if (!$level_id || !SystemUserLevel::merSet($this->merId)->where('id', $level_id)->find()) {
  234. return $this->fail('参数错误');
  235. }
  236. return $this->success(SystemUserTask::taskList($level_id, $where));
  237. }
  238. /**
  239. * 快速编辑等级任务 目前仅排序 sort:2
  240. * $id 等级任务id
  241. * @param $id
  242. * @return mixed
  243. * @throws Exception
  244. */
  245. public function set_task_value($id)
  246. {
  247. $data = UtilService::postMore([
  248. ['field', ''],
  249. ['value', '']
  250. ]);
  251. if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
  252. return $this->fail('不存在任务');
  253. }
  254. if ($data['field'] == '' || $data['value'] == '') return $this->fail('缺少参数');
  255. if (SystemUserTask::where(['id' => $id])->update([$data['field'] => $data['value']]))
  256. return $this->success('保存成功');
  257. else
  258. return $this->fail('保存失败');
  259. }
  260. /**
  261. * 设置等级任务显示|隐藏
  262. *
  263. * @param string $is_show
  264. * @param string $id
  265. * @return mixed
  266. */
  267. public function set_task_show($is_show = '', $id = '')
  268. {
  269. ($is_show == '' || $id == '') && $this->fail('缺少参数');
  270. if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
  271. return $this->fail('不存在任务');
  272. }
  273. $res = SystemUserTask::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  274. if ($res) {
  275. return $this->success($is_show == 1 ? '显示成功' : '隐藏成功');
  276. } else {
  277. return $this->fail($is_show == 1 ? '显示失败' : '隐藏失败');
  278. }
  279. }
  280. /**
  281. * 设置任务是否务必达成
  282. *
  283. * @param string $is_must
  284. * @param string $id
  285. * @return mixed
  286. */
  287. public function set_task_must($is_must = '', $id = '')
  288. {
  289. ($is_must == '' || $id == '') && $this->fail('缺少参数');
  290. if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
  291. return $this->fail('不存在任务');
  292. }
  293. $res = SystemUserTask::where(['id' => $id])->update(['is_must' => (int)$is_must]);
  294. if ($res) {
  295. return $this->success('设置成功');
  296. } else {
  297. return $this->fail('设置失败');
  298. }
  299. }
  300. /**
  301. * 生成任务表单
  302. * @return Form
  303. *
  304. * @throws FormBuilderException
  305. * @throws Exception
  306. */
  307. public function create_task()
  308. {
  309. $where = UtilService::getMore([
  310. ['id', 0],
  311. ['level_id', 0]
  312. ]);
  313. if ($where['id']) $tash = SystemUserTask::get($where['id']);
  314. $field[] = Form::select('task_type', '任务类型', isset($tash) ? $tash->task_type : '')->setOptions(function () {
  315. $list = SystemUserTask::getTaskTypeAll();
  316. $menus = [];
  317. foreach ($list as $menu) {
  318. $menus[] = ['value' => $menu['type'], 'label' => $menu['name'] . '----单位[' . $menu['unit'] . ']'];
  319. }
  320. return $menus;
  321. })->filterable(1);
  322. if ($where['id']) $field[] = Form::hidden('id', $where['id']);
  323. $field[] = Form::hidden('level_id', $where['level_id']);
  324. $field[] = Form::number('number', '限定数量', isset($tash) ? $tash->number : 0)->min(0)->col(24);
  325. $field[] = Form::number('sort', '排序', isset($tash) ? $tash->sort : 0)->min(0)->col(24);
  326. $field[] = Form::radio('is_show', '是否显示', isset($tash) ? $tash->is_show : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  327. $field[] = Form::radio('is_must', '是否务必达成', isset($tash) ? $tash->is_must : 1)->options([['label' => '务必达成', 'value' => 1], ['label' => '完成其一', 'value' => 0]])->col(24);
  328. $field[] = Form::textarea('illustrate', '任务说明', isset($tash) ? $tash->illustrate : '');
  329. return $this->makePostForm('添加任务', $field, Url::buildUrl('/user/user_level/save_task'), 'POST');
  330. }
  331. /**
  332. * 保存或者修改任务
  333. * @return mixed
  334. * @throws Exception
  335. */
  336. public function save_task()
  337. {
  338. $data = UtilService::postMore([
  339. ['id', 0],
  340. ['level_id', 0],
  341. ['task_type', ''],
  342. ['number', 0],
  343. ['is_show', 0],
  344. ['sort', 0],
  345. ['is_must', 0],
  346. ['illustrate', ''],
  347. ]);
  348. if (!$data['level_id'] || !SystemUserLevel::merSet($this->merId)->where('id', $data['level_id'])->find()) {
  349. return $this->fail('缺少参数');
  350. }
  351. if (!$data['task_type']) return $this->fail('请选择任务类型');
  352. if ($data['number'] < 1) return $this->fail('请输入限定数量,数量不能小于1');
  353. $tash = SystemUserTask::getTaskType($data['task_type']);
  354. if ($tash['max_number'] != 0 && $data['number'] > $tash['max_number']) return $this->fail('您设置的限定数量超出最大限制,最大限制为:' . $tash['max_number']);
  355. $data['name'] = SystemUserTask::setTaskName($data['task_type'], $data['number']);
  356. try {
  357. if ($data['id']) {
  358. SystemUserTask::edit($data, $data['id']);
  359. return $this->success('修改成功');
  360. } else {
  361. $data['add_time'] = time();
  362. $data['real_name'] = $tash['real_name'];
  363. if (SystemUserTask::create($data))
  364. return $this->success('添加成功');
  365. else
  366. return $this->fail('添加失败');
  367. }
  368. } catch (Exception $e) {
  369. return $this->fail($e->getMessage());
  370. }
  371. }
  372. /**
  373. * 删除任务
  374. * @param int 任务id
  375. *
  376. * @return
  377. */
  378. public function delete_task($id)
  379. {
  380. if (!$id) return $this->fail('缺少参数');
  381. if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
  382. return $this->fail('不存在任务');
  383. }
  384. if (SystemUserTask::del($id))
  385. return $this->success('删除成功');
  386. else
  387. return $this->fail('删除失败');
  388. }
  389. }