Store.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * 店铺API
  4. */
  5. namespace app\smartbc\controller\api\v1;
  6. use app\smartbc\controller\api\Base;
  7. use app\smartbc\model\SmartbcStore;
  8. use app\smartbc\model\SmartbcStoreBill;
  9. use app\smartbc\model\SmartbcStoreGroup;
  10. use app\smartbc\model\SmartbcStoreUnion;
  11. use app\smartbc\model\SmartbcCouponUser;
  12. use app\smartbc\model\SmartbcOrder;
  13. use app\smartbc\model\SmartbcCoupon;
  14. use think\helper\Time;
  15. use filter\Filter;
  16. class Store extends Base{
  17. /**
  18. * 初始化当前应用管理员是不是联盟城市账户
  19. * @return void
  20. */
  21. public function initialize() {
  22. parent::initialize();
  23. $this->isUserAuth();
  24. }
  25. /**
  26. * 判断是否商家
  27. * @return boolean
  28. */
  29. public function isstore(){
  30. $store = SmartbcStore::manageStore($this->user->id);
  31. if ($store) {
  32. return enjson(200,$store);
  33. }
  34. return enjson(204,'你不是商家');
  35. }
  36. /**
  37. * 申请入驻商家/修改商家信息
  38. */
  39. public function edit(){
  40. if (request()->isPost()) {
  41. $param = [
  42. 'name' => $this->request->param('name/s'),
  43. 'address' => $this->request->param('address/s'),
  44. 'telphone' => $this->request->param('telphone/s'),
  45. 'latitude' => $this->request->param('latitude/s'),
  46. 'longitude' => $this->request->param('longitude/s'),
  47. 'imgs' => $this->request->param('imgs/s','[]','htmlspecialchars_decode'),
  48. ];
  49. $rel = $this->apiSign($param);
  50. if ($rel['code'] != 200) {
  51. return enjson($rel['code'],'签名验证失败');
  52. }
  53. $validate = $this->validate($param,'Store.reg');
  54. if(true !== $validate){
  55. return enjson(403,$validate);
  56. }
  57. //判断是修改还是创建
  58. $store = SmartbcStore::manageStore($this->user->id);
  59. if($store){
  60. $data['is_lock'] = 1;
  61. $data['sort'] = 0;
  62. }
  63. $data['is_pass'] = 0;
  64. $data['member_miniapp_id'] = $this->miniapp_id;
  65. $data['manage_uid'] = $this->user->id;
  66. $data['name'] = $param['name'];
  67. $data['telphone'] = $param['telphone'];
  68. $data['address'] = $param['address'];
  69. $data['latitude'] = $param['latitude'];
  70. $data['longitude'] = $param['longitude'];
  71. $imgs = Filter::filter_escape(json_decode($param['imgs'],true));
  72. if(!empty($imgs)){
  73. $data['imgs'] = json_encode(Filter::filter_escape(json_decode($param['imgs'],true)));
  74. $data['img'] = $imgs[0];
  75. }
  76. $rel = $store ? SmartbcStore::where(['id' => $store->id])->update($data) : SmartbcStore::create($data);
  77. if($rel){
  78. return enjson(200,"提交成功");
  79. }
  80. return enjson(403,"提交失败");
  81. }
  82. }
  83. //读取商家信息
  84. public function storeInfo(){
  85. $rel = $this->apiSign();
  86. if ($rel['code'] != 200) {
  87. return enjson($rel['code'],'签名验证失败');
  88. }
  89. //判断是修改还是创建
  90. $store = SmartbcStore::manageStore($this->user->id);
  91. if (!$store) {
  92. return enjson(404);
  93. }
  94. return enjson(200,$store);
  95. }
  96. //商家数据统计
  97. public function storeData(){
  98. $rel = $this->apiSign();
  99. if ($rel['code'] != 200) {
  100. return enjson($rel['code'],'签名验证失败');
  101. }
  102. //判断是修改还是创建
  103. $store = SmartbcStore::manageStore($this->user->id);
  104. if (!$store) {
  105. return enjson(404);
  106. }
  107. list($start, $end) = Time::yesterday();
  108. $amount = SmartbcOrder::where([['store_id','=', $store->id],['state', '=',1],['update_time', '>=',$start],['update_time', '<=',$end]])->sum('price');
  109. $coupon = SmartbcCoupon::where(['store_id' => $store->id])->count(); //已领取
  110. $couponuser = SmartbcCouponUser::where(['store_id' => $store->id])->where('parent_store_id','>',0)->count(); //引流
  111. $from = SmartbcCouponUser::where([['store_id', '=', $store->id],['parent_store_id', '>',0],['money', '>',0],['is_end', '=',1]])->count(); //引流
  112. return enjson(200,['store'=>$store,'amount' => $amount,'coupon'=>$coupon,'couponuser' => $couponuser,'from'=> $from]);
  113. }
  114. //商家收入
  115. public function bill(){
  116. $param = [
  117. 'page' => $this->request->param('page/d',1),
  118. 'store_id' => $this->request->param('store_id'),
  119. 'store_chain_id' => $this->request->param('store_chain_id'),
  120. 'fixed_date' => $this->request->param('fixed_date'),
  121. 'times' => $this->request->param('times','[]','htmlspecialchars_decode'),
  122. ];
  123. $rel = $this->apiSign($param);
  124. if ($rel['code'] != 200) {
  125. return enjson(500,'签名验证失败');
  126. }
  127. //判断是修改还是创建
  128. $store = SmartbcStore::manageStore($this->user->id);
  129. if (!$store) {
  130. return enjson(404);
  131. }
  132. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  133. $condition[] = ['store_id', '=',$store->id];
  134. if($param['store_chain_id']){
  135. $condition[] = ['store_chain_id', '=',$param['store_chain_id']];
  136. }
  137. if($param['fixed_date'] == 3){
  138. $times = Filter::filter_escape(json_decode($param['times'],true));
  139. if(!empty($times['startime']) || !empty($times['endtime'])){
  140. $start = strtotime($times['startime'].'00:00:00');
  141. $end = strtotime($times['endtime'].'23:59:59');
  142. if($start >= $end){
  143. return enjson(403,'开启日期禁止大于结束日期');
  144. }
  145. $condition[] = ['update_time', '>=', $start];
  146. $condition[] = ['update_time', '<=', $end];
  147. }
  148. }else{
  149. switch ($param['fixed_date']) {
  150. case 1:
  151. list($start, $end) = Time::yesterday();
  152. break;
  153. case 2:
  154. list($start, $end) = Time::month();
  155. break;
  156. default:
  157. list($start, $end) = Time::today();
  158. break;
  159. }
  160. $condition[] = ['update_time', '>=', $start];
  161. $condition[] = ['update_time', '<=', $end];
  162. }
  163. $rel = SmartbcStoreBill::withAttr('update_time', function ($value, $data) {
  164. return date('Y-m-d H:i',$value);
  165. })->with(['user'=> function($query) {
  166. $query->field('id,face,nickname');
  167. }])->where($condition)->order('id desc')->page($param['page'],20)->select();
  168. if (empty($rel)) {
  169. return enjson(204);
  170. }
  171. $amount['inc'] = 0;
  172. $amount['dec'] = 0;
  173. $amount['order'] = 0;
  174. if($param['page'] == 1){
  175. $amount['inc'] = SmartbcStoreBill::where($condition)->where('money','>',0)->sum('money');
  176. $amount['dec'] = SmartbcStoreBill::where($condition)->where('money','<',0)->sum('money');
  177. $amount['order'] = SmartbcOrder::where($condition)->where('state','=',1)->sum('price');
  178. }
  179. return enjson(200,['bill' => $rel,'amount' => $amount]);
  180. }
  181. }