UserController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Http\Controllers\Channel\User;
  3. use Illuminate\Http\Request;
  4. use App\Modules\User\Services\UserService;
  5. use App\Http\Controllers\Channel\BaseController;
  6. use App\Modules\Subscribe\Services\BookOrderService;
  7. use App\Modules\Subscribe\Services\ChapterOrderService;
  8. use App\Modules\Subscribe\Services\OrderService;
  9. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  10. use GuzzleHttp\Client;
  11. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  12. use App\Modules\Book\Services\BookConfigService;
  13. use Log;
  14. class UserController extends BaseController
  15. {
  16. protected $secret_key = 'zhuishuyun#_2017';
  17. /**
  18. * @apiDefine User 用户
  19. */
  20. /**
  21. * @apiVersion 1.0.0
  22. * @apiDescription 获取用户信息
  23. * @api {GET} user/userinfo 获取用户信息
  24. * @apiGroup User
  25. * @apiName index
  26. * @apiParam {Number} uid 用户ID.
  27. * @apiSuccess {Number} id 用户ID.
  28. * @apiSuccess {String} openid 微信openid.
  29. * @apiSuccess {String} unionid 微信unionid.
  30. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  31. * @apiSuccess {String} province 省份.
  32. * @apiSuccess {String} city 城市.
  33. * @apiSuccess {String} country 国家.
  34. * @apiSuccess {String} headimgurl 头像地址.
  35. * @apiSuccess {Number} send_order_id 派单ID.
  36. * @apiSuccess {Number=0,1} sex 性别.
  37. * @apiSuccess {String} balance 书币余额.
  38. * @apiSuccess {Int} is_subscribed 是否关注 -1没关注 0 取关,1已关注.
  39. * @apiSuccess {String} subscribed_time 关注时间
  40. * @apiSuccess {String} subscribed_src 关注二维码
  41. * @apiSuccess {String} subscribe_official 公众号名称
  42. * @apiSuccess {String} is_subscribed_text 文本(取关|已关注)
  43. * @apiSuccess {String} created_at 书币余额.
  44. * @apiSuccess {String} balance 书币余额.
  45. * @apiSuccess {String} total_charge 累计余额.
  46. * @apiSuccess {String} total_consume 累计消耗.
  47. * @apiSuccess {String} total_get 累计获取.
  48. * @apiSuccess {String} label 标签
  49. * @apiSuccessExample {json} Success-Response:
  50. *
  51. * {
  52. * "code": 0,
  53. * "msg": "",
  54. * "data": {
  55. * "id": 9,
  56. * "openid": "1212",
  57. * "unionid": "12312322",
  58. * "head_img": "http://www.cmread.com/u/cover_file/3528/413493528/20161228175952/cover180240.jpg",
  59. * "distribution_channel_id": 13,
  60. * "register_ip": "172.18.0.1",
  61. * "send_order_id": 0,
  62. * "balance": 88,
  63. * "charge_balance": 88,
  64. * "reward_balance": 0,
  65. * "sex": 0,
  66. * "country": "",
  67. * "city": "asdf",
  68. * "province": "广东",
  69. * "created_at": "2017-11-24 20:50:05",
  70. * "updated_at": "2017-12-18 22:09:01",
  71. * "nickname": "赵六",
  72. * "is_new": 0,
  73. * "is_subscribed": 1,
  74. * "subscribed_time": "2017-12-11 10:02:43",
  75. * "subscribe_official": "嘉言小说",
  76. * "subscribed_src": false,
  77. * "is_subscribed_text": "已关注",
  78. * "total_charge": 0,
  79. * "total_consume": 123,
  80. * "total_get": 456,
  81. * "label": "都市爱情",
  82. * }
  83. * }
  84. */
  85. public function index(Request $request){
  86. $uid = $request->input('uid');
  87. if(empty($uid)){
  88. return response()->error('PARAM_EMPTY');
  89. }
  90. $channel_id = $this->getChannelId();
  91. if(empty($channel_id)){
  92. return response()->error('PARAM_EMPTY');
  93. }
  94. $data = UserService::getById($uid);
  95. if(!$data){
  96. return response()->error('USER_NOT_FOUND');
  97. }
  98. if($data->distribution_channel_id != $channel_id){
  99. return response()->error('USER_NOT_FOUND');
  100. }
  101. //关注
  102. $forceSubscribe = ForceSubscribeService::forceSubscribeUsersByUidIncludeCancel(['uid'=>$uid]);
  103. if($forceSubscribe){
  104. $data->is_subscribed = $forceSubscribe->is_subscribed;
  105. $data->subscribed_time = date('Y-m-d H:i:s',strtotime($forceSubscribe->created_at));
  106. $official_info = OfficialAccountService::officialAccountByAppid(['appid'=>$forceSubscribe->appid]);
  107. if($official_info){
  108. $data->subscribe_official = $official_info->nickname;
  109. }else{
  110. $data->subscribe_official = '';
  111. }
  112. $data->subscribed_src = $official_info->qrcode_url;
  113. //$this->getRcodeInfo(['appid'=>$forceSubscribe->appid],$forceSubscribe->bid,$uid);
  114. $data->label = '';
  115. if($forceSubscribe->bid){
  116. $book_info = BookConfigService::getBookById($forceSubscribe->bid);
  117. if($book_info) $data->label = $book_info->category_name;
  118. }
  119. if($forceSubscribe->is_subscribed == 1){
  120. $data->is_subscribed_text = '已关注';
  121. }else{
  122. $data->is_subscribed_text = '取关';
  123. }
  124. }else{
  125. $data->label = '';
  126. $data->is_subscribed = -1;
  127. $data->is_subscribed_text = '未关注';
  128. $data->subscribed_time = '';
  129. $data->subscribe_official = '';
  130. $data->subscribed_src = '';
  131. }
  132. $charge_lists = OrderService::totalChargeList($uid);
  133. $total_charge = 0;
  134. $total_money = 0;
  135. if($charge_lists){
  136. foreach ($charge_lists as $v){
  137. $total_charge += 100*$v->price+$v->given;
  138. $total_money += $v->price;
  139. }
  140. }
  141. $data->total_charge = number_format($total_charge);
  142. $data->balance = number_format($data->balance);
  143. $chapter_consume = ChapterOrderService::getChapterTotalConsume($uid);
  144. $book_consume = BookOrderService::getBookTotalConsume($uid);
  145. $data->total_consume = number_format($chapter_consume+$book_consume);
  146. $data->total_get = 0;
  147. $data->total_money = $total_money;
  148. return response()->success($data);
  149. }
  150. protected function getRcodeInfo($param,$bid,$uid){
  151. $param_need = [
  152. 'gzh_app_id'=>$param['appid'],
  153. 'scene_id' =>$uid,
  154. 'timestamp'=>time(),
  155. ];
  156. $param_need['sign'] = $this->getSign($param_need);
  157. $client = new Client(['timeout' => 3.0,]);
  158. try{
  159. $qrcode_url_res = $client->request('get','http://zsyauth.aizhuishu.com/api/get_qrcode_url?'.http_build_query($param_need))->getBody()->getContents();
  160. if($qrcode_url_res){
  161. $qrcode_url = json_decode($qrcode_url_res,true);
  162. if($qrcode_url['code'] == 1){
  163. //保存强关时的bid
  164. Redis::hset('force_subscribe_from_bid',$param['appid'].'_'.$this->uid,$bid);
  165. return $qrcode_url['data'];
  166. }
  167. }
  168. }catch (\Exception $e){
  169. }
  170. return false;
  171. }
  172. /**
  173. * 公众号签名@华灯初上
  174. * @param $params
  175. * @return string
  176. */
  177. protected function getSign($params)
  178. {
  179. $url = $this->arr_to_url($params, false);
  180. $url = $url . '&key=' . $this->secret_key;
  181. $sign = md5($url);
  182. return $sign;
  183. }
  184. /**
  185. * 公众号签名@华灯初上
  186. * @param $array
  187. * @param bool $has_sign
  188. * @return string
  189. */
  190. protected function arr_to_url($array, $has_sign = false)
  191. {
  192. ksort($array);
  193. reset($array);
  194. $arg = "";
  195. while (list ($name, $val) = each($array)) {
  196. if ($name == 'sign' && !$has_sign) continue;
  197. if (strpos($name, "_") === 0)
  198. continue;
  199. if (is_array($val))
  200. $val = join(',', $val);
  201. if ($val === "")
  202. continue;
  203. $arg .= $name . "=" . $val . "&";
  204. }
  205. $arg = substr($arg, 0, count($arg) - 2);
  206. return $arg;
  207. }
  208. }