OpenidUserController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Jobs\MockThumbJob;
  6. use App\Jobs\WelcomeJob;
  7. use App\Models\PartnerModel;
  8. use App\Models\User\AuthKey;
  9. use App\Models\User\MockThumbModel;
  10. use App\Models\User\OpenidUserModel;
  11. use App\Models\User\UserModel;
  12. use App\Services\Home\FeedService;
  13. use App\Services\User\OpenidUserService;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Config;
  17. use Illuminate\Support\Facades\Redis;
  18. use Tymon\JWTAuth\Exceptions\JWTException;
  19. /**
  20. * Class OpenidUserController
  21. * @package App\Http\Controllers\User
  22. */
  23. class OpenidUserController extends Controller
  24. {
  25. /**
  26. * 更新用户信息
  27. * @param Request $request
  28. * @return array
  29. */
  30. public function update(Request $request)
  31. {
  32. $this->validate($request, ['openid' => 'required']);
  33. $openid = $request->get('openid', null);
  34. $data = $request->post();
  35. $ous = new OpenidUserService();
  36. $ous->update($openid, $data);
  37. return array(
  38. 'code' => 200,
  39. 'message' => 'success'
  40. );
  41. }
  42. /**
  43. * 获取用户信息
  44. * @param Request $request
  45. * @return array
  46. */
  47. public function get(Request $request)
  48. {
  49. $this->validate($request, ['openid' => 'required']);
  50. $openid = $request->get('openid', null);
  51. $ous = new OpenidUserService();
  52. $data = $ous->get($openid);
  53. MockThumbJob::dispatch($openid)->delay(Carbon::now()->addHours(1));
  54. return array(
  55. 'code' => 200,
  56. 'message' => 'success',
  57. 'data' => $data
  58. );
  59. }
  60. /**
  61. * 用户信息流
  62. * @param Request $request
  63. * @param FeedService $fs
  64. * @return array
  65. */
  66. public function feed(Request $request, FeedService $fs)
  67. {
  68. $this->validate($request, ['openid' => 'required']);
  69. $openid = $request->get('openid', null);
  70. /** @var OpenidUserModel $openuser */
  71. $openuser = OpenidUserModel::where('openid', $openid)->firstOrFail();
  72. // 滑动的性别
  73. if (in_array($openuser->feed_sex, [1, 2])) {
  74. $sxo = $openuser->feed_sex;
  75. } else {
  76. $sxo = 2;
  77. }
  78. $data = $openuser->toArray();
  79. $data['uid'] = $openuser->openid;
  80. $data['partner_id'] = 0;
  81. $data['feed_level'] = 'A';
  82. $data['only_last'] = 0;
  83. $res = $fs->feed($data, $sxo);
  84. return array(
  85. 'code' => 200,
  86. 'message' => 'success',
  87. 'data' => $res
  88. );
  89. }
  90. /**
  91. * 同步openid信息
  92. * @param Request $request
  93. * @return array
  94. */
  95. public function sync(Request $request)
  96. {
  97. $this->validate($request, ['openid' => 'required']);
  98. try {
  99. $openid = $request->get('openid', null);
  100. $uid = Auth::auth();
  101. $opendata = OpenidUserModel::where('openid', $openid)->firstOrFail();
  102. $user = UserModel::findOrFail($uid);
  103. empty($user->age) && $user->age = $opendata->age;
  104. 0 == $user->sex && $user->sex = $opendata->sex;
  105. if (!UserModel::where('phone', $opendata->phone)->exists() && empty($user->phone)) {
  106. $user->phone = $opendata->phone;
  107. }
  108. $user->save();
  109. $opendata->sync_at = time();
  110. $opendata->save();
  111. } catch (\Exception $exception) {
  112. }
  113. return array(
  114. 'code' => 200,
  115. 'message' => 'success'
  116. );
  117. }
  118. /**
  119. * 发送欢迎语
  120. * @param Request $request
  121. * @return array
  122. * @throws \Exception
  123. */
  124. public function welcome(Request $request)
  125. {
  126. $this->validate($request, ['openid' => 'required']);
  127. $openid = $request->get('openid', null);
  128. WelcomeJob::dispatch($openid)->delay(Carbon::now()->addHours(1));
  129. Redis::sadd("fpdx:welcome:inqueue:openids", $openid);
  130. return array(
  131. 'code' => 200,
  132. 'message' => 'success'
  133. );
  134. }
  135. /**
  136. * 喜欢我的
  137. * @param Request $request
  138. * @return array
  139. */
  140. public function likeme(Request $request)
  141. {
  142. $openid = $request->get('openid', null);
  143. $where = array();
  144. try {
  145. $uid = Auth::auth();
  146. $openid = AuthKey::where([
  147. ['uid', $uid],
  148. ['auth_type', config('miniprogram.public_id')]
  149. ])->value('auth_key');
  150. } catch (JWTException $exception) {
  151. $uid = null;
  152. }
  153. !is_null($openid) && array_push($where, $openid);
  154. !is_null($uid) && array_push($where, (string)$uid);
  155. $datas = array();
  156. try {
  157. /** @var MockThumbModel[] $mocks */
  158. $mocks = MockThumbModel::whereIn('thumb_user', $where)->distinct()->get();
  159. foreach ($mocks as $mock) {
  160. /** @var PartnerModel $partner */
  161. $partner = PartnerModel::where('uid', $mock->uid)->firstOrFail();
  162. $datas[] = array(
  163. 'lock' => 0,
  164. 'read' => 1,
  165. 'praise_id' => 0,
  166. 'id' => $partner->id,
  167. 'uid' => $partner->uid,
  168. 'photo_src' => empty($partner->photo_src) ? "" : "http://oss.pocketuniversity.cn{$partner->photo_src}",
  169. 'voice' => $partner->voice,
  170. 'photo_1' => empty($partner->photo_1) ? "" : "http://oss.pocketuniversity.cn{$partner->photo_1}",
  171. 'photo_2' => empty($partner->photo_2) ? "" : "http://oss.pocketuniversity.cn{$partner->photo_2}",
  172. 'photo_3' => empty($partner->photo_3) ? "" : "http://oss.pocketuniversity.cn{$partner->photo_3}",
  173. 'photo_4' => empty($partner->photo_4) ? "" : "http://oss.pocketuniversity.cn{$partner->photo_4}",
  174. 'praises' => $partner->praises,
  175. 'updated_at' => $mock->created_at->timestamp,
  176. 'user' => UserModel::find($mock->uid),
  177. );
  178. }
  179. } catch (\Exception $exception) {
  180. }
  181. return array(
  182. 'code' => 200,
  183. 'message' => 'success',
  184. 'data' => [
  185. 'list' => $datas,
  186. ]
  187. );
  188. }
  189. }