UserController.php 10 KB

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