DdController.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * 超级管理员-管理爱答题
  5. * User: guanxl
  6. * Date: 2018/7/4
  7. * Time: 11:45
  8. */
  9. namespace App\Http\Controllers;
  10. use App\Http\Requests;
  11. use App\Services\DdService;
  12. use Illuminate\Http\Request;
  13. use App\Common\Api;
  14. use DB;
  15. use App\Models\Messages;
  16. use App\Models\AdminVip;
  17. use App\Models\Admin;
  18. use App\Models\Apps;
  19. use App\Models\Activitys;
  20. use App\Models\Configs;
  21. use App\Models\CorpVip;
  22. use App\Services\QuestionService;
  23. class DdController extends Controller
  24. {
  25. /**
  26. * 获取公告列表
  27. * @param Request $request
  28. * @return string
  29. */
  30. public function getMessageList(Request $request){
  31. $res = Messages::orderByDesc('create_time')->limit(10)->where('status', 1)->get();
  32. return Api::json(config('code.success'), trans('msg.success'), $res);
  33. }
  34. /**
  35. * DD会员开通及初始活动创建
  36. * @param Request $request
  37. * @return string
  38. */
  39. public function openVip(Request $request){
  40. $adminId = $request->get('admin_id');
  41. if(empty($adminId)){
  42. return Api::json(config('code.params_error'), trans('msg.params_error'));
  43. }
  44. //默认参数赋值
  45. $appId = 1;
  46. $authType = 'dingtalk_add';
  47. $clientType = 'dingtalk';
  48. $title = '叮当答题初体验(示例活动)';
  49. $sponsor_corp = '本活动解释权归迷倒万千少男少女的答题君所有';
  50. $extendConfigs = '{"must_question_num":2,"must_answer_wrong_num":1,"must_answer_success_km":100,"blockade_question_num":3,"blockade_answer_wrong_num":1,"blockade_answer_success_km":30,"pk_question_num":3,"pk_last_double":1,"pk_kms":"10\/20\/50","must_category_ids":"","blockade_category_ids":"","pk_category_ids":"","top_prize_num":3,"top_prize_text":"\u6392\u884c\u524d3\u540d\u8fd8\u53ef\u83b7\u5f97\u989d\u5916\u5956\u52b1\uff01","top_prize_open":1,"blockade_answer_count_down":10,"must_answer_count_down":10}';
  51. $adminInfo = Admin::where('id', $adminId)->where('status', 1)->first();
  52. if(!$adminInfo){
  53. return Api::json(config('code.fail'), trans('msg.admin_not_exist'));
  54. }
  55. //管理员VIP开通记录
  56. $adminCorpId = Admin::where('id', $adminId)->value('corp_id');
  57. $vipInfo = CorpVip::where('corp_id', $adminCorpId)->first();
  58. if(!$vipInfo){
  59. //未开通过的企业赋予七天体验版VIP
  60. $query = [];
  61. $query['corp_id'] = $adminCorpId;
  62. $query['app_id'] = $appId; //默认为爱答题,后续若新增新的应用,需要在管理员表表明管理员所属应用
  63. $query['vip_level'] = 1;
  64. $query['vip_start_time'] = time();
  65. $query['vip_end_time'] = time()+3600*24*7; //赋予七天免费版VIP
  66. $query['create_time'] = time();
  67. $query['update_time'] = time();
  68. $res = CorpVip::insert($query);
  69. if(!$res){
  70. return Api::json(config('code.fail'), '会员开通失败');
  71. }
  72. }
  73. //活动创建记录
  74. $activityInfo = Activitys::where('creator', $adminId)->first();
  75. if(!$activityInfo){
  76. $vipInfo = CorpVip::where('corp_id', $adminCorpId)->first();
  77. if($vipInfo['vip_end_time'] <= time()){
  78. return Api::arr(config('code.fail'), trans('msg.vip_time_out'));
  79. }
  80. //获取应用默认数据
  81. $app_info = Apps::where('app_id', $appId)->value('default_data');
  82. $default_data = json_decode($app_info, true);
  83. //activitys表参数拼接
  84. $query = [];
  85. $query['corp_id'] = $adminInfo['corp_id'];
  86. $query['app_id'] = $appId;
  87. $query['create_time'] = time();
  88. $query['update_time'] = time();
  89. $query['title'] = $title;
  90. $query['logo'] = $default_data['logo'];
  91. $query['regulation'] = $default_data['regulation'];
  92. $query['is_jobtime_limit'] = $default_data['is_jobtime_limit'];
  93. $query['jobtimes'] = $default_data['jobtimes'];
  94. $query['start_time'] = strtotime(date('Y-m-d'));
  95. $query['end_time'] = strtotime(date('Y-m-d'))+30*3600*24;
  96. $query['auth_type'] = $authType;
  97. $query['client_type'] = $clientType;
  98. $query['creator'] = $adminId;
  99. $query['is_complete_set'] = 1;
  100. $query['activity_type'] = $vipInfo['vip_level'] == 1 ? 1 : 2; //根据企业会员状态赋予活动收费状态
  101. //企业VIP信息
  102. $vip_level = CorpVip::where('corp_id', $adminCorpId)->value('vip_level');
  103. $query['vip_level'] = $vip_level; //活动vip等级,用于手机端权限控制
  104. $activity_res = Activitys::insertGetId($query);
  105. //configs表参数拼接
  106. $configQuery = [];
  107. $categoryRes = QuestionService::addDefault($activity_res); //添加默认题库
  108. if(!$categoryRes){
  109. return Api::arr($categoryRes['code'], $categoryRes['msg']);
  110. } else {
  111. $extendConfig = json_decode($extendConfigs, true);
  112. $extendConfig['must_category_ids'] = $categoryRes['data']['category_id'];
  113. $extendConfig['blockade_category_ids'] = $categoryRes['data']['category_id'];
  114. $extendConfig['pk_category_ids'] = $categoryRes['data']['category_id'];
  115. $configQuery['extend_configs'] = json_encode($extendConfig);
  116. }
  117. $configQuery['activity_id'] = $activity_res;
  118. $configQuery['corp_id'] = $adminInfo['corp_id'];
  119. $configQuery['top_banner'] = $default_data['top_banner'];
  120. $configQuery['home_background'] = $default_data['home_background'];
  121. $configQuery['mark_background'] = $default_data['mark_background'];
  122. $configQuery['count_down_background'] = $default_data['count_down_background'];
  123. $configQuery['sponsor_corp'] = $sponsor_corp;
  124. $configQuery['prize_banner'] = '//s1-hn.aghcdn.com/management/20181025/069cc3cd0b76a7e1ec194b97946a62052d31d143.png';
  125. $configQuery['copper_box_km'] = $default_data['copper_box_km'];
  126. $configQuery['silver_box_km'] = $default_data['silver_box_km'];
  127. $configQuery['gold_box_km'] = $default_data['gold_box_km'];
  128. $configQuery['is_open_prize'] = 1; //默认开启抽奖
  129. $configQuery['open_department_top'] = $default_data['open_department_top']; //默认不开启单位排行
  130. $configQuery['level_star_config'] = json_encode($default_data['level_star_config']); //段位的默认配置
  131. $configQuery['update_time'] = time();
  132. $configQuery['create_time'] = time();
  133. $config_res = Configs::insert($configQuery);
  134. //添加示例宝箱
  135. $box_res = DdService::ExampleActBox($activity_res, $adminInfo['corp_id']);
  136. if($activity_res && $config_res && $box_res) {
  137. //清理并生成题库缓存
  138. $questions_cache = env('WEB_URL').'/brainstorming/console/cache/question?activity_id='.$activity_res;
  139. $config_cache = env('WEB_URL').'/brainstorming/console/cache/config?activity_id='.$activity_res;
  140. Api::curl($config_cache);
  141. Api::curl($questions_cache);
  142. return Api::json(config('code.success'), trans('msg.success'));
  143. } else {
  144. return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
  145. }
  146. }
  147. // return Api::json(config('code.success'), trans('msg.success'));
  148. }
  149. }