UserController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Http\Controllers\QuickApp\BaseController;
  4. use App\Libs\AliSMS;
  5. use App\Modules\Subscribe\Services\YearOrderService;
  6. use App\Modules\User\Services\QappUserService;
  7. use App\Modules\User\Services\UserSignService;
  8. use Redis;
  9. class UserController extends BaseController
  10. {
  11. /**
  12. * @apiDefine User 用户
  13. */
  14. /**
  15. * @apiVersion 1.0.0
  16. * @apiDescription 获取用户信息
  17. * @api {GET} userinfo 获取用户信息
  18. * @apiHeader {String} [Authorization] token
  19. * @apiGroup User
  20. * @apiName index
  21. * @apiSuccess {Number} id 用户ID.
  22. * @apiSuccess {String} openid 微信openid.
  23. * @apiSuccess {String} unionid 微信unionid.
  24. * @apiSuccess {Number} distribution_channel_id 分销渠道ID.
  25. * @apiSuccess {String} province 省份.
  26. * @apiSuccess {String} city 城市.
  27. * @apiSuccess {String} country 国家.
  28. * @apiSuccess {String} headimgurl 头像地址.
  29. * @apiSuccess {Number} send_order_id 派单ID.
  30. * @apiSuccess {Number=0,1} sex 性别.
  31. * @apiSuccess {String} balance 书币余额.
  32. * @apiSuccess {Int} is_vip 是否vip
  33. * @apiSuccess {String} vip_days 364天.
  34. * @apiSuccess {String} phone 手机号.
  35. * @apiSuccessExample {json} Success-Response:
  36. *
  37. * {
  38. * "code": 0,
  39. * "msg": "",
  40. * "data": {
  41. * "id": 56,
  42. * "openid": "sdfs34ssdfdsf",
  43. * "unionid": "SDFSD3343S",
  44. * "distribution_channel_id": 1212,
  45. * "province": "浙江省",
  46. * "city": "杭州",
  47. * "country": "中国",
  48. * "headimgurl": "http://..",
  49. * "send_order_id": 323,
  50. * "balance": 8956,
  51. * "register_time": "2017-12-12 12:12:12",
  52. * "phone": "12312435",
  53. * }
  54. * }
  55. */
  56. public function index()
  57. {
  58. $data = $this->user_info;
  59. if (!$data->head_img) {
  60. $data->head_img = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/defaulthead.png';
  61. }
  62. $data['is_vip'] = 0;
  63. $data['vip_days'] = 0;
  64. $data['phone'] = $this->phone;
  65. $year_record = YearOrderService::getRecord($this->uid);
  66. if ($year_record) {
  67. $data['is_vip'] = 1;
  68. $time = strtotime($year_record['end_time']) - time();
  69. if ($time >= 86400) {
  70. $data['vip_days'] = floor($time / 86400) . '天';
  71. } elseif ($time > 3600) {
  72. $data['vip_days'] = floor($time / 3600) . '小时';
  73. } elseif ($time > 60) {
  74. $data['vip_days'] = floor($time / 60) . '分钟';
  75. } else {
  76. $data['vip_days'] = $time . '秒';
  77. }
  78. }
  79. return response()->success($data);
  80. }
  81. /**
  82. * @apiVersion 1.0.0
  83. * @apiDescription 用户签到
  84. * @api {GET} sign 用户签到
  85. * @apiHeader {String} [Authorization] token
  86. * @apiGroup User
  87. * @apiName sign
  88. * @apiSuccess {Double} fee 签到奖励
  89. * @apiSuccess {Number} days 签到天数
  90. * @apiSuccessExample {json} Success-Response:
  91. *
  92. * {
  93. * "code": 0,
  94. * "msg": "",
  95. * "data": {
  96. * "fee": 30,
  97. * "days": 1
  98. * }
  99. * }
  100. */
  101. public function sign()
  102. {
  103. $result = UserSignService::signV2($this->uid, date('Y-m-d'));
  104. if ($result) {
  105. return response()->success($result);
  106. }
  107. return response()->error('QAPP_SYS_ERROR');
  108. }
  109. /**
  110. * @apiVersion 1.0.0
  111. * @apiDescription 发送验证码
  112. * @api {GET} sendCode 发送验证码
  113. * @apiHeader {String} [Authorization] token
  114. * @apiGroup User
  115. * @apiName sendCode
  116. * @apiSuccess {String} phone 手机号
  117. * @apiSuccessExample {json} Success-Response:
  118. *
  119. * {
  120. * "code": 0,
  121. * "msg": "",
  122. * "data": {}
  123. * }
  124. */
  125. public function sendCode(Request $request)
  126. {
  127. $phone = $request->post('phone');
  128. $code = random_int(1000, 9999);
  129. Redis::setex('quser_code:' . $phone, 120, $code);
  130. AliSMS::send($phone, 'quickapp_user_bind_phone', ['code' => $code]);
  131. return response()->success();
  132. }
  133. /**
  134. * @apiVersion 1.0.0
  135. * @apiDescription 绑定手机号
  136. * @api {GET} bindPhone 绑定手机号
  137. * @apiHeader {String} [Authorization] token
  138. * @apiGroup User
  139. * @apiName bindPhone
  140. * @apiSuccess {String} phone 手机号
  141. * @apiSuccess {String} code 验证码
  142. * @apiSuccessExample {json} Success-Response:
  143. *
  144. * {
  145. * "code": 0,
  146. * "msg": "",
  147. * "data": {}
  148. * }
  149. */
  150. public function bindPhone(Request $request)
  151. {
  152. $code = $request->post('code');
  153. $phone = $request->post('phone');
  154. $old = Redis::get('quser_code:' . $phone);
  155. if ($old && $old == $code) {
  156. try {
  157. Redis::del('quser_code:' . $phone);
  158. $result = QappUserService::bindPhoneStatic($this->uid, $phone);
  159. if (!$result) {
  160. return response()->error('WAP_BIND_PHONE_EXIST');
  161. } else {
  162. return response()->success();
  163. }
  164. } catch (\Exception $e) {
  165. return response()->error();
  166. }
  167. } else {
  168. return response()->error('WAP_SEND_CODE_ERROR');
  169. }
  170. }
  171. }