UserLevel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\controller\AuthController;
  4. use think\facade\Route as Url;
  5. use crmeb\traits\CurdControllerTrait;
  6. use app\admin\model\user\UserLevel as UserLevelModel;
  7. use app\admin\model\system\{SystemUserLevel,SystemUserTask};
  8. use crmeb\services\{UtilService,JsonService,FormBuilder as Form};
  9. /**
  10. * 会员设置
  11. * Class UserLevel
  12. * @package app\admin\controller\user
  13. */
  14. class UserLevel extends AuthController
  15. {
  16. use CurdControllerTrait;
  17. /*
  18. * 等级展示
  19. * */
  20. public function index()
  21. {
  22. return $this->fetch();
  23. }
  24. /*
  25. * 创建form表单
  26. * */
  27. public function create($id = 0)
  28. {
  29. if ($id) $vipinfo = SystemUserLevel::get($id);
  30. $field[] = Form::input('name', '等级名称', isset($vipinfo) ? $vipinfo->name : '')->col(Form::col(24));
  31. // $field[] = Form::radio('is_forever', '是否为永久', isset($vipinfo) ? $vipinfo->is_forever : 0)->options([['label' => '永久', 'value' => 1], ['label' => '非永久', 'value' => 0]])->col(24);
  32. //$field[]= Form::number('money','等级价格',isset($vipinfo) ? $vipinfo->money : 0)->min(0)->col(24);
  33. //$field[]= Form::radio('is_pay','是否需要购买',isset($vipinfo) ? $vipinfo->is_pay : 0)->options([['label'=>'需要','value'=>1],['label'=>'免费','value'=>0]])->col(24);
  34. // $field[] = Form::number('valid_date', '有效时间(天)', isset($vipinfo) ? $vipinfo->valid_date : 0)->min(0)->col(8);
  35. $field[] = Form::number('grade', '等级', isset($vipinfo) ? $vipinfo->grade : 0)->min(0)->col(8);
  36. // $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 0)->min(0)->col(8);
  37. $field[] = Form::number('layer_award_layer', '团队奖层数', isset($vipinfo) ? $vipinfo->layer_award_layer : 0)->min(0)->col(8);
  38. $field[] = Form::number('layer_award_ratio', '团队奖百分比', isset($vipinfo) ? $vipinfo->layer_award_ratio : 0)->min(0)->col(8);
  39. $field[] = Form::number('recommend_award_ratio', '直推奖比例', isset($vipinfo) ? $vipinfo->recommend_award_ratio : 0)->min(0)->col(8);
  40. // $field[] = Form::number('group_creat_award_ratio', '团队产币分红', isset($vipinfo) ? $vipinfo->group_creat_award_ratio : 0)->min(0)->col(8);
  41. $field[] = Form::radio('system_create_award_ratio', '平台产币分红', isset($vipinfo) ? $vipinfo->system_create_award_ratio : 0)->options([['label' => '有', 'value' => 1], ['label' => '无', 'value' => 0]])->col(8);
  42. // $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 0)->min(0)->col(8);
  43. $field[] = Form::frameImageOne('icon', '图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'icon')), isset($vipinfo) ? $vipinfo->icon : '')->icon('image')->width('100%')->height('500px');
  44. $field[] = Form::frameImageOne('image', '会员背景', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), isset($vipinfo) ? $vipinfo->image : '')->icon('image')->width('100%')->height('500px');
  45. $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(8);
  46. $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '');
  47. $form = Form::make_post_form('添加等级设置', $field, Url::buildUrl('save', ['id' => $id]), 2);
  48. $this->assign(compact('form'));
  49. return $this->fetch('public/form-builder');
  50. }
  51. /*
  52. * 会员等级添加或者修改
  53. * @param $id 修改的等级id
  54. * @return json
  55. * */
  56. public function save($id = 0)
  57. {
  58. $data = UtilService::postMore([
  59. ['name', ''],
  60. ['is_forever', 1],
  61. ['money', 0],
  62. ['layer_award_layer', 0],
  63. ['layer_award_ratio', 0],
  64. ['recommend_award_ratio', 0],
  65. ['group_creat_award_ratio', 0],
  66. ['system_create_award_ratio', 0],
  67. ['is_pay', 0],
  68. ['valid_date', 0],
  69. ['grade', 0],
  70. ['discount', 0],
  71. ['icon', ''],
  72. ['image', ''],
  73. ['is_show', ''],
  74. ['explain', ''],
  75. ]);
  76. if (!$data['name']) return JsonService::fail('请输入等级名称');
  77. if (!$data['grade']) return JsonService::fail('请输入等级');
  78. if (!$data['explain']) return JsonService::fail('请输入等级说明');
  79. if ($data['is_forever'] == 0 && !$data['valid_date']) return JsonService::fail('请输入有效时间(天)');
  80. if ($data['is_pay']) return JsonService::fail('会员等级购买功能正在开发中,暂时关闭可购买功能!');
  81. if ($data['is_pay'] && !$data['money']) return JsonService::fail('请输入购买金额');
  82. if (!$data['icon']) return JsonService::fail('请上传等级图标');
  83. if (!$data['image']) return JsonService::fail('请上传等级背景图标');
  84. if (!$id && SystemUserLevel::be(['is_del' => 0, 'grade' => $data['grade']])) return JsonService::fail('已检测到您设置过的会员等级,此等级不可重复');
  85. SystemUserLevel::beginTrans();
  86. try {
  87. //修改
  88. if ($id) {
  89. if (SystemUserLevel::edit($data, $id)) {
  90. SystemUserLevel::commitTrans();
  91. return JsonService::successful('修改成功');
  92. } else {
  93. SystemUserLevel::rollbackTrans();
  94. return JsonService::fail('修改失败');
  95. }
  96. } else {
  97. //新增
  98. $data['add_time'] = time();
  99. if (SystemUserLevel::create($data)) {
  100. SystemUserLevel::commitTrans();
  101. return JsonService::successful('添加成功');
  102. } else {
  103. SystemUserLevel::rollbackTrans();
  104. return JsonService::fail('添加失败');
  105. }
  106. }
  107. } catch (\Exception $e) {
  108. SystemUserLevel::rollbackTrans();
  109. return JsonService::fail($e->getMessage());
  110. }
  111. }
  112. /*
  113. * 获取系统设置的vip列表
  114. * @param int page
  115. * @param int limit
  116. * */
  117. public function get_system_vip_list()
  118. {
  119. $where = UtilService::getMore([
  120. ['page', 0],
  121. ['limit', 10],
  122. ['title', ''],
  123. ['is_show', ''],
  124. ]);
  125. return JsonService::successlayui(SystemUserLevel::getSytemList($where));
  126. }
  127. /*
  128. * 删除会员等级
  129. * @param int $id
  130. * */
  131. public function delete($id = 0)
  132. {
  133. if (SystemUserLevel::edit(['is_del' => 1], $id))
  134. return JsonService::successful('删除成功');
  135. else
  136. return JsonService::fail('删除失败');
  137. }
  138. /**
  139. * 设置单个产品上架|下架
  140. *
  141. * @return json
  142. */
  143. public function set_show($is_show = '', $id = '')
  144. {
  145. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  146. $res = SystemUserLevel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  147. if ($res) {
  148. return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  149. } else {
  150. return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  151. }
  152. }
  153. /**
  154. * 快速编辑
  155. *
  156. * @return json
  157. */
  158. public function set_value($field = '', $id = '', $value = '')
  159. {
  160. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  161. if (SystemUserLevel::where(['id' => $id])->update([$field => $value]))
  162. return JsonService::successful('保存成功');
  163. else
  164. return JsonService::fail('保存失败');
  165. }
  166. /*
  167. * 等级任务列表
  168. * @param int $vip_id 等级id
  169. * @return json
  170. * */
  171. public function tash($level_id = 0)
  172. {
  173. $this->assign('level_id', $level_id);
  174. return $this->fetch();
  175. }
  176. /**
  177. * 快速编辑
  178. *
  179. * @return json
  180. */
  181. public function set_tash_value($field = '', $id = '', $value = '')
  182. {
  183. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  184. if (SystemUserTask::where(['id' => $id])->update([$field => $value]))
  185. return JsonService::successful('保存成功');
  186. else
  187. return JsonService::fail('保存失败');
  188. }
  189. /**
  190. * 设置单个产品上架|下架
  191. *
  192. * @return json
  193. */
  194. public function set_tash_show($is_show = '', $id = '')
  195. {
  196. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  197. $res = SystemUserTask::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  198. if ($res) {
  199. return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  200. } else {
  201. return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  202. }
  203. }
  204. /**
  205. * 设置单个产品上架|下架
  206. *
  207. * @return json
  208. */
  209. public function set_tash_must($is_must = '', $id = '')
  210. {
  211. ($is_must == '' || $id == '') && Json::fail('缺少参数');
  212. $res = SystemUserTask::where(['id' => $id])->update(['is_must' => (int)$is_must]);
  213. if ($res) {
  214. return JsonService::successful('设置成功');
  215. } else {
  216. return JsonService::fail('设置失败');
  217. }
  218. }
  219. /*
  220. * 生成任务表单
  221. * @param int $id 任务id
  222. * @param int $vip_id 会员id
  223. * @return html
  224. * */
  225. public function create_tash($id = 0, $level_id = 0)
  226. {
  227. if ($id) $tash = SystemUserTask::get($id);
  228. $field[] = Form::select('task_type', '任务类型', isset($tash) ? $tash->task_type : '')->setOptions(function () {
  229. $list = SystemUserTask::getTaskTypeAll();
  230. $menus = [];
  231. foreach ($list as $menu) {
  232. $menus[] = ['value' => $menu['type'], 'label' => $menu['name'] . '----单位[' . $menu['unit'] . ']'];
  233. }
  234. return $menus;
  235. })->filterable(1);
  236. $field[] = Form::number('number', '限定数量', isset($tash) ? $tash->number : 0)->min(0)->col(24);
  237. $field[] = Form::number('sort', '排序', isset($tash) ? $tash->sort : 0)->min(0)->col(24);
  238. $field[] = Form::radio('is_show', '是否显示', isset($tash) ? $tash->is_show : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  239. $field[] = Form::radio('is_must', '是否务必达成', isset($tash) ? $tash->is_must : 1)->options([['label' => '务必达成', 'value' => 1], ['label' => '完成其一', 'value' => 0]])->col(24);
  240. $field[] = Form::textarea('illustrate', '任务说明', isset($tash) ? $tash->illustrate : '');
  241. $form = Form::make_post_form('添加任务', $field, Url::buildUrl('save_tash', ['id' => $id, 'level_id' => $level_id]), 2);
  242. $this->assign(compact('form'));
  243. return $this->fetch('public/form-builder');
  244. }
  245. /*
  246. * 保存或者修改任务
  247. * @param int $id 任务id
  248. * @param int $vip_id 会员id
  249. * */
  250. public function save_tash($id = 0, $level_id = 0)
  251. {
  252. if (!$level_id) return JsonService::fail('缺少参数');
  253. $data = UtilService::postMore([
  254. ['task_type', ''],
  255. ['number', 0],
  256. ['is_show', 0],
  257. ['sort', 0],
  258. ['is_must', 0],
  259. ['illustrate', ''],
  260. ]);
  261. if (!$data['task_type']) return JsonService::fail('请选择任务类型');
  262. if ($data['number'] < 1) return JsonService::fail('请输入限定数量,数量不能小于1');
  263. $tash = SystemUserTask::getTaskType($data['task_type']);
  264. if ($tash['max_number'] != 0 && $data['number'] > $tash['max_number']) return JsonService::fail('您设置的限定数量超出最大限制,最大限制为:' . $tash['max_number']);
  265. $data['name'] = SystemUserTask::setTaskName($data['task_type'], $data['number']);
  266. try {
  267. if ($id) {
  268. SystemUserTask::edit($data, $id);
  269. return JsonService::successful('修改成功');
  270. } else {
  271. $data['level_id'] = $level_id;
  272. $data['add_time'] = time();
  273. $data['real_name'] = $tash['real_name'];
  274. if (SystemUserTask::create($data))
  275. return JsonService::successful('添加成功');
  276. else
  277. return JsonService::fail('添加失败');
  278. }
  279. } catch (\Exception $e) {
  280. return JsonService::fail($e->getMessage());
  281. }
  282. }
  283. /*
  284. * 异步获取等级任务列表
  285. * @param int $vip_id 会员id
  286. * @param int $page 分页
  287. * @param int $limit 显示条数
  288. * @return json
  289. * */
  290. public function get_tash_list($level_id = 0)
  291. {
  292. list($page, $limit) = UtilService::getMore([
  293. ['page', 1],
  294. ['limit', 10],
  295. ], $this->request, true);
  296. return JsonService::successlayui(SystemUserTask::getTashList($level_id, (int)$page, (int)$limit));
  297. }
  298. /*
  299. * 删除任务
  300. * @param int 任务id
  301. * */
  302. public function delete_tash($id = 0)
  303. {
  304. if (!$id) return JsonService::fail('缺少差参数');
  305. if (SystemUserTask::del($id))
  306. return JsonService::successful('删除成功');
  307. else
  308. return JsonService::fail('删除失败');
  309. }
  310. /*
  311. * 会员等级展示
  312. *
  313. * */
  314. public function user_level_list()
  315. {
  316. $this->assign('level', SystemUserLevel::where('is_del', 0)->where('is_show', 1)->order('grade asc')->field(['id', 'name'])->select());
  317. return $this->fetch();
  318. }
  319. public function get_user_vip_list()
  320. {
  321. $where = UtilService::getMore([
  322. ['page', 1],
  323. ['limit', 10],
  324. ['nickname', ''],
  325. ['level_id', ''],
  326. ]);
  327. return JsonService::successlayui(UserLevelModel::getUserVipList($where));
  328. }
  329. }