Send.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 小程序公共API服务
  7. */
  8. namespace app\citys\controller\api\v1;
  9. use app\citys\controller\api\Base;
  10. use app\citys\model\CitysCate;
  11. use app\citys\model\CitysType;
  12. use app\citys\model\Citys;
  13. use app\citys\model\CitysReply;
  14. use app\citys\model\CitysConfig;
  15. use app\citys\model\CitysFollow;
  16. use app\citys\model\CitysMp;
  17. use app\citys\model\MchId;
  18. use app\common\facade\WechatPay;
  19. use app\common\facade\Upload;
  20. use app\common\model\SystemUser;
  21. use think\facade\Config;
  22. use filter\Filter;
  23. class Send extends Base{
  24. public function initialize() {
  25. parent::initialize();
  26. $this->isUserAuth();
  27. }
  28. /**
  29. * 获取配置参数
  30. * @return void
  31. */
  32. public function getconfig(){
  33. $this->apiSign();
  34. $result = CitysConfig::config($this->member_miniapp_id);
  35. if(empty($result)){
  36. return enjson(204);
  37. }else{
  38. return enjson(200,$result->top);
  39. }
  40. }
  41. /**
  42. * VIP认证
  43. */
  44. public function isVip(){
  45. if(request()->isPost()){
  46. $rel = CitysMp::where(['uid' => $this->user->id,'is_lock' => 0,'is_vip' => 1])->count();
  47. if($rel){
  48. return enjson(200,'认证用户',['vip'=>1]);
  49. }
  50. }
  51. return enjson(200,'非认证用户',['vip'=>0]);
  52. }
  53. /**
  54. * 编辑开通城市号和申请认证城市号
  55. *
  56. * @return void
  57. */
  58. public function editMp(){
  59. $param['logo'] = $this->request->param('logo/s');
  60. $param['skin'] = $this->request->param('skin/s');
  61. $param['title'] = $this->request->param('title/s');
  62. $param['note'] = $this->request->param('note/s');
  63. $param['telphone'] = $this->request->param('telphone/s');
  64. $param['vip_title'] = $this->request->param('vip_title/s','');
  65. $param['vip_about'] = $this->request->param('vip_about/s','');
  66. $param['checked'] = $this->request->param('checked/d',0);
  67. $this->apiSign($param);
  68. $data['title'] = $param['title'];
  69. $data['note'] = $param['note'];
  70. $rel = CitysMp::where(['uid' => $this->user->id])->find();
  71. if($rel){
  72. if($rel->is_lock){
  73. return enjson(403,'帐号已被锁定');
  74. }
  75. //判断是否修改skin
  76. $rel->note = $param['note'];
  77. $rel->title = $param['title'];
  78. //判断是否修改skin
  79. if(!empty($param['skin'])){
  80. $rel->skin = $param['skin'];
  81. }
  82. //判断是否修改logo
  83. if(!empty($param['logo'])){
  84. $rel->logo = $param['logo'];
  85. }
  86. if(!$rel->is_apply && !$rel->is_vip){
  87. $rel->is_apply = $param['checked'] ? 1 : 0;
  88. $rel->vip_title = $param['vip_title'];
  89. $rel->vip_about = $param['vip_about'];
  90. }
  91. $result = $rel->save();
  92. if($result){
  93. return enjson(200,'信息设置成功');
  94. }
  95. }else{
  96. $data['member_miniapp_id'] = $this->member_miniapp_id;
  97. $data['uid'] = $this->user->id;
  98. $data['is_apply'] = $param['checked'] ? 1 : 0;
  99. $data['vip_title'] = $param['vip_title'];
  100. $data['vip_about'] = $param['vip_about'];
  101. $data['reg_time'] = time();
  102. $data['vip_time'] = time();
  103. $data['fans'] = CitysFollow::where(['uid' => $this->user->id])->count();
  104. $data['logo'] = empty($param['logo']) ? $this->user->face: $param['logo'];
  105. $data['skin'] = empty($param['skin']) ? Config::get('upload.upload_relative').'static/citys/skin/2.jpg':$param['skin'];
  106. $data['is_vip'] = 0;
  107. $result = CitysMp::create($data);
  108. if($result){
  109. return enjson(200,'帐号开通成功');
  110. }
  111. }
  112. return enjson(403,'设置失败');
  113. }
  114. /**
  115. * 发表信息
  116. */
  117. public function send(){
  118. if(request()->isPost()){
  119. $param = [
  120. 'content' => $this->request->param('content/s'),
  121. 'telphone' => $this->request->param('telphone/s',''),
  122. 'topday' => $this->request->param('topday/d',0),
  123. 'cate_id' => $this->request->param('cate_id/d',0),
  124. 'type_id' => $this->request->param('type_id/d',0),
  125. 'imgs' => $this->request->param('imgs/s','[]','htmlspecialchars_decode'),
  126. 'fields' => $this->request->param('fields/s','[]','htmlspecialchars_decode'),
  127. 'is_get' => $this->request->param('is_get/d',0),
  128. 'price' => $this->request->param('price/d'),
  129. 'sign' => $this->request->param('sign/s',0),
  130. ];
  131. $this->apiSign($param);
  132. $param['member_miniapp_id'] = $this->miniapp_id;
  133. $param['uid'] = $this->user->id;
  134. $validate = $this->validate($param,'Info.send');
  135. if(true !== $validate){
  136. return json(['code'=>403,'msg'=>$validate]);
  137. }
  138. $param['imgs'] = array_column(Filter::filter_escape(json_decode($param['imgs'],true)),'url');
  139. $param['fields'] = Filter::filter_escape(json_decode($param['fields'],true));
  140. //置顶
  141. $amount = 0;
  142. $config = CitysConfig::config($this->miniapp_id);
  143. if($param['topday'] && !empty($config)){
  144. foreach ($config->top as $value) {
  145. if($value['day'] == $param['topday']){
  146. $amount = $value['money'];
  147. $param['topday'] = $value['day'];
  148. }
  149. }
  150. }
  151. $param['top_money'] = $amount; //置顶金额
  152. $param['order_no'] = $this->user->invite_code.order_no();
  153. $rel = CitysType::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $param['type_id']])->find();
  154. $cate = CitysCate::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $param['cate_id']])->find();
  155. $price = $rel->price+$cate->price; //发布价格
  156. $param['send_price'] = $price;
  157. $param['is_lock'] = $price > 0 ? 1 : 0;
  158. $result = Citys::postThemes($param);
  159. if($result){
  160. if($price> 0 && $amount > 0){
  161. $msg = '信息发布与推广费';
  162. }elseif($price > 0 && $amount = 0){
  163. $msg = '信息发布费';
  164. }else{
  165. $msg = '信息推广费';
  166. }
  167. $amount = $amount+$price;
  168. if($amount > 0){
  169. $order = [
  170. 'openid' => $this->user['miniapp_uid'],
  171. 'miniapp_id' => $this->miniapp_id,
  172. 'name' => '信息置顶',
  173. 'order_no' => $param['order_no'],
  174. 'total_fee' => $amount*100,
  175. 'notify_url' => api(1,'citys/notify/index',$this->miniapp_id)
  176. ];
  177. //读取配置
  178. if($this->member_miniapp->is_psp){
  179. if (empty($config->mch_id)) {
  180. return enjson(403,'服务商模式下,必须配置默认商户号');
  181. }
  182. $order['mchid'] = $config->mch_id;
  183. }
  184. $ispay = WechatPay::orderPay($order);;
  185. if($ispay['code'] == 0){
  186. return json(['code'=>403,'msg'=>$ispay['msg']]);
  187. }
  188. return enjson(200,'发布成功,内容需审核!',$ispay['data']);
  189. }else{
  190. return enjson(204,'发布成功,内容需审核!');
  191. }
  192. }else{
  193. return enjson(403,'发布失败');
  194. }
  195. }
  196. }
  197. /**
  198. * 点赞
  199. **/
  200. public function onLike(){
  201. $param['id'] = $this->request->param('id/d',0);
  202. $this->apiSign($param);
  203. $condition['member_miniapp_id'] = $this->miniapp_id;
  204. $condition['id'] = $param['id'];
  205. $rel = Citys::where($condition)->find();
  206. $like_face = empty($rel->like_face) ? [] : $rel->like_face;
  207. if(count($like_face) < 10){
  208. if(!in_array($this->user->face,$like_face)){
  209. array_push($like_face,$this->user->face);
  210. $rel->like_face = $like_face;
  211. }
  212. }
  213. $rel->like = ['inc',rand(10,200)];
  214. $rel->save();
  215. if(empty($rel)){
  216. return enjson(403,'不用重复点赞');
  217. }else{
  218. return enjson(200);
  219. }
  220. }
  221. /**
  222. * 可以已关闭当前信息
  223. **/
  224. public function onClose(){
  225. $param['id'] = $this->request->param('id/d',0);
  226. $this->apiSign($param);
  227. $condition['id'] = $param['id'];
  228. $condition['uid'] = $this->user->id;
  229. $rel = Citys::where($condition)->find();
  230. if(empty($rel)){
  231. return enjson(403,'未找到你的信息');
  232. }else{
  233. $rel->is_end = 1;
  234. $rel->save();
  235. return enjson(200);
  236. }
  237. }
  238. /**
  239. * 信息单独置顶
  240. */
  241. public function onTop(){
  242. $param['id'] = $this->request->param('id/d',0);
  243. $param['day'] = $this->request->param('day/d',0);
  244. $this->apiSign($param);
  245. $condition['id'] = $param['id'];
  246. $condition['uid'] = $this->user->id;
  247. $rel = Citys::where($condition)->find();
  248. if(empty($rel)){
  249. return enjson(403,'未找到你的信息');
  250. }else{
  251. //置顶
  252. $amount = 0;
  253. $config = CitysConfig::config($this->miniapp_id);
  254. foreach ($config->top as $value) {
  255. if($value['day'] == $param['day']){
  256. $amount = $value['money'];
  257. $topday = $value['day'];
  258. }
  259. }
  260. if($amount > 0){
  261. $rel->topday = $topday;
  262. $rel->top_money = $amount;
  263. $rel->order_no = order_no($this->user->invite_code);
  264. $rel->save();
  265. $order = [
  266. 'openid' => $this->user->miniapp_uid,
  267. 'miniapp_id' => $this->miniapp_id,
  268. 'name' => '信息置顶',
  269. 'order_no' => $rel->order_no ,
  270. 'total_fee' => $amount,
  271. 'notify_url' => api(1,'citys/notify/index',$this->miniapp_id)
  272. ];
  273. //读取配置
  274. if($this->member_miniapp->is_psp){
  275. if (empty($config->mch_id)) {
  276. return enjson(403,'服务商模式下,必须配置默认商户号');
  277. }
  278. $order['mchid'] = $config->mch_id;
  279. }
  280. $ispay = WechatPay::orderPay($order);;
  281. if($ispay['code'] == 0){
  282. return json(['code'=>403,'msg'=>$ispay['msg']]);
  283. }
  284. return enjson(200,$ispay['data']);
  285. }else{
  286. return enjson(403,'信息置顶异常');
  287. }
  288. }
  289. }
  290. /**
  291. * 关注或取消
  292. */
  293. public function onFollow(){
  294. $param['uid'] = $this->request->param('uid/d');
  295. $param['state'] = $this->request->param('state/d',0);
  296. $this->apiSign($param);
  297. if($param['uid'] == $this->user->id){
  298. return enjson(403,'不用关注自己');
  299. }
  300. //判断关注用户是否存在
  301. $is_user = SystemUser::where(['id' => $param['uid'],'is_lock' => 0,'is_delete' => 0])->find();
  302. if(empty($is_user)){
  303. return enjson(403,'当前用户已被封号');
  304. }
  305. //关注
  306. $condition['like_uid'] = $param['uid'];
  307. $condition['uid'] = $this->user->id;
  308. $info = CitysFollow::where($condition)->find();
  309. if(empty($info)){
  310. $follow = CitysFollow::where(['uid' => $this->user->id,'like_uid' => 0])->find();
  311. if($param['state'] == 1){
  312. if(empty($follow)){
  313. $rel = CitysFollow::create(['uid' => $this->user->id,'like_uid' => $param['uid']]);
  314. }else{
  315. $follow->like_uid = $param['uid'];
  316. $rel = $follow->save();
  317. }
  318. }else{
  319. return enjson(403,'关注失败');
  320. }
  321. }else{
  322. if($param['state'] == 0){
  323. $info->like_uid = 0;
  324. $rel = $info->save();
  325. }
  326. }
  327. if(empty($rel)){
  328. return enjson(403,'关注失败');
  329. }else{
  330. $infomp = CitysMp::where(['uid' => $param['uid']])->find();
  331. if($infomp){
  332. if($param['state']){
  333. $infomp->fans = ['inc',1];
  334. }else{
  335. $infomp->fans = ['dec',1];
  336. }
  337. $infomp->save();
  338. }
  339. return enjson(200,$param['state']?'已关注':'取消关注');
  340. }
  341. }
  342. /**
  343. * 发布评论
  344. */
  345. public function postReply(){
  346. if(request()->isPost()){
  347. $param['content'] = $this->request->param('content/s');
  348. $param['id'] = $this->request->param('id/s');
  349. $this->apiSign($param);
  350. $data = [
  351. 'content' => $param['content'],
  352. 'info_id' => $param['id'],
  353. 'uid' => $this->user->id,
  354. 'member_miniapp_id' => $this->member_miniapp_id,
  355. ];
  356. $validate = $this->validate($data,'Info.sendReply');
  357. if(true !== $validate){
  358. return enjson(403,$validate);
  359. }
  360. $result = CitysReply::postReply($data);
  361. if($result){
  362. $result->info->reply_num = ['inc',1];
  363. $result->info->save();
  364. return enjson(200,'评论成功');
  365. }else{
  366. return enjson(403,'评论失败');
  367. }
  368. }
  369. }
  370. /**
  371. * 评论点赞
  372. * @return void
  373. */
  374. public function onReplylike(){
  375. $param['id'] = $this->request->param('id/d',0);
  376. $param['sign'] = $this->request->param('sign');
  377. $rel = $this->apiSign($param);
  378. if($rel['code'] != 200){
  379. return enjson($rel['code'],'签名验证失败');
  380. }
  381. $condition['member_miniapp_id'] = $this->miniapp_id;
  382. $condition['id'] = $param['id'];
  383. $rel = CitysReply::where($condition)->setInc('like',1);
  384. if(empty($rel)){
  385. return enjson(403,'不用重复点赞');
  386. }else{
  387. return enjson(200);
  388. }
  389. }
  390. }