UserController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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'] = true;
  86. return response()->success($data);
  87. }
  88. /**
  89. * @apiVersion 1.0.0
  90. * @apiDescription 用户签到
  91. * @api {GET} sign 用户签到
  92. * @apiHeader {String} [Authorization] token
  93. * @apiGroup User
  94. * @apiName sign
  95. * @apiSuccess {Double} fee 签到奖励
  96. * @apiSuccess {Number} days 签到天数
  97. * @apiSuccess {Array} day_list 签到列表
  98. * @apiSuccessExample {json} Success-Response:
  99. *
  100. * {
  101. * "code": 0,
  102. * "msg": "",
  103. * "data": {
  104. * "fee": 30,
  105. * "days": 1
  106. * "day_list": []
  107. * }
  108. * }
  109. */
  110. public function sign()
  111. {
  112. $result = UserSignService::sign($this->uid, date('Y-m-d'));
  113. if ($result) {
  114. return response()->success($result);
  115. }
  116. return response()->error('QAPP_SYS_ERROR');
  117. }
  118. /**
  119. * @apiVersion 1.0.0
  120. * @apiDescription 发送验证码
  121. * @api {POST} user/sendCode 发送验证码
  122. * @apiHeader {String} [Authorization] token
  123. * @apiGroup User
  124. * @apiName sendCode
  125. * @apiParam {String} phone 手机号
  126. * @apiSuccessExample {json} Success-Response:
  127. *
  128. * {
  129. * "code": 0,
  130. * "msg": "",
  131. * "data": {}
  132. * }
  133. */
  134. public function sendCode(Request $request)
  135. {
  136. $phone = $request->post('phone');
  137. $code = random_int(1000, 9999);
  138. Redis::setex('quser_code:' . $phone, 120, $code);
  139. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  140. return response()->success();
  141. }
  142. /**
  143. * @apiVersion 1.0.0
  144. * @apiDescription 绑定手机号
  145. * @api {POST} user/bindPhone 绑定手机号
  146. * @apiHeader {String} [Authorization] token
  147. * @apiGroup User
  148. * @apiName bindPhone
  149. * @apiParam {String} phone 手机号
  150. * @apiParam {String} code 验证码
  151. * @apiSuccessExample {json} Success-Response:
  152. *
  153. * {
  154. * "code": 0,
  155. * "msg": "",
  156. * "data": {}
  157. * }
  158. */
  159. public function bindPhone(Request $request)
  160. {
  161. $code = $request->post('code');
  162. $phone = $request->post('phone');
  163. $old = Redis::get('quser_code:' . $phone);
  164. if ($old && $old == $code) {
  165. try {
  166. Redis::del('quser_code:' . $phone);
  167. $result = QappUserService::bindPhoneStatic($this->uid, $phone);
  168. if (!$result) {
  169. return response()->error('WAP_BIND_PHONE_EXIST');
  170. } else {
  171. return response()->success();
  172. }
  173. } catch (\Exception $e) {
  174. return response()->error();
  175. }
  176. } else {
  177. return response()->error('WAP_SEND_CODE_ERROR');
  178. }
  179. }
  180. /**
  181. * @apiVersion 1.0.0
  182. * @apiDescription 用户签到记录
  183. * @api {GET} user/sign_record 用户签到记录
  184. * @apiGroup User
  185. * @apiName signRecord
  186. * @apiParam {String} date 查询日期每个月一号
  187. * @apiSuccess {int} code 状态码
  188. * @apiSuccess {String} msg 信息
  189. * @apiSuccess {object} data 结果集
  190. * @apiSuccess {sign_status} data.sign_status .
  191. * @apiSuccess {sign_result} data.sign_result .
  192. * @apiSuccess {sign_today} data.sign_today .
  193. * @apiParam {page} page
  194. * @apiSuccessExample {json} Success-Response:
  195. *
  196. * {
  197. * code: 0,
  198. * msg: "",
  199. * data: {
  200. * "sign_status": true,
  201. * "sign_result": {
  202. * "list": [
  203. * {
  204. * "reward": 30,
  205. * "sign_time": "2019-11-01 14:20:30"
  206. * }
  207. * ],
  208. * "meta": {
  209. * "total": 1,
  210. * "per_page": 15,
  211. * "current_page": 1,
  212. * "last_page": 1,
  213. * "next_page_url": "",
  214. * "prev_page_url": ""
  215. * }
  216. * },
  217. * "sign_today": {
  218. * "uid": 162261523,
  219. * "price": 50,
  220. * "day": "2019-11-01",
  221. * "sign_time": "2019-11-01 09:04:43",
  222. * "created_at": "2019-11-01 09:04:43",
  223. * "updated_at": "2019-11-01 09:04:43",
  224. * "reward": 50
  225. * }
  226. * }
  227. */
  228. public function signRecord(Request $request)
  229. {
  230. $month = $request->get('date', date('Y-m-01'));
  231. $page_size = $request->get('page_size', 15);
  232. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  233. $sign_status = UserSignService::isSign($this->uid);
  234. $sign_today = [];
  235. if ($sign_status) {
  236. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  237. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  238. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  239. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  240. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  241. }
  242. $result = [
  243. 'sign_status' => $sign_status,
  244. 'sign_result' => $sign_result,
  245. 'sign_today' => $sign_today
  246. ];
  247. return response()->success($result);
  248. }
  249. /**
  250. * @apiVersion 1.0.0
  251. * @apiDescription 催更
  252. * @api {POST} user/urgeUpdate 催更
  253. * @apiHeader {String} [Authorization] token
  254. * @apiGroup User
  255. * @apiName urgeUpdate
  256. * @apiParam {String} bid 书号
  257. * @apiSuccessExample {json} Success-Response:
  258. *
  259. * {
  260. * "code": 0,
  261. * "msg": "",
  262. * "data": {}
  263. * }
  264. */
  265. public function urgeUpdate(Request $request)
  266. {
  267. $bid = $request->input('bid');
  268. if (empty($bid)) {
  269. return response()->error('PARAM_ERROR');
  270. }
  271. $bid = BookService::decodeBidStatic($bid);
  272. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  273. if ($result && !$result->id) {
  274. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  275. }
  276. return response()->success();
  277. }
  278. /**
  279. * @apiVersion 1.0.0
  280. * @apiDescription 更新派单ID
  281. * @api {GET} user/setSendOrder 更新派单ID
  282. * @apiHeader {String} [Authorization] token
  283. * @apiGroup User
  284. * @apiName setSendOrder
  285. * @apiSuccessExample {json} Success-Response:
  286. *
  287. * {
  288. * "code": 0,
  289. * "msg": "",
  290. * "data": {}
  291. */
  292. public function setSendOrder(Request $request)
  293. {
  294. $send_order_id = $request->get('send_order_id', 0);
  295. if ($send_order_id) {
  296. UserService::setUserSendOrderStatic($this->uid, $send_order_id);
  297. }
  298. return response()->success();
  299. }
  300. }