Controller.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Credit;
  4. use App\Models\Doing;
  5. use App\Models\Notification;
  6. use App\Models\User;
  7. use App\Models\UserData;
  8. use App\Services\CreditService;
  9. use App\Services\DoingService;
  10. use App\Services\NotificationService;
  11. use Carbon\Carbon;
  12. use Illuminate\Foundation\Bus\DispatchesJobs;
  13. use Illuminate\Routing\Controller as BaseController;
  14. use Illuminate\Foundation\Validation\ValidatesRequests;
  15. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\Config;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Mail;
  20. use Illuminate\Support\Facades\Session;
  21. use Larastarscn\AliDaYu\Facades\AliDaYu;
  22. abstract class Controller extends BaseController
  23. {
  24. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  25. /**
  26. * 操作成功提示
  27. * @param $url string
  28. * @param $message 消息内容
  29. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  30. */
  31. protected function success($url,$message,$intended=false)
  32. {
  33. Session::flash('message',$message);
  34. Session::flash('message_type',2);
  35. if($intended){
  36. return redirect()->intended($url);
  37. }
  38. return redirect($url);
  39. }
  40. protected function error($url,$message)
  41. {
  42. Session::flash('message',$message);
  43. Session::flash('message_type',1);
  44. return redirect($url);
  45. }
  46. protected function showErrorMsg($url , $message){
  47. return view('errors.error')->with(compact('url','message'));
  48. }
  49. /**
  50. * 成功回调
  51. * @param $message
  52. */
  53. protected function ajaxSuccess($message){
  54. $data = array(
  55. 'code' => 0,
  56. 'message' => $message
  57. );
  58. return response()->json($data);
  59. }
  60. /**
  61. * 错误处理
  62. * @param $code
  63. * @param $message
  64. */
  65. protected function ajaxError($code,$message){
  66. $data = array(
  67. 'code' => $code,
  68. 'message' => $message
  69. );
  70. return response()->json($data);
  71. }
  72. /**
  73. * 修改用户积分
  74. * @param $user_id 用户id
  75. * @param $action 执行动作:提问、回答、发起文章
  76. * @param int $source_id 源:问题id、回答id、文章id等
  77. * @param string $source_subject 源主题:问题标题、文章标题等
  78. * @param int $coins 金币数/财富值
  79. * @param int $credits 经验值
  80. * @return bool 操作成功返回true 否则 false
  81. */
  82. protected function credit($user_id,$action,$coins = 0,$credits = 0,$source_id = 0 ,$source_subject = null,$through=false)
  83. {
  84. return CreditService::create($user_id,$action,$coins,$credits,$source_id,$source_subject,$through);
  85. }
  86. /**
  87. * 记录用户动态
  88. * @param $user_id 动态发起人
  89. * @param $action 动作 ['ask','answer',...]
  90. * @param $source_id 问题或文章ID
  91. * @param $subject 问题或文章标题
  92. * @param string $content 回答或评论内容
  93. * @param int $refer_id 问题或者文章ID
  94. * @param int $refer_user_id 引用内容作者ID
  95. * @param null $refer_content 引用内容
  96. * @return static
  97. */
  98. protected function doing($user_id,$action,$source_type,$source_id,$subject,$content='',$refer_id=0,$refer_user_id=0,$refer_content=null)
  99. {
  100. return DoingService::create($user_id,$action,$source_type,$source_id,$subject,$content,$refer_id,$refer_user_id,$refer_content);
  101. }
  102. /**
  103. * 发送用户通知
  104. * @param $from_user_id
  105. * @param $to_user_id
  106. * @param $type
  107. * @param $subject
  108. * @param $source_id
  109. * @return static
  110. */
  111. protected function notify($from_user_id,$to_user_id,$type,$subject='',$source_id=0,$content='',$refer_type='',$refer_id=0)
  112. {
  113. $notificationService = new NotificationService();
  114. return $notificationService->notify($from_user_id,$to_user_id,$type,$subject,$source_id,$content,$refer_type,$refer_id);
  115. }
  116. /**
  117. * 将通知设置为已读
  118. * @param $source_id
  119. * @param string $refer_type
  120. * @return mixed
  121. */
  122. protected function readNotifications($source_id,$refer_type='question')
  123. {
  124. $types = [];
  125. if($refer_type=='article'){
  126. $types = ['comment_article'];
  127. }else if($refer_type=='question'){
  128. $types = ['answer','follow_question','comment_question','invite_answer','adopt_answer'];
  129. }else if($refer_type=='answer'){
  130. $types = ['comment_answer'];
  131. }else if($refer_type == 'user'){
  132. $types = ['follow_user'];
  133. }
  134. $types[] = 'reply_comment';
  135. return Notification::where('to_user_id','=',Auth()->user()->id)->where('source_id','=',$source_id)->whereIn('type',$types)->where('is_read','=',0)->update(['is_read'=>1]);
  136. }
  137. /*邮件发送*/
  138. protected function sendEmail($email,$subject,$message){
  139. return NotificationService::sendEmail($email,$subject,$message);
  140. }
  141. /**
  142. * 业务层计数器
  143. * @param $key 计数器key
  144. * @param null $step 级数步子
  145. * @param int $expiration 有效期
  146. * @return Int count
  147. */
  148. protected function counter($key,$step=null,$expiration=1440){
  149. $count = Cache::get($key,0);
  150. /*直接获取值*/
  151. if( $step === null ){
  152. return $count;
  153. }
  154. $count = $count + $step;
  155. Cache::put($key,$count,$expiration);
  156. return $count;
  157. }
  158. }