UserController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Http\Controllers\QuickApp\BaseController;
  4. use App\Http\Controllers\Wap\User\Transformers\SignRecordTransformer;
  5. use App\Libs\AliSMS;
  6. use App\Modules\Book\Services\BookService;
  7. use App\Modules\Book\Services\BookUrgeUpdateService;
  8. use App\Modules\Subscribe\Services\YearOrderService;
  9. use App\Modules\User\Services\QappUserService;
  10. use App\Modules\User\Services\ReadRecordService;
  11. use App\Modules\User\Services\UserService;
  12. use App\Modules\User\Services\UserSignService;
  13. use Illuminate\Http\Request;
  14. use Redis;
  15. class UserController extends BaseController
  16. {
  17. /**
  18. * @apiDefine User 用户
  19. */
  20. /**
  21. * @apiVersion 1.0.0
  22. * @apiDescription 获取用户信息
  23. * @api {GET} userinfo 获取用户信息
  24. * @apiHeader {String} [Authorization] token
  25. * @apiGroup User
  26. * @apiName index
  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_vip 是否vip
  39. * @apiSuccess {String} vip_days 364天.
  40. * @apiSuccess {String} phone 手机号.
  41. * @apiSuccessExample {json} Success-Response:
  42. *
  43. * {
  44. * "code": 0,
  45. * "msg": "",
  46. * "data": {
  47. * "id": 56,
  48. * "openid": "sdfs34ssdfdsf",
  49. * "unionid": "SDFSD3343S",
  50. * "distribution_channel_id": 1212,
  51. * "province": "浙江省",
  52. * "city": "杭州",
  53. * "country": "中国",
  54. * "headimgurl": "http://..",
  55. * "send_order_id": 323,
  56. * "balance": 8956,
  57. * "register_time": "2017-12-12 12:12:12",
  58. * "phone": "12312435",
  59. * }
  60. * }
  61. */
  62. public function index()
  63. {
  64. $data = $this->user_info;
  65. if (!$data->head_img) {
  66. $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
  67. }
  68. $data['is_vip'] = 0;
  69. $data['vip_days'] = 0;
  70. $data['phone'] = $this->phone;
  71. $year_record = YearOrderService::getRecord($this->uid);
  72. if ($year_record) {
  73. $data['is_vip'] = 1;
  74. $time = strtotime($year_record['end_time']) - time();
  75. if ($time >= 86400) {
  76. $data['vip_days'] = floor($time / 86400) . '天';
  77. } elseif ($time > 3600) {
  78. $data['vip_days'] = floor($time / 3600) . '小时';
  79. } elseif ($time > 60) {
  80. $data['vip_days'] = floor($time / 60) . '分钟';
  81. } else {
  82. $data['vip_days'] = $time . '秒';
  83. }
  84. }
  85. // $data['is_check'] = false;
  86. $data['is_check'] = !$this->phone;
  87. return response()->success($data);
  88. }
  89. /**
  90. * @apiVersion 1.0.0
  91. * @apiDescription 用户签到
  92. * @api {GET} sign 用户签到
  93. * @apiHeader {String} [Authorization] token
  94. * @apiGroup User
  95. * @apiName sign
  96. * @apiSuccess {Double} fee 签到奖励
  97. * @apiSuccess {Number} days 签到天数
  98. * @apiSuccess {Array} day_list 签到列表
  99. * @apiSuccessExample {json} Success-Response:
  100. *
  101. * {
  102. * "code": 0,
  103. * "msg": "",
  104. * "data": {
  105. * "fee": 30,
  106. * "days": 1
  107. * "day_list": []
  108. * }
  109. * }
  110. */
  111. public function sign()
  112. {
  113. $result = UserSignService::sign($this->uid, date('Y-m-d'));
  114. if ($result) {
  115. return response()->success($result);
  116. }
  117. return response()->error('QAPP_SYS_ERROR');
  118. }
  119. /**
  120. * @apiVersion 1.0.0
  121. * @apiDescription 发送验证码
  122. * @api {POST} user/sendCode 发送验证码
  123. * @apiHeader {String} [Authorization] token
  124. * @apiGroup User
  125. * @apiName sendCode
  126. * @apiParam {String} phone 手机号
  127. * @apiSuccessExample {json} Success-Response:
  128. *
  129. * {
  130. * "code": 0,
  131. * "msg": "",
  132. * "data": {}
  133. * }
  134. */
  135. public function sendCode(Request $request)
  136. {
  137. $phone = $request->post('phone');
  138. $code = random_int(1000, 9999);
  139. Redis::setex('quser_code:' . $phone, 120, $code);
  140. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  141. return response()->success();
  142. }
  143. /**
  144. * @apiVersion 1.0.0
  145. * @apiDescription 绑定手机号
  146. * @api {POST} user/bindPhone 绑定手机号
  147. * @apiHeader {String} [Authorization] token
  148. * @apiGroup User
  149. * @apiName bindPhone
  150. * @apiParam {String} phone 手机号
  151. * @apiParam {String} code 验证码
  152. * @apiSuccessExample {json} Success-Response:
  153. *
  154. * {
  155. * "code": 0,
  156. * "msg": "",
  157. * "data": {}
  158. * }
  159. */
  160. public function bindPhone(Request $request)
  161. {
  162. $code = $request->post('code');
  163. $phone = $request->post('phone');
  164. $old = Redis::get('quser_code:' . $phone);
  165. if ($old && $old == $code) {
  166. try {
  167. Redis::del('quser_code:' . $phone);
  168. $result = QappUserService::bindPhoneStatic($this->uid, $phone);
  169. if (!$result) {
  170. return response()->error('WAP_BIND_PHONE_EXIST');
  171. } else {
  172. return response()->success();
  173. }
  174. } catch (\Exception $e) {
  175. return response()->error();
  176. }
  177. } else {
  178. return response()->error('WAP_SEND_CODE_ERROR');
  179. }
  180. }
  181. /**
  182. * @apiVersion 1.0.0
  183. * @apiDescription 用户签到记录
  184. * @api {GET} user/sign_record 用户签到记录
  185. * @apiGroup User
  186. * @apiName signRecord
  187. * @apiParam {String} date 查询日期每个月一号
  188. * @apiSuccess {int} code 状态码
  189. * @apiSuccess {String} msg 信息
  190. * @apiSuccess {object} data 结果集
  191. * @apiSuccess {sign_status} data.sign_status .
  192. * @apiSuccess {sign_result} data.sign_result .
  193. * @apiSuccess {sign_today} data.sign_today .
  194. * @apiParam {page} page
  195. * @apiSuccessExample {json} Success-Response:
  196. *
  197. * {
  198. * code: 0,
  199. * msg: "",
  200. * data: {
  201. * "sign_status": true,
  202. * "sign_result": {
  203. * "list": [
  204. * {
  205. * "reward": 30,
  206. * "sign_time": "2019-11-01 14:20:30"
  207. * }
  208. * ],
  209. * "meta": {
  210. * "total": 1,
  211. * "per_page": 15,
  212. * "current_page": 1,
  213. * "last_page": 1,
  214. * "next_page_url": "",
  215. * "prev_page_url": ""
  216. * }
  217. * },
  218. * "sign_today": {
  219. * "uid": 162261523,
  220. * "price": 50,
  221. * "day": "2019-11-01",
  222. * "sign_time": "2019-11-01 09:04:43",
  223. * "created_at": "2019-11-01 09:04:43",
  224. * "updated_at": "2019-11-01 09:04:43",
  225. * "reward": 50
  226. * }
  227. * }
  228. */
  229. public function signRecord(Request $request)
  230. {
  231. $month = $request->get('date', date('Y-m-01'));
  232. $page_size = $request->get('page_size', 15);
  233. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  234. $sign_status = UserSignService::isSign($this->uid);
  235. $sign_today = [];
  236. if ($sign_status) {
  237. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  238. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  239. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  240. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  241. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  242. }
  243. $result = [
  244. 'sign_status' => $sign_status,
  245. 'sign_result' => $sign_result,
  246. 'sign_today' => $sign_today
  247. ];
  248. return response()->success($result);
  249. }
  250. /**
  251. * @apiVersion 1.0.0
  252. * @apiDescription 催更
  253. * @api {POST} user/urgeUpdate 催更
  254. * @apiHeader {String} [Authorization] token
  255. * @apiGroup User
  256. * @apiName urgeUpdate
  257. * @apiParam {String} bid 书号
  258. * @apiSuccessExample {json} Success-Response:
  259. *
  260. * {
  261. * "code": 0,
  262. * "msg": "",
  263. * "data": {}
  264. * }
  265. */
  266. public function urgeUpdate(Request $request)
  267. {
  268. $bid = $request->input('bid');
  269. if (empty($bid)) {
  270. return response()->error('PARAM_ERROR');
  271. }
  272. $bid = BookService::decodeBidStatic($bid);
  273. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  274. if ($result && !$result->id) {
  275. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  276. }
  277. return response()->success();
  278. }
  279. /**
  280. * @apiVersion 1.0.0
  281. * @apiDescription 更新派单ID
  282. * @api {GET} user/setSendOrder 更新派单ID
  283. * @apiHeader {String} [Authorization] token
  284. * @apiGroup User
  285. * @apiName setSendOrder
  286. * @apiParam {Int} send_order_id 派单ID
  287. * @apiSuccessExample {json} Success-Response:
  288. *
  289. * {
  290. * "code": 0,
  291. * "msg": "",
  292. * "data": {}
  293. */
  294. public function setSendOrder(Request $request)
  295. {
  296. $send_order_id = $request->get('send_order_id', 0);
  297. if ($send_order_id) {
  298. UserService::setUserSendOrderStatic($this->uid, $send_order_id);
  299. }
  300. return response()->success();
  301. }
  302. }