UserController.php 10 KB

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