Index.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\green\controller\api\v1;
  3. use app\green\controller\api\Base;
  4. use app\common\model\SystemUserAddress;
  5. use app\common\facade\Inform;
  6. use app\green\model\GreenDevice;
  7. use app\green\model\GreenRetrieve;
  8. use app\green\model\GreenStaff;
  9. use app\green\model\GreenUser;
  10. use app\green\model\GreenUserLog;
  11. use think\helper\Time;
  12. class Index extends Base{
  13. public function user(){
  14. $this->isUserAuth();
  15. $param['signkey'] = $this->request->param('signkey/s');
  16. $param['sign'] = $this->request->param('sign');
  17. $rel = $this->apiSign($param);
  18. if ($rel['code'] != 200) {
  19. return enjson(500, '签名验证失败');
  20. }
  21. $info = GreenUser::where(['member_miniapp_id' => $this->miniapp_id])->where(['uid' => $this->user->id])->find();
  22. if($info){
  23. //用户管理的回收柜
  24. $info->list = GreenDevice::where(['member_miniapp_id' => $this->miniapp_id,'manage_uid' => $this->user->id])->column('device_id');;
  25. //加入天数
  26. $info->day = (int)((time() - $this->user->create_time)/(24*60*60));
  27. //投递地点
  28. $info->address = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->group('device_id')->count();
  29. //累计投递次数
  30. $info->count = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->count();
  31. //累计投递重量
  32. $info->weight = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->sum('weight');
  33. //共有多少人参与
  34. $info->join = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id])->group('uid')->count();
  35. //排名
  36. $info->top = GreenUser::where(['member_miniapp_id' => $this->miniapp_id])->where('weight','>',$info->weight)->count() + 1;
  37. $info->weight = round($info->weight/1000);
  38. return enjson(200,'success', $info);
  39. }else{
  40. $data = [];
  41. $data['points'] = 0;
  42. $data['list'] = 0;
  43. $data['day'] = 0;
  44. $data['address'] = 0;
  45. $data['count'] = 0;
  46. $data['weight'] = 0;
  47. $data['join'] = 0;
  48. $data['top'] = 0;
  49. $data['weight'] = 0;
  50. return enjson(200,'success', $data);
  51. }
  52. }
  53. /**
  54. * 地图
  55. */
  56. public function map(){
  57. $param['longitude'] = $this->request->param('longitude/s','');
  58. $param['latitude'] = $this->request->param('latitude/s','');
  59. $param['signkey'] = $this->request->param('signkey/s');
  60. $param['sign'] = $this->request->param('sign');
  61. $rel = $this->apiSign($param);
  62. if($rel['code'] != 200){
  63. return enjson(500,'签名验证失败');
  64. }
  65. $data = [];
  66. $info = GreenDevice::where(['member_miniapp_id' => $this->miniapp_id])->where(['state' => 0])->field("id,longitude,latitude,title,address")->select()->toArray();
  67. $data['device'] = $info;
  68. if(empty($data['device'])){
  69. return enjson(204,'empty');
  70. }
  71. if(empty($param['longitude']) && empty($param['latitude'])){
  72. $data['near'] = [];
  73. }else{
  74. $mps = txMap_to_bdMap($param['latitude'],$param['longitude']);
  75. $param['latitude'] = $mps['lat'];
  76. $param['longitude'] = $mps['lng'];
  77. $data['near'] = model('GreenDevice')->selectNear($param);
  78. }
  79. return enjson(200,'success',$data);
  80. }
  81. /**
  82. * 记录
  83. */
  84. public function log(){
  85. $this->isUserAuth();
  86. $param['today'] = $this->request->param('today/d',0);
  87. $param['page'] = $this->request->param('page/d',1);
  88. $param['sign'] = $this->request->param('sign');
  89. $rel = $this->apiSign($param);
  90. if($rel['code'] != 200){
  91. return enjson(500,'签名验证失败');
  92. }
  93. $condition[] = ['uid','=',$this->user->id];
  94. list($start,$end) = Time::month();
  95. if($param['today']){
  96. $condition[] = ['create_time','<=',$start];
  97. }else{
  98. $condition[] = ['create_time','>=',$start];
  99. $condition[] = ['create_time','<=',$end];
  100. }
  101. $count = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id])->where($condition)->count();
  102. $weight = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id])->where($condition)->sum('weight');
  103. $info = GreenUserLog::withAttr('Create_time',function($value,$data) {
  104. return date('Y-m-d H:i',$value);
  105. })->where(['member_miniapp_id' => $this->miniapp_id])->where($condition)->order('id desc')->paginate(10)->toArray();
  106. if(empty($info['data'])){
  107. return enjson(204,'empty');
  108. }else{
  109. return enjson(200,'success',['list' => $info['data'],'count' => $count,'weight' => $weight]);
  110. }
  111. }
  112. /**
  113. * 当月账单
  114. */
  115. public function moonBill(){
  116. $this->isUserAuth();
  117. $param['signkey'] = $this->request->param('signkey/s');
  118. $param['sign'] = $this->request->param('sign');
  119. $rel = $this->apiSign($param);
  120. if($rel['code'] != 200){
  121. return enjson(500,'签名验证失败');
  122. }
  123. $condition[] = ['uid','=',$this->user->id];
  124. list($start,$end) = Time::month();
  125. $condition[] = ['create_time','>=',$start];
  126. $condition[] = ['create_time','<=',$end];
  127. $count = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id])->where($condition)->count();
  128. $weight = GreenUserLog::where(['member_miniapp_id' => $this->miniapp_id])->where($condition)->sum('weight');
  129. return enjson(200,'success',['count' => $count,'weight' => round($weight/1000)]);
  130. }
  131. /**
  132. * @return \think\response\Json
  133. * 添加预约信息
  134. */
  135. public function add(){
  136. $this->isUserAuth();
  137. if(request()->isPost()){
  138. $param = [
  139. 'weight' => $this->request->param('weight/d'),
  140. 'date' => $this->request->param('date/s'),
  141. 'address' => $this->request->param('address/s', 0),
  142. 'message' => $this->request->param('message/s'),
  143. 'longitude' => $this->request->param('longitude/s'),
  144. 'latitude' => $this->request->param('latitude/s'),
  145. 'sign' => $this->request->param('sign/s'),
  146. ];
  147. $rel = $this->apiSign($param);
  148. if($rel['code'] != 200){
  149. return enjson($rel['code'],'签名验证失败');
  150. }
  151. $validate = $this->validate($param,'Order.add');
  152. if(true !== $validate){
  153. return enjson(403,$validate);
  154. }
  155. //读取收货地址
  156. $address = SystemUserAddress::where(['user_id'=>$this->user->id,'address' =>$param['address']])->find();
  157. if(empty($address)){
  158. return enjson(403,'请选择地址');
  159. }
  160. //查询运营商
  161. $info = model('GreenOperate')->selectAll($param);
  162. if( $info){
  163. $order['member_miniapp_id'] = $this->miniapp_id;
  164. $order['uid'] = $this->user->id;
  165. $order['realname'] = $address['name'];
  166. $order['phone'] = $address['telphone'];
  167. $order['address'] = $address['address'];
  168. $order['order_no'] = $this->user->invite_code . order_no();
  169. $order['message'] = $param['message'];
  170. $order['longitude'] = $param['longitude'];
  171. $order['latitude'] = $param['latitude'];
  172. $order['create_time'] = time();
  173. $order['date'] = $param['date'];
  174. $order['weight'] = $param['weight'];
  175. $result = GreenRetrieve::create($order);
  176. if($result){
  177. //通知到回收员
  178. $list = GreenStaff::where(['member_miniapp_id' => $this->miniapp_id,'operate_id' => $info[0]['id']])->select()->toArray();
  179. foreach ($list as $info){
  180. Inform::sms($info['uid'],$this->miniapp_id,['title' =>'业务进展通知','type' => '新回收申请','content' =>'您有新的回收任务','state' => '待回收']);
  181. }
  182. return enjson(200,'操作成功',['url'=>url('category/index')]);
  183. }else{
  184. return enjson(0);
  185. }
  186. }else{
  187. return enjson(403,'未查找到附近运营商');
  188. }
  189. }
  190. }
  191. /**
  192. * @return \think\response\Json
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. * @throws \think\exception\DbException
  196. * 回收员设置回收状态
  197. */
  198. public function retrieveState(){
  199. $this->isUserAuth();
  200. if(request()->isPost()){
  201. $param = [
  202. 'retrieve_id' => $this->request->param('retrieve_id/d'),
  203. 'sign' => $this->request->param('sign/s'),
  204. ];
  205. $rel = $this->apiSign($param);
  206. if($rel['code'] != 200){
  207. return enjson($rel['code'],'签名验证失败');
  208. }
  209. $info = GreenRetrieve::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['retrieve_id']])->find();
  210. if($info){
  211. $info->state = 1;
  212. $info->update_time = time();
  213. $info->save();
  214. return enjson(200,'success');
  215. }else{
  216. return enjson(0);
  217. }
  218. }
  219. }
  220. }