UserController.php 10 KB

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