Infosend.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\allwin\model\AllwinInfoCate;
  11. use app\allwin\model\AllwinInfo;
  12. use app\allwin\model\AllwinInfoReply;
  13. use app\allwin\model\AllwinInfoConfig;
  14. use app\allwin\model\AllwinInfoFollow;
  15. use app\allwin\model\AllwinConfig;
  16. use app\allwin\model\AllwinInfoMp;
  17. use app\allwin\model\MchId;
  18. use app\common\facade\WechatPay;
  19. use app\common\facade\Upload;
  20. use app\common\model\SystemUser;
  21. use app\common\model\SystemMemberSms;
  22. use think\facade\Config;
  23. use filter\Filter;
  24. class Infosend extends Base{
  25. /**
  26. * 初始化当前应用是否登录
  27. * @return void
  28. */
  29. public function initialize() {
  30. parent::initialize();
  31. $this->isUserAuth();
  32. }
  33. /**
  34. * 判断是否注册城市号
  35. */
  36. public function isRegMp(){
  37. $rel = AllwinInfoMp::where(['uid' => $this->user->id])->count();
  38. if($rel){
  39. return enjson(200);
  40. }
  41. return enjson(204);
  42. }
  43. /**
  44. * 编辑开通城市号和申请认证城市号
  45. *
  46. * @return void
  47. */
  48. public function editMp(){
  49. $param['logo'] = $this->request->param('logo/s');
  50. $param['skin'] = $this->request->param('skin/s');
  51. $param['title'] = $this->request->param('title/s');
  52. $param['note'] = $this->request->param('note/s');
  53. $param['telphone'] = $this->request->param('telphone/s');
  54. $param['vip_title'] = $this->request->param('vip_title/s','');
  55. $param['vip_about'] = $this->request->param('vip_about/s','');
  56. $param['checked'] = $this->request->param('checked/d',0);
  57. $param['signkey'] = $this->request->param('signkey/s');
  58. $param['sign'] = $this->request->param('sign');
  59. $rel = $this->apiSign($param);
  60. if($rel['code'] != 200){
  61. return enjson($rel['code'],'签名验证失败');
  62. }
  63. $data['title'] = $param['title'];
  64. $data['note'] = $param['note'];
  65. $rel = AllwinInfoMp::where(['uid' => $this->user->id])->find();
  66. if($rel){
  67. if($rel->is_lock){
  68. return enjson(403,'城市号已被锁定');
  69. }
  70. //判断是否修改skin
  71. if(!empty($param['skin'])){
  72. $rel->skin = $param['skin'];
  73. }
  74. //判断是否修改logo
  75. if(!empty($param['logo'])){
  76. $rel->logo = $param['logo'];
  77. }
  78. if(!$rel->is_apply && !$rel->is_vip){
  79. $rel->is_apply = $param['checked'] ? 1 : 0;
  80. $rel->vip_title = $param['vip_title'];
  81. $rel->vip_about = $param['vip_about'];
  82. }
  83. $result = $rel->save();
  84. }else{
  85. $data['member_miniapp_id'] = $this->miniapp_id;
  86. $data['uid'] = $this->user->id;
  87. $data['is_apply'] = $param['checked'] ? 1 : 0;
  88. $data['is_vip'] = 0;
  89. $data['vip_title'] = $param['vip_title'];
  90. $data['vip_about'] = $param['vip_about'];
  91. $data['reg_time'] = time();
  92. $data['vip_time'] = time();
  93. $data['fans'] = AllwinInfoFollow::where(['uid' => $this->user->id])->count();
  94. $data['logo'] = empty($param['logo']) ? $this->user->face: $param['logo'];
  95. $data['skin'] = empty($param['skin']) ? Config::get('upload.upload_relative').'static/allwin/skin/2.jpg':$param['skin'];
  96. $result = AllwinInfoMp::create($data);
  97. }
  98. if($result){
  99. return enjson(200,'城市号设置成功');
  100. }
  101. return enjson(403,'设置失败');
  102. }
  103. /**
  104. * VIP认证
  105. */
  106. public function vipAuth(){
  107. if(request()->isPost()){
  108. $rel = AllwinInfoMp::where(['uid' => $this->user->id,'is_lock' => 0,'is_vip' => 1])->count();
  109. if($rel){
  110. return enjson(200,'认证用户',['vip'=>1]);
  111. }
  112. }
  113. return enjson(200,'非认证用户',['vip'=>0]);
  114. }
  115. /**
  116. * 商城图片
  117. */
  118. public function upImg(){
  119. if(request()->isPost()){
  120. $rel = Upload::index();
  121. if($rel['error'] == 0){
  122. return enjson(200,'success',$rel['url']);
  123. }else{
  124. return enjson(204);
  125. }
  126. }
  127. }
  128. /**
  129. * 城市号信息
  130. */
  131. public function getMp(){
  132. $param['uid'] = $this->request->param('uid/d',0);
  133. $param['signkey'] = $this->request->param('signkey/s');
  134. $param['sign'] = $this->request->param('sign');
  135. $rel = $this->apiSign($param);
  136. if($rel['code'] != 200){
  137. return enjson($rel['code'],'签名验证失败');
  138. }
  139. $where = [];
  140. $where['uid'] = $param['uid'];
  141. $rel = AllwinInfoMp::where($where)->find();
  142. if($rel){
  143. if($rel->is_lock){
  144. return enjson(403,'城市号不存在');
  145. }
  146. $rel->views = AllwinInfo::where(['user_id' => $rel->uid])->count();
  147. $rel->like = AllwinInfoFollow::where(['uid' => $rel->uid])->count();
  148. $rel->is_follow = 0;
  149. $rel->vip_time = empty($rel->vip_time)?'0':date('Y-m-d',$rel->vip_time);
  150. $rel->reg_time = empty($rel->reg_time)?'0':date('Y-m-d',$rel->reg_time);
  151. if($this->user){
  152. $rel->is_follow = AllwinInfoFollow::where(['uid' => $this->user->id,'like_uid'=>$rel->uid])->count();
  153. }
  154. return enjson(200,'成功',$rel);
  155. }
  156. return enjson(204);
  157. }
  158. /**
  159. * 发表信息
  160. */
  161. public function send(){
  162. if(request()->isPost()){
  163. $param = [
  164. 'content' => $this->request->param('content/s'),
  165. 'telphone' => $this->request->param('telphone/s',''),
  166. 'topday' => $this->request->param('topday/d',0),
  167. 'cate_id' => $this->request->param('cate_id/d',0),
  168. 'store_id' => $this->request->param('store_id/d',0),
  169. 'imgs' => $this->request->param('imgs/s','[]','htmlspecialchars_decode'),
  170. 'fields' => $this->request->param('fields/s','[]','htmlspecialchars_decode'),
  171. 'is_get' => $this->request->param('is_get/d',0),
  172. 'price' => $this->request->param('price/d'),
  173. 'sign' => $this->request->param('sign/s',0),
  174. ];
  175. $rel = $this->apiSign($param);
  176. if($rel['code'] != 200){
  177. return enjson($rel['code'],'签名验证失败');
  178. }
  179. $param['member_miniapp_id'] = $this->miniapp_id;
  180. $param['user_id'] = $this->user->id;
  181. $validate = $this->validate($param,'Info.sendInfo');
  182. if(true !== $validate){
  183. return json(['code'=>403,'msg'=>$validate]);
  184. }
  185. $param['images'] = json_encode(Filter::filter_escape(json_decode($param['imgs'],true)));
  186. $param['fields'] = json_encode(Filter::filter_escape(json_decode($param['fields'],true)));
  187. //置顶
  188. $amount = 0;
  189. $config = AllwinInfoConfig::config($this->miniapp_id);
  190. if($param['topday'] && !empty($config)){
  191. $topday = json_decode($config['topday'],true);
  192. foreach ($topday as $value) {
  193. if($value['day'] == $param['topday']){
  194. $amount = $value['money'];
  195. $param['topday'] = $value['day'];
  196. }
  197. }
  198. }
  199. $param['top_money'] = $amount; //置顶金额
  200. $param['order_no'] = $this->user->invite_code.order_no();
  201. $result = AllwinInfo::postThemes($param);
  202. if($result){
  203. SystemMemberSms::sms($this->miniapp_id,'您有一条信息管理待审核',url('allwin/info.index/index'));
  204. if($amount > 0){
  205. $order = [
  206. 'openid' => $this->user['miniapp_uid'],
  207. 'miniapp_id' => $this->miniapp_id,
  208. 'name' => '信息置顶',
  209. 'order_no' => $param['order_no'],
  210. 'total_fee' => $amount,
  211. 'notify_url' => api(4,'allwin/infonotify/index',$this->miniapp_id)
  212. ];
  213. $setting = AllwinConfig::getConfig($this->miniapp_id);
  214. if($setting->is_psp == 1){
  215. $default_mchid = MchId::getMch(0,$this->miniapp_id);
  216. if (empty($default_mchid)) {
  217. return enjson(403,'未找到商户号');
  218. }
  219. $order['mchid'] = $default_mchid->mchid;
  220. }
  221. $ispay = WechatPay::orderPay($order);;
  222. if($ispay['code'] == 0){
  223. return json(['code'=>403,'msg'=>$ispay['msg']]);
  224. }
  225. return enjson(200,'发布成功,内容需审核!',$ispay['data']);
  226. }else{
  227. return enjson(204,'发布成功,内容需审核!');
  228. }
  229. }else{
  230. return enjson(403,'发布失败');
  231. }
  232. }
  233. }
  234. /**
  235. * 发布评论
  236. */
  237. public function postReply(){
  238. if(request()->isPost()){
  239. $param = [
  240. 'content' => $this->request->param('content/s'),
  241. 'info_id' => $this->request->param('info_id/d'),
  242. 'sign' => $this->request->param('sign/s',0),
  243. ];
  244. $rel = $this->apiSign($param);
  245. if($rel['code'] != 200){
  246. return enjson($rel['code'],'签名验证失败');
  247. }
  248. $data = [
  249. 'content' => $param['content'],
  250. 'info_id' => $param['info_id'],
  251. 'user_id' => $this->user->id,
  252. 'member_miniapp_id' => $this->miniapp_id,
  253. ];
  254. $validate = $this->validate($data,'Info.sendReply');
  255. if(true !== $validate){
  256. return enjson(403,$validate);
  257. }
  258. $result = AllwinInfoReply::postReply($data);
  259. if($result){
  260. AllwinInfo::where(['id' => $param['info_id']])->setInc('reply_num',1);
  261. return enjson(200,'评论成功');
  262. }else{
  263. return enjson(403,'评论失败');
  264. }
  265. }
  266. }
  267. /**
  268. * 读取微信API
  269. * @param integer 读取ID
  270. * @return json
  271. */
  272. public function getTpl(){
  273. $param['id'] = $this->request->param('id/d',0);
  274. $param['signkey'] = $this->request->param('signkey');
  275. $param['sign'] = $this->request->param('sign');
  276. $rel = $this->apiSign($param);
  277. if($rel['code'] != 200){
  278. return enjson($rel['code'],'签名验证失败');
  279. }
  280. $rel = AllwinInfoCate::cateTpl($param['id']);
  281. return enjson(200,'成功',$rel);
  282. }
  283. /**
  284. * 点赞
  285. **/
  286. public function like(){
  287. $param['id'] = $this->request->param('id/d',0);
  288. $param['sign'] = $this->request->param('sign');
  289. $rel = $this->apiSign($param);
  290. if($rel['code'] != 200){
  291. return enjson($rel['code'],'签名验证失败');
  292. }
  293. $condition['member_miniapp_id'] = $this->miniapp_id;
  294. $condition['id'] = $param['id'];
  295. $rel = AllwinInfo::where($condition)->find();
  296. $like_face = json_decode(empty($rel->like_face) ? '[]':$rel->like_face,true);
  297. if(count($like_face) < 10){
  298. if(!in_array($this->user->face,$like_face)){
  299. array_push($like_face,$this->user->face);
  300. $rel->like_face = json_encode($like_face);
  301. }
  302. }
  303. $rel->like = ['inc',1];
  304. $rel->save();
  305. if(empty($rel)){
  306. return enjson(403,'不用重复点赞');
  307. }else{
  308. return enjson(200);
  309. }
  310. }
  311. /**
  312. * 评论点赞
  313. * @return void
  314. */
  315. public function replylike(){
  316. $param['id'] = $this->request->param('id/d',0);
  317. $param['sign'] = $this->request->param('sign');
  318. $rel = $this->apiSign($param);
  319. if($rel['code'] != 200){
  320. return enjson($rel['code'],'签名验证失败');
  321. }
  322. $condition['member_miniapp_id'] = $this->miniapp_id;
  323. $condition['id'] = $param['id'];
  324. $rel = AllwinInfoReply::where($condition)->setInc('like',1);
  325. if(empty($rel)){
  326. return enjson(403,'不用重复点赞');
  327. }else{
  328. return enjson(200);
  329. }
  330. }
  331. /**
  332. * 关注或取消
  333. */
  334. public function follow(){
  335. $param['uid'] = $this->request->param('uid/d');
  336. $param['state'] = $this->request->param('state/d',0);
  337. $param['sign'] = $this->request->param('sign');
  338. $rel = $this->apiSign($param);
  339. if($rel['code'] != 200){
  340. return enjson($rel['code'],'签名验证失败');
  341. }
  342. if($param['uid'] == $this->user->id){
  343. return enjson(403,'不用关注自己');
  344. }
  345. //判断关注用户是否存在
  346. $is_user = SystemUser::where(['id' => $param['uid'],'is_lock' => 0,'is_delete' => 0])->find();
  347. if(empty($is_user)){
  348. return enjson(403,'当前用户已被封号');
  349. }
  350. //关注
  351. $condition['like_uid'] = $param['uid'];
  352. $condition['uid'] = $this->user->id;
  353. $info = AllwinInfoFollow::where($condition)->find();
  354. if(empty($info)){
  355. $follow = AllwinInfoFollow::where(['uid' => $this->user->id,'like_uid' => 0])->find();
  356. if($param['state'] == 1){
  357. if(empty($follow)){
  358. $rel = AllwinInfoFollow::create(['uid' => $this->user->id,'like_uid' => $param['uid']]);
  359. }else{
  360. $follow->like_uid = $param['uid'];
  361. $rel = $follow->save();
  362. }
  363. }else{
  364. return enjson(403,'关注失败');
  365. }
  366. }else{
  367. if($param['state'] == 0){
  368. $info->like_uid = 0;
  369. $rel = $info->save();
  370. }
  371. }
  372. if(empty($rel)){
  373. return enjson(403,'关注失败');
  374. }else{
  375. //增加粉丝
  376. $infomp = AllwinInfoMp::where(['uid' => $param['uid']])->find();
  377. if($infomp){
  378. if($param['state']){
  379. $infomp->fans = ['inc',1];
  380. }else{
  381. $infomp->fans = ['dec',1];
  382. }
  383. $infomp->save();
  384. }
  385. return enjson(200,$param['state']?'已关注':'取消关注');
  386. }
  387. }
  388. }