UserService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 10:42
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\User\Models\User;
  10. use DB;
  11. use GuzzleHttp\Client;
  12. use GuzzleHttp\Promise;
  13. class UserService
  14. {
  15. /**
  16. * 创建用户
  17. * $params
  18. * 必穿参数 openid、unionid、distribution_channel_id
  19. * 可传 province、city、country、headimgurl、send_order_id、sex、register_ip
  20. */
  21. static function addUser($params)
  22. {
  23. return User::addUser($params);
  24. }
  25. static function getUserList($limit, $offset)
  26. {
  27. return User::getUserList($limit, $offset);
  28. }
  29. static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
  30. {
  31. return User::getUserByNickAndChannelId($distribution_channel_id, $nickname);
  32. }
  33. /**
  34. * 获取用户信息列表
  35. * @param $pageSize 每页显示的条数
  36. */
  37. static function getPaginationList($pageSize = 20)
  38. {
  39. return User::getPaginationList($pageSize);
  40. }
  41. /**
  42. * 获取用户信息列表
  43. * @param $pageSize 每页显示的条数
  44. */
  45. static function getUsers($pageSize = 20, $careStatus)
  46. {
  47. return User::getPaginationList($pageSize, $careStatus);
  48. }
  49. /**
  50. * 根据id获取用户信息
  51. * @return user object
  52. */
  53. static function getById($id)
  54. {
  55. return User::getById($id);
  56. }
  57. /**
  58. * 根据id获取用户信息
  59. * @return user object
  60. */
  61. static function getUserDataById($id)
  62. {
  63. return User::getUserDataById($id);
  64. }
  65. /**
  66. * 根据id获取用户信息
  67. * @return user object
  68. */
  69. static function getUserDataDetailById($id)
  70. {
  71. return User::getUserDataDetailById($id);
  72. }
  73. /**
  74. * 根据id获取用户名称
  75. * @return string [用户昵称]
  76. */
  77. static function getNicknameById($id)
  78. {
  79. return User::getById($id)->nickname;
  80. }
  81. /**
  82. * 更新用户信息
  83. * @param int $id 用户ID
  84. * @param array $params 可传 province、city、country、headimgurl、sex
  85. * @return boolen
  86. */
  87. static function updateInfo($id, $params)
  88. {
  89. \Log::info('~~~~~~~~~~~~~~~~~====update User====~~~~~~~~~~~~~~~~' . "\n");
  90. \Log::info($id);
  91. \Log::info($params);
  92. return User::updateInfo($id, $params);
  93. }
  94. /**
  95. * 根据union和分销id获取用户信息
  96. * @param $union
  97. * @param $channel_id
  98. * @return mixed
  99. */
  100. static function getUserByUnionAndChannelId($openid, $channel_id)
  101. {
  102. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->select('id', 'openid', 'unionid')->first();
  103. }
  104. /**
  105. * 用户余额充值
  106. * @param $uid
  107. * @param $fee
  108. * @return mixed
  109. */
  110. static function addBalance($uid, $fee, $charge, $given)
  111. {
  112. return User::addBalance($uid, $fee, $charge, $given);
  113. }
  114. /**
  115. * 查询推广注册用户总数
  116. * @param $send_order_id
  117. * @return mixed
  118. */
  119. static function getPromotionTotal($send_order_id)
  120. {
  121. return User::getPromotionTotal($send_order_id);
  122. }
  123. /**
  124. * 查询渠道某天注册用户总数
  125. * @param $channel_id
  126. * @param $date
  127. * @return mixed
  128. */
  129. static function getChannelDayTotal($channel_id, $date)
  130. {
  131. return User::getChannelDayTotal($channel_id, $date);
  132. }
  133. /**
  134. * 查询渠道注册用户总数
  135. * @param $channel_id
  136. * @return mixed
  137. */
  138. static function getChannelTotal($channel_id)
  139. {
  140. return User::getChannelTotal($channel_id);
  141. }
  142. /**
  143. * 查询渠道某段时间注册用户总数
  144. * @param $channel_id
  145. * @param $startDate
  146. * @param $endDate
  147. * @return mixed
  148. */
  149. static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
  150. {
  151. return User::getChannelDayToDayTotal($channel_id, $startDate, $endDate);
  152. }
  153. /**
  154. * 查询渠道某段时间注册用户总数
  155. * @param array $channelIds
  156. * @param $startDate
  157. * @param $endDate
  158. * @return mixed
  159. */
  160. static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
  161. {
  162. return User::getChannelsDayToDayTotal($channelIds, $startDate, $endDate);
  163. }
  164. /**
  165. * 查询注册用户总数
  166. * @param $params
  167. */
  168. static function getTotalCount($params)
  169. {
  170. return User::getTotalCount($params);
  171. }
  172. /**
  173. * 查询渠道是否有登录
  174. * @param $channelId
  175. * @param $start_time
  176. * @param $end_time
  177. * @return int
  178. */
  179. static function judgeUserYesterdayLogin($channelId, $start_time, $end_time)
  180. {
  181. $user_behavior = DB::table('channel_operate_record')
  182. ->where([
  183. ['distribution_channel_id', '=', $channelId],
  184. ['created_at', '>=', $start_time],
  185. ['created_at', '<=', $end_time]
  186. ])
  187. ->first();
  188. if ($user_behavior) return 1;
  189. return 0;
  190. }
  191. /**
  192. * @param $date
  193. * @param $company_id
  194. * @return mixed
  195. */
  196. static function getCompanyDayTotal($date, $company_id)
  197. {
  198. $end_date = date('Y-m-d', strtotime($date) + 86400);
  199. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  200. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  201. ->where([
  202. ['users.created_at', '>=', $date],
  203. ['users.created_at', '<', $end_date],
  204. ['channel_users.company_id', '=', $company_id]
  205. ])
  206. ->groupBy('channel_users.company_id')
  207. ->select(DB::raw("count(users.id) as register_sum"))
  208. ->first();
  209. if ($info) return $info->register_sum;
  210. //\Log::info('getCompanyDayTotal error:' . $company_id);
  211. return $info;
  212. }
  213. static function getCompanyTotal($company_id)
  214. {
  215. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  216. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  217. ->where('channel_users.company_id', $company_id)
  218. ->groupBy('channel_users.company_id')
  219. ->select(DB::raw("count(users.id) as register_sum"))
  220. ->first();
  221. if ($info) return $info->register_sum;
  222. //\Log::error('getCompanyTotal error:' . $company_id);
  223. return $info;
  224. }
  225. static function transfromBalanceByUser($from_uid, $to_uid)
  226. {
  227. $add_balance_sql = "update users,(select * from users where id =" . $from_uid . ") as u2
  228. set users.balance = users.balance + u2.balance,
  229. users.reward_balance = users.reward_balance +u2.reward_balance,
  230. users.charge_balance = users.charge_balance +u2.charge_balance where users.id =" . $to_uid;
  231. $res = DB::update($add_balance_sql);
  232. return User::updateInfo($from_uid, ['balance' => 0, 'reward_balance' => 0, 'charge_balance' => 0]);
  233. }
  234. /**
  235. * http://test3.ycsd.cn/szzg/other/otherusercharge.aspx?action=addUser&uid=1&openid=1&token=none&username=%E6%9A%82%E6%97%A0&timestamp=1552378761&ip=0.0.0.0&sign=04bc37da70df587f77440afbaaf67224
  236. *
  237. * @param unknown_type $param
  238. */
  239. static public function request_ycsd_action($param) {
  240. $time = time();
  241. $sign = md5($time.'ycsd2019');
  242. $param['timestamp'] = $time;
  243. $param['sign'] = $sign;
  244. $param['ip'] = isset($param['ip'])?$param['ip']:'0.0.0.0';
  245. \Log::Info('request_ycsd_action_param');\Log::Info($param);
  246. //获取封面图片media_id
  247. $ycsdJson = self::getYcsdClient()->request("GET",'',
  248. ['query'=>$param,'connect_timeout' => 5]
  249. )->getBody()->getContents();
  250. \Log::info("request_ycsd_action:".$param['action']);
  251. \Log::info($ycsdJson);
  252. $ycsdJson = json_decode($ycsdJson,true);
  253. return $ycsdJson;
  254. }
  255. static public function getYcsdClient(){
  256. return new Client(['base_uri' => env('YCSD_REQUEST_URL')]);
  257. }
  258. }