Voice.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Http\Controllers\Gnight;
  3. use App\Exceptions\AlertException;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Gnight\GetVoiceModel;
  7. use App\Models\Gnight\UserModel;
  8. use App\Http\Controllers\Miniprogram\Auth;
  9. class Voice extends Controller
  10. {
  11. public static function __callStatic($name, $args)
  12. {
  13. $getVoice = new GetVoiceModel();
  14. return call_user_func_array([$getVoice, $name], $args);
  15. }
  16. /**
  17. * @param int $voice_id
  18. * @return array
  19. * @throws \ApiException
  20. * @api {get} /api/gnight/voices/:voice_id 语音信息
  21. * @apiName get
  22. * @apiGroup Gnight
  23. *
  24. * @apiSuccess {int} uid 用户id
  25. * @apiSuccess {string} headimgurl 头像
  26. * @apiSuccess {string} nickname 昵称
  27. * @apiSuccess {int} sex 性别0未知1男2女
  28. * @apiSuccess {int} like 喜欢0所有1男2女
  29. * @apiSuccess {string} voice 语音
  30. * @apiSuccess {int} voice_state 语音状态:-1:审核不通过;-2:系统下线;0下线;1:上线;2:审核
  31. * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
  32. * @apiSuccess {string} contact 联系方式,未解锁时无此字段
  33. * @apiSuccess {int} lock 解锁进度
  34. *
  35. */
  36. public function get(int $voice_id)
  37. {
  38. try {
  39. $uid = Auth::auth();
  40. $getVoice = GetVoiceModel::where([
  41. ['uid', $uid],
  42. ['get_uid', $voice_id]
  43. ])->first();
  44. if (collect($getVoice)->isEmpty()) {
  45. throw new \ApiException("你没有获得这个语音", 401);
  46. }
  47. $user = UserModel::findOrFail(
  48. $voice_id,
  49. ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type', 'contact']
  50. );
  51. if ($getVoice->lock < 3) {
  52. unset($user->contact);
  53. }
  54. return array(
  55. 'code' => 200,
  56. 'message' => 'success',
  57. 'data' => $user
  58. );
  59. } catch (\Exception $e) {
  60. throw new \ApiException($e->getMessage(), intval($e->getCode()));
  61. }
  62. }
  63. /**
  64. * @return array
  65. * @throws \ApiException
  66. * @api {get} /api/gnight/voices/getbyshare 群分享获取语音
  67. * @apiName getbyshare
  68. * @apiGroup Gnight
  69. *
  70. * @apiSuccess {int} uid 语音id
  71. * @apiSuccess {string} headimgurl 头像
  72. * @apiSuccess {string} nickname 昵称
  73. * @apiSuccess {int} sex 性别0未知1男2女
  74. * @apiSuccess {int} like 喜欢0所有1男2女
  75. * @apiSuccess {string} voice 语音
  76. * @apiSuccess {int} voice_state 语音状态:-1:审核不通过;-2:系统下线;0下线;1:上线;2:审核
  77. * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
  78. * @apiSuccess {int} lock 解锁进度
  79. *
  80. */
  81. public function getByShare()
  82. {
  83. try {
  84. $uid = Auth::auth();
  85. $user = UserModel::findOrFail($uid);
  86. if ($user->share_group >= 1) {
  87. throw new \ApiException("分享次数已达上限", 101);
  88. }
  89. $getVoiceModel = new GetVoiceModel();
  90. $voice_id = $getVoiceModel->pop($uid);
  91. $getVoice = $getVoiceModel->fill([
  92. 'uid' => $uid,
  93. 'get_uid' => $voice_id
  94. ]);
  95. $getVoice->save();
  96. UserModel::where('uid', $uid)->increment('share_group', 1);
  97. $voice = UserModel::findOrFail(
  98. $voice_id,
  99. ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type']
  100. );
  101. return array(
  102. 'code' => 200,
  103. 'message' => 'success',
  104. 'data' => $voice
  105. );
  106. } catch (\Exception $e) {
  107. throw new \ApiException($e->getMessage(), intval($e->getCode()));
  108. }
  109. }
  110. /**
  111. * @return array
  112. * @throws AlertException
  113. * @throws \ApiException
  114. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  115. * @api {get} /api/gnight/voices/getbyfirst 获取语音
  116. * @apiDescription 首次上次语音获取语音
  117. * @apiName getbyfirst
  118. * @apiGroup Gnight
  119. *
  120. * @apiSuccess {int} uid 语音id
  121. * @apiSuccess {string} headimgurl 头像
  122. * @apiSuccess {string} nickname 昵称
  123. * @apiSuccess {int} sex 性别0未知1男2女
  124. * @apiSuccess {int} like 喜欢0所有1男2女
  125. * @apiSuccess {string} voice 语音
  126. * @apiSuccess {int} voice_state 语音状态:-1:审核不通过;-2:系统下线;0下线;1:上线;2:审核
  127. * @apiSuccess {int} contact_type 联系方式0:不存在;1电话2:微信;3:qq
  128. * @apiSuccess {int} lock 解锁进度
  129. *
  130. */
  131. public function getByFirst()
  132. {
  133. $uid = Auth::auth();
  134. $user = UserModel::findOrFail($uid);
  135. if ($user->first_voice >= 1) {
  136. throw new AlertException("你已领取过", 101);
  137. }
  138. $getVoiceModel = new GetVoiceModel();
  139. $voice_id = $getVoiceModel->pop($uid);
  140. $getVoice = $getVoiceModel->fill([
  141. 'uid' => $uid,
  142. 'get_uid' => $voice_id
  143. ]);
  144. $getVoice->save();
  145. UserModel::where('uid', $uid)->increment('first_voice', 1);
  146. $voice = UserModel::findOrFail(
  147. $voice_id,
  148. ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type']
  149. );
  150. return array(
  151. 'code' => 200,
  152. 'message' => 'success',
  153. 'data' => $voice
  154. );
  155. }
  156. /**
  157. * @param int $voice_id
  158. * @return array
  159. * @api {get} /api/gnight/voices/getshare/:voince_id 获取分享语音信息
  160. * @apiName getshare
  161. * @apiGroup Gnight
  162. *
  163. * @apiSuccess {int} uid 用户id
  164. * @apiSuccess {string} headimgurl 头像
  165. * @apiSuccess {string} nickname 昵称
  166. * @apiSuccess {int} sex 性别0未知1男2女
  167. * @apiSuccess {string} voice 语音
  168. */
  169. public function getShare(int $voice_id)
  170. {
  171. $userModel = new UserModel();
  172. $user = $userModel->findOrFail(
  173. $voice_id,
  174. ['uid', 'headimgurl', 'nickname', 'sex', 'voice', 'voice_state', 'contact_type']
  175. );
  176. return array(
  177. 'code' => 200,
  178. 'message' => 'success',
  179. 'data' => $user
  180. );
  181. }
  182. /**
  183. * @param int $voice_id
  184. * @return array
  185. * @throws \ApiException
  186. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  187. * @api {get} /api/gnight/voices/getvoice/:voince_id 获取分享语音
  188. * @apiName getvoice
  189. * @apiGroup Gnight
  190. *
  191. * @apiSuccess {int} code 200
  192. * @apiSuccess {string} message success
  193. * @apiSuccess {int} data.get_voice_id 获取语音记录的id
  194. */
  195. public function getVoice(int $voice_id)
  196. {
  197. $uid = Auth::auth();
  198. $userModel = new UserModel();
  199. $user = UserModel::findOrFail(
  200. $voice_id,
  201. ['uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type', 'contact']
  202. );
  203. if ($uid == $user->uid) {
  204. throw new \ApiException("不能获取自己的语音", 105);
  205. }
  206. $getVoiceModel = new GetVoiceModel();
  207. $getVoice = $getVoiceModel->where([
  208. ['uid', $uid],
  209. ['get_uid', $voice_id]
  210. ])->first();
  211. if (collect($getVoice)->isEmpty()) {
  212. $getVoiceModel = new GetVoiceModel();
  213. $getVoice = $getVoiceModel->fill([
  214. 'uid' => $uid,
  215. 'get_uid' => $user->uid
  216. ]);
  217. $getVoice->save();
  218. }
  219. $user->get_voice_id = $getVoice->id;
  220. $user->lock = $getVoice->lock ?? 0;
  221. if ($user->lock < 3) {
  222. unset($user->contact);
  223. }
  224. return array(
  225. 'code' => 200,
  226. 'message' => 'success',
  227. 'data' => $user,
  228. );
  229. }
  230. /**
  231. * 修改语音状态
  232. * @param Request $request
  233. * @return array
  234. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  235. */
  236. public function updateState(Request $request)
  237. {
  238. $uid = Auth::auth();
  239. $userModel = new UserModel();
  240. $user = UserModel::findOrFail($uid);
  241. $user->voice_state = $request->input('voice_state');
  242. if ($user->save()) {
  243. return array(
  244. 'code' => 200,
  245. 'message' => 'success'
  246. );
  247. }
  248. }
  249. }