Gmgroup.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\ais\controller\api\v1;
  3. use app\ais\controller\api\Base;
  4. use app\ais\model\AisConfig;
  5. use app\ais\model\AisCouponUser;
  6. use app\ais\model\AisStore;
  7. use app\ais\model\AisStoreBill;
  8. use app\ais\model\AisStoreGroup;
  9. use app\ais\model\AisStoreUnion;
  10. use filter\Filter;
  11. use think\Db;
  12. class Gmgroup extends Base{
  13. protected $store;
  14. public function initialize() {
  15. parent::initialize();
  16. $this->isUserAuth();
  17. $this->store = AisStore::manageStore($this->user->id);
  18. if(empty($this->store)){
  19. exit(json_encode(['code' => 403,'msg'=>'无法找到该商家']));
  20. }
  21. }
  22. /**
  23. * 我的商圈
  24. * @return void
  25. */
  26. public function index(){
  27. $this->apiSign();
  28. $flag = [];
  29. $list = AisStoreGroup::whereIn('id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'store_id'=> $this->store->id,'type' => 0])->column('group_id'))->select()->toArray();
  30. if (!empty($list)) {
  31. foreach ($list as $key => $value) {
  32. $list[$key]['red'] = 0;
  33. $list[$key]['is_manage'] = 0;
  34. if($value['uid'] == $this->user->id){
  35. $list[$key]['is_manage'] = 1;
  36. $unionList = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id'=> $value['id'] ,'type' => 1])->select();
  37. if(!$unionList->isEmpty()){
  38. $list[$key]['red'] = count($unionList); //如果有值 代表你是圈主,有n个人待审核
  39. }
  40. }
  41. $list[$key]['num'] = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id'=> $value['id'] ,'type' => 0])->count();
  42. $flag[] = $list[$key]['is_manage'];
  43. }
  44. }
  45. array_multisort($flag,SORT_DESC,$list); //排序把圈主排在第一
  46. //申请待通过
  47. $apply = AisStoreGroup::whereIn('id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'store_id'=> $this->store->id ?? 0,'type' => 1])->column('group_id'))->select();
  48. return enjson(200,'success',['list' => $list,'apply' => $apply]);
  49. }
  50. //创建和更新商圈
  51. public function edit(){
  52. $param['title'] = $this->request->param('title/s');
  53. $param['content'] = $this->request->param('content/s');
  54. $param['face'] = $this->request->param('face/s','[]','htmlspecialchars_decode');
  55. $this->apiSign($param);
  56. $validate = $this->validate($param,'Group.edit');
  57. if(true !== $validate){
  58. return enjson(403,$validate);
  59. }
  60. $face = Filter::filter_escape(json_decode($param['face'],true));
  61. $store = AisStore::manageStore($this->user->id);
  62. if (!$store) {
  63. return enjson(403,'你不是商家,禁止创建商圈');
  64. }
  65. $result = AisStoreGroup::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->find();
  66. if($result){
  67. $days = intval((time()-$result->update_time)/86400);
  68. if($days < 1){
  69. return enjson(403,'每天只能修改一次商圈信息');
  70. }
  71. $result->title = $param['title'];
  72. $result->content = $param['content'];
  73. $result->update_time = time();
  74. if(!empty($face)){
  75. $result->face = $face[0];
  76. }
  77. $result->save();
  78. return enjson(200,'商圈修改成功');
  79. }else{
  80. $data['member_miniapp_id'] = $this->miniapp_id;
  81. $data['uid'] = $this->user->id;
  82. $data['title'] = $param['title'];
  83. $data['content'] = $param['content'];
  84. if(!empty($face)){
  85. $data['face'] = $face[0];
  86. }
  87. $group = AisStoreGroup::create($data);
  88. AisStoreUnion::create(['member_miniapp_id' => $this->miniapp_id,'group_id' => $group->id,'store_id' => $store->id,'type' => 0,'create_time' => time(),'update_time' => time()]);
  89. return enjson(200,'商圈创建成功');
  90. }
  91. }
  92. //商圈详情
  93. public function read(){
  94. $param['id'] = $this->request->param('id'); //商圈id
  95. $this->apiSign($param);
  96. $info = AisStoreGroup::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
  97. if (!$info) {
  98. return enjson(204);
  99. }
  100. $info->is_manage = $info->uid == $this->user->id?1:0;
  101. $info->red = 0;
  102. $info->num = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id'=> $info->id,'type' => 0])->count();
  103. if($info->is_manage){
  104. $info->red = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id'=> $info->id,'type' => 1])->count();
  105. }
  106. //买单
  107. $order = AisStoreBill::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('store_id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => 0])->column('store_id'))->sum('money');
  108. //次数
  109. $count = AisCouponUser::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('store_id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => 0])->column('store_id'))->count();
  110. //互助
  111. $help = AisCouponUser::where(['member_miniapp_id' => $this->miniapp_id,'is_end' => 1])->where('','EXP',Db::raw("FIND_IN_SET(".$param['id'].",group_ids)"))->count();
  112. return enjson(200,['group' => $info,'order' => $order,'count' => $count,'help' => $help]);
  113. }
  114. //商圈中的店铺
  115. public function store(){
  116. $param['id'] = $this->request->param('id'); //商圈id
  117. $param['type'] = $this->request->param('type'); //0正常 1待审核 2拒绝 3退出 4踢出
  118. $this->apiSign($param);
  119. $list = AisStore::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => $param['type']])->column('store_id'))->select();
  120. if ($list->isEmpty()) {
  121. return enjson(200);
  122. }
  123. return enjson(200,$list);
  124. }
  125. //申请加入商圈
  126. public function add(){
  127. $param['id'] = $this->request->param('id'); //商圈id
  128. $this->apiSign($param);
  129. $store = AisStore::where(['member_miniapp_id' => $this->miniapp_id,'manage_uid' => $this->user->id])->find();
  130. if(empty($store)){
  131. return enjson(403,'你不是商家,禁止加入商家');
  132. }
  133. $info = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'store_id' => $store->id,'type' => 0])->find();
  134. if($info){
  135. return enjson(403,'您已经加入该商圈');
  136. }
  137. $info = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'store_id' => $store->id,'type' => 1])->find();
  138. if($info){
  139. return enjson(403,'您已经申请过该商圈');
  140. }
  141. $config = AisConfig::getConfig($this->miniapp_id);
  142. $list = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => 0])->select();
  143. if(!empty($config->group_member) && count($list) >= $config->group_member){
  144. return enjson(403,'商圈只能有'.$config->group_member.'个成员');
  145. }
  146. $size = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $store->id,'type' => 0])->select();
  147. if(!empty($config->group_join) && count($size) >= $config->group_join){
  148. return enjson(403,'只能加入'.$config->group_join.'个商圈');
  149. }
  150. $code = AisStoreUnion::create(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'store_id' => $store->id,'type' => 1,'create_time' => time(),'update_time' => time()]);
  151. if ($code) {
  152. return enjson(200,"已提交申请");
  153. }
  154. return enjson(204,'未找到商圈');
  155. }
  156. //商圈操作 0同意 2拒绝 3退出 4踢出
  157. public function operate(){
  158. $param['group_id'] = $this->request->param('group_id'); //商圈ID
  159. $param['store_id'] = $this->request->param('store_id',0); //商店ID
  160. $param['type'] = $this->request->param('type',1); //0同意 2拒绝 3退出 4踢出
  161. $this->apiSign($param);
  162. //读取商店信息
  163. if($param['store_id']){
  164. $condition['id'] = $param['store_id'];
  165. }else{
  166. $condition['manage_uid'] = $this->user->id;
  167. }
  168. $store = AisStore::where(['member_miniapp_id' => $this->miniapp_id])->where($condition)->find();
  169. if(empty($store)){
  170. return enjson(403,'你不是商家');
  171. }
  172. $info = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['group_id'],'store_id' => $store->id])->find();
  173. if(empty($info)){
  174. return enjson(403,'你不在当前商圈');
  175. }
  176. $group = AisStoreGroup::where(['member_miniapp_id' => $this->miniapp_id,'id' => $info->group_id])->find();
  177. if(!empty($group) && $group->uid == $this->user->id && ($param['type'] == 0 || $param['type'] == 2 || $param['type'] == 4)){
  178. switch ($param['type']) {
  179. case 0:
  180. //默认通过
  181. $info->type = 0;
  182. $msg = '已审核通过';
  183. //判断权限
  184. $config = AisConfig::getConfig($this->miniapp_id);
  185. $store_group_num = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $info->group_id,'type' => 0])->count();
  186. if(!empty($config->group_member) && $store_group_num >= $config->group_member){
  187. $info->type = 2;
  188. $msg = '你的商圈只能有'.$config->group_member.'个成员';
  189. }
  190. $size = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $store->id,'type' => 0])->count();
  191. if(!empty($config->group_join) && $size >= $config->group_join){
  192. $info->type = 2;
  193. $msg = '已加入'.$size.'个商圈,每个商家只能加入.'.$config->group_join.'个商圈';
  194. }
  195. break;
  196. case 2:
  197. $info->type = 2;
  198. $msg = '已拒绝加圈申请';
  199. break;
  200. case 4:
  201. $info->type = 4;
  202. $msg = '已成功踢出我的商圈';
  203. break;
  204. default:
  205. return enjson(403,'操作失败');
  206. break;
  207. }
  208. }elseif($param['type'] == 3){
  209. $info->type = 3;
  210. $msg = '你成功退出商圈';
  211. }else{
  212. return enjson(403,'操作失败');
  213. }
  214. $info->save();
  215. return enjson(200,$msg);
  216. }
  217. }