Group.php 12 KB

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