UserController.php 9.4 KB

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