UserController.php 8.4 KB

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