UserLevel.php 13 KB

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