UserController.php 5.6 KB

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