UserController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if ($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. } else {
  144. return response()->error('WAP_BIND_PHONE_EXIST');
  145. }
  146. }
  147. /**
  148. * @apiVersion 1.0.0
  149. * @apiDescription 绑定手机号
  150. * @api {POST} user/bindPhone 绑定手机号
  151. * @apiHeader {String} [Authorization] token
  152. * @apiGroup User
  153. * @apiName bindPhone
  154. * @apiParam {String} phone 手机号
  155. * @apiParam {String} code 验证码
  156. * @apiSuccessExample {json} Success-Response:
  157. *
  158. * {
  159. * "code": 0,
  160. * "msg": "",
  161. * "data": {}
  162. * }
  163. */
  164. public function bindPhone(Request $request)
  165. {
  166. $code = $request->post('code');
  167. $phone = $request->post('phone');
  168. $old = Redis::get('quser_code:' . $phone);
  169. if ($old && $old == $code) {
  170. Redis::del('quser_code:' . $phone);
  171. if (!$this->phone) {
  172. $result = (new QappUserService)->bindPhone($this->uid, $phone);
  173. if ($result) {
  174. return response()->success();
  175. } else {
  176. return response()->error('WAP_BIND_PHONE_EXIST');
  177. }
  178. } else {
  179. return response()->success();
  180. }
  181. } else {
  182. return response()->error('WAP_SEND_CODE_ERROR');
  183. }
  184. }
  185. /**
  186. * @apiVersion 1.0.0
  187. * @apiDescription 用户签到记录
  188. * @api {GET} user/sign_record 用户签到记录
  189. * @apiGroup User
  190. * @apiName signRecord
  191. * @apiParam {String} date 查询日期每个月一号
  192. * @apiSuccess {int} code 状态码
  193. * @apiSuccess {String} msg 信息
  194. * @apiSuccess {object} data 结果集
  195. * @apiSuccess {sign_status} data.sign_status .
  196. * @apiSuccess {sign_result} data.sign_result .
  197. * @apiSuccess {sign_today} data.sign_today .
  198. * @apiParam {page} page
  199. * @apiSuccessExample {json} Success-Response:
  200. *
  201. * {
  202. * code: 0,
  203. * msg: "",
  204. * data: {
  205. * "sign_status": true,
  206. * "sign_result": {
  207. * "list": [
  208. * {
  209. * "reward": 30,
  210. * "sign_time": "2019-11-01 14:20:30"
  211. * }
  212. * ],
  213. * "meta": {
  214. * "total": 1,
  215. * "per_page": 15,
  216. * "current_page": 1,
  217. * "last_page": 1,
  218. * "next_page_url": "",
  219. * "prev_page_url": ""
  220. * }
  221. * },
  222. * "sign_today": {
  223. * "uid": 162261523,
  224. * "price": 50,
  225. * "day": "2019-11-01",
  226. * "sign_time": "2019-11-01 09:04:43",
  227. * "created_at": "2019-11-01 09:04:43",
  228. * "updated_at": "2019-11-01 09:04:43",
  229. * "reward": 50
  230. * }
  231. * }
  232. */
  233. public function signRecord(Request $request)
  234. {
  235. $month = $request->get('date', date('Y-m-01'));
  236. $page_size = $request->get('page_size', 15);
  237. $sign_result = paginationTransform(new SignRecordTransformer(), UserSignService::getUserSignRecord($this->uid, $month, $page_size));
  238. $sign_status = UserSignService::isSign($this->uid);
  239. $sign_today = [];
  240. if ($sign_status) {
  241. $sign_today = ReadRecordService::getByField($this->uid, 'sign_info');
  242. if ($sign_today) $sign_today = json_decode($sign_today, 1);
  243. isset($sign_today['sign_time']) && $sign_today['sign_time'] = date('Y-m-d H:i:s', $sign_today['sign_time']);
  244. isset($sign_today['sign_at']) && $sign_today['sign_time'] = $sign_today['sign_at'];
  245. isset($sign_today['price']) && $sign_today['reward'] = $sign_today['price'];
  246. }
  247. $result = [
  248. 'sign_status' => $sign_status,
  249. 'sign_result' => $sign_result,
  250. 'sign_today' => $sign_today
  251. ];
  252. return response()->success($result);
  253. }
  254. /**
  255. * @apiVersion 1.0.0
  256. * @apiDescription 催更
  257. * @api {POST} user/urgeUpdate 催更
  258. * @apiHeader {String} [Authorization] token
  259. * @apiGroup User
  260. * @apiName urgeUpdate
  261. * @apiParam {String} bid 书号
  262. * @apiSuccessExample {json} Success-Response:
  263. *
  264. * {
  265. * "code": 0,
  266. * "msg": "",
  267. * "data": {}
  268. * }
  269. */
  270. public function urgeUpdate(Request $request)
  271. {
  272. $bid = $request->input('bid');
  273. if (empty($bid)) {
  274. return response()->error('PARAM_ERROR');
  275. }
  276. $bid = BookService::decodeBidStatic($bid);
  277. $result = BookUrgeUpdateService::UrgeRecord($this->uid, $bid);
  278. if ($result && !$result->id) {
  279. BookUrgeUpdateService::UrgeUpdate($this->uid, $bid, $result->updated_at);
  280. }
  281. return response()->success();
  282. }
  283. /**
  284. * @apiVersion 1.0.0
  285. * @apiDescription 更新派单ID
  286. * @api {GET} user/setSendOrder 更新派单ID
  287. * @apiHeader {String} [Authorization] token
  288. * @apiGroup User
  289. * @apiName setSendOrder
  290. * @apiParam {Int} send_order_id 派单ID
  291. * @apiSuccessExample {json} Success-Response:
  292. *
  293. * {
  294. * "code": 0,
  295. * "msg": "",
  296. * "data": {}
  297. */
  298. public function setSendOrder(Request $request)
  299. {
  300. $send_order_id = $request->get('send_order_id', 0);
  301. if ($send_order_id) {
  302. UserService::setUserSendOrderStatic($this->uid, $send_order_id);
  303. }
  304. return response()->success();
  305. }
  306. }