UserLevel.php 13 KB

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