DreamController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\AccountLog;
  4. use App\Models\BaseSettingsModel;
  5. use App\Models\DreamImages;
  6. use App\Models\DreamInfoModel;
  7. use App\Models\SupportDreamModel;
  8. use App\Models\UserCareDream;
  9. use App\Models\UserInfoModel;
  10. use Illuminate\Http\Request;
  11. use App\Services\Base\ErrorCode;
  12. class DreamController extends Controller
  13. {
  14. /**
  15. * @api {get} /api/dream/index 梦想
  16. * @apiDescription 梦想
  17. * @apiGroup Dream
  18. * @apiPermission Passport
  19. * @apiVersion 0.1.0
  20. * @apiParam {int} type interaction paihang 默认index
  21. * @apiSuccessExample {json} Success-Response:
  22. * HTTP/1.1 200 OK
  23. * 主页
  24. *{
  25. * "status": true,
  26. * "status_code": 0,
  27. * "message": "",
  28. * "data": {
  29. * "dream": { 梦想
  30. * "id": 7,
  31. * "user_id": 2,
  32. * "dream": "梦想标1123",
  33. * "about": "梦想烧1111213",
  34. * "video": "",
  35. * "sign": null,
  36. * "money": 10000,
  37. * "time": 720000,
  38. * "get_money": 0,
  39. * "status": 0,
  40. * "updated_at": "2017-06-22 09:47:03"
  41. * },
  42. * "imgs": [ 梦想图片
  43. * {
  44. * "title": "",
  45. * "pic": "http://w17.9026.com/img/banner/banner_3.png"
  46. * },
  47. * {
  48. * "title": "",
  49. * "pic": "http://w17.9026.com/img/banner/banner_3.png"
  50. * }
  51. * ],
  52. * "arr": { 支持者前三(user_idh和头像)
  53. * "1": "http://www.wsfjq.com/photos/bd119684755.jpg",
  54. * "6": "http://www.wsfjq.com/photos/bd119684755.jpg"
  55. * }
  56. * "money" : "1000" 用户余额
  57. * "score" : "1000" 梦想分数
  58. * "a": "0.001", 支持乘数计算 y = -a * b +c b = updated_at-当前时间 (分钟) y>=1
  59. * "c": "8"
  60. * }
  61. *}
  62. * 排行
  63. *{
  64. * "status": true,
  65. * "status_code": 0,
  66. * "message": "",
  67. * "data": [
  68. * {
  69. * "nickname": "ha", 昵称
  70. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg", 头像
  71. * "score": 112000 支持分
  72. * },
  73. * ]
  74. *}
  75. *
  76. * @apiErrorExample {json} Error-Response:
  77. *HTTP/1.1 400 Bad Request
  78. *{
  79. * "status": false,
  80. * "status_code": 1105,
  81. * "message": "用户不存在",
  82. * "data": null
  83. * }
  84. */
  85. public function index(Request $request)
  86. {
  87. $user = $this->getUser();
  88. $type = $request->type; //类型
  89. $id = $request->id; //梦想ID
  90. if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
  91. if ($type == 'paihang') {
  92. $data = [];
  93. $info = SupportDreamModel::where('dream_id',$id)->get();
  94. $arr = [] ;
  95. foreach ($info as $item) {
  96. if (!array_key_exists($item->user_id,$arr)) {
  97. $arr[$item->user_id] = $item->score;
  98. }else{
  99. $arr[$item->user_id] += $item->score;
  100. }
  101. }
  102. arsort($arr);
  103. foreach ($arr as $k => $v){
  104. $user = UserInfoModel::find($k);
  105. $user->score = $v;
  106. $data[] = $user;
  107. }
  108. return $this->api($data);
  109. } elseif ($type == 'interaction') {
  110. }else{
  111. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  112. $score = 0 ;
  113. $a = SupportDreamModel::where('dream_id',$id)->get();
  114. foreach ($a as $value){
  115. $score += $value->score;
  116. }
  117. $money = $user->money;
  118. $number = BaseSettingsModel::where('category','score')->select('key','value')->first();
  119. $a = count($number) > 0 ? $number->key : '';
  120. $c = count($number) > 0 ? $number->value : '';
  121. $dream = DreamInfoModel::find($id);
  122. $imgs = DreamImages::where('dream_id',$id)->orderBy('id','desc')->get();
  123. $info = SupportDreamModel::where('dream_id',$id)->get();
  124. $arr = [] ;
  125. foreach ($info as $item) {
  126. if (!array_key_exists($item->user_id,$arr)) {
  127. $arr[$item->user_id] = $item->score;
  128. }else{
  129. $arr[$item->user_id] += $item->score;
  130. }
  131. }
  132. arsort($arr);
  133. foreach ($arr as $k => $v){
  134. $arr[$k] = UserInfoModel::find($k)->pic;
  135. }
  136. return $this->api(compact('dream','imgs','money','arr','a','c','score'));
  137. }
  138. }
  139. /**
  140. * @api {post} /api/dream/support 支持梦想
  141. * @apiDescription 支持梦想
  142. * @apiGroup Dream
  143. * @apiPermission Passport
  144. * @apiVersion 0.1.0
  145. * @apiParam {int} coin 支持梦想币数量
  146. * @apiParam {int} id 梦想ID
  147. * @apiParam {int} number 支持乘数
  148. * @apiSuccessExample {json} Success-Response:
  149. * HTTP/1.1 200 OK
  150. *{
  151. * "status": true,
  152. * "status_code": 0,
  153. * "message": "",
  154. * "data": ""
  155. *}
  156. * @apiErrorExample {json} Error-Response:
  157. *HTTP/1.1 400 Bad Request
  158. * {
  159. * "state": false,
  160. * "code": 1000,
  161. * "message": "传入参数不正确",
  162. * "data": null or []
  163. * }
  164. * 可能出现的代码
  165. * {
  166. * "status": false,
  167. * "status_code": 1303,
  168. * "message": "商户余额不足",
  169. * "data": null
  170. * }
  171. *
  172. */
  173. public function support(Request $request)
  174. {
  175. $validator = \Validator::make($request->all(),
  176. [
  177. 'coin' => 'required',
  178. 'id' => 'required',
  179. 'number' => 'required',
  180. ],
  181. [
  182. 'coin.required' => '梦想币不能为空',
  183. 'id.required' => '支持梦想不能为空',
  184. 'number.required' => '支持乘数不能为空',
  185. ]
  186. );
  187. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  188. $user = $this->getUser();
  189. $dream_id = $request->id;
  190. $dream_info = DreamInfoModel::find($dream_id);
  191. $user_id = $dream_info->user_id;
  192. $coin = $request->coin;
  193. $number = $request->number;
  194. if ($user->money < $coin) {
  195. return $this->error(ErrorCode::MERCHANT_BALANCE_NOT_ENOUGH);
  196. }else{
  197. $user->money = $user->money - $coin;
  198. $user->save();
  199. $dream_info->get_money += $coin;
  200. $dream_info->save();
  201. $data = [
  202. 'user_id'=>$user->id,
  203. 'dream_id'=>$dream_id,
  204. 'to_user_id'=>$user_id,
  205. 'coin'=>$coin,
  206. 'score'=>$coin*$number,
  207. ];
  208. $ok = SupportDreamModel::create($data);
  209. if (!$ok) {
  210. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  211. }
  212. // 记录充值记录
  213. $data = [
  214. 'from_id' =>$user->id,
  215. 'to_id' => $user_id,
  216. 'from_amount' => $coin,
  217. 'to_amount' => $coin,
  218. 'from_type' => AccountLog::TYPE_COIN,
  219. 'to_type' => AccountLog::TYPE_COIN,
  220. 'op' => AccountLog::OP_SUPPORT,
  221. ];
  222. AccountLog::create($data);
  223. return $this->api('');
  224. }
  225. }
  226. // 收藏关注梦想
  227. /**
  228. * @api {get} /api/dream/collection 收藏梦想
  229. * @apiDescription 收藏梦想
  230. * @apiGroup Dream
  231. * @apiPermission Passport
  232. * @apiVersion 0.1.0
  233. * @apiParam {int} id 梦想ID
  234. * @apiSuccessExample {json} Success-Response:
  235. * HTTP/1.1 200 OK
  236. *{
  237. * "status": true,
  238. * "status_code": 0,
  239. * "message": "",
  240. * "data": ""
  241. *}
  242. * @apiErrorExample {json} Error-Response:
  243. *HTTP/1.1 400 Bad Request
  244. * {
  245. * "state": false,
  246. * "code": 1000,
  247. * "message": "传入参数不正确",
  248. * "data": null or []
  249. * }
  250. *
  251. */
  252. public function collection(Request $request)
  253. {
  254. if (empty($request->id)) return $this->error(ErrorCode::KEY_ERROR);
  255. $user = $this->getUser();
  256. $data = [
  257. 'user_id' =>$user->id,
  258. 'dream_id' =>$request->id,
  259. ];
  260. $info = UserCareDream::where('user_id',$user->id)->
  261. where('dream_id',$request->id)->first();
  262. if (count($info) == 0) {
  263. UserCareDream::create($data);
  264. }
  265. return $this->api('');
  266. }
  267. }